summaryrefslogtreecommitdiffstats
path: root/src/test/regress/expected/updatable_views.out
blob: cdff914b93ae8f9be86fbf855e4591715b97455e (plain)
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
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
--
-- UPDATABLE VIEWS
--
-- avoid bit-exact output here because operations may not be bit-exact.
SET extra_float_digits = 0;
-- check that non-updatable views and columns are rejected with useful error
-- messages
CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified');
INSERT INTO base_tbl SELECT i, 'Row ' || i FROM generate_series(-2, 2) g(i);
CREATE VIEW ro_view1 AS SELECT DISTINCT a, b FROM base_tbl; -- DISTINCT not supported
CREATE VIEW ro_view2 AS SELECT a, b FROM base_tbl GROUP BY a, b; -- GROUP BY not supported
CREATE VIEW ro_view3 AS SELECT 1 FROM base_tbl HAVING max(a) > 0; -- HAVING not supported
CREATE VIEW ro_view4 AS SELECT count(*) FROM base_tbl; -- Aggregate functions not supported
CREATE VIEW ro_view5 AS SELECT a, rank() OVER() FROM base_tbl; -- Window functions not supported
CREATE VIEW ro_view6 AS SELECT a, b FROM base_tbl UNION SELECT -a, b FROM base_tbl; -- Set ops not supported
CREATE VIEW ro_view7 AS WITH t AS (SELECT a, b FROM base_tbl) SELECT * FROM t; -- WITH not supported
CREATE VIEW ro_view8 AS SELECT a, b FROM base_tbl ORDER BY a OFFSET 1; -- OFFSET not supported
CREATE VIEW ro_view9 AS SELECT a, b FROM base_tbl ORDER BY a LIMIT 1; -- LIMIT not supported
CREATE VIEW ro_view10 AS SELECT 1 AS a; -- No base relations
CREATE VIEW ro_view11 AS SELECT b1.a, b2.b FROM base_tbl b1, base_tbl b2; -- Multiple base relations
CREATE VIEW ro_view12 AS SELECT * FROM generate_series(1, 10) AS g(a); -- SRF in rangetable
CREATE VIEW ro_view13 AS SELECT a, b FROM (SELECT * FROM base_tbl) AS t; -- Subselect in rangetable
CREATE VIEW rw_view14 AS SELECT ctid, a, b FROM base_tbl; -- System columns may be part of an updatable view
CREATE VIEW rw_view15 AS SELECT a, upper(b) FROM base_tbl; -- Expression/function may be part of an updatable view
CREATE VIEW rw_view16 AS SELECT a, b, a AS aa FROM base_tbl; -- Repeated column may be part of an updatable view
CREATE VIEW ro_view17 AS SELECT * FROM ro_view1; -- Base relation not updatable
CREATE VIEW ro_view18 AS SELECT * FROM (VALUES(1)) AS tmp(a); -- VALUES in rangetable
CREATE SEQUENCE uv_seq;
CREATE VIEW ro_view19 AS SELECT * FROM uv_seq; -- View based on a sequence
CREATE VIEW ro_view20 AS SELECT a, b, generate_series(1, a) g FROM base_tbl; -- SRF in targetlist not supported
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE E'r_\\_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 ro_view1   | NO
 ro_view10  | NO
 ro_view11  | NO
 ro_view12  | NO
 ro_view13  | NO
 ro_view17  | NO
 ro_view18  | NO
 ro_view19  | NO
 ro_view2   | NO
 ro_view20  | NO
 ro_view3   | NO
 ro_view4   | NO
 ro_view5   | NO
 ro_view6   | NO
 ro_view7   | NO
 ro_view8   | NO
 ro_view9   | NO
 rw_view14  | YES
 rw_view15  | YES
 rw_view16  | YES
(20 rows)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE E'r_\\_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 ro_view1   | NO           | NO
 ro_view10  | NO           | NO
 ro_view11  | NO           | NO
 ro_view12  | NO           | NO
 ro_view13  | NO           | NO
 ro_view17  | NO           | NO
 ro_view18  | NO           | NO
 ro_view19  | NO           | NO
 ro_view2   | NO           | NO
 ro_view20  | NO           | NO
 ro_view3   | NO           | NO
 ro_view4   | NO           | NO
 ro_view5   | NO           | NO
 ro_view6   | NO           | NO
 ro_view7   | NO           | NO
 ro_view8   | NO           | NO
 ro_view9   | NO           | NO
 rw_view14  | YES          | YES
 rw_view15  | YES          | YES
 rw_view16  | YES          | YES
(20 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE E'r_\\_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 ro_view1   | a           | NO
 ro_view1   | b           | NO
 ro_view10  | a           | NO
 ro_view11  | a           | NO
 ro_view11  | b           | NO
 ro_view12  | a           | NO
 ro_view13  | a           | NO
 ro_view13  | b           | NO
 ro_view17  | a           | NO
 ro_view17  | b           | NO
 ro_view18  | a           | NO
 ro_view19  | last_value  | NO
 ro_view19  | log_cnt     | NO
 ro_view19  | is_called   | NO
 ro_view2   | a           | NO
 ro_view2   | b           | NO
 ro_view20  | a           | NO
 ro_view20  | b           | NO
 ro_view20  | g           | NO
 ro_view3   | ?column?    | NO
 ro_view4   | count       | NO
 ro_view5   | a           | NO
 ro_view5   | rank        | NO
 ro_view6   | a           | NO
 ro_view6   | b           | NO
 ro_view7   | a           | NO
 ro_view7   | b           | NO
 ro_view8   | a           | NO
 ro_view8   | b           | NO
 ro_view9   | a           | NO
 ro_view9   | b           | NO
 rw_view14  | ctid        | NO
 rw_view14  | a           | YES
 rw_view14  | b           | YES
 rw_view15  | a           | YES
 rw_view15  | upper       | NO
 rw_view16  | a           | YES
 rw_view16  | b           | YES
 rw_view16  | aa          | YES
(39 rows)

-- Read-only views
DELETE FROM ro_view1;
ERROR:  cannot delete from view "ro_view1"
DETAIL:  Views containing DISTINCT are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
DELETE FROM ro_view2;
ERROR:  cannot delete from view "ro_view2"
DETAIL:  Views containing GROUP BY are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
DELETE FROM ro_view3;
ERROR:  cannot delete from view "ro_view3"
DETAIL:  Views containing HAVING are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
DELETE FROM ro_view4;
ERROR:  cannot delete from view "ro_view4"
DETAIL:  Views that return aggregate functions are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
DELETE FROM ro_view5;
ERROR:  cannot delete from view "ro_view5"
DETAIL:  Views that return window functions are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
DELETE FROM ro_view6;
ERROR:  cannot delete from view "ro_view6"
DETAIL:  Views containing UNION, INTERSECT, or EXCEPT are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
UPDATE ro_view7 SET a=a+1;
ERROR:  cannot update view "ro_view7"
DETAIL:  Views containing WITH are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
UPDATE ro_view8 SET a=a+1;
ERROR:  cannot update view "ro_view8"
DETAIL:  Views containing LIMIT or OFFSET are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
UPDATE ro_view9 SET a=a+1;
ERROR:  cannot update view "ro_view9"
DETAIL:  Views containing LIMIT or OFFSET are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
UPDATE ro_view10 SET a=a+1;
ERROR:  cannot update view "ro_view10"
DETAIL:  Views that do not select from a single table or view are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
UPDATE ro_view11 SET a=a+1;
ERROR:  cannot update view "ro_view11"
DETAIL:  Views that do not select from a single table or view are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
UPDATE ro_view12 SET a=a+1;
ERROR:  cannot update view "ro_view12"
DETAIL:  Views that do not select from a single table or view are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
INSERT INTO ro_view13 VALUES (3, 'Row 3');
ERROR:  cannot insert into view "ro_view13"
DETAIL:  Views that do not select from a single table or view are not automatically updatable.
HINT:  To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.
-- Partially updatable view
INSERT INTO rw_view14 VALUES (null, 3, 'Row 3'); -- should fail
ERROR:  cannot insert into column "ctid" of view "rw_view14"
DETAIL:  View columns that refer to system columns are not updatable.
INSERT INTO rw_view14 (a, b) VALUES (3, 'Row 3'); -- should be OK
UPDATE rw_view14 SET ctid=null WHERE a=3; -- should fail
ERROR:  cannot update column "ctid" of view "rw_view14"
DETAIL:  View columns that refer to system columns are not updatable.
UPDATE rw_view14 SET b='ROW 3' WHERE a=3; -- should be OK
SELECT * FROM base_tbl;
 a  |   b    
----+--------
 -2 | Row -2
 -1 | Row -1
  0 | Row 0
  1 | Row 1
  2 | Row 2
  3 | ROW 3
(6 rows)

DELETE FROM rw_view14 WHERE a=3; -- should be OK
-- Partially updatable view
INSERT INTO rw_view15 VALUES (3, 'ROW 3'); -- should fail
ERROR:  cannot insert into column "upper" of view "rw_view15"
DETAIL:  View columns that are not columns of their base relation are not updatable.
INSERT INTO rw_view15 (a) VALUES (3); -- should be OK
INSERT INTO rw_view15 (a) VALUES (3) ON CONFLICT DO NOTHING; -- succeeds
SELECT * FROM rw_view15;
 a  |    upper    
----+-------------
 -2 | ROW -2
 -1 | ROW -1
  0 | ROW 0
  1 | ROW 1
  2 | ROW 2
  3 | UNSPECIFIED
(6 rows)

INSERT INTO rw_view15 (a) VALUES (3) ON CONFLICT (a) DO NOTHING; -- succeeds
SELECT * FROM rw_view15;
 a  |    upper    
----+-------------
 -2 | ROW -2
 -1 | ROW -1
  0 | ROW 0
  1 | ROW 1
  2 | ROW 2
  3 | UNSPECIFIED
(6 rows)

INSERT INTO rw_view15 (a) VALUES (3) ON CONFLICT (a) DO UPDATE set a = excluded.a; -- succeeds
SELECT * FROM rw_view15;
 a  |    upper    
----+-------------
 -2 | ROW -2
 -1 | ROW -1
  0 | ROW 0
  1 | ROW 1
  2 | ROW 2
  3 | UNSPECIFIED
(6 rows)

INSERT INTO rw_view15 (a) VALUES (3) ON CONFLICT (a) DO UPDATE set upper = 'blarg'; -- fails
ERROR:  cannot insert into column "upper" of view "rw_view15"
DETAIL:  View columns that are not columns of their base relation are not updatable.
SELECT * FROM rw_view15;
 a  |    upper    
----+-------------
 -2 | ROW -2
 -1 | ROW -1
  0 | ROW 0
  1 | ROW 1
  2 | ROW 2
  3 | UNSPECIFIED
(6 rows)

SELECT * FROM rw_view15;
 a  |    upper    
----+-------------
 -2 | ROW -2
 -1 | ROW -1
  0 | ROW 0
  1 | ROW 1
  2 | ROW 2
  3 | UNSPECIFIED
(6 rows)

ALTER VIEW rw_view15 ALTER COLUMN upper SET DEFAULT 'NOT SET';
INSERT INTO rw_view15 (a) VALUES (4); -- should fail
ERROR:  cannot insert into column "upper" of view "rw_view15"
DETAIL:  View columns that are not columns of their base relation are not updatable.
UPDATE rw_view15 SET upper='ROW 3' WHERE a=3; -- should fail
ERROR:  cannot update column "upper" of view "rw_view15"
DETAIL:  View columns that are not columns of their base relation are not updatable.
UPDATE rw_view15 SET upper=DEFAULT WHERE a=3; -- should fail
ERROR:  cannot update column "upper" of view "rw_view15"
DETAIL:  View columns that are not columns of their base relation are not updatable.
UPDATE rw_view15 SET a=4 WHERE a=3; -- should be OK
SELECT * FROM base_tbl;
 a  |      b      
----+-------------
 -2 | Row -2
 -1 | Row -1
  0 | Row 0
  1 | Row 1
  2 | Row 2
  4 | Unspecified
(6 rows)

DELETE FROM rw_view15 WHERE a=4; -- should be OK
-- Partially updatable view
INSERT INTO rw_view16 VALUES (3, 'Row 3', 3); -- should fail
ERROR:  multiple assignments to same column "a"
INSERT INTO rw_view16 (a, b) VALUES (3, 'Row 3'); -- should be OK
UPDATE rw_view16 SET a=3, aa=-3 WHERE a=3; -- should fail
ERROR:  multiple assignments to same column "a"
UPDATE rw_view16 SET aa=-3 WHERE a=3; -- should be OK
SELECT * FROM base_tbl;
 a  |   b    
----+--------
 -2 | Row -2
 -1 | Row -1
  0 | Row 0
  1 | Row 1
  2 | Row 2
 -3 | Row 3
(6 rows)

DELETE FROM rw_view16 WHERE a=-3; -- should be OK
-- Read-only views
INSERT INTO ro_view17 VALUES (3, 'ROW 3');
ERROR:  cannot insert into view "ro_view1"
DETAIL:  Views containing DISTINCT are not automatically updatable.
HINT:  To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.
DELETE FROM ro_view18;
ERROR:  cannot delete from view "ro_view18"
DETAIL:  Views that do not select from a single table or view are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
UPDATE ro_view19 SET last_value=1000;
ERROR:  cannot update view "ro_view19"
DETAIL:  Views that do not select from a single table or view are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
UPDATE ro_view20 SET b=upper(b);
ERROR:  cannot update view "ro_view20"
DETAIL:  Views that return set-returning functions are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
-- A view with a conditional INSTEAD rule but no unconditional INSTEAD rules
-- or INSTEAD OF triggers should be non-updatable and generate useful error
-- messages with appropriate detail
CREATE RULE rw_view16_ins_rule AS ON INSERT TO rw_view16
  WHERE NEW.a > 0 DO INSTEAD INSERT INTO base_tbl VALUES (NEW.a, NEW.b);
CREATE RULE rw_view16_upd_rule AS ON UPDATE TO rw_view16
  WHERE OLD.a > 0 DO INSTEAD UPDATE base_tbl SET b=NEW.b WHERE a=OLD.a;
CREATE RULE rw_view16_del_rule AS ON DELETE TO rw_view16
  WHERE OLD.a > 0 DO INSTEAD DELETE FROM base_tbl WHERE a=OLD.a;
INSERT INTO rw_view16 (a, b) VALUES (3, 'Row 3'); -- should fail
ERROR:  cannot insert into view "rw_view16"
DETAIL:  Views with conditional DO INSTEAD rules are not automatically updatable.
HINT:  To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.
UPDATE rw_view16 SET b='ROW 2' WHERE a=2; -- should fail
ERROR:  cannot update view "rw_view16"
DETAIL:  Views with conditional DO INSTEAD rules are not automatically updatable.
HINT:  To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.
DELETE FROM rw_view16 WHERE a=2; -- should fail
ERROR:  cannot delete from view "rw_view16"
DETAIL:  Views with conditional DO INSTEAD rules are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 16 other objects
DETAIL:  drop cascades to view ro_view1
drop cascades to view ro_view17
drop cascades to view ro_view2
drop cascades to view ro_view3
drop cascades to view ro_view4
drop cascades to view ro_view5
drop cascades to view ro_view6
drop cascades to view ro_view7
drop cascades to view ro_view8
drop cascades to view ro_view9
drop cascades to view ro_view11
drop cascades to view ro_view13
drop cascades to view rw_view14
drop cascades to view rw_view15
drop cascades to view rw_view16
drop cascades to view ro_view20
DROP VIEW ro_view10, ro_view12, ro_view18;
DROP SEQUENCE uv_seq CASCADE;
NOTICE:  drop cascades to view ro_view19
-- simple updatable view
CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified');
INSERT INTO base_tbl SELECT i, 'Row ' || i FROM generate_series(-2, 2) g(i);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WHERE a>0;
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name = 'rw_view1';
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | YES
(1 row)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name = 'rw_view1';
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view1   | YES          | YES
(1 row)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name = 'rw_view1'
 ORDER BY ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | YES
 rw_view1   | b           | YES
(2 rows)

INSERT INTO rw_view1 VALUES (3, 'Row 3');
INSERT INTO rw_view1 (a) VALUES (4);
UPDATE rw_view1 SET a=5 WHERE a=4;
DELETE FROM rw_view1 WHERE b='Row 2';
SELECT * FROM base_tbl;
 a  |      b      
----+-------------
 -2 | Row -2
 -1 | Row -1
  0 | Row 0
  1 | Row 1
  3 | Row 3
  5 | Unspecified
(6 rows)

EXPLAIN (costs off) UPDATE rw_view1 SET a=6 WHERE a=5;
                    QUERY PLAN                    
--------------------------------------------------
 Update on base_tbl
   ->  Index Scan using base_tbl_pkey on base_tbl
         Index Cond: ((a > 0) AND (a = 5))
(3 rows)

EXPLAIN (costs off) DELETE FROM rw_view1 WHERE a=5;
                    QUERY PLAN                    
--------------------------------------------------
 Delete on base_tbl
   ->  Index Scan using base_tbl_pkey on base_tbl
         Index Cond: ((a > 0) AND (a = 5))
(3 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- view on top of view
CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified');
INSERT INTO base_tbl SELECT i, 'Row ' || i FROM generate_series(-2, 2) g(i);
CREATE VIEW rw_view1 AS SELECT b AS bb, a AS aa FROM base_tbl WHERE a>0;
CREATE VIEW rw_view2 AS SELECT aa AS aaa, bb AS bbb FROM rw_view1 WHERE aa<10;
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name = 'rw_view2';
 table_name | is_insertable_into 
------------+--------------------
 rw_view2   | YES
(1 row)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name = 'rw_view2';
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view2   | YES          | YES
(1 row)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name = 'rw_view2'
 ORDER BY ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view2   | aaa         | YES
 rw_view2   | bbb         | YES
(2 rows)

INSERT INTO rw_view2 VALUES (3, 'Row 3');
INSERT INTO rw_view2 (aaa) VALUES (4);
SELECT * FROM rw_view2;
 aaa |     bbb     
-----+-------------
   1 | Row 1
   2 | Row 2
   3 | Row 3
   4 | Unspecified
(4 rows)

UPDATE rw_view2 SET bbb='Row 4' WHERE aaa=4;
DELETE FROM rw_view2 WHERE aaa=2;
SELECT * FROM rw_view2;
 aaa |  bbb  
-----+-------
   1 | Row 1
   3 | Row 3
   4 | Row 4
(3 rows)

EXPLAIN (costs off) UPDATE rw_view2 SET aaa=5 WHERE aaa=4;
                       QUERY PLAN                       
--------------------------------------------------------
 Update on base_tbl
   ->  Index Scan using base_tbl_pkey on base_tbl
         Index Cond: ((a < 10) AND (a > 0) AND (a = 4))
(3 rows)

EXPLAIN (costs off) DELETE FROM rw_view2 WHERE aaa=4;
                       QUERY PLAN                       
--------------------------------------------------------
 Delete on base_tbl
   ->  Index Scan using base_tbl_pkey on base_tbl
         Index Cond: ((a < 10) AND (a > 0) AND (a = 4))
(3 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
-- view on top of view with rules
CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified');
INSERT INTO base_tbl SELECT i, 'Row ' || i FROM generate_series(-2, 2) g(i);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WHERE a>0 OFFSET 0; -- not updatable without rules/triggers
CREATE VIEW rw_view2 AS SELECT * FROM rw_view1 WHERE a<10;
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | NO
 rw_view2   | NO
(2 rows)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view1   | NO           | NO
 rw_view2   | NO           | NO
(2 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | NO
 rw_view1   | b           | NO
 rw_view2   | a           | NO
 rw_view2   | b           | NO
(4 rows)

CREATE RULE rw_view1_ins_rule AS ON INSERT TO rw_view1
  DO INSTEAD INSERT INTO base_tbl VALUES (NEW.a, NEW.b) RETURNING *;
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | YES
 rw_view2   | YES
(2 rows)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view1   | NO           | YES
 rw_view2   | NO           | YES
(2 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | NO
 rw_view1   | b           | NO
 rw_view2   | a           | NO
 rw_view2   | b           | NO
(4 rows)

CREATE RULE rw_view1_upd_rule AS ON UPDATE TO rw_view1
  DO INSTEAD UPDATE base_tbl SET b=NEW.b WHERE a=OLD.a RETURNING NEW.*;
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | YES
 rw_view2   | YES
(2 rows)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view1   | NO           | YES
 rw_view2   | NO           | YES
(2 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | NO
 rw_view1   | b           | NO
 rw_view2   | a           | NO
 rw_view2   | b           | NO
(4 rows)

CREATE RULE rw_view1_del_rule AS ON DELETE TO rw_view1
  DO INSTEAD DELETE FROM base_tbl WHERE a=OLD.a RETURNING OLD.*;
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | YES
 rw_view2   | YES
(2 rows)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view1   | YES          | YES
 rw_view2   | YES          | YES
(2 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | YES
 rw_view1   | b           | YES
 rw_view2   | a           | YES
 rw_view2   | b           | YES
(4 rows)

INSERT INTO rw_view2 VALUES (3, 'Row 3') RETURNING *;
 a |   b   
---+-------
 3 | Row 3
(1 row)

UPDATE rw_view2 SET b='Row three' WHERE a=3 RETURNING *;
 a |     b     
---+-----------
 3 | Row three
(1 row)

SELECT * FROM rw_view2;
 a |     b     
---+-----------
 1 | Row 1
 2 | Row 2
 3 | Row three
(3 rows)

DELETE FROM rw_view2 WHERE a=3 RETURNING *;
 a |     b     
---+-----------
 3 | Row three
(1 row)

SELECT * FROM rw_view2;
 a |   b   
---+-------
 1 | Row 1
 2 | Row 2
(2 rows)

EXPLAIN (costs off) UPDATE rw_view2 SET a=3 WHERE a=2;
                           QUERY PLAN                           
----------------------------------------------------------------
 Update on base_tbl
   ->  Nested Loop
         ->  Index Scan using base_tbl_pkey on base_tbl
               Index Cond: (a = 2)
         ->  Subquery Scan on rw_view1
               Filter: ((rw_view1.a < 10) AND (rw_view1.a = 2))
               ->  Bitmap Heap Scan on base_tbl base_tbl_1
                     Recheck Cond: (a > 0)
                     ->  Bitmap Index Scan on base_tbl_pkey
                           Index Cond: (a > 0)
(10 rows)

EXPLAIN (costs off) DELETE FROM rw_view2 WHERE a=2;
                           QUERY PLAN                           
----------------------------------------------------------------
 Delete on base_tbl
   ->  Nested Loop
         ->  Index Scan using base_tbl_pkey on base_tbl
               Index Cond: (a = 2)
         ->  Subquery Scan on rw_view1
               Filter: ((rw_view1.a < 10) AND (rw_view1.a = 2))
               ->  Bitmap Heap Scan on base_tbl base_tbl_1
                     Recheck Cond: (a > 0)
                     ->  Bitmap Index Scan on base_tbl_pkey
                           Index Cond: (a > 0)
(10 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
-- view on top of view with triggers
CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified');
INSERT INTO base_tbl SELECT i, 'Row ' || i FROM generate_series(-2, 2) g(i);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WHERE a>0 OFFSET 0; -- not updatable without rules/triggers
CREATE VIEW rw_view2 AS SELECT * FROM rw_view1 WHERE a<10;
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | NO
 rw_view2   | NO
(2 rows)

SELECT table_name, is_updatable, is_insertable_into,
       is_trigger_updatable, is_trigger_deletable,
       is_trigger_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
------------+--------------+--------------------+----------------------+----------------------+----------------------------
 rw_view1   | NO           | NO                 | NO                   | NO                   | NO
 rw_view2   | NO           | NO                 | NO                   | NO                   | NO
(2 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | NO
 rw_view1   | b           | NO
 rw_view2   | a           | NO
 rw_view2   | b           | NO
(4 rows)

CREATE FUNCTION rw_view1_trig_fn()
RETURNS trigger AS
$$
BEGIN
  IF TG_OP = 'INSERT' THEN
    INSERT INTO base_tbl VALUES (NEW.a, NEW.b);
    RETURN NEW;
  ELSIF TG_OP = 'UPDATE' THEN
    UPDATE base_tbl SET b=NEW.b WHERE a=OLD.a;
    RETURN NEW;
  ELSIF TG_OP = 'DELETE' THEN
    DELETE FROM base_tbl WHERE a=OLD.a;
    RETURN OLD;
  END IF;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER rw_view1_ins_trig INSTEAD OF INSERT ON rw_view1
  FOR EACH ROW EXECUTE PROCEDURE rw_view1_trig_fn();
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | NO
 rw_view2   | NO
(2 rows)

SELECT table_name, is_updatable, is_insertable_into,
       is_trigger_updatable, is_trigger_deletable,
       is_trigger_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
------------+--------------+--------------------+----------------------+----------------------+----------------------------
 rw_view1   | NO           | NO                 | NO                   | NO                   | YES
 rw_view2   | NO           | NO                 | NO                   | NO                   | NO
(2 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | NO
 rw_view1   | b           | NO
 rw_view2   | a           | NO
 rw_view2   | b           | NO
(4 rows)

CREATE TRIGGER rw_view1_upd_trig INSTEAD OF UPDATE ON rw_view1
  FOR EACH ROW EXECUTE PROCEDURE rw_view1_trig_fn();
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | NO
 rw_view2   | NO
(2 rows)

SELECT table_name, is_updatable, is_insertable_into,
       is_trigger_updatable, is_trigger_deletable,
       is_trigger_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
------------+--------------+--------------------+----------------------+----------------------+----------------------------
 rw_view1   | NO           | NO                 | YES                  | NO                   | YES
 rw_view2   | NO           | NO                 | NO                   | NO                   | NO
(2 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | NO
 rw_view1   | b           | NO
 rw_view2   | a           | NO
 rw_view2   | b           | NO
(4 rows)

CREATE TRIGGER rw_view1_del_trig INSTEAD OF DELETE ON rw_view1
  FOR EACH ROW EXECUTE PROCEDURE rw_view1_trig_fn();
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | NO
 rw_view2   | NO
(2 rows)

SELECT table_name, is_updatable, is_insertable_into,
       is_trigger_updatable, is_trigger_deletable,
       is_trigger_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
------------+--------------+--------------------+----------------------+----------------------+----------------------------
 rw_view1   | NO           | NO                 | YES                  | YES                  | YES
 rw_view2   | NO           | NO                 | NO                   | NO                   | NO
(2 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE 'rw_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | a           | NO
 rw_view1   | b           | NO
 rw_view2   | a           | NO
 rw_view2   | b           | NO
(4 rows)

INSERT INTO rw_view2 VALUES (3, 'Row 3') RETURNING *;
 a |   b   
---+-------
 3 | Row 3
(1 row)

UPDATE rw_view2 SET b='Row three' WHERE a=3 RETURNING *;
 a |     b     
---+-----------
 3 | Row three
(1 row)

SELECT * FROM rw_view2;
 a |     b     
---+-----------
 1 | Row 1
 2 | Row 2
 3 | Row three
(3 rows)

DELETE FROM rw_view2 WHERE a=3 RETURNING *;
 a |     b     
---+-----------
 3 | Row three
(1 row)

SELECT * FROM rw_view2;
 a |   b   
---+-------
 1 | Row 1
 2 | Row 2
(2 rows)

EXPLAIN (costs off) UPDATE rw_view2 SET a=3 WHERE a=2;
                        QUERY PLAN                        
----------------------------------------------------------
 Update on rw_view1 rw_view1_1
   ->  Subquery Scan on rw_view1
         Filter: ((rw_view1.a < 10) AND (rw_view1.a = 2))
         ->  Bitmap Heap Scan on base_tbl
               Recheck Cond: (a > 0)
               ->  Bitmap Index Scan on base_tbl_pkey
                     Index Cond: (a > 0)
(7 rows)

EXPLAIN (costs off) DELETE FROM rw_view2 WHERE a=2;
                        QUERY PLAN                        
----------------------------------------------------------
 Delete on rw_view1 rw_view1_1
   ->  Subquery Scan on rw_view1
         Filter: ((rw_view1.a < 10) AND (rw_view1.a = 2))
         ->  Bitmap Heap Scan on base_tbl
               Recheck Cond: (a > 0)
               ->  Bitmap Index Scan on base_tbl_pkey
                     Index Cond: (a > 0)
(7 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
DROP FUNCTION rw_view1_trig_fn();
-- update using whole row from view
CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified');
INSERT INTO base_tbl SELECT i, 'Row ' || i FROM generate_series(-2, 2) g(i);
CREATE VIEW rw_view1 AS SELECT b AS bb, a AS aa FROM base_tbl;
CREATE FUNCTION rw_view1_aa(x rw_view1)
  RETURNS int AS $$ SELECT x.aa $$ LANGUAGE sql;
UPDATE rw_view1 v SET bb='Updated row 2' WHERE rw_view1_aa(v)=2
  RETURNING rw_view1_aa(v), v.bb;
 rw_view1_aa |      bb       
-------------+---------------
           2 | Updated row 2
(1 row)

SELECT * FROM base_tbl;
 a  |       b       
----+---------------
 -2 | Row -2
 -1 | Row -1
  0 | Row 0
  1 | Row 1
  2 | Updated row 2
(5 rows)

EXPLAIN (costs off)
UPDATE rw_view1 v SET bb='Updated row 2' WHERE rw_view1_aa(v)=2
  RETURNING rw_view1_aa(v), v.bb;
                    QUERY PLAN                    
--------------------------------------------------
 Update on base_tbl
   ->  Index Scan using base_tbl_pkey on base_tbl
         Index Cond: (a = 2)
(3 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to function rw_view1_aa(rw_view1)
-- permissions checks
CREATE USER regress_view_user1;
CREATE USER regress_view_user2;
SET SESSION AUTHORIZATION regress_view_user1;
CREATE TABLE base_tbl(a int, b text, c float);
INSERT INTO base_tbl VALUES (1, 'Row 1', 1.0);
CREATE VIEW rw_view1 AS SELECT b AS bb, c AS cc, a AS aa FROM base_tbl;
INSERT INTO rw_view1 VALUES ('Row 2', 2.0, 2);
GRANT SELECT ON base_tbl TO regress_view_user2;
GRANT SELECT ON rw_view1 TO regress_view_user2;
GRANT UPDATE (a,c) ON base_tbl TO regress_view_user2;
GRANT UPDATE (bb,cc) ON rw_view1 TO regress_view_user2;
RESET SESSION AUTHORIZATION;
SET SESSION AUTHORIZATION regress_view_user2;
CREATE VIEW rw_view2 AS SELECT b AS bb, c AS cc, a AS aa FROM base_tbl;
SELECT * FROM base_tbl; -- ok
 a |   b   | c 
---+-------+---
 1 | Row 1 | 1
 2 | Row 2 | 2
(2 rows)

SELECT * FROM rw_view1; -- ok
  bb   | cc | aa 
-------+----+----
 Row 1 |  1 |  1
 Row 2 |  2 |  2
(2 rows)

SELECT * FROM rw_view2; -- ok
  bb   | cc | aa 
-------+----+----
 Row 1 |  1 |  1
 Row 2 |  2 |  2
(2 rows)

INSERT INTO base_tbl VALUES (3, 'Row 3', 3.0); -- not allowed
ERROR:  permission denied for table base_tbl
INSERT INTO rw_view1 VALUES ('Row 3', 3.0, 3); -- not allowed
ERROR:  permission denied for view rw_view1
INSERT INTO rw_view2 VALUES ('Row 3', 3.0, 3); -- not allowed
ERROR:  permission denied for table base_tbl
UPDATE base_tbl SET a=a, c=c; -- ok
UPDATE base_tbl SET b=b; -- not allowed
ERROR:  permission denied for table base_tbl
UPDATE rw_view1 SET bb=bb, cc=cc; -- ok
UPDATE rw_view1 SET aa=aa; -- not allowed
ERROR:  permission denied for view rw_view1
UPDATE rw_view2 SET aa=aa, cc=cc; -- ok
UPDATE rw_view2 SET bb=bb; -- not allowed
ERROR:  permission denied for table base_tbl
DELETE FROM base_tbl; -- not allowed
ERROR:  permission denied for table base_tbl
DELETE FROM rw_view1; -- not allowed
ERROR:  permission denied for view rw_view1
DELETE FROM rw_view2; -- not allowed
ERROR:  permission denied for table base_tbl
RESET SESSION AUTHORIZATION;
SET SESSION AUTHORIZATION regress_view_user1;
GRANT INSERT, DELETE ON base_tbl TO regress_view_user2;
RESET SESSION AUTHORIZATION;
SET SESSION AUTHORIZATION regress_view_user2;
INSERT INTO base_tbl VALUES (3, 'Row 3', 3.0); -- ok
INSERT INTO rw_view1 VALUES ('Row 4', 4.0, 4); -- not allowed
ERROR:  permission denied for view rw_view1
INSERT INTO rw_view2 VALUES ('Row 4', 4.0, 4); -- ok
DELETE FROM base_tbl WHERE a=1; -- ok
DELETE FROM rw_view1 WHERE aa=2; -- not allowed
ERROR:  permission denied for view rw_view1
DELETE FROM rw_view2 WHERE aa=2; -- ok
SELECT * FROM base_tbl;
 a |   b   | c 
---+-------+---
 3 | Row 3 | 3
 4 | Row 4 | 4
(2 rows)

RESET SESSION AUTHORIZATION;
SET SESSION AUTHORIZATION regress_view_user1;
REVOKE INSERT, DELETE ON base_tbl FROM regress_view_user2;
GRANT INSERT, DELETE ON rw_view1 TO regress_view_user2;
RESET SESSION AUTHORIZATION;
SET SESSION AUTHORIZATION regress_view_user2;
INSERT INTO base_tbl VALUES (5, 'Row 5', 5.0); -- not allowed
ERROR:  permission denied for table base_tbl
INSERT INTO rw_view1 VALUES ('Row 5', 5.0, 5); -- ok
INSERT INTO rw_view2 VALUES ('Row 6', 6.0, 6); -- not allowed
ERROR:  permission denied for table base_tbl
DELETE FROM base_tbl WHERE a=3; -- not allowed
ERROR:  permission denied for table base_tbl
DELETE FROM rw_view1 WHERE aa=3; -- ok
DELETE FROM rw_view2 WHERE aa=4; -- not allowed
ERROR:  permission denied for table base_tbl
SELECT * FROM base_tbl;
 a |   b   | c 
---+-------+---
 4 | Row 4 | 4
 5 | Row 5 | 5
(2 rows)

RESET SESSION AUTHORIZATION;
DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
-- nested-view permissions
CREATE TABLE base_tbl(a int, b text, c float);
INSERT INTO base_tbl VALUES (1, 'Row 1', 1.0);
SET SESSION AUTHORIZATION regress_view_user1;
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl;
SELECT * FROM rw_view1;  -- not allowed
ERROR:  permission denied for table base_tbl
SELECT * FROM rw_view1 FOR UPDATE;  -- not allowed
ERROR:  permission denied for table base_tbl
UPDATE rw_view1 SET b = 'foo' WHERE a = 1;  -- not allowed
ERROR:  permission denied for table base_tbl
SET SESSION AUTHORIZATION regress_view_user2;
CREATE VIEW rw_view2 AS SELECT * FROM rw_view1;
SELECT * FROM rw_view2;  -- not allowed
ERROR:  permission denied for view rw_view1
SELECT * FROM rw_view2 FOR UPDATE;  -- not allowed
ERROR:  permission denied for view rw_view1
UPDATE rw_view2 SET b = 'bar' WHERE a = 1;  -- not allowed
ERROR:  permission denied for view rw_view1
RESET SESSION AUTHORIZATION;
GRANT SELECT ON base_tbl TO regress_view_user1;
SET SESSION AUTHORIZATION regress_view_user1;
SELECT * FROM rw_view1;
 a |   b   | c 
---+-------+---
 1 | Row 1 | 1
(1 row)

SELECT * FROM rw_view1 FOR UPDATE;  -- not allowed
ERROR:  permission denied for table base_tbl
UPDATE rw_view1 SET b = 'foo' WHERE a = 1;  -- not allowed
ERROR:  permission denied for table base_tbl
SET SESSION AUTHORIZATION regress_view_user2;
SELECT * FROM rw_view2;  -- not allowed
ERROR:  permission denied for view rw_view1
SELECT * FROM rw_view2 FOR UPDATE;  -- not allowed
ERROR:  permission denied for view rw_view1
UPDATE rw_view2 SET b = 'bar' WHERE a = 1;  -- not allowed
ERROR:  permission denied for view rw_view1
SET SESSION AUTHORIZATION regress_view_user1;
GRANT SELECT ON rw_view1 TO regress_view_user2;
SET SESSION AUTHORIZATION regress_view_user2;
SELECT * FROM rw_view2;
 a |   b   | c 
---+-------+---
 1 | Row 1 | 1
(1 row)

SELECT * FROM rw_view2 FOR UPDATE;  -- not allowed
ERROR:  permission denied for view rw_view1
UPDATE rw_view2 SET b = 'bar' WHERE a = 1;  -- not allowed
ERROR:  permission denied for view rw_view1
RESET SESSION AUTHORIZATION;
GRANT UPDATE ON base_tbl TO regress_view_user1;
SET SESSION AUTHORIZATION regress_view_user1;
SELECT * FROM rw_view1;
 a |   b   | c 
---+-------+---
 1 | Row 1 | 1
(1 row)

SELECT * FROM rw_view1 FOR UPDATE;
 a |   b   | c 
---+-------+---
 1 | Row 1 | 1
(1 row)

UPDATE rw_view1 SET b = 'foo' WHERE a = 1;
SET SESSION AUTHORIZATION regress_view_user2;
SELECT * FROM rw_view2;
 a |  b  | c 
---+-----+---
 1 | foo | 1
(1 row)

SELECT * FROM rw_view2 FOR UPDATE;  -- not allowed
ERROR:  permission denied for view rw_view1
UPDATE rw_view2 SET b = 'bar' WHERE a = 1;  -- not allowed
ERROR:  permission denied for view rw_view1
SET SESSION AUTHORIZATION regress_view_user1;
GRANT UPDATE ON rw_view1 TO regress_view_user2;
SET SESSION AUTHORIZATION regress_view_user2;
SELECT * FROM rw_view2;
 a |  b  | c 
---+-----+---
 1 | foo | 1
(1 row)

SELECT * FROM rw_view2 FOR UPDATE;
 a |  b  | c 
---+-----+---
 1 | foo | 1
(1 row)

UPDATE rw_view2 SET b = 'bar' WHERE a = 1;
RESET SESSION AUTHORIZATION;
REVOKE UPDATE ON base_tbl FROM regress_view_user1;
SET SESSION AUTHORIZATION regress_view_user1;
SELECT * FROM rw_view1;
 a |  b  | c 
---+-----+---
 1 | bar | 1
(1 row)

SELECT * FROM rw_view1 FOR UPDATE;  -- not allowed
ERROR:  permission denied for table base_tbl
UPDATE rw_view1 SET b = 'foo' WHERE a = 1;  -- not allowed
ERROR:  permission denied for table base_tbl
SET SESSION AUTHORIZATION regress_view_user2;
SELECT * FROM rw_view2;
 a |  b  | c 
---+-----+---
 1 | bar | 1
(1 row)

SELECT * FROM rw_view2 FOR UPDATE;  -- not allowed
ERROR:  permission denied for table base_tbl
UPDATE rw_view2 SET b = 'bar' WHERE a = 1;  -- not allowed
ERROR:  permission denied for table base_tbl
RESET SESSION AUTHORIZATION;
DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
DROP USER regress_view_user1;
DROP USER regress_view_user2;
-- column defaults
CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified', c serial);
INSERT INTO base_tbl VALUES (1, 'Row 1');
INSERT INTO base_tbl VALUES (2, 'Row 2');
INSERT INTO base_tbl VALUES (3);
CREATE VIEW rw_view1 AS SELECT a AS aa, b AS bb FROM base_tbl;
ALTER VIEW rw_view1 ALTER COLUMN bb SET DEFAULT 'View default';
INSERT INTO rw_view1 VALUES (4, 'Row 4');
INSERT INTO rw_view1 (aa) VALUES (5);
SELECT * FROM base_tbl;
 a |      b       | c 
---+--------------+---
 1 | Row 1        | 1
 2 | Row 2        | 2
 3 | Unspecified  | 3
 4 | Row 4        | 4
 5 | View default | 5
(5 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- Table having triggers
CREATE TABLE base_tbl (a int PRIMARY KEY, b text DEFAULT 'Unspecified');
INSERT INTO base_tbl VALUES (1, 'Row 1');
INSERT INTO base_tbl VALUES (2, 'Row 2');
CREATE FUNCTION rw_view1_trig_fn()
RETURNS trigger AS
$$
BEGIN
  IF TG_OP = 'INSERT' THEN
    UPDATE base_tbl SET b=NEW.b WHERE a=1;
    RETURN NULL;
  END IF;
  RETURN NULL;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER rw_view1_ins_trig AFTER INSERT ON base_tbl
  FOR EACH ROW EXECUTE PROCEDURE rw_view1_trig_fn();
CREATE VIEW rw_view1 AS SELECT a AS aa, b AS bb FROM base_tbl;
INSERT INTO rw_view1 VALUES (3, 'Row 3');
select * from base_tbl;
 a |   b   
---+-------
 2 | Row 2
 3 | Row 3
 1 | Row 3
(3 rows)

DROP VIEW rw_view1;
DROP TRIGGER rw_view1_ins_trig on base_tbl;
DROP FUNCTION rw_view1_trig_fn();
DROP TABLE base_tbl;
-- view with ORDER BY
CREATE TABLE base_tbl (a int, b int);
INSERT INTO base_tbl VALUES (1,2), (4,5), (3,-3);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl ORDER BY a+b;
SELECT * FROM rw_view1;
 a | b  
---+----
 3 | -3
 1 |  2
 4 |  5
(3 rows)

INSERT INTO rw_view1 VALUES (7,-8);
SELECT * FROM rw_view1;
 a | b  
---+----
 7 | -8
 3 | -3
 1 |  2
 4 |  5
(4 rows)

EXPLAIN (verbose, costs off) UPDATE rw_view1 SET b = b + 1 RETURNING *;
                   QUERY PLAN                    
-------------------------------------------------
 Update on public.base_tbl
   Output: base_tbl.a, base_tbl.b
   ->  Seq Scan on public.base_tbl
         Output: (base_tbl.b + 1), base_tbl.ctid
(4 rows)

UPDATE rw_view1 SET b = b + 1 RETURNING *;
 a | b  
---+----
 1 |  3
 4 |  6
 3 | -2
 7 | -7
(4 rows)

SELECT * FROM rw_view1;
 a | b  
---+----
 7 | -7
 3 | -2
 1 |  3
 4 |  6
(4 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- multiple array-column updates
CREATE TABLE base_tbl (a int, arr int[]);
INSERT INTO base_tbl VALUES (1,ARRAY[2]), (3,ARRAY[4]);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl;
UPDATE rw_view1 SET arr[1] = 42, arr[2] = 77 WHERE a = 3;
SELECT * FROM rw_view1;
 a |   arr   
---+---------
 1 | {2}
 3 | {42,77}
(2 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- views with updatable and non-updatable columns
CREATE TABLE base_tbl(a float);
INSERT INTO base_tbl SELECT i/10.0 FROM generate_series(1,10) g(i);
CREATE VIEW rw_view1 AS
  SELECT ctid, sin(a) s, a, cos(a) c
  FROM base_tbl
  WHERE a != 0
  ORDER BY abs(a);
INSERT INTO rw_view1 VALUES (null, null, 1.1, null); -- should fail
ERROR:  cannot insert into column "ctid" of view "rw_view1"
DETAIL:  View columns that refer to system columns are not updatable.
INSERT INTO rw_view1 (s, c, a) VALUES (null, null, 1.1); -- should fail
ERROR:  cannot insert into column "s" of view "rw_view1"
DETAIL:  View columns that are not columns of their base relation are not updatable.
INSERT INTO rw_view1 (a) VALUES (1.1) RETURNING a, s, c; -- OK
  a  |         s         |         c         
-----+-------------------+-------------------
 1.1 | 0.891207360061435 | 0.453596121425577
(1 row)

UPDATE rw_view1 SET s = s WHERE a = 1.1; -- should fail
ERROR:  cannot update column "s" of view "rw_view1"
DETAIL:  View columns that are not columns of their base relation are not updatable.
UPDATE rw_view1 SET a = 1.05 WHERE a = 1.1 RETURNING s; -- OK
         s         
-------------------
 0.867423225594017
(1 row)

DELETE FROM rw_view1 WHERE a = 1.05; -- OK
CREATE VIEW rw_view2 AS
  SELECT s, c, s/c t, a base_a, ctid
  FROM rw_view1;
INSERT INTO rw_view2 VALUES (null, null, null, 1.1, null); -- should fail
ERROR:  cannot insert into column "t" of view "rw_view2"
DETAIL:  View columns that are not columns of their base relation are not updatable.
INSERT INTO rw_view2(s, c, base_a) VALUES (null, null, 1.1); -- should fail
ERROR:  cannot insert into column "s" of view "rw_view1"
DETAIL:  View columns that are not columns of their base relation are not updatable.
INSERT INTO rw_view2(base_a) VALUES (1.1) RETURNING t; -- OK
        t         
------------------
 1.96475965724865
(1 row)

UPDATE rw_view2 SET s = s WHERE base_a = 1.1; -- should fail
ERROR:  cannot update column "s" of view "rw_view1"
DETAIL:  View columns that are not columns of their base relation are not updatable.
UPDATE rw_view2 SET t = t WHERE base_a = 1.1; -- should fail
ERROR:  cannot update column "t" of view "rw_view2"
DETAIL:  View columns that are not columns of their base relation are not updatable.
UPDATE rw_view2 SET base_a = 1.05 WHERE base_a = 1.1; -- OK
DELETE FROM rw_view2 WHERE base_a = 1.05 RETURNING base_a, s, c, t; -- OK
 base_a |         s         |         c         |        t         
--------+-------------------+-------------------+------------------
   1.05 | 0.867423225594017 | 0.497571047891727 | 1.74331530998317
(1 row)

CREATE VIEW rw_view3 AS
  SELECT s, c, s/c t, ctid
  FROM rw_view1;
INSERT INTO rw_view3 VALUES (null, null, null, null); -- should fail
ERROR:  cannot insert into column "t" of view "rw_view3"
DETAIL:  View columns that are not columns of their base relation are not updatable.
INSERT INTO rw_view3(s) VALUES (null); -- should fail
ERROR:  cannot insert into column "s" of view "rw_view1"
DETAIL:  View columns that are not columns of their base relation are not updatable.
UPDATE rw_view3 SET s = s; -- should fail
ERROR:  cannot update column "s" of view "rw_view1"
DETAIL:  View columns that are not columns of their base relation are not updatable.
DELETE FROM rw_view3 WHERE s = sin(0.1); -- should be OK
SELECT * FROM base_tbl ORDER BY a;
  a  
-----
 0.2
 0.3
 0.4
 0.5
 0.6
 0.7
 0.8
 0.9
   1
(9 rows)

SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name LIKE E'r_\\_view%'
 ORDER BY table_name;
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | YES
 rw_view2   | YES
 rw_view3   | NO
(3 rows)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name LIKE E'r_\\_view%'
 ORDER BY table_name;
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view1   | YES          | YES
 rw_view2   | YES          | YES
 rw_view3   | NO           | NO
(3 rows)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name LIKE E'r_\\_view%'
 ORDER BY table_name, ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | ctid        | NO
 rw_view1   | s           | NO
 rw_view1   | a           | YES
 rw_view1   | c           | NO
 rw_view2   | s           | NO
 rw_view2   | c           | NO
 rw_view2   | t           | NO
 rw_view2   | base_a      | YES
 rw_view2   | ctid        | NO
 rw_view3   | s           | NO
 rw_view3   | c           | NO
 rw_view3   | t           | NO
 rw_view3   | ctid        | NO
(13 rows)

SELECT events & 4 != 0 AS upd,
       events & 8 != 0 AS ins,
       events & 16 != 0 AS del
  FROM pg_catalog.pg_relation_is_updatable('rw_view3'::regclass, false) t(events);
 upd | ins | del 
-----+-----+-----
 f   | f   | t
(1 row)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 3 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
drop cascades to view rw_view3
-- view on table with GENERATED columns
CREATE TABLE base_tbl (id int, idplus1 int GENERATED ALWAYS AS (id + 1) STORED);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl;
INSERT INTO base_tbl (id) VALUES (1);
INSERT INTO rw_view1 (id) VALUES (2);
INSERT INTO base_tbl (id, idplus1) VALUES (3, DEFAULT);
INSERT INTO rw_view1 (id, idplus1) VALUES (4, DEFAULT);
INSERT INTO base_tbl (id, idplus1) VALUES (5, 6);  -- error
ERROR:  cannot insert a non-DEFAULT value into column "idplus1"
DETAIL:  Column "idplus1" is a generated column.
INSERT INTO rw_view1 (id, idplus1) VALUES (6, 7);  -- error
ERROR:  cannot insert a non-DEFAULT value into column "idplus1"
DETAIL:  Column "idplus1" is a generated column.
SELECT * FROM base_tbl;
 id | idplus1 
----+---------
  1 |       2
  2 |       3
  3 |       4
  4 |       5
(4 rows)

UPDATE base_tbl SET id = 2000 WHERE id = 2;
UPDATE rw_view1 SET id = 3000 WHERE id = 3;
SELECT * FROM base_tbl;
  id  | idplus1 
------+---------
    1 |       2
    4 |       5
 2000 |    2001
 3000 |    3001
(4 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- inheritance tests
CREATE TABLE base_tbl_parent (a int);
CREATE TABLE base_tbl_child (CHECK (a > 0)) INHERITS (base_tbl_parent);
INSERT INTO base_tbl_parent SELECT * FROM generate_series(-8, -1);
INSERT INTO base_tbl_child SELECT * FROM generate_series(1, 8);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl_parent;
CREATE VIEW rw_view2 AS SELECT * FROM ONLY base_tbl_parent;
SELECT * FROM rw_view1 ORDER BY a;
 a  
----
 -8
 -7
 -6
 -5
 -4
 -3
 -2
 -1
  1
  2
  3
  4
  5
  6
  7
  8
(16 rows)

SELECT * FROM ONLY rw_view1 ORDER BY a;
 a  
----
 -8
 -7
 -6
 -5
 -4
 -3
 -2
 -1
  1
  2
  3
  4
  5
  6
  7
  8
(16 rows)

SELECT * FROM rw_view2 ORDER BY a;
 a  
----
 -8
 -7
 -6
 -5
 -4
 -3
 -2
 -1
(8 rows)

INSERT INTO rw_view1 VALUES (-100), (100);
INSERT INTO rw_view2 VALUES (-200), (200);
UPDATE rw_view1 SET a = a*10 WHERE a IN (-1, 1); -- Should produce -10 and 10
UPDATE ONLY rw_view1 SET a = a*10 WHERE a IN (-2, 2); -- Should produce -20 and 20
UPDATE rw_view2 SET a = a*10 WHERE a IN (-3, 3); -- Should produce -30 only
UPDATE ONLY rw_view2 SET a = a*10 WHERE a IN (-4, 4); -- Should produce -40 only
DELETE FROM rw_view1 WHERE a IN (-5, 5); -- Should delete -5 and 5
DELETE FROM ONLY rw_view1 WHERE a IN (-6, 6); -- Should delete -6 and 6
DELETE FROM rw_view2 WHERE a IN (-7, 7); -- Should delete -7 only
DELETE FROM ONLY rw_view2 WHERE a IN (-8, 8); -- Should delete -8 only
SELECT * FROM ONLY base_tbl_parent ORDER BY a;
  a   
------
 -200
 -100
  -40
  -30
  -20
  -10
  100
  200
(8 rows)

SELECT * FROM base_tbl_child ORDER BY a;
 a  
----
  3
  4
  7
  8
 10
 20
(6 rows)

CREATE TABLE other_tbl_parent (id int);
CREATE TABLE other_tbl_child () INHERITS (other_tbl_parent);
INSERT INTO other_tbl_parent VALUES (7),(200);
INSERT INTO other_tbl_child VALUES (8),(100);
EXPLAIN (costs off)
UPDATE rw_view1 SET a = a + 1000 FROM other_tbl_parent WHERE a = id;
                               QUERY PLAN                                
-------------------------------------------------------------------------
 Update on base_tbl_parent
   Update on base_tbl_parent base_tbl_parent_1
   Update on base_tbl_child base_tbl_parent_2
   ->  Merge Join
         Merge Cond: (base_tbl_parent.a = other_tbl_parent.id)
         ->  Sort
               Sort Key: base_tbl_parent.a
               ->  Append
                     ->  Seq Scan on base_tbl_parent base_tbl_parent_1
                     ->  Seq Scan on base_tbl_child base_tbl_parent_2
         ->  Sort
               Sort Key: other_tbl_parent.id
               ->  Append
                     ->  Seq Scan on other_tbl_parent other_tbl_parent_1
                     ->  Seq Scan on other_tbl_child other_tbl_parent_2
(15 rows)

UPDATE rw_view1 SET a = a + 1000 FROM other_tbl_parent WHERE a = id;
SELECT * FROM ONLY base_tbl_parent ORDER BY a;
  a   
------
 -200
 -100
  -40
  -30
  -20
  -10
 1100
 1200
(8 rows)

SELECT * FROM base_tbl_child ORDER BY a;
  a   
------
    3
    4
   10
   20
 1007
 1008
(6 rows)

DROP TABLE base_tbl_parent, base_tbl_child CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
DROP TABLE other_tbl_parent CASCADE;
NOTICE:  drop cascades to table other_tbl_child
-- simple WITH CHECK OPTION
CREATE TABLE base_tbl (a int, b int DEFAULT 10);
INSERT INTO base_tbl VALUES (1,2), (2,3), (1,-1);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WHERE a < b
  WITH LOCAL CHECK OPTION;
\d+ rw_view1
                          View "public.rw_view1"
 Column |  Type   | Collation | Nullable | Default | Storage | Description 
--------+---------+-----------+----------+---------+---------+-------------
 a      | integer |           |          |         | plain   | 
 b      | integer |           |          |         | plain   | 
View definition:
 SELECT base_tbl.a,
    base_tbl.b
   FROM base_tbl
  WHERE base_tbl.a < base_tbl.b;
Options: check_option=local

SELECT * FROM information_schema.views WHERE table_name = 'rw_view1';
 table_catalog | table_schema | table_name |          view_definition           | check_option | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
---------------+--------------+------------+------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------
 regression    | public       | rw_view1   |  SELECT base_tbl.a,               +| LOCAL        | YES          | YES                | NO                   | NO                   | NO
               |              |            |     base_tbl.b                    +|              |              |                    |                      |                      | 
               |              |            |    FROM base_tbl                  +|              |              |                    |                      |                      | 
               |              |            |   WHERE (base_tbl.a < base_tbl.b); |              |              |                    |                      |                      | 
(1 row)

INSERT INTO rw_view1 VALUES(3,4); -- ok
INSERT INTO rw_view1 VALUES(4,3); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (4, 3).
INSERT INTO rw_view1 VALUES(5,null); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (5, null).
UPDATE rw_view1 SET b = 5 WHERE a = 3; -- ok
UPDATE rw_view1 SET b = -5 WHERE a = 3; -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (3, -5).
INSERT INTO rw_view1(a) VALUES (9); -- ok
INSERT INTO rw_view1(a) VALUES (10); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (10, 10).
SELECT * FROM base_tbl;
 a | b  
---+----
 1 |  2
 2 |  3
 1 | -1
 3 |  5
 9 | 10
(5 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- WITH LOCAL/CASCADED CHECK OPTION
CREATE TABLE base_tbl (a int);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WHERE a > 0;
CREATE VIEW rw_view2 AS SELECT * FROM rw_view1 WHERE a < 10
  WITH CHECK OPTION; -- implicitly cascaded
\d+ rw_view2
                          View "public.rw_view2"
 Column |  Type   | Collation | Nullable | Default | Storage | Description 
--------+---------+-----------+----------+---------+---------+-------------
 a      | integer |           |          |         | plain   | 
View definition:
 SELECT rw_view1.a
   FROM rw_view1
  WHERE rw_view1.a < 10;
Options: check_option=cascaded

SELECT * FROM information_schema.views WHERE table_name = 'rw_view2';
 table_catalog | table_schema | table_name |      view_definition       | check_option | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
---------------+--------------+------------+----------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------
 regression    | public       | rw_view2   |  SELECT rw_view1.a        +| CASCADED     | YES          | YES                | NO                   | NO                   | NO
               |              |            |    FROM rw_view1          +|              |              |                    |                      |                      | 
               |              |            |   WHERE (rw_view1.a < 10); |              |              |                    |                      |                      | 
(1 row)

INSERT INTO rw_view2 VALUES (-5); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (-5).
INSERT INTO rw_view2 VALUES (5); -- ok
INSERT INTO rw_view2 VALUES (15); -- should fail
ERROR:  new row violates check option for view "rw_view2"
DETAIL:  Failing row contains (15).
SELECT * FROM base_tbl;
 a 
---
 5
(1 row)

UPDATE rw_view2 SET a = a - 10; -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (-5).
UPDATE rw_view2 SET a = a + 10; -- should fail
ERROR:  new row violates check option for view "rw_view2"
DETAIL:  Failing row contains (15).
CREATE OR REPLACE VIEW rw_view2 AS SELECT * FROM rw_view1 WHERE a < 10
  WITH LOCAL CHECK OPTION;
\d+ rw_view2
                          View "public.rw_view2"
 Column |  Type   | Collation | Nullable | Default | Storage | Description 
--------+---------+-----------+----------+---------+---------+-------------
 a      | integer |           |          |         | plain   | 
View definition:
 SELECT rw_view1.a
   FROM rw_view1
  WHERE rw_view1.a < 10;
Options: check_option=local

SELECT * FROM information_schema.views WHERE table_name = 'rw_view2';
 table_catalog | table_schema | table_name |      view_definition       | check_option | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
---------------+--------------+------------+----------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------
 regression    | public       | rw_view2   |  SELECT rw_view1.a        +| LOCAL        | YES          | YES                | NO                   | NO                   | NO
               |              |            |    FROM rw_view1          +|              |              |                    |                      |                      | 
               |              |            |   WHERE (rw_view1.a < 10); |              |              |                    |                      |                      | 
(1 row)

INSERT INTO rw_view2 VALUES (-10); -- ok, but not in view
INSERT INTO rw_view2 VALUES (20); -- should fail
ERROR:  new row violates check option for view "rw_view2"
DETAIL:  Failing row contains (20).
SELECT * FROM base_tbl;
  a  
-----
   5
 -10
(2 rows)

ALTER VIEW rw_view1 SET (check_option=here); -- invalid
ERROR:  invalid value for enum option "check_option": here
DETAIL:  Valid values are "local" and "cascaded".
ALTER VIEW rw_view1 SET (check_option=local);
INSERT INTO rw_view2 VALUES (-20); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (-20).
INSERT INTO rw_view2 VALUES (30); -- should fail
ERROR:  new row violates check option for view "rw_view2"
DETAIL:  Failing row contains (30).
ALTER VIEW rw_view2 RESET (check_option);
\d+ rw_view2
                          View "public.rw_view2"
 Column |  Type   | Collation | Nullable | Default | Storage | Description 
--------+---------+-----------+----------+---------+---------+-------------
 a      | integer |           |          |         | plain   | 
View definition:
 SELECT rw_view1.a
   FROM rw_view1
  WHERE rw_view1.a < 10;

SELECT * FROM information_schema.views WHERE table_name = 'rw_view2';
 table_catalog | table_schema | table_name |      view_definition       | check_option | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
---------------+--------------+------------+----------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------
 regression    | public       | rw_view2   |  SELECT rw_view1.a        +| NONE         | YES          | YES                | NO                   | NO                   | NO
               |              |            |    FROM rw_view1          +|              |              |                    |                      |                      | 
               |              |            |   WHERE (rw_view1.a < 10); |              |              |                    |                      |                      | 
(1 row)

INSERT INTO rw_view2 VALUES (30); -- ok, but not in view
SELECT * FROM base_tbl;
  a  
-----
   5
 -10
  30
(3 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
-- WITH CHECK OPTION with no local view qual
CREATE TABLE base_tbl (a int);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WITH CHECK OPTION;
CREATE VIEW rw_view2 AS SELECT * FROM rw_view1 WHERE a > 0;
CREATE VIEW rw_view3 AS SELECT * FROM rw_view2 WITH CHECK OPTION;
SELECT * FROM information_schema.views WHERE table_name LIKE E'rw\\_view_' ORDER BY table_name;
 table_catalog | table_schema | table_name |      view_definition      | check_option | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into 
---------------+--------------+------------+---------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------
 regression    | public       | rw_view1   |  SELECT base_tbl.a       +| CASCADED     | YES          | YES                | NO                   | NO                   | NO
               |              |            |    FROM base_tbl;         |              |              |                    |                      |                      | 
 regression    | public       | rw_view2   |  SELECT rw_view1.a       +| NONE         | YES          | YES                | NO                   | NO                   | NO
               |              |            |    FROM rw_view1         +|              |              |                    |                      |                      | 
               |              |            |   WHERE (rw_view1.a > 0); |              |              |                    |                      |                      | 
 regression    | public       | rw_view3   |  SELECT rw_view2.a       +| CASCADED     | YES          | YES                | NO                   | NO                   | NO
               |              |            |    FROM rw_view2;         |              |              |                    |                      |                      | 
(3 rows)

INSERT INTO rw_view1 VALUES (-1); -- ok
INSERT INTO rw_view1 VALUES (1); -- ok
INSERT INTO rw_view2 VALUES (-2); -- ok, but not in view
INSERT INTO rw_view2 VALUES (2); -- ok
INSERT INTO rw_view3 VALUES (-3); -- should fail
ERROR:  new row violates check option for view "rw_view2"
DETAIL:  Failing row contains (-3).
INSERT INTO rw_view3 VALUES (3); -- ok
DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 3 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
drop cascades to view rw_view3
-- WITH CHECK OPTION with scalar array ops
CREATE TABLE base_tbl (a int, b int[]);
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WHERE a = ANY (b)
  WITH CHECK OPTION;
INSERT INTO rw_view1 VALUES (1, ARRAY[1,2,3]); -- ok
INSERT INTO rw_view1 VALUES (10, ARRAY[4,5]); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (10, {4,5}).
UPDATE rw_view1 SET b[2] = -b[2] WHERE a = 1; -- ok
UPDATE rw_view1 SET b[1] = -b[1] WHERE a = 1; -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (1, {-1,-2,3}).
PREPARE ins(int, int[]) AS INSERT INTO rw_view1 VALUES($1, $2);
EXECUTE ins(2, ARRAY[1,2,3]); -- ok
EXECUTE ins(10, ARRAY[4,5]); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (10, {4,5}).
DEALLOCATE PREPARE ins;
DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- WITH CHECK OPTION with subquery
CREATE TABLE base_tbl (a int);
CREATE TABLE ref_tbl (a int PRIMARY KEY);
INSERT INTO ref_tbl SELECT * FROM generate_series(1,10);
CREATE VIEW rw_view1 AS
  SELECT * FROM base_tbl b
  WHERE EXISTS(SELECT 1 FROM ref_tbl r WHERE r.a = b.a)
  WITH CHECK OPTION;
INSERT INTO rw_view1 VALUES (5); -- ok
INSERT INTO rw_view1 VALUES (15); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (15).
UPDATE rw_view1 SET a = a + 5; -- ok
UPDATE rw_view1 SET a = a + 5; -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (15).
EXPLAIN (costs off) INSERT INTO rw_view1 VALUES (5);
                       QUERY PLAN                        
---------------------------------------------------------
 Insert on base_tbl b
   ->  Result
   SubPlan 1
     ->  Index Only Scan using ref_tbl_pkey on ref_tbl r
           Index Cond: (a = b.a)
(5 rows)

EXPLAIN (costs off) UPDATE rw_view1 SET a = a + 5;
                        QUERY PLAN                         
-----------------------------------------------------------
 Update on base_tbl b
   ->  Hash Join
         Hash Cond: (b.a = r.a)
         ->  Seq Scan on base_tbl b
         ->  Hash
               ->  Seq Scan on ref_tbl r
   SubPlan 1
     ->  Index Only Scan using ref_tbl_pkey on ref_tbl r_1
           Index Cond: (a = b.a)
(9 rows)

DROP TABLE base_tbl, ref_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- WITH CHECK OPTION with BEFORE trigger on base table
CREATE TABLE base_tbl (a int, b int);
CREATE FUNCTION base_tbl_trig_fn()
RETURNS trigger AS
$$
BEGIN
  NEW.b := 10;
  RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER base_tbl_trig BEFORE INSERT OR UPDATE ON base_tbl
  FOR EACH ROW EXECUTE PROCEDURE base_tbl_trig_fn();
CREATE VIEW rw_view1 AS SELECT * FROM base_tbl WHERE a < b WITH CHECK OPTION;
INSERT INTO rw_view1 VALUES (5,0); -- ok
INSERT INTO rw_view1 VALUES (15, 20); -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (15, 10).
UPDATE rw_view1 SET a = 20, b = 30; -- should fail
ERROR:  new row violates check option for view "rw_view1"
DETAIL:  Failing row contains (20, 10).
DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
DROP FUNCTION base_tbl_trig_fn();
-- WITH LOCAL CHECK OPTION with INSTEAD OF trigger on base view
CREATE TABLE base_tbl (a int, b int);
CREATE VIEW rw_view1 AS SELECT a FROM base_tbl WHERE a < b;
CREATE FUNCTION rw_view1_trig_fn()
RETURNS trigger AS
$$
BEGIN
  IF TG_OP = 'INSERT' THEN
    INSERT INTO base_tbl VALUES (NEW.a, 10);
    RETURN NEW;
  ELSIF TG_OP = 'UPDATE' THEN
    UPDATE base_tbl SET a=NEW.a WHERE a=OLD.a;
    RETURN NEW;
  ELSIF TG_OP = 'DELETE' THEN
    DELETE FROM base_tbl WHERE a=OLD.a;
    RETURN OLD;
  END IF;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER rw_view1_trig
  INSTEAD OF INSERT OR UPDATE OR DELETE ON rw_view1
  FOR EACH ROW EXECUTE PROCEDURE rw_view1_trig_fn();
CREATE VIEW rw_view2 AS
  SELECT * FROM rw_view1 WHERE a > 0 WITH LOCAL CHECK OPTION;
INSERT INTO rw_view2 VALUES (-5); -- should fail
ERROR:  new row violates check option for view "rw_view2"
DETAIL:  Failing row contains (-5).
INSERT INTO rw_view2 VALUES (5); -- ok
INSERT INTO rw_view2 VALUES (50); -- ok, but not in view
UPDATE rw_view2 SET a = a - 10; -- should fail
ERROR:  new row violates check option for view "rw_view2"
DETAIL:  Failing row contains (-5).
SELECT * FROM base_tbl;
 a  | b  
----+----
  5 | 10
 50 | 10
(2 rows)

-- Check option won't cascade down to base view with INSTEAD OF triggers
ALTER VIEW rw_view2 SET (check_option=cascaded);
INSERT INTO rw_view2 VALUES (100); -- ok, but not in view (doesn't fail rw_view1's check)
UPDATE rw_view2 SET a = 200 WHERE a = 5; -- ok, but not in view (doesn't fail rw_view1's check)
SELECT * FROM base_tbl;
  a  | b  
-----+----
  50 | 10
 100 | 10
 200 | 10
(3 rows)

-- Neither local nor cascaded check options work with INSTEAD rules
DROP TRIGGER rw_view1_trig ON rw_view1;
CREATE RULE rw_view1_ins_rule AS ON INSERT TO rw_view1
  DO INSTEAD INSERT INTO base_tbl VALUES (NEW.a, 10);
CREATE RULE rw_view1_upd_rule AS ON UPDATE TO rw_view1
  DO INSTEAD UPDATE base_tbl SET a=NEW.a WHERE a=OLD.a;
INSERT INTO rw_view2 VALUES (-10); -- ok, but not in view (doesn't fail rw_view2's check)
INSERT INTO rw_view2 VALUES (5); -- ok
INSERT INTO rw_view2 VALUES (20); -- ok, but not in view (doesn't fail rw_view1's check)
UPDATE rw_view2 SET a = 30 WHERE a = 5; -- ok, but not in view (doesn't fail rw_view1's check)
INSERT INTO rw_view2 VALUES (5); -- ok
UPDATE rw_view2 SET a = -5 WHERE a = 5; -- ok, but not in view (doesn't fail rw_view2's check)
SELECT * FROM base_tbl;
  a  | b  
-----+----
  50 | 10
 100 | 10
 200 | 10
 -10 | 10
  20 | 10
  30 | 10
  -5 | 10
(7 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
DROP FUNCTION rw_view1_trig_fn();
CREATE TABLE base_tbl (a int);
CREATE VIEW rw_view1 AS SELECT a,10 AS b FROM base_tbl;
CREATE RULE rw_view1_ins_rule AS ON INSERT TO rw_view1
  DO INSTEAD INSERT INTO base_tbl VALUES (NEW.a);
CREATE VIEW rw_view2 AS
  SELECT * FROM rw_view1 WHERE a > b WITH LOCAL CHECK OPTION;
INSERT INTO rw_view2 VALUES (2,3); -- ok, but not in view (doesn't fail rw_view2's check)
DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
-- security barrier view
CREATE TABLE base_tbl (person text, visibility text);
INSERT INTO base_tbl VALUES ('Tom', 'public'),
                            ('Dick', 'private'),
                            ('Harry', 'public');
CREATE VIEW rw_view1 AS
  SELECT person FROM base_tbl WHERE visibility = 'public';
CREATE FUNCTION snoop(anyelement)
RETURNS boolean AS
$$
BEGIN
  RAISE NOTICE 'snooped value: %', $1;
  RETURN true;
END;
$$
LANGUAGE plpgsql COST 0.000001;
CREATE OR REPLACE FUNCTION leakproof(anyelement)
RETURNS boolean AS
$$
BEGIN
  RETURN true;
END;
$$
LANGUAGE plpgsql STRICT IMMUTABLE LEAKPROOF;
SELECT * FROM rw_view1 WHERE snoop(person);
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Dick
NOTICE:  snooped value: Harry
 person 
--------
 Tom
 Harry
(2 rows)

UPDATE rw_view1 SET person=person WHERE snoop(person);
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Dick
NOTICE:  snooped value: Harry
DELETE FROM rw_view1 WHERE NOT snoop(person);
NOTICE:  snooped value: Dick
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Harry
ALTER VIEW rw_view1 SET (security_barrier = true);
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name = 'rw_view1';
 table_name | is_insertable_into 
------------+--------------------
 rw_view1   | YES
(1 row)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name = 'rw_view1';
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view1   | YES          | YES
(1 row)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name = 'rw_view1'
 ORDER BY ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view1   | person      | YES
(1 row)

SELECT * FROM rw_view1 WHERE snoop(person);
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Harry
 person 
--------
 Tom
 Harry
(2 rows)

UPDATE rw_view1 SET person=person WHERE snoop(person);
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Harry
DELETE FROM rw_view1 WHERE NOT snoop(person);
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Harry
EXPLAIN (costs off) SELECT * FROM rw_view1 WHERE snoop(person);
                  QUERY PLAN                   
-----------------------------------------------
 Subquery Scan on rw_view1
   Filter: snoop(rw_view1.person)
   ->  Seq Scan on base_tbl
         Filter: (visibility = 'public'::text)
(4 rows)

EXPLAIN (costs off) UPDATE rw_view1 SET person=person WHERE snoop(person);
                            QUERY PLAN                             
-------------------------------------------------------------------
 Update on base_tbl
   ->  Seq Scan on base_tbl
         Filter: ((visibility = 'public'::text) AND snoop(person))
(3 rows)

EXPLAIN (costs off) DELETE FROM rw_view1 WHERE NOT snoop(person);
                               QUERY PLAN                                
-------------------------------------------------------------------------
 Delete on base_tbl
   ->  Seq Scan on base_tbl
         Filter: ((visibility = 'public'::text) AND (NOT snoop(person)))
(3 rows)

-- security barrier view on top of security barrier view
CREATE VIEW rw_view2 WITH (security_barrier = true) AS
  SELECT * FROM rw_view1 WHERE snoop(person);
SELECT table_name, is_insertable_into
  FROM information_schema.tables
 WHERE table_name = 'rw_view2';
 table_name | is_insertable_into 
------------+--------------------
 rw_view2   | YES
(1 row)

SELECT table_name, is_updatable, is_insertable_into
  FROM information_schema.views
 WHERE table_name = 'rw_view2';
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 rw_view2   | YES          | YES
(1 row)

SELECT table_name, column_name, is_updatable
  FROM information_schema.columns
 WHERE table_name = 'rw_view2'
 ORDER BY ordinal_position;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 rw_view2   | person      | YES
(1 row)

SELECT * FROM rw_view2 WHERE snoop(person);
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Harry
NOTICE:  snooped value: Harry
 person 
--------
 Tom
 Harry
(2 rows)

UPDATE rw_view2 SET person=person WHERE snoop(person);
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Harry
NOTICE:  snooped value: Harry
DELETE FROM rw_view2 WHERE NOT snoop(person);
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Tom
NOTICE:  snooped value: Harry
NOTICE:  snooped value: Harry
EXPLAIN (costs off) SELECT * FROM rw_view2 WHERE snoop(person);
                     QUERY PLAN                      
-----------------------------------------------------
 Subquery Scan on rw_view2
   Filter: snoop(rw_view2.person)
   ->  Subquery Scan on rw_view1
         Filter: snoop(rw_view1.person)
         ->  Seq Scan on base_tbl
               Filter: (visibility = 'public'::text)
(6 rows)

EXPLAIN (costs off) UPDATE rw_view2 SET person=person WHERE snoop(person);
                                     QUERY PLAN                                      
-------------------------------------------------------------------------------------
 Update on base_tbl
   ->  Seq Scan on base_tbl
         Filter: ((visibility = 'public'::text) AND snoop(person) AND snoop(person))
(3 rows)

EXPLAIN (costs off) DELETE FROM rw_view2 WHERE NOT snoop(person);
                                        QUERY PLAN                                         
-------------------------------------------------------------------------------------------
 Delete on base_tbl
   ->  Seq Scan on base_tbl
         Filter: ((visibility = 'public'::text) AND snoop(person) AND (NOT snoop(person)))
(3 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to view rw_view1
drop cascades to view rw_view2
-- security barrier view on top of table with rules
CREATE TABLE base_tbl(id int PRIMARY KEY, data text, deleted boolean);
INSERT INTO base_tbl VALUES (1, 'Row 1', false), (2, 'Row 2', true);
CREATE RULE base_tbl_ins_rule AS ON INSERT TO base_tbl
  WHERE EXISTS (SELECT 1 FROM base_tbl t WHERE t.id = new.id)
  DO INSTEAD
    UPDATE base_tbl SET data = new.data, deleted = false WHERE id = new.id;
CREATE RULE base_tbl_del_rule AS ON DELETE TO base_tbl
  DO INSTEAD
    UPDATE base_tbl SET deleted = true WHERE id = old.id;
CREATE VIEW rw_view1 WITH (security_barrier=true) AS
  SELECT id, data FROM base_tbl WHERE NOT deleted;
SELECT * FROM rw_view1;
 id | data  
----+-------
  1 | Row 1
(1 row)

EXPLAIN (costs off) DELETE FROM rw_view1 WHERE id = 1 AND snoop(data);
                            QUERY PLAN                             
-------------------------------------------------------------------
 Update on base_tbl base_tbl_1
   ->  Nested Loop
         ->  Index Scan using base_tbl_pkey on base_tbl base_tbl_1
               Index Cond: (id = 1)
         ->  Index Scan using base_tbl_pkey on base_tbl
               Index Cond: (id = 1)
               Filter: ((NOT deleted) AND snoop(data))
(7 rows)

DELETE FROM rw_view1 WHERE id = 1 AND snoop(data);
NOTICE:  snooped value: Row 1
EXPLAIN (costs off) INSERT INTO rw_view1 VALUES (2, 'New row 2');
                        QUERY PLAN                         
-----------------------------------------------------------
 Insert on base_tbl
   InitPlan 1 (returns $0)
     ->  Index Only Scan using base_tbl_pkey on base_tbl t
           Index Cond: (id = 2)
   ->  Result
         One-Time Filter: ($0 IS NOT TRUE)
 
 Update on base_tbl
   InitPlan 1 (returns $0)
     ->  Index Only Scan using base_tbl_pkey on base_tbl t
           Index Cond: (id = 2)
   ->  Result
         One-Time Filter: $0
         ->  Index Scan using base_tbl_pkey on base_tbl
               Index Cond: (id = 2)
(15 rows)

INSERT INTO rw_view1 VALUES (2, 'New row 2');
SELECT * FROM base_tbl;
 id |   data    | deleted 
----+-----------+---------
  1 | Row 1     | t
  2 | New row 2 | f
(2 rows)

DROP TABLE base_tbl CASCADE;
NOTICE:  drop cascades to view rw_view1
-- security barrier view based on inheritance set
CREATE TABLE t1 (a int, b float, c text);
CREATE INDEX t1_a_idx ON t1(a);
INSERT INTO t1
SELECT i,i,'t1' FROM generate_series(1,10) g(i);
ANALYZE t1;
CREATE TABLE t11 (d text) INHERITS (t1);
CREATE INDEX t11_a_idx ON t11(a);
INSERT INTO t11
SELECT i,i,'t11','t11d' FROM generate_series(1,10) g(i);
ANALYZE t11;
CREATE TABLE t12 (e int[]) INHERITS (t1);
CREATE INDEX t12_a_idx ON t12(a);
INSERT INTO t12
SELECT i,i,'t12','{1,2}'::int[] FROM generate_series(1,10) g(i);
ANALYZE t12;
CREATE TABLE t111 () INHERITS (t11, t12);
NOTICE:  merging multiple inherited definitions of column "a"
NOTICE:  merging multiple inherited definitions of column "b"
NOTICE:  merging multiple inherited definitions of column "c"
CREATE INDEX t111_a_idx ON t111(a);
INSERT INTO t111
SELECT i,i,'t111','t111d','{1,1,1}'::int[] FROM generate_series(1,10) g(i);
ANALYZE t111;
CREATE VIEW v1 WITH (security_barrier=true) AS
SELECT *, (SELECT d FROM t11 WHERE t11.a = t1.a LIMIT 1) AS d
FROM t1
WHERE a > 5 AND EXISTS(SELECT 1 FROM t12 WHERE t12.a = t1.a);
SELECT * FROM v1 WHERE a=3; -- should not see anything
 a | b | c | d 
---+---+---+---
(0 rows)

SELECT * FROM v1 WHERE a=8;
 a | b |  c   |  d   
---+---+------+------
 8 | 8 | t1   | t11d
 8 | 8 | t11  | t11d
 8 | 8 | t12  | t11d
 8 | 8 | t111 | t11d
(4 rows)

EXPLAIN (VERBOSE, COSTS OFF)
UPDATE v1 SET a=100 WHERE snoop(a) AND leakproof(a) AND a < 7 AND a != 6;
                                             QUERY PLAN                                              
-----------------------------------------------------------------------------------------------------
 Update on public.t1
   Update on public.t1 t1_1
   Update on public.t11 t1_2
   Update on public.t12 t1_3
   Update on public.t111 t1_4
   ->  Result
         Output: 100, t1.tableoid, t1.ctid
         ->  Append
               ->  Index Scan using t1_a_idx on public.t1 t1_1
                     Output: t1_1.tableoid, t1_1.ctid
                     Index Cond: ((t1_1.a > 5) AND (t1_1.a < 7))
                     Filter: ((t1_1.a <> 6) AND (SubPlan 1) AND snoop(t1_1.a) AND leakproof(t1_1.a))
                     SubPlan 1
                       ->  Append
                             ->  Seq Scan on public.t12 t12_1
                                   Filter: (t12_1.a = t1_1.a)
                             ->  Seq Scan on public.t111 t12_2
                                   Filter: (t12_2.a = t1_1.a)
               ->  Index Scan using t11_a_idx on public.t11 t1_2
                     Output: t1_2.tableoid, t1_2.ctid
                     Index Cond: ((t1_2.a > 5) AND (t1_2.a < 7))
                     Filter: ((t1_2.a <> 6) AND (SubPlan 1) AND snoop(t1_2.a) AND leakproof(t1_2.a))
               ->  Index Scan using t12_a_idx on public.t12 t1_3
                     Output: t1_3.tableoid, t1_3.ctid
                     Index Cond: ((t1_3.a > 5) AND (t1_3.a < 7))
                     Filter: ((t1_3.a <> 6) AND (SubPlan 1) AND snoop(t1_3.a) AND leakproof(t1_3.a))
               ->  Index Scan using t111_a_idx on public.t111 t1_4
                     Output: t1_4.tableoid, t1_4.ctid
                     Index Cond: ((t1_4.a > 5) AND (t1_4.a < 7))
                     Filter: ((t1_4.a <> 6) AND (SubPlan 1) AND snoop(t1_4.a) AND leakproof(t1_4.a))
(30 rows)

UPDATE v1 SET a=100 WHERE snoop(a) AND leakproof(a) AND a < 7 AND a != 6;
SELECT * FROM v1 WHERE a=100; -- Nothing should have been changed to 100
 a | b | c | d 
---+---+---+---
(0 rows)

SELECT * FROM t1 WHERE a=100; -- Nothing should have been changed to 100
 a | b | c 
---+---+---
(0 rows)

EXPLAIN (VERBOSE, COSTS OFF)
UPDATE v1 SET a=a+1 WHERE snoop(a) AND leakproof(a) AND a = 8;
                                    QUERY PLAN                                     
-----------------------------------------------------------------------------------
 Update on public.t1
   Update on public.t1 t1_1
   Update on public.t11 t1_2
   Update on public.t12 t1_3
   Update on public.t111 t1_4
   ->  Result
         Output: (t1.a + 1), t1.tableoid, t1.ctid
         ->  Append
               ->  Index Scan using t1_a_idx on public.t1 t1_1
                     Output: t1_1.a, t1_1.tableoid, t1_1.ctid
                     Index Cond: ((t1_1.a > 5) AND (t1_1.a = 8))
                     Filter: ((SubPlan 1) AND snoop(t1_1.a) AND leakproof(t1_1.a))
                     SubPlan 1
                       ->  Append
                             ->  Seq Scan on public.t12 t12_1
                                   Filter: (t12_1.a = t1_1.a)
                             ->  Seq Scan on public.t111 t12_2
                                   Filter: (t12_2.a = t1_1.a)
               ->  Index Scan using t11_a_idx on public.t11 t1_2
                     Output: t1_2.a, t1_2.tableoid, t1_2.ctid
                     Index Cond: ((t1_2.a > 5) AND (t1_2.a = 8))
                     Filter: ((SubPlan 1) AND snoop(t1_2.a) AND leakproof(t1_2.a))
               ->  Index Scan using t12_a_idx on public.t12 t1_3
                     Output: t1_3.a, t1_3.tableoid, t1_3.ctid
                     Index Cond: ((t1_3.a > 5) AND (t1_3.a = 8))
                     Filter: ((SubPlan 1) AND snoop(t1_3.a) AND leakproof(t1_3.a))
               ->  Index Scan using t111_a_idx on public.t111 t1_4
                     Output: t1_4.a, t1_4.tableoid, t1_4.ctid
                     Index Cond: ((t1_4.a > 5) AND (t1_4.a = 8))
                     Filter: ((SubPlan 1) AND snoop(t1_4.a) AND leakproof(t1_4.a))
(30 rows)

UPDATE v1 SET a=a+1 WHERE snoop(a) AND leakproof(a) AND a = 8;
NOTICE:  snooped value: 8
NOTICE:  snooped value: 8
NOTICE:  snooped value: 8
NOTICE:  snooped value: 8
SELECT * FROM v1 WHERE b=8;
 a | b |  c   |  d   
---+---+------+------
 9 | 8 | t1   | t11d
 9 | 8 | t11  | t11d
 9 | 8 | t12  | t11d
 9 | 8 | t111 | t11d
(4 rows)

DELETE FROM v1 WHERE snoop(a) AND leakproof(a); -- should not delete everything, just where a>5
NOTICE:  snooped value: 6
NOTICE:  snooped value: 7
NOTICE:  snooped value: 9
NOTICE:  snooped value: 10
NOTICE:  snooped value: 9
NOTICE:  snooped value: 6
NOTICE:  snooped value: 7
NOTICE:  snooped value: 9
NOTICE:  snooped value: 10
NOTICE:  snooped value: 9
NOTICE:  snooped value: 6
NOTICE:  snooped value: 7
NOTICE:  snooped value: 9
NOTICE:  snooped value: 10
NOTICE:  snooped value: 9
NOTICE:  snooped value: 6
NOTICE:  snooped value: 7
NOTICE:  snooped value: 9
NOTICE:  snooped value: 10
NOTICE:  snooped value: 9
TABLE t1; -- verify all a<=5 are intact
 a | b |  c   
---+---+------
 1 | 1 | t1
 2 | 2 | t1
 3 | 3 | t1
 4 | 4 | t1
 5 | 5 | t1
 1 | 1 | t11
 2 | 2 | t11
 3 | 3 | t11
 4 | 4 | t11
 5 | 5 | t11
 1 | 1 | t12
 2 | 2 | t12
 3 | 3 | t12
 4 | 4 | t12
 5 | 5 | t12
 1 | 1 | t111
 2 | 2 | t111
 3 | 3 | t111
 4 | 4 | t111
 5 | 5 | t111
(20 rows)

DROP TABLE t1, t11, t12, t111 CASCADE;
NOTICE:  drop cascades to view v1
DROP FUNCTION snoop(anyelement);
DROP FUNCTION leakproof(anyelement);
CREATE TABLE tx1 (a integer);
CREATE TABLE tx2 (b integer);
CREATE TABLE tx3 (c integer);
CREATE VIEW vx1 AS SELECT a FROM tx1 WHERE EXISTS(SELECT 1 FROM tx2 JOIN tx3 ON b=c);
INSERT INTO vx1 values (1);
SELECT * FROM tx1;
 a 
---
 1
(1 row)

SELECT * FROM vx1;
 a 
---
(0 rows)

DROP VIEW vx1;
DROP TABLE tx1;
DROP TABLE tx2;
DROP TABLE tx3;
CREATE TABLE tx1 (a integer);
CREATE TABLE tx2 (b integer);
CREATE TABLE tx3 (c integer);
CREATE VIEW vx1 AS SELECT a FROM tx1 WHERE EXISTS(SELECT 1 FROM tx2 JOIN tx3 ON b=c);
INSERT INTO vx1 VALUES (1);
INSERT INTO vx1 VALUES (1);
SELECT * FROM tx1;
 a 
---
 1
 1
(2 rows)

SELECT * FROM vx1;
 a 
---
(0 rows)

DROP VIEW vx1;
DROP TABLE tx1;
DROP TABLE tx2;
DROP TABLE tx3;
CREATE TABLE tx1 (a integer, b integer);
CREATE TABLE tx2 (b integer, c integer);
CREATE TABLE tx3 (c integer, d integer);
ALTER TABLE tx1 DROP COLUMN b;
ALTER TABLE tx2 DROP COLUMN c;
ALTER TABLE tx3 DROP COLUMN d;
CREATE VIEW vx1 AS SELECT a FROM tx1 WHERE EXISTS(SELECT 1 FROM tx2 JOIN tx3 ON b=c);
INSERT INTO vx1 VALUES (1);
INSERT INTO vx1 VALUES (1);
SELECT * FROM tx1;
 a 
---
 1
 1
(2 rows)

SELECT * FROM vx1;
 a 
---
(0 rows)

DROP VIEW vx1;
DROP TABLE tx1;
DROP TABLE tx2;
DROP TABLE tx3;
--
-- Test handling of vars from correlated subqueries in quals from outer
-- security barrier views, per bug #13988
--
CREATE TABLE t1 (a int, b text, c int);
INSERT INTO t1 VALUES (1, 'one', 10);
CREATE TABLE t2 (cc int);
INSERT INTO t2 VALUES (10), (20);
CREATE VIEW v1 WITH (security_barrier = true) AS
  SELECT * FROM t1 WHERE (a > 0)
  WITH CHECK OPTION;
CREATE VIEW v2 WITH (security_barrier = true) AS
  SELECT * FROM v1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.cc = v1.c)
  WITH CHECK OPTION;
INSERT INTO v2 VALUES (2, 'two', 20); -- ok
INSERT INTO v2 VALUES (-2, 'minus two', 20); -- not allowed
ERROR:  new row violates check option for view "v1"
DETAIL:  Failing row contains (-2, minus two, 20).
INSERT INTO v2 VALUES (3, 'three', 30); -- not allowed
ERROR:  new row violates check option for view "v2"
DETAIL:  Failing row contains (3, three, 30).
UPDATE v2 SET b = 'ONE' WHERE a = 1; -- ok
UPDATE v2 SET a = -1 WHERE a = 1; -- not allowed
ERROR:  new row violates check option for view "v1"
DETAIL:  Failing row contains (-1, ONE, 10).
UPDATE v2 SET c = 30 WHERE a = 1; -- not allowed
ERROR:  new row violates check option for view "v2"
DETAIL:  Failing row contains (1, ONE, 30).
DELETE FROM v2 WHERE a = 2; -- ok
SELECT * FROM v2;
 a |  b  | c  
---+-----+----
 1 | ONE | 10
(1 row)

DROP VIEW v2;
DROP VIEW v1;
DROP TABLE t2;
DROP TABLE t1;
--
-- Test CREATE OR REPLACE VIEW turning a non-updatable view into an
-- auto-updatable view and adding check options in a single step
--
CREATE TABLE t1 (a int, b text);
CREATE VIEW v1 AS SELECT null::int AS a;
CREATE OR REPLACE VIEW v1 AS SELECT * FROM t1 WHERE a > 0 WITH CHECK OPTION;
INSERT INTO v1 VALUES (1, 'ok'); -- ok
INSERT INTO v1 VALUES (-1, 'invalid'); -- should fail
ERROR:  new row violates check option for view "v1"
DETAIL:  Failing row contains (-1, invalid).
DROP VIEW v1;
DROP TABLE t1;
-- check that an auto-updatable view on a partitioned table works correctly
create table uv_pt (a int, b int, v varchar) partition by range (a, b);
create table uv_pt1 (b int not null, v varchar, a int not null) partition by range (b);
create table uv_pt11 (like uv_pt1);
alter table uv_pt11 drop a;
alter table uv_pt11 add a int;
alter table uv_pt11 drop a;
alter table uv_pt11 add a int not null;
alter table uv_pt1 attach partition uv_pt11 for values from (2) to (5);
alter table uv_pt attach partition uv_pt1 for values from (1, 2) to (1, 10);
create view uv_ptv as select * from uv_pt;
select events & 4 != 0 AS upd,
       events & 8 != 0 AS ins,
       events & 16 != 0 AS del
  from pg_catalog.pg_relation_is_updatable('uv_pt'::regclass, false) t(events);
 upd | ins | del 
-----+-----+-----
 t   | t   | t
(1 row)

select pg_catalog.pg_column_is_updatable('uv_pt'::regclass, 1::smallint, false);
 pg_column_is_updatable 
------------------------
 t
(1 row)

select pg_catalog.pg_column_is_updatable('uv_pt'::regclass, 2::smallint, false);
 pg_column_is_updatable 
------------------------
 t
(1 row)

select table_name, is_updatable, is_insertable_into
  from information_schema.views where table_name = 'uv_ptv';
 table_name | is_updatable | is_insertable_into 
------------+--------------+--------------------
 uv_ptv     | YES          | YES
(1 row)

select table_name, column_name, is_updatable
  from information_schema.columns where table_name = 'uv_ptv' order by column_name;
 table_name | column_name | is_updatable 
------------+-------------+--------------
 uv_ptv     | a           | YES
 uv_ptv     | b           | YES
 uv_ptv     | v           | YES
(3 rows)

insert into uv_ptv values (1, 2);
select tableoid::regclass, * from uv_pt;
 tableoid | a | b | v 
----------+---+---+---
 uv_pt11  | 1 | 2 | 
(1 row)

create view uv_ptv_wco as select * from uv_pt where a = 0 with check option;
insert into uv_ptv_wco values (1, 2);
ERROR:  new row violates check option for view "uv_ptv_wco"
DETAIL:  Failing row contains (1, 2, null).
drop view uv_ptv, uv_ptv_wco;
drop table uv_pt, uv_pt1, uv_pt11;
-- check that wholerow vars appearing in WITH CHECK OPTION constraint expressions
-- work fine with partitioned tables
create table wcowrtest (a int) partition by list (a);
create table wcowrtest1 partition of wcowrtest for values in (1);
create view wcowrtest_v as select * from wcowrtest where wcowrtest = '(2)'::wcowrtest with check option;
insert into wcowrtest_v values (1);
ERROR:  new row violates check option for view "wcowrtest_v"
DETAIL:  Failing row contains (1).
alter table wcowrtest add b text;
create table wcowrtest2 (b text, c int, a int);
alter table wcowrtest2 drop c;
alter table wcowrtest attach partition wcowrtest2 for values in (2);
create table sometable (a int, b text);
insert into sometable values (1, 'a'), (2, 'b');
create view wcowrtest_v2 as
    select *
      from wcowrtest r
      where r in (select s from sometable s where r.a = s.a)
with check option;
-- WITH CHECK qual will be processed with wcowrtest2's
-- rowtype after tuple-routing
insert into wcowrtest_v2 values (2, 'no such row in sometable');
ERROR:  new row violates check option for view "wcowrtest_v2"
DETAIL:  Failing row contains (2, no such row in sometable).
drop view wcowrtest_v, wcowrtest_v2;
drop table wcowrtest, sometable;
-- Check INSERT .. ON CONFLICT DO UPDATE works correctly when the view's
-- columns are named and ordered differently than the underlying table's.
create table uv_iocu_tab (a text unique, b float);
insert into uv_iocu_tab values ('xyxyxy', 0);
create view uv_iocu_view as
   select b, b+1 as c, a, '2.0'::text as two from uv_iocu_tab;
insert into uv_iocu_view (a, b) values ('xyxyxy', 1)
   on conflict (a) do update set b = uv_iocu_view.b;
select * from uv_iocu_tab;
   a    | b 
--------+---
 xyxyxy | 0
(1 row)

insert into uv_iocu_view (a, b) values ('xyxyxy', 1)
   on conflict (a) do update set b = excluded.b;
select * from uv_iocu_tab;
   a    | b 
--------+---
 xyxyxy | 1
(1 row)

-- OK to access view columns that are not present in underlying base
-- relation in the ON CONFLICT portion of the query
insert into uv_iocu_view (a, b) values ('xyxyxy', 3)
   on conflict (a) do update set b = cast(excluded.two as float);
select * from uv_iocu_tab;
   a    | b 
--------+---
 xyxyxy | 2
(1 row)

explain (costs off)
insert into uv_iocu_view (a, b) values ('xyxyxy', 3)
   on conflict (a) do update set b = excluded.b where excluded.c > 0;
                                    QUERY PLAN                                     
-----------------------------------------------------------------------------------
 Insert on uv_iocu_tab
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: uv_iocu_tab_a_key
   Conflict Filter: ((excluded.b + '1'::double precision) > '0'::double precision)
   ->  Result
(5 rows)

insert into uv_iocu_view (a, b) values ('xyxyxy', 3)
   on conflict (a) do update set b = excluded.b where excluded.c > 0;
select * from uv_iocu_tab;
   a    | b 
--------+---
 xyxyxy | 3
(1 row)

drop view uv_iocu_view;
drop table uv_iocu_tab;
-- Test whole-row references to the view
create table uv_iocu_tab (a int unique, b text);
create view uv_iocu_view as
    select b as bb, a as aa, uv_iocu_tab::text as cc from uv_iocu_tab;
insert into uv_iocu_view (aa,bb) values (1,'x');
explain (costs off)
insert into uv_iocu_view (aa,bb) values (1,'y')
   on conflict (aa) do update set bb = 'Rejected: '||excluded.*
   where excluded.aa > 0
   and excluded.bb != ''
   and excluded.cc is not null;
                                               QUERY PLAN                                                
---------------------------------------------------------------------------------------------------------
 Insert on uv_iocu_tab
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: uv_iocu_tab_a_key
   Conflict Filter: ((excluded.a > 0) AND (excluded.b <> ''::text) AND ((excluded.*)::text IS NOT NULL))
   ->  Result
(5 rows)

insert into uv_iocu_view (aa,bb) values (1,'y')
   on conflict (aa) do update set bb = 'Rejected: '||excluded.*
   where excluded.aa > 0
   and excluded.bb != ''
   and excluded.cc is not null;
select * from uv_iocu_view;
           bb            | aa |               cc                
-------------------------+----+---------------------------------
 Rejected: (y,1,"(1,y)") |  1 | (1,"Rejected: (y,1,""(1,y)"")")
(1 row)

-- Test omitting a column of the base relation
delete from uv_iocu_view;
insert into uv_iocu_view (aa,bb) values (1,'x');
insert into uv_iocu_view (aa) values (1)
   on conflict (aa) do update set bb = 'Rejected: '||excluded.*;
select * from uv_iocu_view;
          bb           | aa |              cc               
-----------------------+----+-------------------------------
 Rejected: (,1,"(1,)") |  1 | (1,"Rejected: (,1,""(1,)"")")
(1 row)

alter table uv_iocu_tab alter column b set default 'table default';
insert into uv_iocu_view (aa) values (1)
   on conflict (aa) do update set bb = 'Rejected: '||excluded.*;
select * from uv_iocu_view;
                          bb                           | aa |                                 cc                                  
-------------------------------------------------------+----+---------------------------------------------------------------------
 Rejected: ("table default",1,"(1,""table default"")") |  1 | (1,"Rejected: (""table default"",1,""(1,""""table default"""")"")")
(1 row)

alter view uv_iocu_view alter column bb set default 'view default';
insert into uv_iocu_view (aa) values (1)
   on conflict (aa) do update set bb = 'Rejected: '||excluded.*;
select * from uv_iocu_view;
                         bb                          | aa |                                cc                                 
-----------------------------------------------------+----+-------------------------------------------------------------------
 Rejected: ("view default",1,"(1,""view default"")") |  1 | (1,"Rejected: (""view default"",1,""(1,""""view default"""")"")")
(1 row)

-- Should fail to update non-updatable columns
insert into uv_iocu_view (aa) values (1)
   on conflict (aa) do update set cc = 'XXX';
ERROR:  cannot insert into column "cc" of view "uv_iocu_view"
DETAIL:  View columns that are not columns of their base relation are not updatable.
drop view uv_iocu_view;
drop table uv_iocu_tab;
-- ON CONFLICT DO UPDATE permissions checks
create user regress_view_user1;
create user regress_view_user2;
set session authorization regress_view_user1;
create table base_tbl(a int unique, b text, c float);
insert into base_tbl values (1,'xxx',1.0);
create view rw_view1 as select b as bb, c as cc, a as aa from base_tbl;
grant select (aa,bb) on rw_view1 to regress_view_user2;
grant insert on rw_view1 to regress_view_user2;
grant update (bb) on rw_view1 to regress_view_user2;
set session authorization regress_view_user2;
insert into rw_view1 values ('yyy',2.0,1)
  on conflict (aa) do update set bb = excluded.cc; -- Not allowed
ERROR:  permission denied for view rw_view1
insert into rw_view1 values ('yyy',2.0,1)
  on conflict (aa) do update set bb = rw_view1.cc; -- Not allowed
ERROR:  permission denied for view rw_view1
insert into rw_view1 values ('yyy',2.0,1)
  on conflict (aa) do update set bb = excluded.bb; -- OK
insert into rw_view1 values ('zzz',2.0,1)
  on conflict (aa) do update set bb = rw_view1.bb||'xxx'; -- OK
insert into rw_view1 values ('zzz',2.0,1)
  on conflict (aa) do update set cc = 3.0; -- Not allowed
ERROR:  permission denied for view rw_view1
reset session authorization;
select * from base_tbl;
 a |   b    | c 
---+--------+---
 1 | yyyxxx | 1
(1 row)

set session authorization regress_view_user1;
grant select (a,b) on base_tbl to regress_view_user2;
grant insert (a,b) on base_tbl to regress_view_user2;
grant update (a,b) on base_tbl to regress_view_user2;
set session authorization regress_view_user2;
create view rw_view2 as select b as bb, c as cc, a as aa from base_tbl;
insert into rw_view2 (aa,bb) values (1,'xxx')
  on conflict (aa) do update set bb = excluded.bb; -- Not allowed
ERROR:  permission denied for table base_tbl
create view rw_view3 as select b as bb, a as aa from base_tbl;
insert into rw_view3 (aa,bb) values (1,'xxx')
  on conflict (aa) do update set bb = excluded.bb; -- OK
reset session authorization;
select * from base_tbl;
 a |  b  | c 
---+-----+---
 1 | xxx | 1
(1 row)

set session authorization regress_view_user2;
create view rw_view4 as select aa, bb, cc FROM rw_view1;
insert into rw_view4 (aa,bb) values (1,'yyy')
  on conflict (aa) do update set bb = excluded.bb; -- Not allowed
ERROR:  permission denied for view rw_view1
create view rw_view5 as select aa, bb FROM rw_view1;
insert into rw_view5 (aa,bb) values (1,'yyy')
  on conflict (aa) do update set bb = excluded.bb; -- OK
reset session authorization;
select * from base_tbl;
 a |  b  | c 
---+-----+---
 1 | yyy | 1
(1 row)

drop view rw_view5;
drop view rw_view4;
drop view rw_view3;
drop view rw_view2;
drop view rw_view1;
drop table base_tbl;
drop user regress_view_user1;
drop user regress_view_user2;
-- Test single- and multi-row inserts with table and view defaults.
-- Table defaults should be used, unless overridden by view defaults.
create table base_tab_def (a int, b text default 'Table default',
                           c text default 'Table default', d text, e text);
create view base_tab_def_view as select * from base_tab_def;
alter view base_tab_def_view alter b set default 'View default';
alter view base_tab_def_view alter d set default 'View default';
insert into base_tab_def values (1);
insert into base_tab_def values (2), (3);
insert into base_tab_def values (4, default, default, default, default);
insert into base_tab_def values (5, default, default, default, default),
                                (6, default, default, default, default);
insert into base_tab_def_view values (11);
insert into base_tab_def_view values (12), (13);
insert into base_tab_def_view values (14, default, default, default, default);
insert into base_tab_def_view values (15, default, default, default, default),
                                     (16, default, default, default, default);
insert into base_tab_def_view values (17), (default);
select * from base_tab_def order by a;
 a  |       b       |       c       |      d       | e 
----+---------------+---------------+--------------+---
  1 | Table default | Table default |              | 
  2 | Table default | Table default |              | 
  3 | Table default | Table default |              | 
  4 | Table default | Table default |              | 
  5 | Table default | Table default |              | 
  6 | Table default | Table default |              | 
 11 | View default  | Table default | View default | 
 12 | View default  | Table default | View default | 
 13 | View default  | Table default | View default | 
 14 | View default  | Table default | View default | 
 15 | View default  | Table default | View default | 
 16 | View default  | Table default | View default | 
 17 | View default  | Table default | View default | 
    | View default  | Table default | View default | 
(14 rows)

-- Adding an INSTEAD OF trigger should cause NULLs to be inserted instead of
-- table defaults, where there are no view defaults.
create function base_tab_def_view_instrig_func() returns trigger
as
$$
begin
  insert into base_tab_def values (new.a, new.b, new.c, new.d, new.e);
  return new;
end;
$$
language plpgsql;
create trigger base_tab_def_view_instrig instead of insert on base_tab_def_view
  for each row execute function base_tab_def_view_instrig_func();
truncate base_tab_def;
insert into base_tab_def values (1);
insert into base_tab_def values (2), (3);
insert into base_tab_def values (4, default, default, default, default);
insert into base_tab_def values (5, default, default, default, default),
                                (6, default, default, default, default);
insert into base_tab_def_view values (11);
insert into base_tab_def_view values (12), (13);
insert into base_tab_def_view values (14, default, default, default, default);
insert into base_tab_def_view values (15, default, default, default, default),
                                     (16, default, default, default, default);
insert into base_tab_def_view values (17), (default);
select * from base_tab_def order by a;
 a  |       b       |       c       |      d       | e 
----+---------------+---------------+--------------+---
  1 | Table default | Table default |              | 
  2 | Table default | Table default |              | 
  3 | Table default | Table default |              | 
  4 | Table default | Table default |              | 
  5 | Table default | Table default |              | 
  6 | Table default | Table default |              | 
 11 | View default  |               | View default | 
 12 | View default  |               | View default | 
 13 | View default  |               | View default | 
 14 | View default  |               | View default | 
 15 | View default  |               | View default | 
 16 | View default  |               | View default | 
 17 | View default  |               | View default | 
    | View default  |               | View default | 
(14 rows)

-- Using an unconditional DO INSTEAD rule should also cause NULLs to be
-- inserted where there are no view defaults.
drop trigger base_tab_def_view_instrig on base_tab_def_view;
drop function base_tab_def_view_instrig_func;
create rule base_tab_def_view_ins_rule as on insert to base_tab_def_view
  do instead insert into base_tab_def values (new.a, new.b, new.c, new.d, new.e);
truncate base_tab_def;
insert into base_tab_def values (1);
insert into base_tab_def values (2), (3);
insert into base_tab_def values (4, default, default, default, default);
insert into base_tab_def values (5, default, default, default, default),
                                (6, default, default, default, default);
insert into base_tab_def_view values (11);
insert into base_tab_def_view values (12), (13);
insert into base_tab_def_view values (14, default, default, default, default);
insert into base_tab_def_view values (15, default, default, default, default),
                                     (16, default, default, default, default);
insert into base_tab_def_view values (17), (default);
select * from base_tab_def order by a;
 a  |       b       |       c       |      d       | e 
----+---------------+---------------+--------------+---
  1 | Table default | Table default |              | 
  2 | Table default | Table default |              | 
  3 | Table default | Table default |              | 
  4 | Table default | Table default |              | 
  5 | Table default | Table default |              | 
  6 | Table default | Table default |              | 
 11 | View default  |               | View default | 
 12 | View default  |               | View default | 
 13 | View default  |               | View default | 
 14 | View default  |               | View default | 
 15 | View default  |               | View default | 
 16 | View default  |               | View default | 
 17 | View default  |               | View default | 
    | View default  |               | View default | 
(14 rows)

-- A DO ALSO rule should cause each row to be inserted twice. The first
-- insert should behave the same as an auto-updatable view (using table
-- defaults, unless overridden by view defaults). The second insert should
-- behave the same as a rule-updatable view (inserting NULLs where there are
-- no view defaults).
drop rule base_tab_def_view_ins_rule on base_tab_def_view;
create rule base_tab_def_view_ins_rule as on insert to base_tab_def_view
  do also insert into base_tab_def values (new.a, new.b, new.c, new.d, new.e);
truncate base_tab_def;
insert into base_tab_def values (1);
insert into base_tab_def values (2), (3);
insert into base_tab_def values (4, default, default, default, default);
insert into base_tab_def values (5, default, default, default, default),
                                (6, default, default, default, default);
insert into base_tab_def_view values (11);
insert into base_tab_def_view values (12), (13);
insert into base_tab_def_view values (14, default, default, default, default);
insert into base_tab_def_view values (15, default, default, default, default),
                                     (16, default, default, default, default);
insert into base_tab_def_view values (17), (default);
select * from base_tab_def order by a, c NULLS LAST;
 a  |       b       |       c       |      d       | e 
----+---------------+---------------+--------------+---
  1 | Table default | Table default |              | 
  2 | Table default | Table default |              | 
  3 | Table default | Table default |              | 
  4 | Table default | Table default |              | 
  5 | Table default | Table default |              | 
  6 | Table default | Table default |              | 
 11 | View default  | Table default | View default | 
 11 | View default  |               | View default | 
 12 | View default  | Table default | View default | 
 12 | View default  |               | View default | 
 13 | View default  | Table default | View default | 
 13 | View default  |               | View default | 
 14 | View default  | Table default | View default | 
 14 | View default  |               | View default | 
 15 | View default  | Table default | View default | 
 15 | View default  |               | View default | 
 16 | View default  | Table default | View default | 
 16 | View default  |               | View default | 
 17 | View default  | Table default | View default | 
 17 | View default  |               | View default | 
    | View default  | Table default | View default | 
    | View default  |               | View default | 
(22 rows)

drop view base_tab_def_view;
drop table base_tab_def;
-- Test defaults with array assignments
create table base_tab (a serial, b int[], c text, d text default 'Table default');
create view base_tab_view as select c, a, b from base_tab;
alter view base_tab_view alter column c set default 'View default';
insert into base_tab_view (b[1], b[2], c, b[5], b[4], a, b[3])
values (1, 2, default, 5, 4, default, 3), (10, 11, 'C value', 14, 13, 100, 12);
select * from base_tab order by a;
  a  |        b         |      c       |       d       
-----+------------------+--------------+---------------
   1 | {1,2,3,4,5}      | View default | Table default
 100 | {10,11,12,13,14} | C value      | Table default
(2 rows)

drop view base_tab_view;
drop table base_tab;