Исходный код СУБД 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
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
--
-- Test deforming composite datums with a descriptor they were not formed
-- with. This file tests scenarios involving a single session.
--
-- To make these tests stable and not crash - even if we do not [yet] prevent
-- unsafe deforming - we change columns from bytea to text. Both are varlena
-- types, thereby preventing out-of-range accesses.  To make it easy to detect
-- that something is wrong, we use the bytea values '\x4649584d45', which
-- deforms to 'FIXME' if wrongly interpreted as text (and '\x464958544f4f' as
-- 'FIXTOO').
-- Executes DDL from within a query
CREATE FUNCTION rta_exec(cmd text) RETURNS void LANGUAGE plpgsql AS $$
BEGIN
  EXECUTE cmd;
END;
$$;
--
-- Portals
--
-- A cursor over a constant of the row type, altered before the first fetch
CREATE TABLE rta_tab (a int, b bytea);
BEGIN;
DECLARE rta_cur CURSOR FOR
  SELECT '(1,\\x4649584d45)'::rta_tab FROM generate_series(1, 3) g;
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
FETCH ALL FROM rta_cur;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
ROLLBACK;
DROP TABLE rta_tab;
-- Same for a partition's row type, altered via its parent
CREATE TABLE rta_tab (a int, b bytea) PARTITION BY LIST (a);
CREATE TABLE rta_tab_1 PARTITION OF rta_tab FOR VALUES IN (1);
BEGIN;
DECLARE rta_cur CURSOR FOR SELECT '(1,\\x4649584d45)'::rta_tab_1;
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
FETCH ALL FROM rta_cur;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
ROLLBACK;
DROP TABLE rta_tab;
-- A holdable cursor's materialized rows outlive the table they were read
-- from, which, if set, would otherwise would prevent the ALTER.
CREATE TABLE rta_ref (b bytea);
CREATE TABLE rta_tab (id int, r rta_ref);
INSERT INTO rta_tab VALUES (1, ROW('\x4649584d45'));
DECLARE rta_cur CURSOR WITH HOLD FOR SELECT * FROM rta_tab;
DROP TABLE rta_tab;
ALTER TABLE rta_ref ALTER COLUMN b TYPE text;
DO $$
DECLARE
  c refcursor := 'rta_cur';
  v record;
BEGIN
  FETCH c INTO v;
  RAISE NOTICE 'v.r.b: %', (v.r).b;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 7 at RAISE
CLOSE rta_cur;
DROP TABLE rta_ref;
-- A CTE scanned before and after the ALTER
BEGIN;
CREATE TABLE rta_tab (a int, b bytea);
DECLARE rta_cur CURSOR FOR
  WITH w AS (SELECT '(1,\\x4649584d45)'::rta_tab AS r)
  SELECT r::text FROM w
  UNION ALL
  SELECT r::text FROM w;
FETCH NEXT FROM rta_cur;
          r          
---------------------
 (1,"\\x4649584d45")
(1 row)
 
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
FETCH NEXT FROM rta_cur;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
ROLLBACK;
-- Same for a composite type, with a row fetched before the ALTER
BEGIN;
CREATE TYPE rta_type AS (a int, b bytea);
DECLARE rta_cur CURSOR FOR
  SELECT '(1,\\x4649584d45)'::rta_type FROM generate_series(1, 3);
FETCH NEXT FROM rta_cur;
      rta_type       
---------------------
 (1,"\\x4649584d45")
(1 row)
 
ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
FETCH NEXT FROM rta_cur;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
ROLLBACK;
-- Holdable cursors are only materialized at commit, after the ALTER
CREATE TABLE rta_tab (a int, b bytea);
BEGIN;
DECLARE rta_cur_const CURSOR WITH HOLD FOR
  SELECT '(1,\\x4649584d45)'::rta_tab FROM generate_series(1, 3);
DECLARE rta_cur_row CURSOR WITH HOLD FOR
  SELECT ROW(g, '\x4649584d45'::bytea)::rta_tab FROM generate_series(1, 3) g;
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
COMMIT;
FETCH NEXT FROM rta_cur_const;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
FETCH NEXT FROM rta_cur_row;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CLOSE rta_cur_const;
CLOSE rta_cur_row;
DROP TABLE rta_tab;
-- Holdable cursor over a domain over the row type
CREATE TABLE rta_tab (a int, b bytea);
CREATE DOMAIN rta_dom AS rta_tab;
DECLARE rta_cur CURSOR WITH HOLD FOR
  SELECT '(1,\\x4649584d45)'::rta_dom FROM generate_series(1, 3);
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
FETCH NEXT FROM rta_cur;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CLOSE rta_cur;
DROP DOMAIN rta_dom;
DROP TABLE rta_tab;
-- Holdable cursor over a range over the row type
CREATE TABLE rta_tab (a text, b bytea);
CREATE TYPE rta_range AS RANGE (SUBTYPE = rta_tab);
DECLARE rta_cur CURSOR WITH HOLD FOR
  SELECT rta_range('(aaa,\\x4649584d45)', '(bbb,\\x4649584d45)')
  FROM generate_series(1, 3);
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
FETCH NEXT FROM rta_cur;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CLOSE rta_cur;
DROP TYPE rta_range;
DROP TABLE rta_tab;
--
-- Cached plans
--
-- These depend on the cached plan surviving the ALTER. To allow for that,
-- disable debug_discard_caches, which would lead to always replanning if > 0.
--
SET debug_discard_caches = 0;
-- Prepared statements embedding constants of the row type.  The first ALTER
-- keeps the column's type.  With more than one VALUES row, the field access
-- is not folded into a constant at plan time.
CREATE TABLE rta_tab (a int, b bytea);
PREPARE rta_const AS SELECT '(1,\\x4649584d45)'::rta_tab;
PREPARE rta_field AS
  SELECT (r).b FROM (VALUES ('(1,\\x4649584d45)'::rta_tab),
                            ('(2,\\x464958544f4f)'::rta_tab)) v(r);
EXECUTE rta_const;
       rta_tab       
---------------------
 (1,"\\x4649584d45")
(1 row)
 
EXECUTE rta_field;
       b        
----------------
 \x4649584d45
 \x464958544f4f
(2 rows)
 
ALTER TABLE rta_tab ALTER COLUMN b TYPE bytea;
EXECUTE rta_const;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
EXECUTE rta_field;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
EXECUTE rta_const;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
EXECUTE rta_field;
ERROR:  attribute 2 has wrong type
DETAIL:  Table has type text, but query expects bytea.
DEALLOCATE rta_const;
DEALLOCATE rta_field;
DROP TABLE rta_tab;
-- Same for a composite type
CREATE TYPE rta_type AS (a int, b bytea);
PREPARE rta_const AS SELECT '(1,\\x4649584d45)'::rta_type;
PREPARE rta_field AS
  SELECT (r).b FROM (VALUES ('(1,\\x4649584d45)'::rta_type),
                            ('(2,\\x464958544f4f)'::rta_type)) v(r);
EXECUTE rta_const;
      rta_type       
---------------------
 (1,"\\x4649584d45")
(1 row)
 
EXECUTE rta_field;
       b        
----------------
 \x4649584d45
 \x464958544f4f
(2 rows)
 
ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE bytea;
EXECUTE rta_const;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
EXECUTE rta_field;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
EXECUTE rta_const;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
EXECUTE rta_field;
ERROR:  attribute 2 has wrong type
DETAIL:  Table has type text, but query expects bytea.
DEALLOCATE rta_const;
DEALLOCATE rta_field;
DROP TYPE rta_type;
-- Prepared statement embedding a range over the row type
CREATE TABLE rta_tab (a text, b bytea);
CREATE TYPE rta_range AS RANGE (SUBTYPE = rta_tab);
PREPARE rta_const AS
  SELECT rta_range('(aaa,\\x4649584d45)', '(bbb,\\x4649584d45)');
EXECUTE rta_const;
                         rta_range                         
-----------------------------------------------------------
 ["(aaa,""\\\\x4649584d45"")","(bbb,""\\\\x4649584d45"")")
(1 row)
 
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
EXECUTE rta_const;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DEALLOCATE rta_const;
DROP TYPE rta_range;
DROP TABLE rta_tab;
-- A constant of the row type passed as an argument to a PL/pgSQL function
CREATE TYPE rta_type AS (a int, b bytea);
CREATE FUNCTION rta_func(r rta_type) RETURNS text LANGUAGE plpgsql AS $$
BEGIN
  RETURN r::text;
END;
$$;
PREPARE rta_const AS SELECT rta_func('(1,\\x4649584d45)'::rta_type);
EXECUTE rta_const;
      rta_func       
---------------------
 (1,"\\x4649584d45")
(1 row)
 
ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
EXECUTE rta_const;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_func(rta_type) while storing call arguments into local variables
DEALLOCATE rta_const;
DROP FUNCTION rta_func(rta_type);
DROP TYPE rta_type;
-- PL/pgSQL cursor whose plan was built before the ALTER
CREATE TYPE rta_type AS (a int, b bytea);
DO $$
DECLARE
  r record;
  c CURSOR FOR
    SELECT NULL UNION ALL SELECT '(1,\\x4649584d45)'::rta_type::text;
BEGIN
  -- build the plan
  OPEN c;
  CLOSE c;
  -- release the type cache reference
  COMMIT AND CHAIN;
  OPEN c;
  FETCH c INTO r;
  RAISE NOTICE '1: %', r;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  FETCH c INTO r;
  RAISE NOTICE '2: %', r;
END;
$$;
NOTICE:  1: ()
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 16 at FETCH
DROP TYPE rta_type;
-- A constant in a PL/pgSQL statement's cached plan, reused after the ALTER
CREATE TABLE rta_tab (a int, b bytea);
DO $$
DECLARE
  t text;
BEGIN
  FOR i IN 1..2 LOOP
    SELECT '(1,\\x4649584d45)'::rta_tab INTO t;
    RAISE NOTICE '%: %', i, t;
    IF i = 1 THEN
      ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
    END IF;
  END LOOP;
END;
$$;
NOTICE:  1: (1,"\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 6 at SQL statement
DROP TABLE rta_tab;
-- A constant in a SQL function's cached plan, reused after the ALTER.  The
-- SET clause keeps the function from being inlined.
--
-- Note: In versions before 18 SQL functions don't perform caching.
CREATE TYPE rta_type AS (a int, b bytea);
CREATE FUNCTION rta_func() RETURNS text LANGUAGE sql
  SET search_path = public
  AS $$SELECT '(1,\\x4649584d45)'::rta_type::text$$;
SELECT rta_func();
      rta_func       
---------------------
 (1,"\\x4649584d45")
(1 row)
 
ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
SELECT rta_func();
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  SQL function "rta_func" statement 1
DROP FUNCTION rta_func();
DROP TYPE rta_type;
-- PL/pgSQL RETURN NEXT of a record variable
CREATE TYPE rta_type AS (a int, b bytea);
CREATE FUNCTION rta_func() RETURNS SETOF rta_type LANGUAGE plpgsql AS $$
DECLARE
  v rta_type;
BEGIN
  v := ROW(1, '\x4649584d45'::bytea)::rta_type;
  RETURN NEXT v;
END;
$$;
SELECT b FROM rta_func();
      b       
--------------
 \x4649584d45
(1 row)
 
ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
SELECT b FROM rta_func();
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_func() line 5 at assignment
DROP FUNCTION rta_func();
DROP TYPE rta_type;
-- PL/pgSQL RETURN NEXT of an expression
CREATE TYPE rta_type AS (a int, b bytea);
CREATE FUNCTION rta_func() RETURNS SETOF rta_type LANGUAGE plpgsql AS $$
BEGIN
  RETURN NEXT ROW(1, '\x4649584d45'::bytea)::rta_type;
END;
$$;
SELECT b FROM rta_func();
      b       
--------------
 \x4649584d45
(1 row)
 
ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
SELECT b FROM rta_func();
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_func() line 3 at RETURN NEXT
DROP FUNCTION rta_func();
DROP TYPE rta_type;
-- PL/pgSQL coercion of a function result to a different row type.  Altering
-- both types keeps the descriptors identical, so no attribute mapping is
-- done.
CREATE TYPE rta_src AS (a int, b bytea);
CREATE TYPE rta_dst AS (a int, b bytea);
CREATE FUNCTION rta_func() RETURNS rta_dst LANGUAGE plpgsql AS $$
BEGIN
  RETURN ROW(1, '\x4649584d45'::bytea)::rta_src;
END;
$$;
SELECT rta_func();
      rta_func       
---------------------
 (1,"\\x4649584d45")
(1 row)
 
ALTER TYPE rta_src ALTER ATTRIBUTE b TYPE text;
ALTER TYPE rta_dst ALTER ATTRIBUTE b TYPE text;
SELECT rta_func();
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_func() while casting return value to function's return type
DROP FUNCTION rta_func();
DROP TYPE rta_src, rta_dst;
-- PL/pgSQL trigger returning a row expression
CREATE TABLE rta_tab (a int, b bytea);
CREATE FUNCTION rta_trig() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
  RETURN ROW(1, '\x4649584d45'::bytea)::rta_tab;
END;
$$;
CREATE TRIGGER rta_trig BEFORE INSERT ON rta_tab
  FOR EACH ROW EXECUTE FUNCTION rta_trig();
INSERT INTO rta_tab VALUES (0, NULL);
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
INSERT INTO rta_tab VALUES (0, NULL);
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_trig() during function exit
SELECT * FROM rta_tab;
 a |      b       
---+--------------
 1 | \x4649584d45
(1 row)
 
DROP TABLE rta_tab;
DROP FUNCTION rta_trig();
-- PL/pgSQL trigger returning a record variable
CREATE TABLE rta_tab (a int, b bytea);
CREATE FUNCTION rta_trig() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE
  v rta_tab;
BEGIN
  v := ROW(1, '\x4649584d45'::bytea)::rta_tab;
  RETURN v;
END;
$$;
CREATE TRIGGER rta_trig BEFORE INSERT ON rta_tab
  FOR EACH ROW EXECUTE FUNCTION rta_trig();
INSERT INTO rta_tab VALUES (0, NULL);
ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
INSERT INTO rta_tab VALUES (0, NULL);
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_trig() line 5 at assignment
SELECT * FROM rta_tab;
 a |      b       
---+--------------
 1 | \x4649584d45
(1 row)
 
DROP TABLE rta_tab;
DROP FUNCTION rta_trig();
RESET debug_discard_caches;
--
-- PL/pgSQL variables
--
-- A datum formed in a rolled back transaction, read after the type was
-- altered again, the same number of times.
CREATE TABLE rta_tab (a bytea, b bytea);
INSERT INTO rta_tab VALUES ('\x4649584d45', '\x464958544f4f');
DO $$
DECLARE
  r record;
BEGIN
  ALTER TABLE rta_tab ALTER COLUMN a TYPE text;
  ALTER TABLE rta_tab ALTER COLUMN a TYPE bytea USING a::bytea;
  r := rta_tab FROM rta_tab;
  RAISE NOTICE 'r before rollback: %', r;
  ROLLBACK AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN a TYPE text;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'r after rollback: %', r;
END;
$$;
NOTICE:  r before rollback: ("\\x4649584d45","\\x464958544f4f")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 12 at RAISE
DROP TABLE rta_tab;
-- Assigning a new value to a variable holding a datum formed before the
-- ALTER
CREATE TABLE rta_tab (a int, b bytea);
INSERT INTO rta_tab VALUES (1, '\x4649584d45');
DO $$
DECLARE
  r rta_tab;
BEGIN
  r := rta_tab FROM rta_tab;
  RAISE NOTICE 'r before: %', r;
  ROLLBACK AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  r := rta_tab FROM rta_tab;
  RAISE NOTICE 'r after: %', r;
END;
$$;
NOTICE:  r before: (1,"\\x4649584d45")
ERROR:  using outdated tuple descriptor when deforming row type
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at assignment
DROP TABLE rta_tab;
-- A variable assigned field-wise, returned after the ALTER
CREATE TYPE rta_type AS (b bytea);
CREATE FUNCTION rta_func() RETURNS rta_type LANGUAGE plpgsql AS $$
DECLARE
  r rta_type;
BEGIN
  SELECT '\x4649584d45'::bytea INTO r.b;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RETURN r;
END;
$$;
SELECT r, record_send(r) FROM (SELECT rta_func() AS r OFFSET 0) s;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP FUNCTION rta_func();
DROP TYPE rta_type;
-- A variable returned from a trigger after its type was altered.  The
-- table's row type has the layout the variable's type had before the ALTER.
CREATE TABLE rta_tab (a int, b bytea);
CREATE TYPE rta_type AS (a int, b bytea);
CREATE FUNCTION rta_trig() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE
  v rta_type;
BEGIN
  v := ROW(1, '\x4649584d45'::bytea)::rta_type;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RETURN v;
END;
$$;
CREATE TRIGGER rta_trig BEFORE INSERT ON rta_tab
  FOR EACH ROW EXECUTE FUNCTION rta_trig();
INSERT INTO rta_tab VALUES (0, NULL);
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
SELECT * FROM rta_tab;
 a | b 
---+---
(0 rows)
 
DROP TABLE rta_tab;
DROP FUNCTION rta_trig();
DROP TYPE rta_type;
-- Same for a function result, coerced to another row type
CREATE TYPE rta_src AS (a int, b bytea);
CREATE TYPE rta_dst AS (a int, b bytea);
CREATE FUNCTION rta_func() RETURNS rta_dst LANGUAGE plpgsql AS $$
DECLARE
  v rta_src;
BEGIN
  v := ROW(1, '\x4649584d45'::bytea)::rta_src;
  ALTER TYPE rta_src ALTER ATTRIBUTE b TYPE text;
  RETURN v;
END;
$$;
SELECT rta_func();
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_func() while casting return value to function's return type
DROP FUNCTION rta_func();
DROP TYPE rta_src, rta_dst;
-- A variable of the row type
CREATE TABLE rta_tab (a int, b bytea);
DO $$
DECLARE
  v rta_tab;
BEGIN
  v := '(1,\\x4649584d45)'::rta_tab;
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'v: %', v;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 8 at RAISE
DROP TABLE rta_tab;
-- A record variable containing datums of the row type
CREATE TABLE rta_tab (a int, b bytea);
DO $$
DECLARE
  f1 rta_tab;
  f2 rta_tab;
  r record;
BEGIN
  f1 := '(1,\\x4649584d45)'::rta_tab;
  f2 := '(2,\\x464958544f4f)'::rta_tab;
  COMMIT AND CHAIN;
  r := ROW(f1, f2);
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'r: %', r;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 12 at RAISE
DROP TABLE rta_tab;
-- A record variable containing an anonymous record containing a datum of the
-- row type
CREATE TABLE rta_tab (a int, b bytea);
DO $$
DECLARE
  r record;
BEGIN
  r := ROW(ROW('(1,\\x4649584d45)'::rta_tab));
  r := ROW(r, 'foo'::text);
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'r.f2: %', r.f2;
  RAISE NOTICE 'r: %', r;
END;
$$;
NOTICE:  r.f2: foo
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 10 at RAISE
DROP TABLE rta_tab;
-- A variable of a composite type containing the altered type
CREATE TYPE rta_inner AS (a int, b bytea);
CREATE TYPE rta_outer AS (i rta_inner);
DO $$
DECLARE
  v rta_outer;
BEGIN
  v := ROW(ROW(1, '\x4649584d45'::bytea)::rta_inner)::rta_outer;
  RAISE NOTICE 'v: %', v;
  COMMIT AND CHAIN;
  ALTER TYPE rta_inner ALTER ATTRIBUTE b TYPE text;
  RAISE NOTICE 'v: %', v;
END;
$$;
NOTICE:  v: ("(1,""\\\\x4649584d45"")")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at RAISE
DROP TYPE rta_outer;
DROP TYPE rta_inner;
-- A variable of an array over the row type
CREATE TABLE rta_tab (a int, b bytea);
DO $$
DECLARE
  v rta_tab[];
BEGIN
  v := ARRAY[ROW(1, '\x4649584d45'::bytea)::rta_tab];
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'v: %', v;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 8 at RAISE
DROP TABLE rta_tab;
-- The same, with the elements returned by a function in FROM.  They are
-- stored in a tuplestore and read back with the function scan's descriptor.
CREATE TABLE rta_tab (a int, b bytea);
DO $$
DECLARE
  v rta_tab[];
  r record;
BEGIN
  v := ARRAY[ROW(1, '\x4649584d45'::bytea)::rta_tab];
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  FOR r IN SELECT * FROM unnest(v) LOOP
    RAISE NOTICE 'r: %', r;
  END LOOP;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at FOR over SELECT rows
DROP TABLE rta_tab;
-- A variable of a domain over the row type
CREATE TABLE rta_tab (a int, b bytea);
CREATE DOMAIN rta_dom AS rta_tab;
DO $$
DECLARE
  v rta_dom;
BEGIN
  v := ROW(1, '\x4649584d45'::bytea)::rta_dom;
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'v: %', v;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 8 at RAISE
DROP DOMAIN rta_dom;
DROP TABLE rta_tab;
-- A variable of a domain over an array of the row type
CREATE TABLE rta_tab (a int, b bytea);
CREATE DOMAIN rta_adom AS rta_tab[];
DO $$
DECLARE
  v rta_adom;
BEGIN
  v := ARRAY[ROW(1, '\x4649584d45'::bytea)::rta_tab];
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'v: %', v;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 8 at RAISE
DROP DOMAIN rta_adom;
DROP TABLE rta_tab;
-- A variable of a range over the row type
CREATE TABLE rta_tab (a text, b bytea);
CREATE TYPE rta_range AS RANGE (SUBTYPE = rta_tab);
DO $$
DECLARE
  v rta_range;
BEGIN
  v := rta_range(ROW('aaa', '\x4649584d45'::bytea)::rta_tab,
                 ROW('bbb', '\x4649584d45'::bytea)::rta_tab);
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'v: %', v;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at RAISE
DROP TYPE rta_range;
DROP TABLE rta_tab;
-- A variable of a multirange over the row type
CREATE TABLE rta_tab (a text, b bytea);
CREATE TYPE rta_range AS RANGE (SUBTYPE = rta_tab);
DO $$
DECLARE
  v rta_multirange;
BEGIN
  v := rta_multirange(rta_range(ROW('aaa', '\x4649584d45'::bytea)::rta_tab,
                              ROW('bbb', '\x4649584d45'::bytea)::rta_tab));
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'v: %', v;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at RAISE
DROP TYPE rta_range;
DROP TABLE rta_tab;
-- A variable of a range over an array of the row type
CREATE TABLE rta_tab (a text, b bytea);
CREATE TYPE rta_range AS RANGE (SUBTYPE = rta_tab[]);
DO $$
DECLARE
  v rta_range;
BEGIN
  v := rta_range(ARRAY[ROW('aaa', '\x4649584d45'::bytea)::rta_tab],
                 ARRAY[ROW('bbb', '\x4649584d45'::bytea)::rta_tab]);
  COMMIT AND CHAIN;
  ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  RAISE NOTICE 'v: %', v;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at RAISE
DROP TYPE rta_range;
DROP TABLE rta_tab;
-- Rows of a FOR loop query, made holdable by the COMMIT
CREATE TABLE rta_tab (a int, b bytea);
CREATE FUNCTION rta_rows() RETURNS SETOF record LANGUAGE sql AS $$
  SELECT '(1,\\x4649584d45)'::rta_tab FROM generate_series(1, 3);
$$;
DO $$
DECLARE
  v rta_tab;
  r record;
BEGIN
  FOR r IN SELECT rta_rows() AS c LOOP
    v := r.c;
    RAISE NOTICE 'v: %', v;
    COMMIT AND CHAIN;
    ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  END LOOP;
END;
$$;
NOTICE:  v: (1,"\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 8 at RAISE
DROP FUNCTION rta_rows();
DROP TABLE rta_tab;
-- Same, without the function
CREATE TABLE rta_tab (a int, b bytea);
DO $$
DECLARE
  v rta_tab;
  r record;
BEGIN
  FOR r IN SELECT '(1,\\x4649584d45)'::rta_tab AS c FROM generate_series(1, 3) LOOP
    v := r.c;
    RAISE NOTICE 'v: %', v;
    COMMIT;
    ALTER TABLE rta_tab ALTER COLUMN b TYPE text;
  END LOOP;
END;
$$;
NOTICE:  v: (1,"\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 8 at RAISE
DROP TABLE rta_tab;
-- Altering the first of two attributes
CREATE TYPE rta_type AS (a bytea, b bytea);
DO $$
DECLARE
  r rta_type;
BEGIN
  r := ROW('\x4649584d45', '\x464958544f4f');
  RAISE NOTICE 'before alter: %', r;
  ALTER TYPE rta_type ALTER ATTRIBUTE a TYPE text;
  RAISE NOTICE 'after alter: %', r;
END;
$$;
NOTICE:  before alter: ("\\x4649584d45","\\x464958544f4f")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 8 at RAISE
DROP TYPE rta_type;
-- Altering an attribute followed by a dropped attribute
CREATE TYPE rta_type AS (a bytea, b bytea);
DO $$
DECLARE
  r rta_type;
BEGIN
  r := ROW('\x4649584d45', '\x464958544f4f');
  ALTER TYPE rta_type DROP ATTRIBUTE b;
  RAISE NOTICE 'after drop: %', r;
  ALTER TYPE rta_type ALTER ATTRIBUTE a TYPE text;
  RAISE NOTICE 'after alter: %', r;
END;
$$;
NOTICE:  after drop: ("\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at RAISE
DROP TYPE rta_type;
-- A datum not containing the altered attribute is safe to deform
CREATE TYPE rta_type AS (a bytea);
DO $$
DECLARE
  r rta_type;
BEGIN
  r := ROW('\x4649584d45');
  RAISE NOTICE 'before alter: %', r;
  ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RAISE NOTICE 'after alter: %', r;
END;
$$;
NOTICE:  before alter: ("\\x4649584d45")
NOTICE:  after alter: ("\\x4649584d45",)
DROP TYPE rta_type;
-- A datum not containing a later attribute, after an earlier attribute it
-- does contain was altered
CREATE TYPE rta_type AS (a bytea);
DO $$
DECLARE
  r rta_type;
BEGIN
  r := ROW('\x4649584d45');
  ALTER TYPE rta_type ADD ATTRIBUTE b int;
  ALTER TYPE rta_type ALTER ATTRIBUTE a TYPE text;
  RAISE NOTICE 'after alter: %', r;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 8 at RAISE
DROP TYPE rta_type;
-- json_populate_recordset() with a base record formed before the ALTER
CREATE TYPE rta_type AS (a int, b bytea);
CREATE PROCEDURE rta_proc() LANGUAGE plpgsql AS $$
DECLARE
  v rta_type;
  r rta_type;
BEGIN
  v := ROW(1, '\x4649584d45'::bytea)::rta_type;
  SELECT * INTO r FROM json_populate_recordset(v, '[{}]'::json);
  RAISE NOTICE 'before alter: %', r;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  SELECT * INTO r FROM json_populate_recordset(v, '[{}]'::json);
  RAISE NOTICE 'after alter: %', r;
END;
$$;
CALL rta_proc();
NOTICE:  before alter: (1,"\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  SQL statement "SELECT *        FROM json_populate_recordset(v, '[{}]'::json)"
PL/pgSQL function rta_proc() line 10 at SQL statement
DROP PROCEDURE rta_proc();
DROP TYPE rta_type;
-- Conversion of a child's row type to the parent's that does not need to
-- map any attributes
CREATE TABLE rta_parent (a text, b bytea);
CREATE TABLE rta_child () INHERITS (rta_parent);
DO $$
DECLARE
  r rta_child;
BEGIN
  r := '(aaa,\\x4649584d45)'::rta_child;
  ALTER TABLE rta_parent ALTER COLUMN b TYPE text;
  RAISE NOTICE 'r: %', (r::rta_parent)::text;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 7 at RAISE
DROP TABLE rta_parent, rta_child;
-- Storing a datum formed before the ALTER, only reading the datum after the
-- INSERT
CREATE TYPE rta_type AS (b bytea);
DO $$
DECLARE
  r rta_type;
BEGIN
  r := ROW('\x4649584d45');
  COMMIT AND CHAIN;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  CREATE TABLE rta_sink (c rta_type);
  COMMIT AND CHAIN;
  INSERT INTO rta_sink VALUES (r);
END;
$$;
SELECT c FROM rta_sink;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
-- remove the potentially stored outdated datum
UPDATE rta_sink SET c = NULL;
SELECT c IS NULL FROM rta_sink;
 ?column? 
----------
 t
(1 row)
 
DROP TABLE rta_sink;
DROP TYPE rta_type;
-- Same, inside an array
CREATE TYPE rta_type AS (b bytea);
DO $$
DECLARE
  r rta_type[];
BEGIN
  r := ARRAY[ROW('\x4649584d45')];
  COMMIT AND CHAIN;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  CREATE TABLE rta_sink (c rta_type[]);
  COMMIT AND CHAIN;
  INSERT INTO rta_sink VALUES (r);
END;
$$;
SELECT c FROM rta_sink;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
UPDATE rta_sink SET c = NULL;
SELECT c IS NULL FROM rta_sink;
 ?column? 
----------
 t
(1 row)
 
DROP TABLE rta_sink;
DROP TYPE rta_type;
--
-- Assignment between PL/pgSQL record variables whose descriptors were
-- acquired before and after the ALTER
--
-- The source row comes straight from the executor
CREATE TYPE rta_type AS (b bytea);
CREATE TABLE rta_src (b bytea);
INSERT INTO rta_src VALUES ('\x4649584d45');
DO $$
DECLARE
  r1 rta_type;
  r2 rta_type;
BEGIN
  SELECT * INTO r1 FROM rta_src;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  r2 := ROW('new')::rta_type;
  r2 := r1;
  RAISE NOTICE 'r2.b: %', r2.b;
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 9 at assignment
DROP TABLE rta_src;
DROP TYPE rta_type;
-- Same, but stored rather than read
CREATE TYPE rta_type AS (b bytea);
CREATE TABLE rta_src (b bytea);
INSERT INTO rta_src VALUES ('\x4649584d45');
DO $$
DECLARE
  r1 rta_type;
  r2 rta_type;
BEGIN
  SELECT * INTO r1 FROM rta_src;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  -- after the ALTER, as otherwise the ALTER would be prevented
  CREATE TABLE rta_sink (c rta_type);
  r2 := ROW('new')::rta_type;
  BEGIN
    r2 := r1;
    INSERT INTO rta_sink VALUES (r2);
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'insert or assign failed: %', sqlerrm;
  END;
END;
$$;
NOTICE:  insert or assign failed: assigned value is incompatible with the variable's type
SELECT (c).b FROM rta_sink;
 b 
---
(0 rows)
 
DROP TABLE rta_sink;
DROP TABLE rta_src;
DROP TYPE rta_type;
-- A domain source, assigned to a variable that has not been assigned yet
CREATE TYPE rta_type AS (b bytea);
CREATE DOMAIN rta_dom AS rta_type;
CREATE TABLE rta_src (b bytea);
INSERT INTO rta_src VALUES ('\x4649584d45');
DO $$
DECLARE
  r1 rta_dom;
  r2 rta_type;
BEGIN
  SELECT * INTO r1 FROM rta_src;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  r2 := r1;
  RAISE NOTICE 'r2.b: %', r2.b;
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 8 at assignment
DROP TABLE rta_src;
DROP DOMAIN rta_dom;
DROP TYPE rta_type;
-- A source variable assigned before the ALTER
CREATE TYPE rta_type AS (b bytea);
DO $$
DECLARE
  r1 rta_type;
  r2 rta_type;
  t text;
BEGIN
  r1 := ROW('\x4649584d45')::rta_type;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  r2 := ROW('new')::rta_type;
  r2 := r1;
  RAISE NOTICE 'r2.b: %', r2.b;
  SELECT (r2).b::text INTO t;
  RAISE NOTICE 'via SQL: %', t;
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 10 at assignment
DROP TYPE rta_type;
-- A target variable assigned before the ALTER, the source after it
CREATE TYPE rta_type AS (b text);
DO $$
DECLARE
  r1 rta_type;
  r2 rta_type;
BEGIN
  r2 := ROW('old')::rta_type;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE bytea;
  r1 := ROW('\x4649584d45')::rta_type;
  r2 := r1;
  RAISE NOTICE 'r2.b: %', r2.b;
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 9 at assignment
DROP TYPE rta_type;
--
-- Reuse of an attribute number, after the attribute's addition was rolled
-- back
--
-- Variable of the composite type
CREATE TYPE rta_type AS (a int);
DO $$
DECLARE
  r rta_type;
BEGIN
  ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
  r := '(1,\\x4649584d45)';
  RAISE NOTICE 'b is bytea: %', r;
  ROLLBACK AND CHAIN;
  RAISE NOTICE 'b does not exist: %', r;
  ALTER TYPE rta_type ADD ATTRIBUTE b text;
  RAISE NOTICE 'b is text: %', r;
END;
$$;
NOTICE:  b is bytea: (1,"\\x4649584d45")
ERROR:  using outdated tuple descriptor when deforming row type
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at RAISE
DROP TYPE rta_type;
-- Same for a table's row type
CREATE TABLE rta_tab (a int);
DO $$
DECLARE
  r rta_tab;
BEGIN
  ALTER TABLE rta_tab ADD COLUMN b bytea;
  r := '(1,\\x4649584d45)';
  RAISE NOTICE 'b is bytea: %', r;
  ROLLBACK AND CHAIN;
  ALTER TABLE rta_tab ADD COLUMN b text;
  RAISE NOTICE 'b is text: %', r;
END;
$$;
NOTICE:  b is bytea: (1,"\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 10 at RAISE
DROP TABLE rta_tab;
-- Record variable
CREATE TYPE rta_type AS (a int);
DO $$
DECLARE
  r record;
BEGIN
  ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
  r := '(1,\\x4649584d45)'::rta_type;
  RAISE NOTICE 'b is bytea: %', r;
  ROLLBACK AND CHAIN;
  ALTER TYPE rta_type ADD ATTRIBUTE b text;
  RAISE NOTICE 'b is text: %', r;
END;
$$;
NOTICE:  b is bytea: (1,"\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 10 at RAISE
DROP TYPE rta_type;
-- Record variable assigned by SELECT INTO, not used before the ALTER
CREATE TYPE rta_type AS (a int);
DO $$
DECLARE
  r record;
BEGIN
  ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
  SELECT '(1,\\x4649584d45)'::rta_type INTO r;
  ROLLBACK AND CHAIN;
  ALTER TYPE rta_type ADD ATTRIBUTE b text;
  RAISE NOTICE 'b is text: %', r;
END;
$$;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 9 at RAISE
DROP TYPE rta_type;
-- Array variable
CREATE TYPE rta_type AS (a int);
DO $$
DECLARE
  r rta_type[];
BEGIN
  ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
  r := ARRAY['(1,\\x4649584d45)'];
  RAISE NOTICE 'b is bytea: %', r;
  ROLLBACK AND CHAIN;
  ALTER TYPE rta_type ADD ATTRIBUTE b text;
  RAISE NOTICE 'b is text: %', r;
END;
$$;
NOTICE:  b is bytea: {"(1,\"\\\\x4649584d45\")"}
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 10 at RAISE
DROP TYPE rta_type;
-- Rows of a FOR loop query started before the ROLLBACK
CREATE TABLE rta_tab (a int);
DO $$
DECLARE
  r record;
BEGIN
  ALTER TABLE rta_tab ADD COLUMN b bytea;
  FOR r IN SELECT ROW(1, '\x4649584d45'::bytea)::rta_tab FROM generate_series(1, 3) LOOP
    RAISE NOTICE 'r: %', r;
    ROLLBACK AND CHAIN;
    ALTER TABLE rta_tab ADD COLUMN b text;
  END LOOP;
END;
$$;
NOTICE:  r: ("(1,""\\\\x4649584d45"")")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 7 at RAISE
DROP TABLE rta_tab;
-- Same, with EXECUTE
CREATE TABLE rta_tab (a int);
DO $$
DECLARE
  r record;
BEGIN
  ALTER TABLE rta_tab ADD COLUMN b bytea;
  FOR r IN EXECUTE $q$SELECT ROW(1, '\x4649584d45'::bytea)::rta_tab FROM generate_series(1, 3)$q$ LOOP
    RAISE NOTICE 'r: %', r;
    ROLLBACK AND CHAIN;
    ALTER TABLE rta_tab ADD COLUMN b text;
  END LOOP;
END;
$$;
NOTICE:  r: ("(1,""\\\\x4649584d45"")")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function inline_code_block line 7 at RAISE
DROP TABLE rta_tab;
-- Prepared statement, prepared in a rolled back transaction.  See earlier
-- comment about debug_discard_caches.
SET debug_discard_caches = 0;
CREATE TYPE rta_type AS (a int);
BEGIN;
ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
PREPARE rta_stmt AS SELECT '(1,\\x4649584d45)'::rta_type;
EXECUTE rta_stmt;
      rta_type       
---------------------
 (1,"\\x4649584d45")
(1 row)
 
ROLLBACK;
EXECUTE rta_stmt;
ERROR:  using outdated tuple descriptor when deforming row type
HINT:  See server log for additional details.
ALTER TYPE rta_type ADD ATTRIBUTE b text;
EXECUTE rta_stmt;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DEALLOCATE rta_stmt;
DROP TYPE rta_type;
-- Same, with a rolled back subtransaction
CREATE TYPE rta_type AS (a int);
BEGIN;
SAVEPOINT s;
ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
PREPARE rta_stmt AS SELECT '(1,\\x4649584d45)'::rta_type;
EXECUTE rta_stmt;
      rta_type       
---------------------
 (1,"\\x4649584d45")
(1 row)
 
ROLLBACK TO SAVEPOINT s;
ALTER TYPE rta_type ADD ATTRIBUTE b text;
EXECUTE rta_stmt;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
COMMIT;
DEALLOCATE rta_stmt;
DROP TYPE rta_type;
RESET debug_discard_caches;
-- Rolled back subtransaction, via an exception block in a procedure
CREATE TYPE rta_type AS (a int);
CREATE PROCEDURE rta_proc() LANGUAGE plpgsql AS $$
DECLARE
  r record;
BEGIN
  BEGIN
    ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
    r := '(1,\\x4649584d45)'::rta_type;
    RAISE NOTICE 'b is bytea: %', r;
    RAISE raise_exception;
  EXCEPTION WHEN raise_exception THEN
    ALTER TYPE rta_type ADD ATTRIBUTE b text;
    RAISE NOTICE 'b is text: %', r;
  END;
END;
$$;
CALL rta_proc();
NOTICE:  b is bytea: (1,"\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_proc() line 12 at RAISE
DROP PROCEDURE rta_proc();
DROP TYPE rta_type;
-- Same in a function
CREATE TYPE rta_type AS (a int);
CREATE FUNCTION rta_func() RETURNS void LANGUAGE plpgsql AS $$
DECLARE
  r record;
BEGIN
  BEGIN
    ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
    r := '(1,\\x4649584d45)'::rta_type;
    RAISE NOTICE 'b is bytea: %', r;
    RAISE raise_exception;
  EXCEPTION WHEN raise_exception THEN
    ALTER TYPE rta_type ADD ATTRIBUTE b text;
    RAISE NOTICE 'b is text: %', r;
  END;
END;
$$;
SELECT rta_func();
NOTICE:  b is bytea: (1,"\\x4649584d45")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_func() line 12 at RAISE
DROP FUNCTION rta_func();
DROP TYPE rta_type;
-- Same in a set returning function, with the datum nested in a table's row
-- type
CREATE TYPE rta_type AS (a int);
CREATE TABLE rta_tab (c rta_type);
CREATE FUNCTION rta_func() RETURNS TABLE (a int, c rta_tab)
LANGUAGE plpgsql AS $$
BEGIN
  a := 1;
  BEGIN
    ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
    c := ROW(ROW(1, '\x4649584d45'::bytea));
    RETURN NEXT;
    RAISE raise_exception;
  EXCEPTION WHEN raise_exception THEN
    RETURN NEXT;
    ALTER TYPE rta_type ADD ATTRIBUTE b text;
    RETURN NEXT;
  END;
END;
$$;
SELECT * FROM rta_func();
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP FUNCTION rta_func();
DROP TABLE rta_tab;
DROP TYPE rta_type;
-- Same in a function, with the datum nested in a variable of a table's row
-- type
CREATE TYPE rta_type AS (a int);
CREATE TABLE rta_tab (c rta_type);
CREATE FUNCTION rta_func() RETURNS void LANGUAGE plpgsql AS $$
DECLARE
  r rta_tab;
BEGIN
  BEGIN
    ALTER TYPE rta_type ADD ATTRIBUTE b bytea;
    r := ROW(ROW(1, '\x4649584d45'::bytea));
    RAISE NOTICE 'b is bytea: %', r;
    RAISE raise_exception;
  EXCEPTION WHEN raise_exception THEN
    ALTER TYPE rta_type ADD ATTRIBUTE b text;
    RAISE NOTICE 'b is text: %', r;
  END;
END;
$$;
SELECT rta_func();
NOTICE:  b is bytea: ("(1,""\\\\x4649584d45"")")
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  PL/pgSQL function rta_func() line 12 at RAISE
DROP FUNCTION rta_func();
DROP TABLE rta_tab;
DROP TYPE rta_type;
--
-- ALTER by a function, while the query calling it is executing
--
-- Constants formed before the ALTER, read after it
CREATE TABLE rta_tab (a int, b bytea);
SELECT '(1,\\x4649584d45)'::rta_tab,
       rta_exec('ALTER TABLE rta_tab ALTER COLUMN b TYPE text'),
       '(2,\\x464958544f4f)'::rta_tab;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP TABLE rta_tab;
-- Same for a composite type, in a prepared statement
CREATE TYPE rta_type AS (a int, b bytea);
PREPARE rta_stmt AS
  SELECT NULL, NULL
  UNION ALL
  SELECT NULL, rta_exec('ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text')
  UNION ALL
  SELECT '(1,\\x4649584d45)'::rta_type::text, NULL;
EXECUTE rta_stmt;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DEALLOCATE rta_stmt;
DROP TYPE rta_type;
-- A datum formed before the ALTER, read via make_expanded_record_from_datum()
CREATE TYPE rta_type AS (a text, b bytea);
SELECT expanded_record_field_text(x, 2),
       rta_exec('ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text'),
       expanded_record_field_text(x, 2)
  FROM (SELECT '(aaa,\\x4649584d45)'::rta_type AS x OFFSET 0) s;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP TYPE rta_type;
-- An expanded record built from a datum before the ALTER, with its field read
-- after it.  expanded_record_identity() returns the expanded record itself,
-- and the ALTER runs while evaluating the second argument.
CREATE TYPE rta_type AS (a text, b bytea);
SELECT expanded_record_field_text(
         expanded_record_identity(x),
         2 + length(rta_exec('ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text')::text))
  FROM (SELECT '(aaa,\\x4649584d45)'::rta_type AS x OFFSET 0) s;
 expanded_record_field_text 
----------------------------
 \x4649584d45
(1 row)
 
DROP TYPE rta_type;
--
-- ALTER while a single expression is being evaluated, after the descriptor
-- has been looked up but before the datum built with it is read
--
-- The CHECK constraints of the domains below execute DDL, which is not
-- allowed in parallel mode.
SET debug_parallel_query = off;
-- A volatile function as a field of a ROW() expression
CREATE TYPE rta_type AS (x int, b bytea);
CREATE FUNCTION rta_func() RETURNS int LANGUAGE plpgsql AS $$
BEGIN
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RETURN 1;
END;
$$;
SELECT (ROW(rta_func(), '\x4649584d45'::bytea)::rta_type)::text;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP FUNCTION rta_func();
DROP TYPE rta_type;
-- A volatile function as json_populate_record()'s argument, altering the
-- type between two rows
CREATE TYPE rta_type AS (b bytea);
CREATE FUNCTION rta_func(n int) RETURNS json LANGUAGE plpgsql AS $$
BEGIN
  IF n = 2 THEN
    ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  END IF;
  RETURN '{"b": "\\x4649584d45"}'::json;
END;
$$;
SELECT n, json_populate_record(NULL::rta_type, rta_func(n))::text
  FROM generate_series(1, 2) g(n);
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP FUNCTION rta_func(int);
DROP TYPE rta_type;
-- A domain over the row type, whose CHECK constraint is evaluated when
-- coercing a value of the row type
CREATE TYPE rta_type AS (b bytea);
CREATE FUNCTION rta_func() RETURNS bool LANGUAGE plpgsql AS $$
BEGIN
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RETURN true;
END;
$$;
CREATE DOMAIN rta_dom AS rta_type CHECK (rta_func());
SELECT ((ROW('\x4649584d45'::bytea)::rta_type)::rta_dom)::text;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP DOMAIN rta_dom;
DROP FUNCTION rta_func();
DROP TYPE rta_type;
-- A domain typed attribute, whose CHECK constraint is evaluated while
-- record_in() builds the datum
CREATE FUNCTION rta_func() RETURNS bool LANGUAGE plpgsql AS $$
BEGIN
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RETURN true;
END;
$$;
CREATE DOMAIN rta_dom AS boolean CHECK (rta_func());
CREATE TYPE rta_type AS (b bytea, gate rta_dom);
-- The first cast to text is to avoid the RANDOMIZE_ALLOCATED_MEMORY logic in
-- coerce_type(), which would otherwise trigger and make the test unstable.
SELECT ('(\\x4649584d45,true)'::text::rta_type)::text;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP TYPE rta_type;
DROP DOMAIN rta_dom;
DROP FUNCTION rta_func();
-- A SQL function altering the type of its argument before reading it.  A
-- generic plan prevents the argument from being folded into the query.
CREATE TYPE rta_type AS (b bytea);
CREATE FUNCTION rta_func(x anyelement) RETURNS text LANGUAGE sql
SET plan_cache_mode = force_generic_plan AS $$
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  SELECT x::text;
$$;
SELECT rta_func('(\\x4649584d45)'::rta_type);
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
CONTEXT:  SQL function "rta_func" statement 2
DROP FUNCTION rta_func(anyelement);
DROP TYPE rta_type;
-- Same, but the datum is the function's polymorphic result
CREATE TYPE rta_type AS (b bytea);
CREATE FUNCTION rta_func(x anyelement) RETURNS anyelement LANGUAGE sql
SET plan_cache_mode = force_generic_plan AS $$
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  SELECT '\x4649584d45'::bytea AS b;
$$;
SELECT rta_func(NULL::rta_type)::text;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP FUNCTION rta_func(anyelement);
DROP TYPE rta_type;
-- A prepared statement comparing an indexed column with a parameter of the
-- row type.  The ALTER is performed by a domain CHECK constraint on another
-- parameter, i.e. between the coercion of the two parameters.  That order
-- needs an index scan, which coerces both parameters before comparing.
SET enable_seqscan = off;
SET enable_bitmapscan = off;
CREATE TYPE rta_key AS (b text);
CREATE TYPE rta_type AS (b bytea);
CREATE FUNCTION rta_func(int) RETURNS bool LANGUAGE plpgsql AS $$
BEGIN
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RETURN true;
END;
$$;
CREATE DOMAIN rta_dom AS integer CHECK (rta_func(VALUE));
CREATE TABLE rta_tab (k rta_key, gate integer);
CREATE INDEX ON rta_tab (k, gate);
-- a key matching the parameter only if it is misread
INSERT INTO rta_tab VALUES (ROW(convert_from('\x4649584d45', 'UTF8')), 1);
PREPARE rta_stmt(text, integer) AS
  SELECT 1 FROM rta_tab WHERE k = $1::rta_type AND gate = $2::rta_dom;
EXPLAIN (COSTS OFF) EXECUTE rta_stmt('(\\x4649584d45)', 1);
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Index Only Scan using rta_tab_k_gate_idx on rta_tab
   Index Cond: ((k = ('(\\x4649584d45)'::cstring)::rta_type) AND (gate = ((1)::rta_dom)::integer))
(2 rows)
 
EXECUTE rta_stmt('(\\x4649584d45)', 1);
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DEALLOCATE rta_stmt;
RESET enable_seqscan;
RESET enable_bitmapscan;
DROP TABLE rta_tab;
DROP DOMAIN rta_dom;
DROP FUNCTION rta_func(int);
DROP TYPE rta_type;
DROP TYPE rta_key;
RESET debug_parallel_query;
--
-- Out-of-line and compressed field values
--
-- Forming a datum from a tuple with out-of-line values takes separate paths,
-- which deform the tuple and form it anew.  Some of them also decompress
-- in-line compressed values.
--
-- comp is stored compressed in-line, ext out-of-line
CREATE TABLE rta_toast (id int, b bytea, comp text, ext text);
ALTER TABLE rta_toast ALTER COLUMN comp SET STORAGE MAIN,
  ALTER COLUMN ext SET STORAGE EXTERNAL;
SELECT pg_relation_size(reltoastrelid) = 0 AS toast_empty
  FROM pg_class WHERE oid = 'rta_toast'::regclass;
 toast_empty 
-------------
 t
(1 row)
 
INSERT INTO rta_toast VALUES (1, '\x4649584d45', repeat('c', 3000), repeat('e', 3000));
SELECT pg_column_compression(comp) IS NOT NULL AS comp_compressed,
       (SELECT pg_relation_size(reltoastrelid) > 0 FROM pg_class
         WHERE oid = 'rta_toast'::regclass) AS ext_out_of_line
  FROM rta_toast;
 comp_compressed | ext_out_of_line 
-----------------+-----------------
 t               | t
(1 row)
 
-- The result of a SQL function returning a row with such values, formed by
-- heap_copy_tuple_as_datum(), read before and after the ALTER
CREATE TYPE rta_type AS (id int, b bytea, comp text, ext text);
CREATE FUNCTION rta_toast_row() RETURNS rta_type LANGUAGE sql
  AS $$SELECT * FROM rta_toast$$;
SELECT (r).id, (r).b, length((r).comp), length((r).ext)
  FROM (SELECT rta_toast_row() AS r OFFSET 0) s;
 id |      b       | length | length 
----+--------------+--------+--------
  1 | \x4649584d45 |   3000 |   3000
(1 row)
 
SELECT split_part(r::text, ',', 2),
       rta_exec('ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text'),
       split_part(r::text, ',', 2)
  FROM (SELECT rta_toast_row() AS r OFFSET 0) s;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP FUNCTION rta_toast_row();
DROP TYPE rta_type;
-- A ROW() expression over such values, inlined by HeapTupleHeaderGetDatum()
-- with the type's current descriptor.  A function in the ROW() alters the
-- type after the descriptor the row is formed with was looked up.
CREATE TYPE rta_type AS (x int, b bytea, ext text);
CREATE FUNCTION rta_func() RETURNS int LANGUAGE plpgsql AS $$
BEGIN
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RETURN 1;
END;
$$;
SELECT (r).b, length((r).ext)
  FROM (SELECT ROW(0, b, ext)::rta_type AS r FROM rta_toast OFFSET 0) s;
      b       | length 
--------------+--------
 \x4649584d45 |   3000
(1 row)
 
SELECT split_part(r::text, ',', 2)
  FROM (SELECT ROW(rta_func(), b, ext)::rta_type AS r FROM rta_toast OFFSET 0) s;
ERROR:  row type of datum tuple was altered after the datum's creation
HINT:  See server log for additional details.
DROP FUNCTION rta_func();
DROP TYPE rta_type;
-- An assignment from a query result in a non-atomic context inlines the
-- values, so that they survive the end of the transaction.  The projection
-- makes the result row a datum-style tuple.
DO $$
DECLARE
  r rta_toast;
BEGIN
  SELECT id + 0, b, comp, ext INTO r FROM rta_toast;
  COMMIT AND CHAIN;
  RAISE NOTICE 'r.b: %, length(r.ext): %', r.b, length(r.ext);
END;
$$;
NOTICE:  r.b: \x4649584d45, length(r.ext): 3000
DROP TABLE rta_toast;
--
-- Parallel query
--
-- Composite datums are passed between leader and workers as they are.  The
-- ALTER below runs in this session, before the parallel query, so a datum
-- formed before it reaches the workers as a parameter.
--
SET parallel_setup_cost = 0;
SET parallel_tuple_cost = 0;
SET min_parallel_table_scan_size = 0;
SET max_parallel_workers_per_gather = 2;
SET parallel_leader_participation = off;
-- the leader runs the plan itself if no worker can be started, so don't
-- show which process reports an error
\set VERBOSITY terse
CREATE TABLE rta_par (id int);
INSERT INTO rta_par SELECT generate_series(1, 10);
-- A variable formed before the ALTER, passed to the workers as a parameter.
-- Unlike SELECT INTO, RETURN QUERY can use parallel workers.  Its query is
-- planned like this prepared statement.
CREATE TYPE rta_type AS (b bytea);
SET plan_cache_mode = force_generic_plan;
PREPARE rta_stmt(rta_type) AS SELECT min($1::text || id) FROM rta_par;
EXPLAIN (COSTS OFF) EXECUTE rta_stmt(NULL);
                   QUERY PLAN                   
------------------------------------------------
 Finalize Aggregate
   ->  Gather
         Workers Planned: 2
         ->  Partial Aggregate
               ->  Parallel Seq Scan on rta_par
(5 rows)
 
DEALLOCATE rta_stmt;
CREATE FUNCTION rta_func() RETURNS SETOF text LANGUAGE plpgsql AS $$
DECLARE
  v rta_type;
BEGIN
  v := ROW('\x4649584d45')::rta_type;
  ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text;
  RETURN QUERY SELECT min(v::text || id) FROM rta_par;
END;
$$;
SELECT * FROM rta_func();
ERROR:  row type of datum tuple was altered after the datum's creation
RESET plan_cache_mode;
DROP FUNCTION rta_func();
DROP TYPE rta_type;
DROP TABLE rta_par;
\set VERBOSITY default
RESET parallel_setup_cost;
RESET parallel_tuple_cost;
RESET min_parallel_table_scan_size;
RESET max_parallel_workers_per_gather;
RESET parallel_leader_participation;
DROP FUNCTION rta_exec(text);