Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
--
-- Tests for PL/pgSQL handling of composite (record) variables
--
create type two_int4s as (f1 int4, f2 int4);
create type more_int4s as (f0 text, f1 int4, f2 int4);
create type two_int8s as (q1 int8, q2 int8);
create type nested_int8s as (c1 two_int8s, c2 two_int8s);
-- base-case return of a composite type
create function retc(int) returns two_int8s language plpgsql as
$$ begin return row($1,1)::two_int8s; end $$;
select retc(42);
  retc  
--------
 (42,1)
(1 row)
 
-- ok to return a matching record type
create or replace function retc(int) returns two_int8s language plpgsql as
$$ begin return row($1::int8, 1::int8); end $$;
select retc(42);
  retc  
--------
 (42,1)
(1 row)
 
-- we don't currently support implicit casting
create or replace function retc(int) returns two_int8s language plpgsql as
$$ begin return row($1,1); end $$;
select retc(42);
ERROR:  returned record type does not match expected record type
DETAIL:  Returned type integer does not match expected type bigint in column "q1" (position 1).
CONTEXT:  PL/pgSQL function retc(integer) while casting return value to function's return type
-- nor extra columns
create or replace function retc(int) returns two_int8s language plpgsql as
$$ begin return row($1::int8, 1::int8, 42); end $$;
select retc(42);
ERROR:  returned record type does not match expected record type
DETAIL:  Number of returned columns (3) does not match expected column count (2).
CONTEXT:  PL/pgSQL function retc(integer) while casting return value to function's return type
-- same cases with an intermediate "record" variable
create or replace function retc(int) returns two_int8s language plpgsql as
$$ declare r record; begin r := row($1::int8, 1::int8); return r; end $$;
select retc(42);
  retc  
--------
 (42,1)
(1 row)
 
create or replace function retc(int) returns two_int8s language plpgsql as
$$ declare r record; begin r := row($1,1); return r; end $$;
select retc(42);
ERROR:  returned record type does not match expected record type
DETAIL:  Returned type integer does not match expected type bigint in column "q1" (position 1).
CONTEXT:  PL/pgSQL function retc(integer) while casting return value to function's return type
create or replace function retc(int) returns two_int8s language plpgsql as
$$ declare r record; begin r := row($1::int8, 1::int8, 42); return r; end $$;
select retc(42);
ERROR:  returned record type does not match expected record type
DETAIL:  Number of returned columns (3) does not match expected column count (2).
CONTEXT:  PL/pgSQL function retc(integer) while casting return value to function's return type
-- but, for mostly historical reasons, we do convert when assigning
-- to a named-composite-type variable
create or replace function retc(int) returns two_int8s language plpgsql as
$$ declare r two_int8s; begin r := row($1::int8, 1::int8, 42); return r; end $$;
select retc(42);
  retc  
--------
 (42,1)
(1 row)
 
do $$ declare c two_int8s;
begin c := row(1,2); raise notice 'c = %', c; end$$;
NOTICE:  c = (1,2)
do $$ declare c two_int8s;
begin for c in select 1,2 loop raise notice 'c = %', c; end loop; end$$;
NOTICE:  c = (1,2)
do $$ declare c4 two_int4s; c8 two_int8s;
begin
  c8 := row(1,2);
  c4 := c8;
  c8 := c4;
  raise notice 'c4 = %', c4;
  raise notice 'c8 = %', c8;
end$$;
NOTICE:  c4 = (1,2)
NOTICE:  c8 = (1,2)
do $$ declare c two_int8s; d nested_int8s;
begin
  c := row(1,2);
  d := row(c, row(c.q1, c.q2+1));
  raise notice 'c = %, d = %', c, d;
  c.q1 := 10;
  d.c1 := row(11,12);
  d.c2.q2 := 42;
  raise notice 'c = %, d = %', c, d;
  raise notice 'c.q1 = %, d.c2 = %', c.q1, d.c2;
  raise notice '(d).c2.q2 = %', (d).c2.q2;  -- doesn't work without parens
  raise notice '(d.c2).q2 = %', (d.c2).q2;  -- doesn't work without parens
end$$;
NOTICE:  c = (1,2), d = ("(1,2)","(1,3)")
NOTICE:  c = (10,2), d = ("(11,12)","(1,42)")
NOTICE:  c.q1 = 10, d.c2 = (1,42)
NOTICE:  (d).c2.q2 = 42
NOTICE:  (d.c2).q2 = 42
-- block-qualified naming
do $$ <<b>> declare c two_int8s; d nested_int8s;
begin
  b.c := row(1,2);
  b.d := row(b.c, row(b.c.q1, b.c.q2+1));
  raise notice 'b.c = %, b.d = %', b.c, b.d;
  b.c.q1 := 10;
  b.d.c1 := row(11,12);
  b.d.c2.q2 := 42;
  raise notice 'b.c = %, b.d = %', b.c, b.d;
  raise notice 'b.c.q1 = %, b.d.c2 = %', b.c.q1, b.d.c2;
  raise notice '(b.d).c2.q2 = %', (b.d).c2.q2;  -- doesn't work without parens
  raise notice '(b.d.c2).q2 = %', (b.d.c2).q2;  -- doesn't work without parens
end$$;
NOTICE:  b.c = (1,2), b.d = ("(1,2)","(1,3)")
NOTICE:  b.c = (10,2), b.d = ("(11,12)","(1,42)")
NOTICE:  b.c.q1 = 10, b.d.c2 = (1,42)
NOTICE:  (b.d).c2.q2 = 42
NOTICE:  (b.d.c2).q2 = 42
-- error cases
do $$ declare c two_int8s; begin c.x = 1; end $$;
ERROR:  record "c" has no field "x"
CONTEXT:  PL/pgSQL assignment "c.x = 1"
PL/pgSQL function inline_code_block line 1 at assignment
do $$ declare c nested_int8s; begin c.x = 1; end $$;
ERROR:  record "c" has no field "x"
CONTEXT:  PL/pgSQL assignment "c.x = 1"
PL/pgSQL function inline_code_block line 1 at assignment
do $$ declare c nested_int8s; begin c.x.q1 = 1; end $$;
ERROR:  record "c" has no field "x"
CONTEXT:  PL/pgSQL assignment "c.x.q1 = 1"
PL/pgSQL function inline_code_block line 1 at assignment
do $$ declare c nested_int8s; begin c.c2.x = 1; end $$;
ERROR:  cannot assign to field "x" of column "c" because there is no such column in data type two_int8s
LINE 1: c.c2.x = 1
        ^
QUERY:  c.c2.x = 1
CONTEXT:  PL/pgSQL function inline_code_block line 1 at assignment
do $$ declare c nested_int8s; begin d.c2.x = 1; end $$;
ERROR:  "d.c2.x" is not a known variable
LINE 1: do $$ declare c nested_int8s; begin d.c2.x = 1; end $$;
                                            ^
do $$ <<b>> declare c two_int8s; begin b.c.x = 1; end $$;
ERROR:  record "c" has no field "x"
CONTEXT:  PL/pgSQL assignment "b.c.x = 1"
PL/pgSQL function inline_code_block line 1 at assignment
do $$ <<b>> declare c nested_int8s; begin b.c.x = 1; end $$;
ERROR:  record "c" has no field "x"
CONTEXT:  PL/pgSQL assignment "b.c.x = 1"
PL/pgSQL function inline_code_block line 1 at assignment
do $$ <<b>> declare c nested_int8s; begin b.c.x.q1 = 1; end $$;
ERROR:  record "c" has no field "x"
CONTEXT:  PL/pgSQL assignment "b.c.x.q1 = 1"
PL/pgSQL function inline_code_block line 1 at assignment
do $$ <<b>> declare c nested_int8s; begin b.c.c2.x = 1; end $$;
ERROR:  cannot assign to field "x" of column "b" because there is no such column in data type two_int8s
LINE 1: b.c.c2.x = 1
        ^
QUERY:  b.c.c2.x = 1
CONTEXT:  PL/pgSQL function inline_code_block line 1 at assignment
do $$ <<b>> declare c nested_int8s; begin b.d.c2.x = 1; end $$;
ERROR:  "b.d.c2" is not a known variable
LINE 1: do $$ <<b>> declare c nested_int8s; begin b.d.c2.x = 1; end ...
                                                  ^
-- check passing composite result to another function
create function getq1(two_int8s) returns int8 language plpgsql as $$
declare r two_int8s; begin r := $1; return r.q1; end $$;
select getq1(retc(344));
 getq1 
-------
   344
(1 row)
 
select getq1(row(1,2));
 getq1 
-------
     1
(1 row)
 
do $$
declare r1 two_int8s; r2 record; x int8;
begin
  r1 := retc(345);
  perform getq1(r1);
  x := getq1(r1);
  raise notice 'x = %', x;
  r2 := retc(346);
  perform getq1(r2);
  x := getq1(r2);
  raise notice 'x = %', x;
end$$;
NOTICE:  x = 345
NOTICE:  x = 346
-- check assignments of composites
do $$
declare r1 two_int8s; r2 two_int8s; r3 record; r4 record;
begin
  r1 := row(1,2);
  raise notice 'r1 = %', r1;
  r1 := r1;  -- shouldn't do anything
  raise notice 'r1 = %', r1;
  r2 := r1;
  raise notice 'r1 = %', r1;
  raise notice 'r2 = %', r2;
  r2.q2 = r1.q1 + 3;  -- check that r2 has distinct storage
  raise notice 'r1 = %', r1;
  raise notice 'r2 = %', r2;
  r1 := null;
  raise notice 'r1 = %', r1;
  raise notice 'r2 = %', r2;
  r1 := row(7,11)::two_int8s;
  r2 := r1;
  raise notice 'r1 = %', r1;
  raise notice 'r2 = %', r2;
  r3 := row(1,2);
  r4 := r3;
  raise notice 'r3 = %', r3;
  raise notice 'r4 = %', r4;
  r4.f1 := r4.f1 + 3;  -- check that r4 has distinct storage
  raise notice 'r3 = %', r3;
  raise notice 'r4 = %', r4;
  r1 := r3;
  raise notice 'r1 = %', r1;
  r4 := r1;
  raise notice 'r4 = %', r4;
  r4.q2 := r4.q2 + 1;  -- r4's field names have changed
  raise notice 'r4 = %', r4;
end$$;
NOTICE:  r1 = (1,2)
NOTICE:  r1 = (1,2)
NOTICE:  r1 = (1,2)
NOTICE:  r2 = (1,2)
NOTICE:  r1 = (1,2)
NOTICE:  r2 = (1,4)
NOTICE:  r1 = <NULL>
NOTICE:  r2 = (1,4)
NOTICE:  r1 = (7,11)
NOTICE:  r2 = (7,11)
NOTICE:  r3 = (1,2)
NOTICE:  r4 = (1,2)
NOTICE:  r3 = (1,2)
NOTICE:  r4 = (4,2)
NOTICE:  r1 = (1,2)
NOTICE:  r4 = (1,2)
NOTICE:  r4 = (1,3)
-- fields of named-type vars read as null if uninitialized
do $$
declare r1 two_int8s;
begin
  raise notice 'r1 = %', r1;
  raise notice 'r1.q1 = %', r1.q1;
  raise notice 'r1.q2 = %', r1.q2;
  raise notice 'r1 = %', r1;
end$$;
NOTICE:  r1 = <NULL>
NOTICE:  r1.q1 = <NULL>
NOTICE:  r1.q2 = <NULL>
NOTICE:  r1 = <NULL>
do $$
declare r1 two_int8s;
begin
  raise notice 'r1.q1 = %', r1.q1;
  raise notice 'r1.q2 = %', r1.q2;
  raise notice 'r1 = %', r1;
  raise notice 'r1.nosuchfield = %', r1.nosuchfield;
end$$;
NOTICE:  r1.q1 = <NULL>
NOTICE:  r1.q2 = <NULL>
NOTICE:  r1 = <NULL>
ERROR:  record "r1" has no field "nosuchfield"
CONTEXT:  PL/pgSQL expression "r1.nosuchfield"
PL/pgSQL function inline_code_block line 7 at RAISE
-- records, not so much
do $$
declare r1 record;
begin
  raise notice 'r1 = %', r1;
  raise notice 'r1.f1 = %', r1.f1;
  raise notice 'r1.f2 = %', r1.f2;
  raise notice 'r1 = %', r1;
end$$;
NOTICE:  r1 = <NULL>
ERROR:  record "r1" is not assigned yet
DETAIL:  The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT:  PL/pgSQL expression "r1.f1"
PL/pgSQL function inline_code_block line 5 at RAISE
-- but OK if you assign first
do $$
declare r1 record;
begin
  raise notice 'r1 = %', r1;
  r1 := row(1,2);
  raise notice 'r1.f1 = %', r1.f1;
  raise notice 'r1.f2 = %', r1.f2;
  raise notice 'r1 = %', r1;
  raise notice 'r1.nosuchfield = %', r1.nosuchfield;
end$$;
NOTICE:  r1 = <NULL>
NOTICE:  r1.f1 = 1
NOTICE:  r1.f2 = 2
NOTICE:  r1 = (1,2)
ERROR:  record "r1" has no field "nosuchfield"
CONTEXT:  PL/pgSQL expression "r1.nosuchfield"
PL/pgSQL function inline_code_block line 9 at RAISE
-- check %type with block-qualified variable names
do $$
<<blk>>
declare
  v int;
  r two_int8s;
  v1 v%type;
  v2 blk.v%type;
  r1 r%type;
  r2 blk.r%type;
begin
  raise notice '%', pg_typeof(v1);
  raise notice '%', pg_typeof(v2);
  raise notice '%', pg_typeof(r1);
  raise notice '%', pg_typeof(r2);
end$$;
NOTICE:  integer
NOTICE:  integer
NOTICE:  two_int8s
NOTICE:  two_int8s
-- check that type record can be passed through %type
do $$
declare r1 record;
        r2 r1%type;
begin
  r2 := row(1,2);
  raise notice 'r2 = %', r2;
  r2 := row(3,4,5);
  raise notice 'r2 = %', r2;
end$$;
NOTICE:  r2 = (1,2)
NOTICE:  r2 = (3,4,5)
-- arrays of record are not supported at the moment
do $$
declare r1 record[];
begin
end$$;
ERROR:  variable "r1" has pseudo-type record[]
CONTEXT:  compilation of PL/pgSQL function "inline_code_block" near line 2
do $$
declare r1 record;
        r2 r1%type[];
begin
end$$;
ERROR:  variable "r2" has pseudo-type record[]
CONTEXT:  compilation of PL/pgSQL function "inline_code_block" near line 3
-- check repeated assignments to composite fields
create table some_table (id int, data text);
do $$
declare r some_table;
begin
  r := (23, 'skidoo');
  for i in 1 .. 10 loop
    r.id := r.id + i;
    r.data := r.data || ' ' || i;
  end loop;
  raise notice 'r = %', r;
end$$;
NOTICE:  r = (78,"skidoo 1 2 3 4 5 6 7 8 9 10")
-- check behavior of function declared to return "record"
create function returnsrecord(int) returns record language plpgsql as
$$ begin return row($1,$1+1); end $$;
select returnsrecord(42);
 returnsrecord 
---------------
 (42,43)
(1 row)
 
select * from returnsrecord(42) as r(x int, y int);
 x  | y  
----+----
 42 | 43
(1 row)
 
select * from returnsrecord(42) as r(x int, y int, z int);  -- fail
ERROR:  returned record type does not match expected record type
DETAIL:  Number of returned columns (2) does not match expected column count (3).
CONTEXT:  PL/pgSQL function returnsrecord(integer) while casting return value to function's return type
select * from returnsrecord(42) as r(x int, y bigint);  -- fail
ERROR:  returned record type does not match expected record type
DETAIL:  Returned type integer does not match expected type bigint in column "y" (position 2).
CONTEXT:  PL/pgSQL function returnsrecord(integer) while casting return value to function's return type
-- same with an intermediate record variable
create or replace function returnsrecord(int) returns record language plpgsql as
$$ declare r record; begin r := row($1,$1+1); return r; end $$;
select returnsrecord(42);
 returnsrecord 
---------------
 (42,43)
(1 row)
 
select * from returnsrecord(42) as r(x int, y int);
 x  | y  
----+----
 42 | 43
(1 row)
 
select * from returnsrecord(42) as r(x int, y int, z int);  -- fail
ERROR:  returned record type does not match expected record type
DETAIL:  Number of returned columns (2) does not match expected column count (3).
CONTEXT:  PL/pgSQL function returnsrecord(integer) while casting return value to function's return type
select * from returnsrecord(42) as r(x int, y bigint);  -- fail
ERROR:  returned record type does not match expected record type
DETAIL:  Returned type integer does not match expected type bigint in column "y" (position 2).
CONTEXT:  PL/pgSQL function returnsrecord(integer) while casting return value to function's return type
-- should work the same with a missing column in the actual result value
create table has_hole(f1 int, f2 int, f3 int);
alter table has_hole drop column f2;
create or replace function returnsrecord(int) returns record language plpgsql as
$$ begin return row($1,$1+1)::has_hole; end $$;
select returnsrecord(42);
 returnsrecord 
---------------
 (42,43)
(1 row)
 
select * from returnsrecord(42) as r(x int, y int);
 x  | y  
----+----
 42 | 43
(1 row)
 
select * from returnsrecord(42) as r(x int, y int, z int);  -- fail
ERROR:  returned record type does not match expected record type
DETAIL:  Number of returned columns (2) does not match expected column count (3).
CONTEXT:  PL/pgSQL function returnsrecord(integer) while casting return value to function's return type
select * from returnsrecord(42) as r(x int, y bigint);  -- fail
ERROR:  returned record type does not match expected record type
DETAIL:  Returned type integer does not match expected type bigint in column "y" (position 2).
CONTEXT:  PL/pgSQL function returnsrecord(integer) while casting return value to function's return type
-- same with an intermediate record variable
create or replace function returnsrecord(int) returns record language plpgsql as
$$ declare r record; begin r := row($1,$1+1)::has_hole; return r; end $$;
select returnsrecord(42);
 returnsrecord 
---------------
 (42,43)
(1 row)
 
select * from returnsrecord(42) as r(x int, y int);
 x  | y  
----+----
 42 | 43
(1 row)
 
select * from returnsrecord(42) as r(x int, y int, z int);  -- fail
ERROR:  returned record type does not match expected record type
DETAIL:  Number of returned columns (2) does not match expected column count (3).
CONTEXT:  PL/pgSQL function returnsrecord(integer) while casting return value to function's return type
select * from returnsrecord(42) as r(x int, y bigint);  -- fail
ERROR:  returned record type does not match expected record type
DETAIL:  Returned type integer does not match expected type bigint in column "y" (position 2).
CONTEXT:  PL/pgSQL function returnsrecord(integer) while casting return value to function's return type
-- check access to a field of an argument declared "record"
create function getf1(x record) returns int language plpgsql as
$$ begin return x.f1; end $$;
select getf1(1);
ERROR:  function getf1(integer) does not exist
LINE 1: select getf1(1);
               ^
DETAIL:  No function of that name accepts the given argument types.
HINT:  You might need to add explicit type casts.
select getf1(row(1,2));
 getf1 
-------
     1
(1 row)
 
select getf1(row(1,2)::two_int4s);
 getf1 
-------
     1
(1 row)
 
select getf1(row('foo',123,456)::more_int4s);
 getf1 
-------
   123
(1 row)
 
-- the context stack is different when debug_discard_caches
-- is set, so suppress context output
\set SHOW_CONTEXT never
select getf1(row(1,2)::two_int8s);
ERROR:  record "x" has no field "f1"
\set SHOW_CONTEXT errors
select getf1(row(1,2));
 getf1 
-------
     1
(1 row)
 
-- this seemingly-equivalent case behaves a bit differently,
-- because the core parser's handling of $N symbols is simplistic
create function getf2(record) returns int language plpgsql as
$$ begin return $1.f2; end $$;
select getf2(row(1,2));  -- ideally would work, but does not
ERROR:  could not identify column "f2" in record data type
LINE 1: $1.f2
        ^
QUERY:  $1.f2
CONTEXT:  PL/pgSQL function getf2(record) line 1 at RETURN
select getf2(row(1,2)::two_int4s);
 getf2 
-------
     2
(1 row)
 
select getf2(row('foo',123,456)::more_int4s);
 getf2 
-------
   456
(1 row)
 
-- check behavior when assignment to FOR-loop variable requires coercion
do $$
declare r two_int8s;
begin
  for r in select i, i+1 from generate_series(1,4) i
  loop
    raise notice 'r = %', r;
  end loop;
end$$;
NOTICE:  r = (1,2)
NOTICE:  r = (2,3)
NOTICE:  r = (3,4)
NOTICE:  r = (4,5)
-- check behavior when returning setof composite
create function returnssetofholes() returns setof has_hole language plpgsql as
$$
declare r record;
  h has_hole;
begin
  return next h;
  r := (1,2);
  h := (3,4);
  return next r;
  return next h;
  return next row(5,6);
  return next row(7,8)::has_hole;
  return query select 9, 10;
end$$;
select returnssetofholes();
 returnssetofholes 
-------------------
 (,)
 (1,2)
 (3,4)
 (5,6)
 (7,8)
 (9,10)
(6 rows)
 
create or replace function returnssetofholes() returns setof has_hole language plpgsql as
$$
declare r record;
begin
  return next r;  -- fails, not assigned yet
end$$;
select returnssetofholes();
ERROR:  record "r" is not assigned yet
DETAIL:  The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT:  PL/pgSQL function returnssetofholes() line 4 at RETURN NEXT
create or replace function returnssetofholes() returns setof has_hole language plpgsql as
$$
begin
  return next row(1,2,3);  -- fails
end$$;
select returnssetofholes();
ERROR:  returned record type does not match expected record type
DETAIL:  Number of returned columns (3) does not match expected column count (2).
CONTEXT:  PL/pgSQL function returnssetofholes() line 3 at RETURN NEXT
create or replace function returnssetofholes() returns setof has_hole language plpgsql as
$$
begin
  return query select 1, 2.0;  -- fails
end$$;
select returnssetofholes();
ERROR:  structure of query does not match function result type
DETAIL:  Returned type numeric does not match expected type integer in column "f3" (position 2).
CONTEXT:  SQL statement "select 1, 2.0"
PL/pgSQL function returnssetofholes() line 3 at RETURN QUERY
-- check behavior with changes of a named rowtype
create table mutable(f1 int, f2 text);
create function sillyaddone(int) returns int language plpgsql as
$$ declare r mutable; begin r.f1 := $1; return r.f1 + 1; end $$;
select sillyaddone(42);
 sillyaddone 
-------------
          43
(1 row)
 
-- test for change of type of column f1 should be here someday;
-- for now see plpgsql_cache test
alter table mutable drop column f1;
-- the context stack is different when debug_discard_caches
-- is set, so suppress context output
\set SHOW_CONTEXT never
select sillyaddone(42);  -- fail
ERROR:  record "r" has no field "f1"
\set SHOW_CONTEXT errors
create function getf3(x mutable) returns int language plpgsql as
$$ begin return x.f3; end $$;
select getf3(null::mutable);  -- doesn't work yet
ERROR:  record "x" has no field "f3"
CONTEXT:  PL/pgSQL expression "x.f3"
PL/pgSQL function getf3(mutable) line 1 at RETURN
alter table mutable add column f3 int;
select getf3(null::mutable);  -- now it works
 getf3 
-------
      
(1 row)
 
alter table mutable drop column f3;
-- the context stack is different when debug_discard_caches
-- is set, so suppress context output
\set SHOW_CONTEXT never
select getf3(null::mutable);  -- fails again
ERROR:  record "x" has no field "f3"
\set SHOW_CONTEXT errors
-- check behavior with creating/dropping a named rowtype
set check_function_bodies = off;  -- else reference to nonexistent type fails
create function sillyaddtwo(int) returns int language plpgsql as
$$ declare r mutable2; begin r.f1 := $1; return r.f1 + 2; end $$;
reset check_function_bodies;
select sillyaddtwo(42);  -- fail
ERROR:  type "mutable2" does not exist
LINE 1:  declare r mutable2; begin r.f1 := $1; return r.f1 + 2; end 
                   ^
QUERY:   declare r mutable2; begin r.f1 := $1; return r.f1 + 2; end 
CONTEXT:  compilation of PL/pgSQL function "sillyaddtwo" near line 1
create table mutable2(f1 int, f2 text);
select sillyaddtwo(42);
 sillyaddtwo 
-------------
          44
(1 row)
 
drop table mutable2;
-- the context stack is different when debug_discard_caches
-- is set, so suppress context output
\set SHOW_CONTEXT never
select sillyaddtwo(42);  -- fail
ERROR:  type "mutable2" does not exist
\set SHOW_CONTEXT errors
create table mutable2(f0 text, f1 int, f2 text);
select sillyaddtwo(42);
 sillyaddtwo 
-------------
          44
(1 row)
 
select sillyaddtwo(43);
 sillyaddtwo 
-------------
          45
(1 row)
 
-- check that a record variable's declared type is re-resolved by name, so a
-- variable can come to mean a different type while the function is running.
-- The re-resolution happens only once the type cache entry the variable was
-- compiled against has been invalidated, so which of the two results below
-- you get depends on whether something invalidated it in the meantime.
create schema recmut1;
create schema recmut2;
create type recmut1.rtype as (a int);
-- Without an invalidation the variable keeps the type it was compiled with,
-- and assigning the wider value silently drops the extra column.  Pin
-- debug_discard_caches, otherwise this case does not reach the assignment
-- with a valid type cache entry.
set debug_discard_caches = 0;
set search_path = recmut1, public;
do $$
declare r rtype;                               -- compiled against recmut1.rtype
begin
  create type recmut2.rtype as (a int, b int);
  set search_path = recmut2, recmut1, public;  -- "rtype" now names recmut2.rtype
  r := row(2,3)::recmut2.rtype;
  raise notice 'without invalidation: %', r;
end$$;
NOTICE:  without invalidation: (2)
reset search_path;
reset debug_discard_caches;
drop type recmut2.rtype;
-- With one, the same code follows the name to the other type instead.
set search_path = recmut1, public;
do $$
declare r rtype;
begin
  create type recmut2.rtype as (a int, b int);
  set search_path = recmut2, recmut1, public;
  alter type recmut1.rtype rename attribute a to a2;
  alter type recmut1.rtype rename attribute a2 to a;
  r := row(2,3)::recmut2.rtype;
  raise notice 'with invalidation:    %', r;
end$$;
NOTICE:  with invalidation:    (2,3)
reset search_path;
drop schema recmut2 cascade;
NOTICE:  drop cascades to type recmut2.rtype
drop schema recmut1 cascade;
NOTICE:  drop cascades to type recmut1.rtype
-- Assignment between different descriptor vintages.  When the attribute types
-- and tuple layout are unchanged, it would be OK for this to work.
-- Copy a source with the old attribute name into a target with the new name.
create type recmove as (a int);
do $$
declare s recmove; d recmove;
begin
  s := row(1)::recmove;
  alter type recmove rename attribute a to renamed;
  d := row(2)::recmove;
  d := s;
  raise notice 'd.renamed = %', d.renamed;
end$$;
NOTICE:  d.renamed = 1
drop type recmove;
-- Assign a current expanded record into a target initialized before a no-op
-- type alteration.
create type recmove as (a int);
do $$
declare r1 recmove; r2 recmove;
begin
  r2 := row(1)::recmove;
  alter type recmove alter attribute a type int;
  r1 := row(2)::recmove;
  r2 := r1;
  raise notice 'r2.a = %', r2.a;
end$$;
ERROR:  using outdated tuple descriptor when deforming row type
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 7 at assignment
drop type recmove;
-- Same, with a plain composite datum as the source.
create type recmove as (a int);
do $$
declare r recmove;
begin
  r := row(1)::recmove;
  alter type recmove alter attribute a type int;
  r := row(2)::recmove;
  raise notice 'r.a = %', r.a;
end$$;
ERROR:  using outdated tuple descriptor when deforming row type
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 6 at assignment
drop type recmove;
-- Copy an executor-derived record with an older descriptor, then assign a
-- fresh datum.  The first copy must not prevent the subsequent assignment.
create type recmove as (a int);
create table recmove_src (a int);
insert into recmove_src values (1);
do $$
declare r1 recmove; r2 recmove;
begin
  select * into r1 from recmove_src;
  alter type recmove alter attribute a type int;
  r2 := row(2)::recmove;
  r2 := r1;
  r2 := row(3)::recmove;
  raise notice 'r2.a = %', r2.a;
end$$;
NOTICE:  r2.a = 3
drop table recmove_src;
drop type recmove;
-- Adding or dropping a column changes the layout, so assignment between
-- variables set before and after the change may fail.
create table recmove_tab (f1 int, f2 int);
insert into recmove_tab values (1, 2);
do $$
declare r1 recmove_tab; r2 recmove_tab;
begin
  select * into r1 from recmove_tab;
  alter table recmove_tab add column f3 int;
  r2 := row(3, 4, 5)::recmove_tab;
  r2 := r1;
  raise notice 'r2 = %', r2;
end$$;
ERROR:  assigned value is incompatible with the variable's type
DETAIL:  A variable can retain the type definition established when it was first used.
CONTEXT:  PL/pgSQL function inline_code_block line 7 at assignment
drop table recmove_tab;
create table recmove_tab (f1 int, f2 int);
insert into recmove_tab values (1, 2);
do $$
declare r1 recmove_tab; r2 recmove_tab;
begin
  select * into r1 from recmove_tab;
  alter table recmove_tab drop column f2;
  r2 := row(3)::recmove_tab;
  r2 := r1;
  raise notice 'r2 = %', r2;
end$$;
ERROR:  assigned value is incompatible with the variable's type
DETAIL:  A variable can retain the type definition established when it was first used.
CONTEXT:  PL/pgSQL function inline_code_block line 7 at assignment
drop table recmove_tab;
-- check access to system columns in a record variable
create function sillytrig() returns trigger language plpgsql as
$$begin
  raise notice 'old.ctid = %', old.ctid;
  raise notice 'old.tableoid = %', old.tableoid::regclass;
  return new;
end$$;
create trigger mutable_trig before update on mutable for each row
execute procedure sillytrig();
insert into mutable values ('foo'), ('bar');
update mutable set f2 = f2 || ' baz';
NOTICE:  old.ctid = (0,1)
NOTICE:  old.tableoid = mutable
NOTICE:  old.ctid = (0,2)
NOTICE:  old.tableoid = mutable
table mutable;
   f2    
---------
 foo baz
 bar baz
(2 rows)
 
-- check that system columns survive assignment between record variables only
-- when the source tuple can be copied verbatim; where a conversion is needed
-- they are silently dropped.  For a domain source that depends on whether the
-- composite type was invalidated between the two statements, so those cases
-- are run both ways.  debug_discard_caches is pinned so that the ANALYZE is
-- the only invalidation, and each ANALYZE case gets its own table because
-- ANALYZE only invalidates when it actually changes the statistics.
set debug_discard_caches = 0;
create table sysmove(f1 int);
insert into sysmove values (1);
create domain sysmoved as sysmove;
do $$
declare s sysmove; d sysmove;
begin
  select * into s from sysmove;
  d := s;
  raise notice 'rowtype -> rowtype: src %, dst %', s.ctid, d.ctid;
end$$;
NOTICE:  rowtype -> rowtype: src (0,1), dst (0,1)
do $$
declare s record; d sysmove;
begin
  select * into s from sysmove;
  d := s;
  raise notice 'record  -> rowtype: src %, dst %', s.ctid, d.ctid;
end$$;
NOTICE:  record  -> rowtype: src (0,1), dst <NULL>
do $$
declare s sysmoved; d sysmove;
begin
  select * into s from sysmove;
  d := s;
  raise notice 'domain  -> rowtype: src %, dst %', s.ctid, d.ctid;
end$$;
NOTICE:  domain  -> rowtype: src (0,1), dst (0,1)
do $$
declare s sysmoved; d record;
begin
  select * into s from sysmove;
  d := s;
  raise notice 'domain  -> record:  src %, dst %', s.ctid, d.ctid;
end$$;
NOTICE:  domain  -> record:  src (0,1), dst (0,1)
create table sysmove2(f1 int);
insert into sysmove2 values (1);
create domain sysmoved2 as sysmove2;
do $$
declare s sysmoved2; d sysmove2;
begin
  select * into s from sysmove2;
  analyze sysmove2;
  d := s;
  raise notice 'domain  -> rowtype, invalidated: src %, dst %', s.ctid, d.ctid;
end$$;
NOTICE:  domain  -> rowtype, invalidated: src (0,1), dst (0,1)
create table sysmove3(f1 int);
insert into sysmove3 values (1);
create domain sysmoved3 as sysmove3;
do $$
declare s sysmoved3; d record;
begin
  select * into s from sysmove3;
  analyze sysmove3;
  d := s;
  raise notice 'domain  -> record,  invalidated: src %, dst %', s.ctid, d.ctid;
end$$;
NOTICE:  domain  -> record,  invalidated: src (0,1), dst (0,1)
-- A rename round trip also invalidates the descriptor without changing the
-- row type.  Copying the tuple should still preserve its system columns.
do $$
declare s sysmoved; d sysmove;
begin
  select * into s from sysmove;
  alter table sysmove rename column f1 to f2;
  alter table sysmove rename column f2 to f1;
  d := s;
  raise notice 'domain  -> rowtype, renamed: src %, dst %', s.ctid, d.ctid;
end$$;
NOTICE:  domain  -> rowtype, renamed: src (0,1), dst (0,1)
drop domain sysmoved, sysmoved2, sysmoved3;
drop table sysmove, sysmove2, sysmove3;
reset debug_discard_caches;
-- Preserve system columns when assigning a rowtype parameter whose declared
-- type name now refers to a different table.
create table sysparam (x int);
insert into sysparam values (1);
create function sysparam_probe(s sysparam) returns boolean language plpgsql as $$
declare d sysparam;
begin
  select * into d from sysparam;
  alter table sysparam rename to sysparam_old;
  create table sysparam (x int8);
  select * into s from sysparam_old;
  d := s;
  raise notice 'tableoid kept: %, ctid kept: %',
    d.tableoid is not null, d.ctid is not null;
  return d.ctid is not null;
end$$;
select sysparam_probe(null::sysparam);
NOTICE:  tableoid kept: t, ctid kept: t
 sysparam_probe 
----------------
 t
(1 row)
 
drop function sysparam_probe(sysparam_old);
drop table sysparam, sysparam_old;
-- Check behavior when using variables used before/after attribute rename, a
-- harmless operation.
CREATE TYPE altered_type AS (a int);
DO $$
DECLARE
  -- variable first assigned before DDL and then again after
  r_again_after altered_type;
  -- variable assigned before DDL
  r_before altered_type;
  -- variable assigned after DDL
  r_after altered_type;
BEGIN
  r_again_after := ROW(1)::altered_type;
  r_before = ROW(2)::altered_type;
  ALTER TYPE altered_type RENAME ATTRIBUTE a TO b;
 
  BEGIN
    RAISE NOTICE 'r_before: %', r_before;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_before failed: %', sqlerrm;
  END;
 
  BEGIN
    r_again_after := ROW(3)::altered_type;
    RAISE NOTICE 'r_again_after: %', r_again_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_again_after failed: %', sqlerrm;
  END;
 
  BEGIN
    r_after = ROW(4)::altered_type;
    RAISE NOTICE 'r_after: %', r_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_after failed: %', sqlerrm;
  END;
END $$;
NOTICE:  r_before: (2)
NOTICE:  r_again_after: (3)
NOTICE:  r_after: (4)
DROP TYPE altered_type;
-- Check behavior when using variables used before/after SET TYPE that doesn't
-- actually change the type.
CREATE TYPE altered_type AS (a int);
DO $$
DECLARE
  -- variable first assigned before DDL and then again after
  r_again_after altered_type;
  -- variable assigned before DDL
  r_before altered_type;
  -- variable assigned after DDL
  r_after altered_type;
BEGIN
  r_again_after := ROW(1)::altered_type;
  r_before = ROW(2)::altered_type;
  ALTER TYPE altered_type ALTER ATTRIBUTE a TYPE int;
 
  BEGIN
    RAISE NOTICE 'r_before: %', r_before;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_before failed: %', sqlerrm;
  END;
 
  BEGIN
    r_again_after := ROW(3)::altered_type;
    RAISE NOTICE 'r_again_after: %', r_again_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_again_after failed: %', sqlerrm;
  END;
 
  BEGIN
    r_after = ROW(4)::altered_type;
    RAISE NOTICE 'r_after: %', r_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_after failed: %', sqlerrm;
  END;
END $$;
NOTICE:  r_before failed: row type of datum tuple was altered after the datum's creation
NOTICE:  r_again_after failed: using outdated tuple descriptor when deforming row type
NOTICE:  r_after: (4)
DROP TYPE altered_type;
-- Check behavior when using variables used before/after SET TYPE that does
-- actually change the type.
CREATE TYPE altered_type AS (a bytea);
DO $$
DECLARE
  -- variable first assigned before DDL and then again after
  r_again_after altered_type;
  -- variable assigned before DDL
  r_before altered_type;
  -- variable assigned after DDL
  r_after altered_type;
BEGIN
  r_again_after := ROW('1')::altered_type;
  r_before = ROW('2')::altered_type;
  ALTER TYPE altered_type ALTER ATTRIBUTE a TYPE text;
 
  BEGIN
    RAISE NOTICE 'r_before: %', r_before;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_before failed: %', sqlerrm;
  END;
 
  BEGIN
    r_again_after := ROW(3)::altered_type;
    RAISE NOTICE 'r_again_after: %', r_again_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_again_after failed: %', sqlerrm;
  END;
 
  BEGIN
    r_after = ROW(4)::altered_type;
    RAISE NOTICE 'r_after: %', r_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_after failed: %', sqlerrm;
  END;
END $$;
NOTICE:  r_before failed: row type of datum tuple was altered after the datum's creation
NOTICE:  r_again_after failed: using outdated tuple descriptor when deforming row type
NOTICE:  r_after: (4)
DROP TYPE altered_type;
-- check returning a composite datum from a trigger
create or replace function sillytrig() returns trigger language plpgsql as
$$begin
  return row(new.*);
end$$;
update mutable set f2 = f2 || ' baz';
table mutable;
     f2      
-------------
 foo baz baz
 bar baz baz
(2 rows)
 
create or replace function sillytrig() returns trigger language plpgsql as
$$declare r record;
begin
  r := row(new.*);
  return r;
end$$;
update mutable set f2 = f2 || ' baz';
table mutable;
       f2        
-----------------
 foo baz baz baz
 bar baz baz baz
(2 rows)
 
--
-- Domains of composite
--
create domain ordered_int8s as two_int8s check((value).q1 <= (value).q2);
create function read_ordered_int8s(p ordered_int8s) returns int8 as $$
begin return p.q1 + p.q2; end
$$ language plpgsql;
select read_ordered_int8s(row(1, 2));
 read_ordered_int8s 
--------------------
                  3
(1 row)
 
select read_ordered_int8s(row(2, 1));  -- fail
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
create function build_ordered_int8s(i int8, j int8) returns ordered_int8s as $$
begin return row(i,j); end
$$ language plpgsql;
select build_ordered_int8s(1,2);
 build_ordered_int8s 
---------------------
 (1,2)
(1 row)
 
select build_ordered_int8s(2,1);  -- fail
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
CONTEXT:  PL/pgSQL function build_ordered_int8s(bigint,bigint) while casting return value to function's return type
create function build_ordered_int8s_2(i int8, j int8) returns ordered_int8s as $$
declare r record; begin r := row(i,j); return r; end
$$ language plpgsql;
select build_ordered_int8s_2(1,2);
 build_ordered_int8s_2 
-----------------------
 (1,2)
(1 row)
 
select build_ordered_int8s_2(2,1);  -- fail
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
CONTEXT:  PL/pgSQL function build_ordered_int8s_2(bigint,bigint) while casting return value to function's return type
create function build_ordered_int8s_3(i int8, j int8) returns ordered_int8s as $$
declare r two_int8s; begin r := row(i,j); return r; end
$$ language plpgsql;
select build_ordered_int8s_3(1,2);
 build_ordered_int8s_3 
-----------------------
 (1,2)
(1 row)
 
select build_ordered_int8s_3(2,1);  -- fail
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
CONTEXT:  PL/pgSQL function build_ordered_int8s_3(bigint,bigint) while casting return value to function's return type
create function build_ordered_int8s_4(i int8, j int8) returns ordered_int8s as $$
declare r ordered_int8s; begin r := row(i,j); return r; end
$$ language plpgsql;
select build_ordered_int8s_4(1,2);
 build_ordered_int8s_4 
-----------------------
 (1,2)
(1 row)
 
select build_ordered_int8s_4(2,1);  -- fail
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
CONTEXT:  PL/pgSQL function build_ordered_int8s_4(bigint,bigint) line 2 at assignment
create function build_ordered_int8s_a(i int8, j int8) returns ordered_int8s[] as $$
begin return array[row(i,j), row(i,j+1)]; end
$$ language plpgsql;
select build_ordered_int8s_a(1,2);
 build_ordered_int8s_a 
-----------------------
 {"(1,2)","(1,3)"}
(1 row)
 
select build_ordered_int8s_a(2,1);  -- fail
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
CONTEXT:  PL/pgSQL function build_ordered_int8s_a(bigint,bigint) while casting return value to function's return type
-- check field assignment
do $$
declare r ordered_int8s;
begin
  r.q1 := null;
  r.q2 := 43;
  r.q1 := 42;
  r.q2 := 41;  -- fail
end$$;
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
CONTEXT:  PL/pgSQL function inline_code_block line 7 at assignment
-- check whole-row assignment
do $$
declare r ordered_int8s;
begin
  r := null;
  r := row(null,null);
  r := row(1,2);
  r := row(2,1);  -- fail
end$$;
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
CONTEXT:  PL/pgSQL function inline_code_block line 7 at assignment
-- check assignment in for-loop
do $$
declare r ordered_int8s;
begin
  for r in values (1,2),(3,4),(6,5) loop
    raise notice 'r = %', r;
  end loop;
end$$;
NOTICE:  r = (1,2)
NOTICE:  r = (3,4)
ERROR:  value for domain ordered_int8s violates check constraint "ordered_int8s_check"
CONTEXT:  PL/pgSQL function inline_code_block line 4 at FOR over SELECT rows
-- check behavior with toastable fields, too
create type two_texts as (f1 text, f2 text);
create domain ordered_texts as two_texts check((value).f1 <= (value).f2);
create table sometable (id int, a text, b text);
-- b should be compressed, but in-line
insert into sometable values (1, 'a', repeat('ffoob',1000));
-- this b should be out-of-line
insert into sometable values (2, 'a', repeat('ffoob',100000));
-- this pair should fail the domain check
insert into sometable values (3, 'z', repeat('ffoob',100000));
do $$
declare d ordered_texts;
begin
  for d in select a, b from sometable loop
    raise notice 'succeeded at "%"', d.f1;
  end loop;
end$$;
NOTICE:  succeeded at "a"
NOTICE:  succeeded at "a"
ERROR:  value for domain ordered_texts violates check constraint "ordered_texts_check"
CONTEXT:  PL/pgSQL function inline_code_block line 4 at FOR over SELECT rows
do $$
declare r record; d ordered_texts;
begin
  for r in select * from sometable loop
    raise notice 'processing row %', r.id;
    d := row(r.a, r.b);
  end loop;
end$$;
NOTICE:  processing row 1
NOTICE:  processing row 2
NOTICE:  processing row 3
ERROR:  value for domain ordered_texts violates check constraint "ordered_texts_check"
CONTEXT:  PL/pgSQL function inline_code_block line 6 at assignment
do $$
declare r record; d ordered_texts;
begin
  for r in select * from sometable loop
    raise notice 'processing row %', r.id;
    d := null;
    d.f1 := r.a;
    d.f2 := r.b;
  end loop;
end$$;
NOTICE:  processing row 1
NOTICE:  processing row 2
NOTICE:  processing row 3
ERROR:  value for domain ordered_texts violates check constraint "ordered_texts_check"
CONTEXT:  PL/pgSQL function inline_code_block line 8 at assignment
-- check coercion of a record result to named-composite function output type
create function compresult(int8) returns two_int8s language plpgsql as
$$ declare r record; begin r := row($1,$1); return r; end $$;
create table two_int8s_tab (f1 two_int8s);
insert into two_int8s_tab values (compresult(42));
-- reconnect so we lose any local knowledge of anonymous record types
\c -
table two_int8s_tab;
   f1    
---------
 (42,42)
(1 row)