This file is indexed.

/usr/lib/python3/dist-packages/postgresql/driver/pq3.py is in python3-postgresql 1.1.0-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
##
# .driver.pq3 - interface to PostgreSQL using PQ v3.0.
##
"""
PG-API interface for PostgreSQL using PQ version 3.0.
"""
import os
import weakref
import socket
from traceback import format_exception
from itertools import repeat, chain, count
from functools import partial
from abc import abstractmethod
from codecs import lookup as lookup_codecs

from operator import itemgetter
get0 = itemgetter(0)
get1 = itemgetter(1)

from .. import lib as pg_lib

from .. import versionstring as pg_version
from .. import iri as pg_iri
from .. import exceptions as pg_exc
from .. import string as pg_str
from .. import api as pg_api
from .. import message as pg_msg
from ..encodings.aliases import get_python_name
from ..string import quote_ident

from ..python.itertools import interlace, chunk
from ..python.socket import SocketFactory
from ..python.functools import process_tuple, process_chunk
from ..python.functools import Composition as compose

from ..protocol import xact3 as xact
from ..protocol import element3 as element
from ..protocol import client3 as client
from ..protocol.message_types import message_types

from ..notifyman import NotificationManager
from .. import types as pg_types
from ..types import io as pg_types_io
from ..types.io import lib as io_lib

import warnings

# Map element3.Notice field identifiers
# to names used by message.Message.
notice_field_to_name = {
	message_types[b'S'[0]] : 'severity',
	message_types[b'C'[0]] : 'code',
	message_types[b'M'[0]] : 'message',
	message_types[b'D'[0]] : 'detail',
	message_types[b'H'[0]] : 'hint',
	message_types[b'W'[0]] : 'context',
	message_types[b'P'[0]] : 'position',
	message_types[b'p'[0]] : 'internal_position',
	message_types[b'q'[0]] : 'internal_query',
	message_types[b'F'[0]] : 'file',
	message_types[b'L'[0]] : 'line',
	message_types[b'R'[0]] : 'function',
}
del message_types

notice_field_from_name = dict(
	(v, k) for (k, v) in notice_field_to_name.items()
)

could_not_connect = element.ClientError((
	(b'S', 'FATAL'),
	(b'C', '08001'),
	(b'M', "could not establish connection to server"),
))

# generate an id for a client statement or cursor
def ID(s, title = None, IDNS = 'py:'):
	return IDNS + hex(id(s))

def declare_statement_string(
	cursor_id,
	statement_string,
	insensitive = True,
	scroll = True,
	hold = True
):
	s = 'DECLARE ' + cursor_id
	if insensitive is True:
		s += ' INSENSITIVE'
	if scroll is True:
		s += ' SCROLL'
	s += ' CURSOR'
	if hold is True:
		s += ' WITH HOLD'
	else:
		s += ' WITHOUT HOLD'
	return s + ' FOR ' + statement_string

def direction_str_to_bool(str):
	s = str.upper()
	if s == 'FORWARD':
		return True
	elif s == 'BACKWARD':
		return False
	else:
		raise ValueError("invalid direction " + repr(str))

def direction_to_bool(v):
	if isinstance(v, str):
		return direction_str_to_bool(v)
	elif v is not True and v is not False:
		raise TypeError("invalid direction " + repr(v))
	else:
		return v

class TypeIO(pg_api.TypeIO):
	"""
	A class that manages I/O for a given configuration. Normally, a connection
	would create an instance, and configure it based upon the version and
	configuration of PostgreSQL that it is connected to.
	"""
	_e_factors = ('database',)
	strio = (None, None, str)

	def __init__(self, database):
		self.database = database
		self.encoding = None
		strio = self.strio
		self._cache = {
			# Encoded character strings
			pg_types.ACLITEMOID : strio, # No binary functions.
			pg_types.NAMEOID : strio,
			pg_types.BPCHAROID : strio,
			pg_types.VARCHAROID : strio,
			pg_types.CSTRINGOID : strio,
			pg_types.TEXTOID : strio,
			pg_types.REGTYPEOID : strio,
			pg_types.REGPROCOID : strio,
			pg_types.REGPROCEDUREOID : strio,
			pg_types.REGOPEROID : strio,
			pg_types.REGOPERATOROID : strio,
			pg_types.REGCLASSOID : strio,
		}
		self.typinfo = {}
		super().__init__()

	def lookup_type_info(self, typid):
		return self.database.sys.lookup_type(typid)

	def lookup_composite_type_info(self, typid):
		return self.database.sys.lookup_composite(typid)

	def lookup_domain_basetype(self, typid):
		if self.database.version_info[:2] >= (8, 4):
			return self.lookup_domain_basetype_84(typid)

		while typid:
			r = self.database.sys.lookup_basetype(typid)
			if not r[0][0]:
				return typid
			else:
				typid = r[0][0]

	def lookup_domain_basetype_84(self, typid):
		r = self.database.sys.lookup_basetype_recursive(typid)
		return r[0][0]

	def set_encoding(self, value):
		"""
		Set a new client encoding.
		"""
		self.encoding = value.lower().strip()
		enc = get_python_name(self.encoding)
		ci = lookup_codecs(enc or self.encoding)
		self._encode, self._decode, *_ = ci

	def encode(self, string_data):
		return self._encode(string_data)[0]

	def decode(self, bytes_data):
		return self._decode(bytes_data)[0]

	def encodes(self, iter, get0 = get0):
		"""
		Encode the items in the iterable in the configured encoding.
		"""
		return map(compose((self._encode, get0)), iter)

	def decodes(self, iter, get0 = get0):
		"""
		Decode the items in the iterable from the configured encoding.
		"""
		return map(compose((self._decode, get0)), iter)

	def resolve_pack(self, typid):
		return self.resolve(typid)[0] or self.encode

	def resolve_unpack(self, typid):
		return self.resolve(typid)[1] or self.decode

	def attribute_map(self, pq_descriptor):
		return zip(self.decodes(pq_descriptor.keys()), count())

	def sql_type_from_oid(self, oid, qi = quote_ident):
		if oid in pg_types.oid_to_sql_name:
			return pg_types.oid_to_sql_name[oid]
		if oid in self.typinfo:
			nsp, name, *_ = self.typinfo[oid]
			return qi(nsp) + '.' + qi(name)
		name = pg_types.oid_to_name.get(oid)
		if name:
			return 'pg_catalog.%s' % name
		else:
			return None

	def type_from_oid(self, oid):
		if oid in self._cache:
			typ = self._cache[oid][2]
		return typ

	def resolve_descriptor(self, desc, index):
		'create a sequence of I/O routines from a pq descriptor'
		return [
			(self.resolve(x[3]) or (None, None))[index] for x in desc
		]

	# lookup a type's IO routines from a given typid
	def resolve(self,
		typid : "The Oid of the type to resolve pack and unpack routines for.",
		from_resolution_of : \
		"Sequence of typid's used to identify infinite recursion" = (),
		builtins : "types.io.resolve" = pg_types_io.resolve,
		quote_ident = quote_ident
	):
		if from_resolution_of and typid in from_resolution_of:
			raise TypeError(
				"type, %d, is already being looked up: %r" %(
					typid, from_resolution_of
				)
			)
		typid = int(typid)
		typio = None

		if typid in self._cache:
			typio = self._cache[typid]
		else:
			typio = builtins(typid)
			if typio is not None:
				# If typio is a tuple, it's a constant pair: (pack, unpack)
				# otherwise, it's an I/O pair constructor.
				if typio.__class__ is not tuple:
					typio = typio(typid, self)
				self._cache[typid] = typio

		if typio is None:
			# Lookup the type information for the typid as it's not cached.
			##
			ti = self.lookup_type_info(typid)
			if ti is not None:
				typnamespace, typname, typtype, typlen, typelem, typrelid, \
					ae_typid, ae_hasbin_input, ae_hasbin_output = ti
				self.typinfo[typid] = (
					typnamespace, typname, typrelid, int(typelem) if ae_typid else None
				)
				if typrelid:
					# Row type
					#
					# The attribute name map,
					#  column I/O,
					#  column type Oids
					# are needed to build the packing pair.
					attmap = {}
					cio = []
					typids = []
					attnames = []
					i = 0
					for x in self.lookup_composite_type_info(typrelid):
						attmap[x[1]] = i
						attnames.append(x[1])
						if x[2]:
							# This is a domain
							fieldtypid = self.lookup_domain_basetype(x[0])
						else:
							fieldtypid = x[0]
						typids.append(x[0])
						te = self.resolve(
							fieldtypid, list(from_resolution_of) + [typid]
						)
						cio.append((te[0] or self.encode, te[1] or self.decode))
						i += 1
					self._cache[typid] = typio = self.record_io_factory(
						cio, typids, attmap, list(
							map(self.sql_type_from_oid, typids)
						), attnames,
						typrelid,
						quote_ident(typnamespace) + '.' + \
						quote_ident(typname),
					)
				elif ae_typid is not None:
					# resolve the element type and I/O pair
					te = self.resolve(
						int(typelem),
						from_resolution_of = list(from_resolution_of) + [typid]
					) or (None, None)
					typio = self.array_io_factory(
						te[0] or self.encode,
						te[1] or self.decode,
						typelem,
						ae_hasbin_input,
						ae_hasbin_output
					)
					self._cache[typid] = typio
				else:
					typio = None
					if typtype == b'd':
						basetype = self.lookup_domain_basetype(typid)
						typio = self.resolve(
							basetype,
							from_resolution_of = list(from_resolution_of) + [typid]
						)
					elif typtype == b'p' and typnamespace == 'pg_catalog' and typname == 'record':
						# anonymous record type
						typio = self.anon_record_io_factory()

					if not typio:
						typio = self.strio

					self._cache[typid] = typio
			else:
				# Throw warning about type without entry in pg_type?
				typio = self.strio
		return typio

	def identify(self, **identity_mappings):
		"""
		Explicitly designate the I/O handler for the specified type.

		Primarily used in cases involving UDTs.
		"""
		# get them ordered; we process separately, then recombine.
		id = list(identity_mappings.items())
		ios = [pg_types_io.resolve(x[0]) for x in id]
		oids = list(self.database.sys.regtypes([x[1] for x in id]))

		self._cache.update([
			(oid, io if io.__class__ is tuple else io(oid, self))
			for oid, io in zip(oids, ios)
		])

	def array_parts(self, array, ArrayType = pg_types.Array):
		if array.__class__ is not ArrayType:
			# Assume the data is a nested list.
			array = ArrayType(array)
		return (
			array.elements(),
			array.dimensions,
			array.lowerbounds
		)

	def array_from_parts(self, parts, ArrayType = pg_types.Array):
		elements, dimensions, lowerbounds = parts
		return ArrayType.from_elements(
			elements,
			lowerbounds = lowerbounds,
			upperbounds = [x + lb - 1 for x, lb in zip(dimensions, lowerbounds)]
		)

	##
	# array_io_factory - build I/O pair for ARRAYs
	##
	def array_io_factory(
		self,
		pack_element, unpack_element,
		typoid, # array element id
		hasbin_input, hasbin_output,
		array_pack = io_lib.array_pack,
		array_unpack = io_lib.array_unpack,
	):
		packed_typoid = io_lib.ulong_pack(typoid)
		if hasbin_input:
			def pack_an_array(data, get_parts = self.array_parts):
				elements, dimensions, lowerbounds = get_parts(data)
				return array_pack((
					0, # unused flags
					typoid, dimensions, lowerbounds,
					(x if x is None else pack_element(x) for x in elements),
				))
		else:
			# signals string formatting
			pack_an_array = None

		if hasbin_output:
			def unpack_an_array(data, array_from_parts = self.array_from_parts):
				flags, typoid, dims, lbs, elements = array_unpack(data)
				return array_from_parts(((x if x is None else unpack_element(x) for x in elements), dims, lbs))
		else:
			# signals string formatting
			unpack_an_array = None

		return (pack_an_array, unpack_an_array, pg_types.Array)

	def RowTypeFactory(self, attribute_map = {}, _Row = pg_types.Row.from_sequence, composite_relid = None):
		return partial(_Row, attribute_map)

	##
	# record_io_factory - Build an I/O pair for RECORDs
	##
	def record_io_factory(self,
		column_io : "sequence (pack,unpack) tuples corresponding to the columns",
		typids : "sequence of type Oids; index must correspond to the composite's",
		attmap : "mapping of column name to index number",
		typnames : "sequence of sql type names in order",
		attnames : "sequence of attribute names in order",
		composite_relid : "oid of the composite relation",
		composite_name : "the name of the composite type",
		get0 = get0,
		get1 = get1,
		fmt_errmsg = "failed to {0} attribute {1}, {2}::{3}, of composite {4} from wire data".format
	):
		fpack = tuple(map(get0, column_io))
		funpack = tuple(map(get1, column_io))
		row_constructor = self.RowTypeFactory(attribute_map = attmap, composite_relid = composite_relid)

		def raise_pack_tuple_error(cause, procs, tup, itemnum):
			data = repr(tup[itemnum])
			if len(data) > 80:
				# Be sure not to fill screen with noise.
				data = data[:75] + ' ...'
			self.raise_client_error(element.ClientError((
				(b'C', '--cIO',),
				(b'S', 'ERROR',),
				(b'M', fmt_errmsg('pack', itemnum, attnames[itemnum], typnames[itemnum], composite_name),),
				(b'W', data,),
				(b'P', str(itemnum),)
			)), cause = cause)

		def raise_unpack_tuple_error(cause, procs, tup, itemnum):
			data = repr(tup[itemnum])
			if len(data) > 80:
				# Be sure not to fill screen with noise.
				data = data[:75] + ' ...'
			self.raise_client_error(element.ClientError((
				(b'C', '--cIO',),
				(b'S', 'ERROR',),
				(b'M', fmt_errmsg('unpack', itemnum, attnames[itemnum], typnames[itemnum], composite_name),),
				(b'W', data,),
				(b'P', str(itemnum),),
			)), cause = cause)

		def unpack_a_record(data,
			unpack = io_lib.record_unpack,
			process_tuple = process_tuple,
			row_constructor = row_constructor
		):
			data = tuple([x[1] for x in unpack(data)])
			return row_constructor(process_tuple(funpack, data, raise_unpack_tuple_error))

		sorted_atts = sorted(attmap.items(), key = get1)
		def pack_a_record(data,
			pack = io_lib.record_pack,
			process_tuple = process_tuple,
		):
			if isinstance(data, dict):
				data = [data.get(k) for k,_ in sorted_atts]
			return pack(
				tuple(zip(
					typids,
					process_tuple(fpack, tuple(data), raise_pack_tuple_error)
				))
			)
		return (pack_a_record, unpack_a_record, tuple)

	def anon_record_io_factory(self):
		def raise_unpack_tuple_error(cause, procs, tup, itemnum):
			data = repr(tup[itemnum])
			if len(data) > 80:
				# Be sure not to fill screen with noise.
				data = data[:75] + ' ...'
			self.raise_client_error(element.ClientError((
				(b'C', '--cIO',),
				(b'S', 'ERROR',),
				(b'M', 'Could not unpack element {} from anonymous record'.format(itemnum)),
				(b'W', data,),
				(b'P', str(itemnum),)
			)), cause = cause)

		def _unpack_record(data, unpack = io_lib.record_unpack, process_tuple = process_tuple):
			record = list(unpack(data))
			coloids = tuple(x[0] for x in record)
			colio = map(self.resolve, coloids)
			column_unpack = tuple(c[1] or self.decode for c in colio)

			data = tuple(x[1] for x in record)

			return process_tuple(column_unpack, data, raise_unpack_tuple_error)

		return (None, _unpack_record)

	def raise_client_error(self, error_message, cause = None, creator = None):
		m = {
			notice_field_to_name[k] : v
			for k, v in error_message.items()
			# don't include unknown messages in this list.
			if k in notice_field_to_name
		}
		c = m.pop('code')
		ms = m.pop('message')
		client_error = self.lookup_exception(c)
		client_error = client_error(ms, code = c, details = m, source = 'CLIENT', creator = creator or self.database)
		client_error.database = self.database
		if cause is not None:
			raise client_error from cause
		else:
			raise client_error

	def lookup_exception(self, code, errorlookup = pg_exc.ErrorLookup,):
		return errorlookup(code)

	def lookup_warning(self, code, warninglookup = pg_exc.WarningLookup,):
		return warninglookup(code)

	def raise_server_error(self, error_message, cause = None, creator = None):
		m = dict(self.decode_notice(error_message))
		c = m.pop('code')
		ms = m.pop('message')
		server_error = self.lookup_exception(c)
		server_error = server_error(ms, code = c, details = m, source = 'SERVER', creator = creator or self.database)
		server_error.database = self.database
		if cause is not None:
			raise server_error from cause
		else:
			raise server_error

	def raise_error(self, error_message, ClientError = element.ClientError, **kw):
		if 'creator' not in kw:
			kw['creator'] = getattr(self.database, '_controller', self.database) or self.database

		if error_message.__class__ is ClientError:
			self.raise_client_error(error_message, **kw)
		else:
			self.raise_server_error(error_message, **kw)

	##
	# Used by decode_notice()
	def _decode_failsafe(self, data):
		decode = self._decode
		i = iter(data)
		for x in i:
			try:
				# prematurely optimized for your viewing displeasure.
				v = x[1]
				yield (x[0], decode(v)[0])
				for x in i:
					v = x[1]
					yield (x[0], decode(v)[0])
			except UnicodeDecodeError:
				# Fallback to the bytes representation.
				# This should be sufficiently informative in most cases,
				# and in the cases where it isn't, an element traceback should
				# ultimately yield the pertinent information
				yield (x[0], repr(x[1])[2:-1])

	def decode_notice(self, notice):
		notice = self._decode_failsafe(notice.items())
		return {
			notice_field_to_name[k] : v
			for k, v in notice
			# don't include unknown messages in this list.
			if k in notice_field_to_name
		}

	def emit_server_message(self, message, creator = None,
		MessageType = pg_msg.Message
	):
		fields = self.decode_notice(message)
		m = fields.pop('message')
		c = fields.pop('code')

		if fields['severity'].upper() == 'WARNING':
			MessageType = self.lookup_warning(c)

		message = MessageType(m, code = c, details = fields,
			creator = creator, source = 'SERVER')
		message.database = self.database
		message.emit()
		return message

	def emit_client_message(self, message, creator = None,
		MessageType = pg_msg.Message
	):
		fields = {
			notice_field_to_name[k] : v
			for k, v in message.items()
			# don't include unknown messages in this list.
			if k in notice_field_to_name
		}
		m = fields.pop('message')
		c = fields.pop('code')

		if fields['severity'].upper() == 'WARNING':
			MessageType = self.lookup_warning(c)

		message = MessageType(m, code = c, details = fields,
			creator = creator, source = 'CLIENT')
		message.database = self.database
		message.emit()
		return message

	def emit_message(self, message, ClientNotice = element.ClientNotice, **kw):
		if message.__class__ is ClientNotice:
			return self.emit_client_message(message, **kw)
		else:
			return self.emit_server_message(message, **kw)

##
# This class manages all the functionality used to get
# rows from a PostgreSQL portal/cursor.
class Output(object):
	_output = None
	_output_io = None
	_output_formats = None
	_output_attmap = None

	closed = False
	cursor_id = None
	statement = None
	parameters = None

	_complete_message = None

	@abstractmethod
	def _init(self):
		"""
		Bind a cursor based on the configured parameters.
		"""
		# The local initialization for the specific cursor.

	def __init__(self, cursor_id, wref = weakref.ref, ID = ID):
		self.cursor_id = cursor_id
		if self.statement is not None:
			stmt = self.statement
			self._output = stmt._output
			self._output_io = stmt._output_io
			self._row_constructor = stmt._row_constructor
			self._output_formats = stmt._output_formats or ()
			self._output_attmap = stmt._output_attmap

		self._pq_cursor_id = self.database.typio.encode(cursor_id)
		# If the cursor's id was generated, it should be garbage collected.
		if cursor_id == ID(self):
			self.database.pq.register_cursor(self, self._pq_cursor_id)
		self._quoted_cursor_id = '"' + cursor_id.replace('"', '""') + '"'
		self._init()

	def __iter__(self):
		return self

	def close(self):
		if self.closed is False:
			self.database.pq.trash_cursor(self._pq_cursor_id)
		self.closed = True

	def _ins(self, *args):
		return xact.Instruction(*args, asynchook = self.database._receive_async)

	def _pq_xp_describe(self):
		return (element.DescribePortal(self._pq_cursor_id),)

	def _pq_xp_bind(self):
		return (
			element.Bind(
				self._pq_cursor_id,
				self.statement._pq_statement_id,
				self.statement._input_formats,
				self.statement._pq_parameters(self.parameters),
				self._output_formats,
			),
		)

	def _pq_xp_fetchall(self):
		return (
			element.Bind(
				b'',
				self.statement._pq_statement_id,
				self.statement._input_formats,
				self.statement._pq_parameters(self.parameters),
				self._output_formats,
			),
			element.Execute(b'', 0xFFFFFFFF),
		)

	def _pq_xp_declare(self):
		return (
			element.Parse(b'', self.database.typio.encode(
					declare_statement_string(
						str(self._quoted_cursor_id),
						str(self.statement.string)
					)
				), ()
			),
			element.Bind(
				b'', b'', self.statement._input_formats,
				self.statement._pq_parameters(self.parameters), ()
			),
			element.Execute(b'', 1),
		)

	def _pq_xp_execute(self, quantity):
		return (
			element.Execute(self._pq_cursor_id, quantity),
		)

	def _pq_xp_fetch(self, direction, quantity):
		##
		# It's an SQL declared cursor, manually construct the fetch commands.
		qstr = "FETCH " + ("FORWARD " if direction else "BACKWARD ")
		if quantity is None:
			qstr = qstr + "ALL IN " + self._quoted_cursor_id
		else:
			qstr = qstr \
				+ str(quantity) + " IN " + self._quoted_cursor_id
		return (
			element.Parse(b'', self.database.typio.encode(qstr), ()),
			element.Bind(b'', b'', (), (), self._output_formats),
			# The "limit" is defined in the fetch query.
			element.Execute(b'', 0xFFFFFFFF),
		)

	def _pq_xp_move(self, position, whence):
		return (
			element.Parse(b'',
				b'MOVE ' + whence + b' ' + position + b' IN ' + \
				self.database.typio.encode(self._quoted_cursor_id),
				()
			),
			element.Bind(b'', b'', (), (), ()),
			element.Execute(b'', 1),
		)

	def _process_copy_chunk(self, x):
		if x:
			if x[0].__class__ is not bytes or x[-1].__class__ is not bytes:
				return [
					y for y in x if y.__class__ is bytes
				]
		return x

	# Process the element.Tuple message in x for column()
	def _process_tuple_chunk_Column(self, x, range = range):
		unpack = self._output_io[0]
		# get the raw data for the first column
		l = [y[0] for y in x]
		# iterate over the range to keep track
		# of which item we're processing.
		r = range(len(l))
		try:
			return [unpack(l[i]) for i in r]
		except Exception:
			cause = sys.exc_info()[1]
		try:
			i = next(r)
		except StopIteration:
			i = len(l)
		self._raise_column_tuple_error(cause, self._output_io, (l[i],), 0)

	# Process the element.Tuple message in x for rows()
	def _process_tuple_chunk_Row(self, x,
		proc = process_chunk,
	):
		rc = self._row_constructor
		return [
			rc(y)
			for y in proc(self._output_io, x, self._raise_column_tuple_error)
		]

	# Process the elemnt.Tuple messages in `x` for chunks()
	def _process_tuple_chunk(self, x, proc = process_chunk):
		return proc(self._output_io, x, self._raise_column_tuple_error)

	def _raise_column_tuple_error(self, cause, procs, tup, itemnum):
		'for column processing'
		# The element traceback will include the full list of parameters.
		data = repr(tup[itemnum])
		if len(data) > 80:
			# Be sure not to fill screen with noise.
			data = data[:75] + ' ...'

		em = element.ClientError((
			(b'S', 'ERROR'),
			(b'C', "--CIO"),
			(b'M', "failed to unpack column %r, %s::%s, from wire data" %(
					itemnum,
					self.column_names[itemnum],
					self.database.typio.sql_type_from_oid(
						self.statement.pg_column_types[itemnum]
					) or '<unknown>',
				)
			),
			(b'D', data),
			(b'H', "Try casting the column to 'text'."),
			(b'P', str(itemnum)),
		))
		self.database.typio.raise_client_error(em, creator = self, cause = cause)

	@property
	def state(self):
		if self.closed:
			return 'closed'
		else:
			return 'open'

	@property
	def column_names(self):
		if self._output is not None:
			return list(self.database.typio.decodes(self._output.keys()))
		# `None` if _output does not exist; not row data

	@property
	def column_types(self):
		if self._output is not None:
			return [self.database.typio.type_from_oid(x[3]) for x in self._output]
		# `None` if _output does not exist; not row data

	@property
	def pg_column_types(self):
		if self._output is not None:
			return [x[3] for x in self._output]
		# `None` if _output does not exist; not row data

	@property
	def sql_column_types(self):
		return [
			self.database.typio.sql_type_from_oid(x)
			for x in self.pg_column_types
		]

	def command(self):
		"The completion message's command identifier"
		if self._complete_message is not None:
			return self._complete_message.extract_command().decode('ascii')

	def count(self):
		"The completion message's count number"
		if self._complete_message is not None:
			return self._complete_message.extract_count()

class Chunks(Output, pg_api.Chunks):
	pass

##
# FetchAll - A Chunks cursor that gets *all* the records in the cursor.
#
# It has added complexity over other variants as in order to stream results,
# chunks have to be removed from the protocol transaction's received messages.
# If this wasn't done, the entire result set would be fully buffered prior
# to processing.
class FetchAll(Chunks):
	_e_factors = ('statement', 'parameters',)
	def _e_metas(self):
		yield ('type', type(self).__name__)

	def __init__(self, statement, parameters):
		self.statement = statement
		self.parameters = parameters
		self.database = statement.database
		Output.__init__(self, '')

	def _init(self,
		null = element.Null.type,
		complete = element.Complete.type,
		bindcomplete = element.BindComplete.type,
		parsecomplete = element.ParseComplete.type,
	):
		expect = self._expect
		self._xact = self._ins(
			self._pq_xp_fetchall() + (element.SynchronizeMessage,)
		)
		self.database._pq_push(self._xact, self)

		# Get more messages until the first Tuple is seen.
		STEP = self.database._pq_step
		while self._xact.state != xact.Complete:
			STEP()
			for x in self._xact.messages_received():
				if x.__class__ is tuple or expect == x.type:
					# No need to step anymore once this is seen.
					return
				elif x.type == null:
					# The protocol transaction is going to be complete..
					self.database._pq_complete()
					self._xact = None
					return
				elif x.type == complete:
					self._complete_message = x
					self.database._pq_complete()
					# If this was a select/copy cursor,
					# the data messages would have caused an earlier
					# return. It's empty.
					self._xact = None
					return
				elif x.type in (bindcomplete, parsecomplete):
					# Noise.
					pass
				else:
					# This should have been caught by the protocol transaction.
					# "Can't happen".
					self.database._pq_complete()
					if self._xact.fatal is None:
						self._xact.fatal = False
						self._xact.error_message = element.ClientError((
							(b'S', 'ERROR'),
							(b'C', "--000"),
							(b'M', "unexpected message type " + repr(x.type))
						))
						self.database.typio.raise_client_error(self._xact.error_message, creator = self)
					return

	def __next__(self,
		data_types = (tuple,bytes),
		complete = element.Complete.type,
	):
		x = self._xact
		# self._xact = None; means that the cursor has been exhausted.
		if x is None:
			raise StopIteration

		# Finish the protocol transaction.
		STEP = self.database._pq_step
		while x.state is not xact.Complete and not x.completed:
			STEP()

		# fatal is None == no error
		# fatal is True == dead connection
		# fatal is False == dead transaction
		if x.fatal is not None:
			self.database.typio.raise_error(x.error_message, creator = getattr(self, '_controller', self) or self)

		# no messages to process?
		if not x.completed:
			# Transaction has been cleaned out of completed? iterator is done.
			self._xact = None
			self.close()
			raise StopIteration

		# Get the chunk to be processed.
		chunk = [
			y for y in x.completed[0][1]
			if y.__class__ in data_types
		]
		r = self._process_chunk(chunk)

		# Scan for _complete_message.
		# Arguably, this can fail, but it would be a case
		# where multiple sync messages were issued. Something that's
		# not naturally occurring.
		for y in x.completed[0][1][-3:]:
			if getattr(y, 'type', None) == complete:
				self._complete_message = y

		# Remove it, it's been processed.
		del x.completed[0]
		return r

class SingleXactCopy(FetchAll):
	_expect = element.CopyToBegin.type
	_process_chunk = FetchAll._process_copy_chunk

class SingleXactFetch(FetchAll):
	_expect = element.Tuple.type

class MultiXactStream(Chunks):
	chunksize = 1024 * 4
	# only tuple streams
	_process_chunk = Output._process_tuple_chunk

	def _e_metas(self):
		yield ('chunksize', self.chunksize)
		yield ('type', self.__class__.__name__)

	def __init__(self, statement, parameters, cursor_id):
		self.statement = statement
		self.parameters = parameters
		self.database = statement.database
		Output.__init__(self, cursor_id or ID(self))

	@abstractmethod
	def _bind(self):
		"""
		Generate the commands needed to bind the cursor.
		"""

	@abstractmethod
	def _fetch(self):
		"""
		Generate the commands needed to bind the cursor.
		"""

	def _init(self):
		self._command = self._fetch()
		self._xact = self._ins(self._bind() + self._command)
		self.database._pq_push(self._xact, self)

	def __next__(self, tuple_type = tuple):
		x = self._xact
		if x is None:
			raise StopIteration

		if self.database.pq.xact is x:
			self.database._pq_complete()

		# get all the element.Tuple messages
		chunk = [
			y for y in x.messages_received() if y.__class__ is tuple_type
		]
		if len(chunk) == self.chunksize:
			# there may be more, dispatch the request for the next chunk
			self._xact = self._ins(self._command)
			self.database._pq_push(self._xact, self)
		else:
			# it's done.
			self._xact = None
			self.close()
			if not chunk:
				# chunk is empty, it's done *right* now.
				raise StopIteration
		chunk = self._process_chunk(chunk)
		return chunk

##
# The cursor is streamed to the client on demand *inside*
# a single SQL transaction block.
class MultiXactInsideBlock(MultiXactStream):
	_bind = MultiXactStream._pq_xp_bind
	def _fetch(self):
		##
		# Use the extended protocol's execute to fetch more.
		return self._pq_xp_execute(self.chunksize) + \
			(element.SynchronizeMessage,)

##
# The cursor is streamed to the client on demand *outside* of
# a single SQL transaction block. [DECLARE ... WITH HOLD]
class MultiXactOutsideBlock(MultiXactStream):
	_bind = MultiXactStream._pq_xp_declare

	def _fetch(self):
		##
		# Use the extended protocol's execute to fetch more *against*
		# an SQL FETCH statement yielding the data in the proper format.
		#
		# MultiXactOutsideBlock uses DECLARE to create the cursor WITH HOLD.
		# When this is done, the cursor is configured to use StringFormat with
		# all columns. It's necessary to use FETCH to adjust the formatting.
		return self._pq_xp_fetch(True, self.chunksize) + \
			(element.SynchronizeMessage,)

##
# Cursor is used to manage scrollable cursors.
class Cursor(Output, pg_api.Cursor):
	_process_tuple = Output._process_tuple_chunk_Row
	def _e_metas(self):
		yield ('direction', 'FORWARD' if self.direction else 'BACKWORD')
		yield ('type', 'Cursor')

	def clone(self):
		return type(self)(self.statement, self.parameters, self.database, None)

	def __init__(self, statement, parameters, database, cursor_id):
		self.database = database or statement.database
		self.statement = statement
		self.parameters = parameters
		self.__dict__['direction'] = True
		if self.statement is None:
			self._e_factors = ('database', 'cursor_id')
		Output.__init__(self, cursor_id or ID(self))

	def get_direction(self):
		return self.__dict__['direction']
	def set_direction(self, value):
		self.__dict__['direction'] = direction_to_bool(value)
	direction = property(
		fget = get_direction,
		fset = set_direction,
	)
	del get_direction, set_direction

	def _which_way(self, direction):
		if direction is not None:
			direction = direction_to_bool(direction)
			# -1 * -1 = 1, -1 * 1 = -1, 1 * 1 = 1
			return not ((not self.direction) ^ (not direction))
		else:
			return self.direction

	def _init(self,
		tupledesc = element.TupleDescriptor.type,
	):
		"""
		Based on the cursor parameters and the current transaction state,
		select a cursor strategy for managing the response from the server.
		"""
		if self.statement is not None:
			x = self._ins(self._pq_xp_declare() + (element.SynchronizeMessage,))
			self.database._pq_push(x, self)
			self.database._pq_complete()
		else:
			x = self._ins(self._pq_xp_describe() + (element.SynchronizeMessage,))
			self.database._pq_push(x, self)
			self.database._pq_complete()
			for m in x.messages_received():
				if m.type == tupledesc:
					typio = self.database.typio
					self._output = m
					self._output_attmap = typio.attribute_map(self._output)
					self._row_constructor = typio.RowTypeFactory(self._output_attmap)
					# tuple output
					self._output_io = typio.resolve_descriptor(
						self._output, 1 # (input, output)[1]
					)
					self._output_formats = [
						element.StringFormat
						if x is None
						else element.BinaryFormat
						for x in self._output_io
					]
					self._output_io = tuple([
						x or typio.decode for x in self._output_io
					])

	def __next__(self):
		result = self._fetch(self.direction, 1)
		if not result:
			raise StopIteration
		else:
			return result[0]

	def read(self, quantity = None, direction = None):
		if quantity == 0:
			return []
		dir = self._which_way(direction)
		return self._fetch(dir, quantity)

	def _fetch(self, direction, quantity):
		x = self._ins(
			self._pq_xp_fetch(direction, quantity) + \
			(element.SynchronizeMessage,)
		)
		self.database._pq_push(x, self)
		self.database._pq_complete()
		return self._process_tuple((
			y for y in x.messages_received() if y.__class__ is tuple
		))

	def seek(self, offset, whence = 'ABSOLUTE'):
		rwhence = self._seek_whence_map.get(whence, whence)
		if rwhence is None or rwhence.upper() not in \
		self._seek_whence_map.values():
			raise TypeError(
				"unknown whence parameter, %r" %(whence,)
			)
		rwhence = rwhence.upper()

		if offset == 'ALL':
			if rwhence not in ('BACKWARD', 'FORWARD'):
				rwhence = 'BACKWARD' if self.direction is False else 'FORWARD'
		else:
			if offset < 0 and rwhence == 'BACKWARD':
				offset = -offset
				rwhence = 'FORWARD'

			if self.direction is False:
				if offset == 'ALL' and rwhence != 'FORWARD':
					rwhence = 'BACKWARD'
				else:
					if rwhence == 'RELATIVE':
						offset = -offset
					elif rwhence == 'ABSOLUTE':
						rwhence = 'FROM_END'
					else:
						rwhence = 'ABSOLUTE'

		if rwhence in ('RELATIVE', 'BACKWARD', 'FORWARD'):
			if offset == 'ALL':
				cmd = self._pq_xp_move(
					str(offset).encode('ascii'), str(rwhence).encode('ascii')
				)
			else:
				if offset < 0:
					cmd = self._pq_xp_move(
						str(-offset).encode('ascii'), b'BACKWARD'
					)
				else:
					cmd = self._pq_xp_move(
						str(offset).encode('ascii'), str(rwhence).encode('ascii')
					)
		elif rwhence == 'ABSOLUTE':
			cmd = self._pq_xp_move(str(offset).encode('ascii'), b'ABSOLUTE')
		else:
			# move to last record, then consume it to put the position at
			# the very end of the cursor.
			cmd = self._pq_xp_move(b'', b'LAST') + \
				self._pq_xp_move(b'', b'NEXT') + \
				self._pq_xp_move(str(offset).encode('ascii'), b'BACKWARD')

		x = self._ins(cmd + (element.SynchronizeMessage,),)
		self.database._pq_push(x, self)
		self.database._pq_complete()

		count = None
		complete = element.Complete.type
		for cm in x.messages_received():
			if getattr(cm, 'type', None) == complete:
				count = cm.extract_count()
				break

		# XXX: Raise if count is None?
		return count

class SingleExecution(pg_api.Execution):
	database = None
	def __init__(self, database):
		self._prepare = database.prepare

	def load_rows(self, query, *parameters):
		return self._prepare(query).load_rows(*parameters)

	def load_chunks(self, query, *parameters):
		return self._prepare(query).load_chunks(*parameters)

	def __call__(self, query, *parameters):
		return self._prepare(query)(*parameters)

	def declare(self, query, *parameters):
		return self._prepare(query).declare(*parameters)

	def rows(self, query, *parameters):
		return self._prepare(query).rows(*parameters)

	def chunks(self, query, *parameters):
		return self._prepare(query).chunks(*parameters)

	def column(self, query, *parameters):
		return self._prepare(query).column(*parameters)

	def first(self, query, *parameters):
		return self._prepare(query).first(*parameters)

class Statement(pg_api.Statement):
	string = None
	database = None
	statement_id = None
	_input = None
	_output = None
	_output_io = None
	_output_formats = None
	_output_attmap = None

	def _e_metas(self):
		yield (None, '[' + self.state + ']')
		if hasattr(self._xact, 'error_message'):
			# be very careful not to trigger an exception.
			# even in the cases of effective protocol errors,
			# it is important not to bomb out.
			pos = self._xact.error_message.get(b'P')
			if pos is not None and pos.isdigit():
				try:
					pos = int(pos)
					# get the statement source
					q = str(self.string)
					# normalize position..
					pos = len('\n'.join(q[:pos].splitlines()))
					# normalize newlines
					q = '\n'.join(q.splitlines())
					line_no = q.count('\n', 0, pos) + 1
					# replace tabs with spaces because there is no way to identify
					# the tab size of the final display. (ie, marker will be wrong)
					q = q.replace('\t', ' ')
					# grab the relevant part of the query string.
					# the full source will be printed elsewhere.
					# beginning of string or the newline before the position
					bov = q.rfind('\n', 0, pos) + 1
					# end of string or the newline after the position
					eov = q.find('\n', pos)
					if eov == -1:
						eov = len(q)
					view = q[bov:eov]
					# position relative to the beginning of the view
					pos = pos-bov
					# analyze lines prior to position
					dlines = view.splitlines()
					marker = ((pos-1) * ' ') + '^' + (
						' [line %d, character %d] ' %(line_no, pos)
					)
					# insert marker
					dlines.append(marker)
					yield ('LINE', os.linesep.join(dlines))
				except:
					import traceback
					yield ('LINE', traceback.format_exc(chain=False))
		spt = self.sql_parameter_types
		if spt is not None:
			yield ('sql_parameter_types', spt)
		cn = self.column_names
		ct = self.sql_column_types
		if cn is not None:
			if ct is not None:
				yield (
					'results',
					'(' + ', '.join([
						'{!r} {!r}'.format(n, t) for n,t in zip(cn,ct)
					]) + ')'
				)
			else:
				yield ('sql_column_names', cn)
		elif ct is not None:
			yield ('sql_column_types', ct)

	def clone(self):
		ps = self.__class__(self.database, None, self.string)
		ps._init()
		ps._fini()
		return ps

	def __init__(self,
		database, statement_id, string,
		wref = weakref.ref
	):
		self.database = database
		self.string = string
		self.statement_id = statement_id or ID(self)
		self._xact = None
		self.closed = None
		self._pq_statement_id = database.typio._encode(self.statement_id)[0]

		if not statement_id:
			# Register statement on a connection to close it automatically on db end
			database.pq.register_statement(self, self._pq_statement_id)

	def __repr__(self):
		return '<{mod}.{name}[{ci}] {state}>'.format(
			mod = self.__class__.__module__,
			name = self.__class__.__name__,
			ci = self.database.connector._pq_iri,
			state = self.state,
		)

	def _pq_parameters(self, parameters, proc = process_tuple):
		return proc(
			self._input_io, parameters,
			self._raise_parameter_tuple_error
		)

	##
	# process_tuple failed(exception). The parameters could not be packed.
	# This function is called with the given information in the context
	# of the original exception(to allow chaining).
	def _raise_parameter_tuple_error(self, cause, procs, tup, itemnum):
		# Find the SQL type name. This should *not* hit the server.
		typ = self.database.typio.sql_type_from_oid(
			self.pg_parameter_types[itemnum]
		) or '<unknown>'

		# Representation of the bad parameter.
		bad_data = repr(tup[itemnum])
		if len(bad_data) > 80:
			# Be sure not to fill screen with noise.
			bad_data = bad_data[:75] + ' ...'

		em = element.ClientError((
			(b'S', 'ERROR'),
			(b'C', '--PIO'),
			(b'M', "could not pack parameter %s::%s for transfer" %(
					('$' + str(itemnum + 1)), typ,
				)
			),
			(b'D', bad_data),
			(b'H', "Try casting the parameter to 'text', then to the target type."),
			(b'P', str(itemnum))
		))
		self.database.typio.raise_client_error(em, creator = self, cause = cause)

	##
	# Similar to the parameter variant.
	def _raise_column_tuple_error(self, cause, procs, tup, itemnum):
		# Find the SQL type name. This should *not* hit the server.
		typ = self.database.typio.sql_type_from_oid(
			self.pg_column_types[itemnum]
		) or '<unknown>'

		# Representation of the bad column.
		data = repr(tup[itemnum])
		if len(data) > 80:
			# Be sure not to fill screen with noise.
			data = data[:75] + ' ...'

		em = element.ClientError((
			(b'S', 'ERROR'),
			(b'C', '--CIO'),
			(b'M', "could not unpack column %r, %s::%s, from wire data" %(
					itemnum, self.column_names[itemnum], typ
				)
			),
			(b'D', data),
			(b'H', "Try casting the column to 'text'."),
			(b'P', str(itemnum)),
		))
		self.database.typio.raise_client_error(em, creator = self, cause = cause)

	@property
	def state(self) -> str:
		if self.closed:
			if self._xact is not None:
				if self.string is not None:
					return 'parsing'
				else:
					return 'describing'
			return 'closed'
		return 'prepared'

	@property
	def column_names(self):
		if self.closed is None:
			self._fini()
		if self._output is not None:
			return list(self.database.typio.decodes(self._output.keys()))

	@property
	def parameter_types(self):
		if self.closed is None:
			self._fini()
		if self._input is not None:
			return [self.database.typio.type_from_oid(x) for x in self._input]

	@property
	def column_types(self):
		if self.closed is None:
			self._fini()
		if self._output is not None:
			return [
				self.database.typio.type_from_oid(x[3]) for x in self._output
			]

	@property
	def pg_parameter_types(self):
		if self.closed is None:
			self._fini()
		return self._input

	@property
	def pg_column_types(self):
		if self.closed is None:
			self._fini()
		if self._output is not None:
			return [x[3] for x in self._output]

	@property
	def sql_column_types(self):
		if self.closed is None:
			self._fini()
		if self._output is not None:
			return [
				self.database.typio.sql_type_from_oid(x)
				for x in self.pg_column_types
			]

	@property
	def sql_parameter_types(self):
		if self.closed is None:
			self._fini()
		if self._input is not None:
			return [
				self.database.typio.sql_type_from_oid(x)
				for x in self.pg_parameter_types
			]

	def close(self):
		if self.closed is False:
			self.database.pq.trash_statement(self._pq_statement_id)
		self.closed = True

	def _init(self):
		"""
		Push initialization messages to the server, but don't wait for
		the return as there may be things that can be done while waiting
		for the return. Use the _fini() to complete.
		"""
		if self.string is not None:
			q = self.database.typio._encode(str(self.string))[0]
			cmd = [
				element.CloseStatement(self._pq_statement_id),
				element.Parse(self._pq_statement_id, q, ()),
			]
		else:
			cmd = []
		cmd.extend(
			(
				element.DescribeStatement(self._pq_statement_id),
				element.SynchronizeMessage,
			)
		)
		self._xact = xact.Instruction(cmd, asynchook = self.database._receive_async)
		self.database._pq_push(self._xact, self)

	def _fini(self, strfmt = element.StringFormat, binfmt = element.BinaryFormat):
		"""
		Complete initialization that the _init() method started.
		"""
		# assume that the transaction has been primed.
		if self._xact is None:
			raise RuntimeError("_fini called prior to _init; invalid state")
		if self._xact is self.database.pq.xact:
			try:
				self.database._pq_complete()
			except Exception:
				self.closed = True
				raise

		(*head, argtypes, tupdesc, last) = self._xact.messages_received()

		typio = self.database.typio
		if tupdesc is None or tupdesc is element.NoDataMessage:
			# Not typed output.
			self._output = None
			self._output_attmap = None
			self._output_io = None
			self._output_formats = None
			self._row_constructor = None
		else:
			self._output = tupdesc
			self._output_attmap = dict(
				typio.attribute_map(tupdesc)
			)
			self._row_constructor = self.database.typio.RowTypeFactory(self._output_attmap)
			# tuple output
			self._output_io = typio.resolve_descriptor(tupdesc, 1)
			self._output_formats = [
				strfmt if x is None else binfmt
				for x in self._output_io
			]
			self._output_io = tuple([
				x or typio.decode for x in self._output_io
			])

		self._input = argtypes
		packs = []
		formats = []
		for x in argtypes:
			pack = (typio.resolve(x) or (None,None))[0]
			packs.append(pack or typio.encode)
			formats.append(
				strfmt if x is None else binfmt
			)
		self._input_io = tuple(packs)
		self._input_formats = formats
		self.closed = False
		self._xact = None

	def __call__(self, *parameters):
		if self._input is not None:
			if len(parameters) != len(self._input):
				raise TypeError("statement requires %d parameters, given %d" %(
					len(self._input), len(parameters)
				))
		##
		# get em' all!
		if self._output is None:
			# might be a copy.
			c = SingleXactCopy(self, parameters)
		else:
			c = SingleXactFetch(self, parameters)
			c._process_chunk = c._process_tuple_chunk_Row

		# iff output is None, it's not a tuple returning query.
		# however, if it's a copy, detect that fact by SingleXactCopy's
		# immediate return after finding the copy begin message(no complete).
		if self._output is None:
			cmd = c.command()
			if cmd is not None:
				return (cmd, c.count())
		# Returns rows, accumulate in a list.
		r = []
		for x in c:
			r.extend(x)
		return r

	def declare(self, *parameters):
		if self.closed is None:
			self._fini()
		if self._input is not None:
			if len(parameters) != len(self._input):
				raise TypeError("statement requires %d parameters, given %d" %(
					len(self._input), len(parameters)
				))
		return Cursor(self, parameters, self.database, None)

	def rows(self, *parameters, **kw):
		chunks = self.chunks(*parameters, **kw)
		if chunks._output_io:
			chunks._process_chunk = chunks._process_tuple_chunk_Row
		return chain.from_iterable(chunks)
	__iter__ = rows

	def chunks(self, *parameters):
		if self.closed is None:
			self._fini()
		if self._input is not None:
			if len(parameters) != len(self._input):
				raise TypeError("statement requires %d parameters, given %d" %(
					len(self._input), len(parameters)
				))

		if self._output is None:
			# It's *probably* a COPY.
			return SingleXactCopy(self, parameters)
		if self.database.pq.state == b'I':
			# Currently, *not* in a Transaction block, so
			# DECLARE the statement WITH HOLD in order to allow
			# access across transactions.
			if self.string is not None:
				return MultiXactOutsideBlock(self, parameters, None)
			else:
				##
				# Statement source unknown, so it can't be DECLARE'd.
				# This happens when statement_from_id is used.
				return SingleXactFetch(self, parameters)
		else:
			# Likely, the best possible case. It gets to use Execute messages.
			return MultiXactInsideBlock(self, parameters, None)

	def column(self, *parameters, **kw):
		chunks = self.chunks(*parameters, **kw)
		chunks._process_chunk = chunks._process_tuple_chunk_Column
		return chain.from_iterable(chunks)

	def first(self, *parameters):
		if self.closed is None:
			# Not fully initialized; assume interrupted.
			self._fini()
		if self._input is not None:
			# Use a regular TypeError.
			if len(parameters) != len(self._input):
				raise TypeError("statement requires %d parameters, given %d" %(
					len(self._input), len(parameters)
				))

		# Parameters? Build em'.
		db = self.database

		if self._input_io:
			params = process_tuple(
				self._input_io, parameters,
				self._raise_parameter_tuple_error
			)
		else:
			params = ()

		# Run the statement
		x = xact.Instruction((
				element.Bind(
					b'',
					self._pq_statement_id,
					self._input_formats,
					params,
					self._output_formats or (),
				),
				# Get all
				element.Execute(b'', 0xFFFFFFFF),
				element.ClosePortal(b''),
				element.SynchronizeMessage
			),
			asynchook = db._receive_async
		)
		# Push and complete protocol transaction.
		db._pq_push(x, self)
		db._pq_complete()

		if self._output_io:
			##
			# It returned rows, look for the first tuple.
			tuple_type = element.Tuple.type
			for xt in x.messages_received():
				if xt.__class__ is tuple:
					break
			else:
				return None

			if len(self._output_io) > 1:
				# Multiple columns, return a Row.
				return self._row_constructor(
					process_tuple(
						self._output_io, xt,
						self._raise_column_tuple_error
					)
				)
			else:
				# Single column output.
				if xt[0] is None:
					return None
				io = self._output_io[0] or self.database.typio.decode
				return io(xt[0])
		else:
			##
			# It doesn't return rows, so return a count.
			##
			# This loop searches through the received messages
			# for the Complete message which contains the count.
			complete = element.Complete.type
			for cm in x.messages_received():
				# Use getattr because COPY doesn't produce
				# element.Message instances.
				if getattr(cm, 'type', None) == complete:
					break
			else:
				# Probably a Null command.
				return None

			count = cm.extract_count()
			if count is None:
				command = cm.extract_command()
				if command is not None:
					return command.decode('ascii')
			return count

	def _load_copy_chunks(self, chunks, *parameters):
		"""
		Given an chunks of COPY lines, execute the COPY ... FROM STDIN
		statement and send the copy lines produced by the iterable to
		the remote end.
		"""
		x = xact.Instruction((
				element.Bind(
					b'',
					self._pq_statement_id,
					(), (), (),
				),
				element.Execute(b'', 1),
				element.SynchronizeMessage,
			),
			asynchook = self.database._receive_async
		)
		self.database._pq_push(x, self)

		# localize
		step = self.database._pq_step

		# Get the COPY started.
		while x.state is not xact.Complete:
			step()
			if hasattr(x, 'CopyFailSequence') and x.messages is x.CopyFailSequence:
				# The protocol transaction has noticed that its a COPY.
				break
		else:
			# Oh, it's not a COPY at all.
			x.fatal = x.fatal or False
			x.error_message = element.ClientError((
				(b'S', 'ERROR'),
				# OperationError
				(b'C', '--OPE'),
				(b'M', "_load_copy_chunks() used on a non-COPY FROM STDIN query"),
			))
			self.database.typio.raise_client_error(x.error_message, creator = self)

		for chunk in chunks:
			x.messages = list(chunk)
			while x.messages is not x.CopyFailSequence:
				# Continue stepping until the transaction
				# sets the CopyFailSequence again. That's
				# the signal that the transaction has sent
				# all the previously set messages.
				step()
		x.messages = x.CopyDoneSequence
		self.database._pq_complete()
		self.database.pq.synchronize()

	def _load_tuple_chunks(self, chunks):
		pte = self._raise_parameter_tuple_error
		last = (element.SynchronizeMessage,)
		try:
			for chunk in chunks:
				bindings = [
					(
						element.Bind(
							b'',
							self._pq_statement_id,
							self._input_formats,
							process_tuple(
								self._input_io, tuple(t), pte
							),
							(),
						),
						element.Execute(b'', 1),
					)
					for t in chunk
				]
				bindings.append(last)
				self.database._pq_push(
					xact.Instruction(
						chain.from_iterable(bindings),
						asynchook = self.database._receive_async
					),
					self
				)
			self.database._pq_complete()
		except:
			##
			# In cases where row packing errors or occur,
			# synchronize, finishing any pending transaction,
			# and raise the error.
			##
			# If the data sent to the remote end is invalid,
			# _complete will raise the exception and the current
			# exception being marked as the cause, so there should
			# be no [exception] information loss.
			##
			self.database.pq.synchronize()
			raise

	def load_chunks(self, chunks, *parameters):
		"""
		Execute the query for each row-parameter set in `iterable`.

		In cases of ``COPY ... FROM STDIN``, iterable must be an iterable of
		sequences of `bytes`.
		"""
		if self.closed is None:
			self._fini()
		if not self._input or parameters:
			return self._load_copy_chunks(chunks)
		else:
			return self._load_tuple_chunks(chunks)

	def load_rows(self, rows, chunksize = 256):
		return self.load_chunks(chunk(rows, chunksize))
PreparedStatement = Statement

class StoredProcedure(pg_api.StoredProcedure):
	_e_factors = ('database', 'procedure_id')
	procedure_id = None

	def _e_metas(self):
		yield ('oid', self.oid)

	def __repr__(self):
		return '<%s:%s>' %(
			self.procedure_id, self.statement.string
		)

	def __call__(self, *args, **kw):
		if kw:
			input = []
			argiter = iter(args)
			try:
				word_idx = [(kw[k], self._input_attmap[k]) for k in kw]
			except KeyError as k:
				raise TypeError("%s got unexpected keyword argument %r" %(
						self.name, k.message
					)
				)
			word_idx.sort(key = get1)
			current_word = word_idx.pop(0)
			for x in range(argc):
				if x == current_word[1]:
					input.append(current_word[0])
					current_word = word_idx.pop(0)
				else:
					input.append(argiter.next())
		else:
			input = args

		if self.srf is True:
			if self.composite is True:
				return self.statement.rows(*input)
			else:
				# A generator expression is very appropriate here
				# as SRFs returning large number of rows would require
				# substantial amounts of memory.
				return map(get0, self.statement.rows(*input))
		else:
			if self.composite is True:
				return self.statement(*input)[0]
			else:
				return self.statement(*input)[0][0]

	def __init__(self, ident, database, description = ()):
		# Lookup pg_proc on database.
		if isinstance(ident, int):
			proctup = database.sys.lookup_procedure_oid(int(ident))
		else:
			proctup = database.sys.lookup_procedure_rp(str(ident))
		if proctup is None:
			raise LookupError("no function with identifier %s" %(str(ident),))

		self.procedure_id = ident
		self.oid = proctup[0]
		self.name = proctup["proname"]

		self._input_attmap = {}
		argnames = proctup.get('proargnames') or ()
		for x in range(len(argnames)):
			an = argnames[x]
			if an is not None:
				self._input_attmap[an] = x

		proargs = proctup['proargtypes']
		for x in proargs:
			# get metadata filled out.
			database.typio.resolve(x)

		self.statement = database.prepare(
			"SELECT * FROM %s(%s) AS func%s" %(
				proctup['_proid'],
				# ($1::type, $2::type, ... $n::type)
				', '.join([
					 '$%d::%s' %(x + 1, database.typio.sql_type_from_oid(proargs[x]))
					 for x in range(len(proargs))
				]),
				# Description for anonymous record returns
				(description and \
					'(' + ','.join(description) + ')' or '')
			)
		)
		self.srf = bool(proctup.get("proretset"))
		self.composite = proctup["composite"]

class SettingsCM(object):
	def __init__(self, database, settings_to_set):
		self.database = database
		self.settings_to_set = settings_to_set

	def __enter__(self):
		if hasattr(self, 'stored_settings'):
			raise RuntimeError("cannot re-use setting CMs")
		self.stored_settings = self.database.settings.getset(
			self.settings_to_set.keys()
		)
		self.database.settings.update(self.settings_to_set)

	def __exit__(self, typ, val, tb):
		self.database.settings.update(self.stored_settings)

class Settings(pg_api.Settings):
	_e_factors = ('database',)

	def __init__(self, database):
		self.database = database
		self.cache = {}

	def _e_metas(self):
		yield (None, str(len(self.cache)))

	def _clear_cache(self):
		self.cache.clear()

	def __getitem__(self, i):
		v = self.cache.get(i)
		if v is None:
			r = self.database.sys.setting_get(i)

			if r:
				v = r[0][0]
			else:
				raise KeyError(i)
		return v

	def __setitem__(self, i, v):
		cv = self.cache.get(i)
		if cv == v:
			return
		setas = self.database.sys.setting_set(i, v)
		self.cache[i] = setas

	def __delitem__(self, k):
		self.database.execute(
			'RESET "' + k.replace('"', '""') + '"'
		)
		self.cache.pop(k, None)

	def __len__(self):
		return self.database.sys.setting_len()

	def __call__(self, **settings):
		return SettingsCM(self.database, settings)

	def path():
		def fget(self):
			return pg_str.split_ident(self["search_path"])
		def fset(self, value):
			self['search_path'] = ','.join([
				'"%s"' %(x.replace('"', '""'),) for x in value
			])
		def fdel(self):
			if self.database.connector.path is not None:
				self.path = self.database.connector.path
			else:
				self.database.execute("RESET search_path")
		doc = 'structured search_path interface'
		return locals()
	path = property(**path())

	def get(self, k, alt = None):
		if k in self.cache:
			return self.cache[k]

		db = self.database
		r = self.database.sys.setting_get(k)
		if r:
			v = r[0][0]
			self.cache[k] = v
		else:
			v = alt
		return v

	def getset(self, keys):
		setmap = {}
		rkeys = []
		for k in keys:
			v = self.cache.get(k)
			if v is not None:
				setmap[k] = v
			else:
				rkeys.append(k)

		if rkeys:
			r = self.database.sys.setting_mget(rkeys)
			self.cache.update(r)
			setmap.update(r)
			rem = set(rkeys) - set([x['name'] for x in r])
			if rem:
				raise KeyError(rem)
		return setmap

	def keys(self):
		return map(get0, self.database.sys.setting_keys())
	__iter__ = keys

	def values(self):
		return map(get0, self.database.sys.setting_values())

	def items(self):
		return self.database.sys.setting_items()

	def update(self, d):
		kvl = [list(x) for x in dict(d).items()]
		self.cache.update(self.database.sys.setting_update(kvl))

	def _notify(self, msg):
		subs = getattr(self, '_subscriptions', {})
		d = self.database.typio._decode
		key = d(msg.name)[0]
		val = d(msg.value)[0]
		for x in subs.get(key, ()):
			x(self.database, key, val)
		if None in subs:
			for x in subs[None]:
				x(self.database, key, val)
		self.cache[key] = val

	def subscribe(self, key, callback):
		"""
		Subscribe to changes of the setting using the callback. When the setting
		is changed, the callback will be invoked with the connection, the key,
		and the new value. If the old value is locally cached, its value will
		still be available for inspection, but there is no guarantee.
		If `None` is passed as the key, the callback will be called whenever any
		setting is remotely changed.

		>>> def watch(connection, key, newval):
		...
		>>> db.settings.subscribe('TimeZone', watch)
		"""
		subs = self._subscriptions = getattr(self, '_subscriptions', {})
		callbacks = subs.setdefault(key, [])
		if callback not in callbacks:
			callbacks.append(callback)

	def unsubscribe(self, key, callback):
		"""
		Stop listening for changes to a setting. The setting name(`key`), and
		the callback used to subscribe must be given again for successful
		termination of the subscription.

		>>> db.settings.unsubscribe('TimeZone', watch)
		"""
		subs = getattr(self, '_subscriptions', {})
		callbacks = subs.get(key, ())
		if callback in callbacks:
			callbacks.remove(callback)

class Transaction(pg_api.Transaction):
	database = None

	mode = None
	isolation = None

	_e_factors = ('database', 'isolation', 'mode')

	def _e_metas(self):
		yield (None, self.state)

	def __init__(self, database, isolation = None, mode = None):
		self.database = database
		self.isolation = isolation
		self.mode = mode
		self.state = 'initialized'
		self.type = None

	def __enter__(self):
		self.start()
		return self

	def __exit__(self, typ, value, tb):
		if typ is None:
			# No exception, but in a failed transaction?
			if self.database.pq.state == b'E':
				if not self.database.closed:
					self.rollback()
				# pg_exc.InFailedTransactionError
				em = element.ClientError((
					(b'S', 'ERROR'),
					(b'C', '25P02'),
					(b'M', 'invalid transaction block exit detected'),
					(b'H', "Database was in an error-state, but no exception was raised.")
				))
				self.database.typio.raise_client_error(em, creator = self)
			else:
				# No exception, and no error state. Everything is good.
				try:
					self.commit()
					# If an error occurs, clean up the transaction state
					# and raise as needed.
				except pg_exc.ActiveTransactionError as err:
					if not self.database.closed:
						# adjust the state so rollback will do the right thing and abort.
						self.state = 'open'
						self.rollback()
					raise
		elif issubclass(typ, Exception):
			# There's an exception, so only rollback if the connection
			# exists. If the rollback() was called here, it would just
			# contribute noise to the error.
			if not self.database.closed:
				self.rollback()

	@staticmethod
	def _start_xact_string(isolation = None, mode = None):
		q = 'START TRANSACTION'
		if isolation is not None:
			if ';' in isolation:
				raise ValueError("invalid transaction isolation " + repr(mode))
			q += ' ISOLATION LEVEL ' + isolation
		if mode is not None:
			if ';' in mode:
				raise ValueError("invalid transaction mode " + repr(isolation))
			q += ' ' + mode
		return q + ';'

	@staticmethod
	def _savepoint_xact_string(id):
		return 'SAVEPOINT "xact(' + id.replace('"', '""') + ')";'

	def start(self):
		if self.state == 'open':
			return
		if self.state != 'initialized':
			em = element.ClientError((
				(b'S', 'ERROR'),
				(b'C', '--OPE'),
				(b'M', "transactions cannot be restarted"),
				(b'H', 'Create a new transaction object instead of re-using an old one.')
			))
			self.database.typio.raise_client_error(em, creator = self)

		if self.database.pq.state == b'I':
			self.type = 'block'
			q = self._start_xact_string(
				isolation = self.isolation,
				mode = self.mode,
			)
		else:
			self.type = 'savepoint'
			if (self.isolation, self.mode) != (None,None):
				em = element.ClientError((
					(b'S', 'ERROR'),
					(b'C', '--OPE'),
					(b'M', "configured transaction used inside a transaction block"),
					(b'H', 'A transaction block was already started.'),
				))
				self.database.typio.raise_client_error(em, creator = self)
			q = self._savepoint_xact_string(hex(id(self)))
		self.database.execute(q)
		self.state = 'open'
	begin = start

	@staticmethod
	def _release_string(id):
		'release "";'
		return 'RELEASE "xact(' + id.replace('"', '""') + ')";'

	def commit(self):
		if self.state == 'committed':
			return
		if self.state != 'open':
			em = element.ClientError((
				(b'S', 'ERROR'),
				(b'C', '--OPE'),
				(b'M', "commit attempted on transaction with unexpected state, " + repr(self.state)),
			))
			self.database.typio.raise_client_error(em, creator = self)

		if self.type == 'block':
			q = 'COMMIT'
		else:
			q = self._release_string(hex(id(self)))
		self.database.execute(q)
		self.state = 'committed'

	@staticmethod
	def _rollback_to_string(id, fmt = 'ROLLBACK TO "xact({0})"; RELEASE "xact({0})";'.format):
		return fmt(id.replace('"', '""'))

	def rollback(self):
		if self.state == 'aborted':
			return
		if self.state not in ('prepared', 'open'):
			em = element.ClientError((
				(b'S', 'ERROR'),
				(b'C', '--OPE'),
				(b'M', "ABORT attempted on transaction with unexpected state, " + repr(self.state)),
			))
			self.database.typio.raise_client_error(em, creator = self)

		if self.type == 'block':
			q = 'ABORT;'
		elif self.type == 'savepoint':
			q = self._rollback_to_string(hex(id(self)))
		else:
			raise RuntimeError("unknown transaction type " + repr(self.type))
		self.database.execute(q)
		self.state = 'aborted'
	abort = rollback

class Connection(pg_api.Connection):
	connector = None

	type = None
	version_info = None
	version = None

	security = None
	backend_id = None
	client_address = None
	client_port = None

	# Replaced with instances on connection instantiation.
	settings = Settings

	def _e_metas(self):
		yield (None, '[' + self.state + ']')
		if self.client_address is not None:
			yield ('client_address', self.client_address)
		if self.client_port is not None:
			yield ('client_port', self.client_port)
		if self.version is not None:
			yield ('version', self.version)
		att = getattr(self, 'failures', None)
		if att:
			count = 0
			for x in att:
				# Format each failure without their traceback.
				errstr = ''.join(format_exception(type(x.error), x.error, None))
				factinfo = str(x.socket_factory)
				if hasattr(x, 'ssl_negotiation'):
					if x.ssl_negotiation is True:
						factinfo = 'SSL ' + factinfo
					else:
						factinfo = 'NOSSL ' + factinfo
				yield (
					'failures[' + str(count) + ']',
					factinfo + os.linesep + errstr
				)
				count += 1

	def __repr__(self):
		return '<%s.%s[%s] %s>' %(
			type(self).__module__,
			type(self).__name__,
			self.connector._pq_iri,
			self.closed and 'closed' or '%s' %(self.pq.state,)
		)

	def __exit__(self, type, value, tb):
		# Don't bother closing unless it's a normal exception.
		if type is None or issubclass(type, Exception):
			self.close()

	def interrupt(self, timeout = None):
		self.pq.interrupt(timeout = timeout)

	def execute(self, query : str) -> None:
		q = xact.Instruction((
				element.Query(self.typio._encode(query)[0]),
			),
			asynchook = self._receive_async
		)
		self._pq_push(q, self)
		self._pq_complete()

	def do(self, language : str, source : str,
		qlit = pg_str.quote_literal,
		qid = pg_str.quote_ident,
	) -> None:
		sql = "DO " + qlit(source) + " LANGUAGE " + qid(language) + ";"
		self.execute(sql)

	def xact(self, isolation = None, mode = None):
		x = Transaction(self, isolation = isolation, mode = mode)
		return x

	def prepare(self,
		sql_statement_string : str,
		statement_id = None,
		Class = Statement
	) -> Statement:
		ps = Class(self, statement_id, sql_statement_string)
		ps._init()
		ps._fini()
		return ps

	@property
	def query(self, Class = SingleExecution):
		return Class(self)

	def statement_from_id(self, statement_id : str) -> Statement:
		ps = Statement(self, statement_id, None)
		ps._init()
		ps._fini()
		return ps

	def proc(self, proc_id : (str, int)) -> StoredProcedure:
		sp = StoredProcedure(proc_id, self)
		return sp

	def cursor_from_id(self, cursor_id : str) -> Cursor:
		c = Cursor(None, None, self, cursor_id)
		c._init()
		return c

	@property
	def closed(self) -> bool:
		if getattr(self, 'pq', None) is None:
			return True
		if hasattr(self.pq, 'socket') and self.pq.xact is not None:
			return self.pq.xact.fatal is True
		return False

	def close(self, getattr = getattr):
		# Write out the disconnect message if the socket is around.
		# If the connection is known to be lost, don't bother. It will
		# generate an extra exception.
		if getattr(self, 'pq', None) is None or getattr(self.pq, 'socket', None) is None:
			# No action to take.
			return

		x = getattr(self.pq, 'xact', None)
		if x is not None and x.fatal is not True:
			# finish the existing pq transaction iff it's not Closing.
			self.pq.complete()

		if self.pq.xact is None:
			# It completed the existing transaction.
			self.pq.push(xact.Closing())
			self.pq.complete()
			if self.pq.socket:
				self.pq.complete()

		# Close the socket if there is one.
		if self.pq.socket:
			self.pq.socket.close()
			self.pq.socket = None

	@property
	def state(self) -> str:
		if not hasattr(self, 'pq'):
			return 'initialized'
		if hasattr(self, 'failures'):
			return 'failed'
		if self.closed:
			return 'closed'
		if isinstance(self.pq.xact, xact.Negotiation):
			return 'negotiating'
		if self.pq.xact is None:
			if self.pq.state == b'E':
				return 'failed block'
			return 'idle' + (' in block' if self.pq.state != b'I' else '')
		else:
			return 'busy'

	def reset(self):
		"""
		restore original settings, reset the transaction, drop temporary
		objects.
		"""
		self.execute("ABORT; RESET ALL;")

	def __enter__(self):
		self.connect()
		return self

	def connect(self):
		'Establish the connection to the server'
		if self.closed is False:
			# already connected? just return.
			return

		if hasattr(self, 'pq'):
			# It's closed, *but* there's a PQ connection..
			x = self.pq.xact
			self.typio.raise_error(x.error_message, cause = getattr(x, 'exception', None), creator = self)

		# It's closed.
		try:
			self._establish()
		except Exception:
			# Close it up on failure.
			self.close()
			raise

	def _establish(self):
		# guts of connect()
		self.pq = None
		# if any exception occurs past this point, the connection
		# will not be usable.
		timeout = self.connector.connect_timeout
		sslmode = self.connector.sslmode or 'prefer'
		failures = []
		exc = None
		try:
			# get the list of sockets to try
			socket_factories = self.connector.socket_factory_sequence()
		except Exception as e:
			socket_factories = ()
			exc = e

		# When ssl is None: SSL negotiation will not occur.
		# When ssl is True: SSL negotiation will occur *and* it must succeed.
		# When ssl is False: SSL negotiation will occur but it may fail(NOSSL).
		if sslmode == 'allow':
			# without ssl, then with. :)
			socket_factories = interlace(
				zip(repeat(None, len(socket_factories)), socket_factories),
				zip(repeat(True, len(socket_factories)), socket_factories)
			)
		elif sslmode == 'prefer':
			# with ssl, then without. [maybe] :)
			socket_factories = interlace(
				zip(repeat(False, len(socket_factories)), socket_factories),
				zip(repeat(None, len(socket_factories)), socket_factories)
			)
			# prefer is special, because it *may* be possible to
			# skip the subsequent "without" in situations where SSL is off.
		elif sslmode == 'require':
			socket_factories = zip(repeat(True, len(socket_factories)), socket_factories)
		elif sslmode == 'disable':
			# None = Do Not Attempt SSL negotiation.
			socket_factories = zip(repeat(None, len(socket_factories)), socket_factories)
		else:
			raise ValueError("invalid sslmode: " + repr(sslmode))

		# can_skip is used when 'prefer' or 'allow' is the sslmode.
		# if the ssl negotiation returns 'N' (nossl), then
		# ssl "failed", but the socket is still usable for nossl.
		# in these cases, can_skip is set to True so that the
		# subsequent non-ssl attempt is skipped if it failed with the 'N' response.
		can_skip = False
		startup = self.connector._startup_parameters
		password = self.connector._password
		Connection3 = client.Connection
		for (ssl, sf) in socket_factories:
			if can_skip is True:
				# the last attempt failed and knows this attempt will fail too.
				can_skip = False
				continue
			pq = Connection3(sf, startup, password = password,)
			if hasattr(self, 'tracer'):
				pq.tracer = self.tracer

			# Grab the negotiation transaction before
			# connecting as it will be needed later if successful.
			neg = pq.xact
			pq.connect(ssl = ssl, timeout = timeout)

			didssl = getattr(pq, 'ssl_negotiation', -1)

			# It successfully connected if pq.xact is None;
			# The startup/negotiation xact completed.
			if pq.xact is None:
				self.pq = pq
				if hasattr(self.pq.socket, 'fileno'):
					self.fileno = self.pq.socket.fileno
				self.security = 'ssl' if didssl is True else None
				showoption_type = element.ShowOption.type
				for x in neg.asyncs:
					if x.type == showoption_type:
						self._receive_async(x)
				# success!
				break
			elif pq.socket is not None:
				# In this case, an application/protocol error occurred.
				# Close out the sockets ourselves.
				pq.socket.close()

			# Identify whether or not we can skip the attempt.
			# Whether or not we can skip depends entirely on the SSL parameter.
			if sslmode == 'prefer' and ssl is False and didssl is False:
				# In this case, the server doesn't support SSL or it's
				# turned off. Therefore, the "without_ssl" attempt need
				# *not* be ran because it has already been noted to be
				# a failure.
				can_skip = True
			elif hasattr(pq.xact, 'exception'):
				# If a Python exception occurred, chances are that it is
				# going to fail again iff it is going to hit the same host.
				if sslmode == 'prefer' and ssl is False:
					# when 'prefer', the first attempt
					# is marked with ssl is "False"
					can_skip = True
				elif sslmode == 'allow' and ssl is None:
					# when 'allow', the first attempt
					# is marked with dossl is "None"
					can_skip = True

			try:
				self.typio.raise_error(pq.xact.error_message)
			except Exception as error:
				pq.error = error
				# Otherwise, infinite recursion in the element traceback.
				error.creator = None
				# The tracebacks of the specific failures aren't particularly useful..
				error.__traceback__ = None
			if getattr(pq.xact, 'exception', None) is not None:
				pq.error.__cause__ = pq.xact.exception

			failures.append(pq)
		else:
			# No servers available. (see the break-statement in the for-loop)
			self.failures = failures or ()
			# it's over.
			self.typio.raise_client_error(could_not_connect, creator = self, cause = exc)
		##
		# connected, now initialize connection information.
		self.backend_id = self.pq.backend_id

		sv = self.settings.cache.get("server_version", "0.0")
		self.version_info = pg_version.normalize(pg_version.split(sv))
		# manual binding
		self.sys = pg_lib.Binding(self, pg_lib.sys)

		vi = self.version_info[:2]
		if vi <= (8,1):
			sd = self.sys.startup_data_only_version()
		elif vi >= (9,2):
			sd = self.sys.startup_data_92()
		else:
			sd = self.sys.startup_data()
		# connection info
		self.version, self.backend_start, \
		self.client_address, self.client_port = sd

		# First word from the version string.
		self.type = self.version.split()[0]

		##
		# Set standard_conforming_strings
		scstr = self.settings.get('standard_conforming_strings')
		if scstr is None or vi == (8,1):
			# There used to be a warning emitted here.
			# It was noisy, and had little added value
			# over a nice WARNING at the top of the driver documentation.
			pass
		elif scstr.lower() not in ('on','true','yes'):
			self.settings['standard_conforming_strings'] = 'on'

		super().connect()

	def _pq_push(self, xact, controller = None):
		x = self.pq.xact
		if x is not None:
			self.pq.complete()
			if x.fatal is not None:
				self.typio.raise_error(x.error_message)
		if controller is not None:
			self._controller = controller
		self.pq.push(xact)

	# Complete the current protocol transaction.
	def _pq_complete(self):
		pq = self.pq
		x = pq.xact
		if x is not None:
			# There is a running transaction, finish it.
			pq.complete()
			# Raise an error *iff* one occurred.
			if x.fatal is not None:
				self.typio.raise_error(x.error_message, cause = getattr(x, 'exception', None))
			del self._controller

	# Process the next message.
	def _pq_step(self, complete_state = globals()['xact'].Complete):
		pq = self.pq
		x = pq.xact
		if x is not None:
			pq.step()
			# If the protocol transaction was completed by
			# the last step, raise the error *iff* one occurred.
			if x.state is complete_state:
				if x.fatal is not None:
					self.typio.raise_error(x.error_message, cause = getattr(x, 'exception', None))
				del self._controller

	def _receive_async(self,
		msg, controller = None,
		showoption = element.ShowOption.type,
		notice = element.Notice.type,
		notify = element.Notify.type,
	):
		c = controller or getattr(self, '_controller', self)
		typ = msg.type
		if typ == showoption:
			if msg.name == b'client_encoding':
				self.typio.set_encoding(msg.value.decode('ascii'))
			self.settings._notify(msg)
		elif typ == notice:
			m = self.typio.emit_message(msg, creator = c)
		elif typ == notify:
			self._notifies.append(msg)
		else:
			self.typio.emit_client_message(
				element.ClientNotice((
					(b'C', '-1000'),
					(b'S', 'WARNING'),
					(b'M', 'cannot process unrecognized asynchronous message'),
					(b'D', repr(msg)),
				)),
				creator = c
			)

	def clone(self, *args, **kw):
		c = self.__class__(self.connector, *args, **kw)
		c.connect()
		return c

	def notify(self, *channels, **channel_and_payload):
		notifies = ""
		if channels:
			notifies += ';'.join((
				'NOTIFY "' + x.replace('"', '""') + '"' # str() case
				if x.__class__ is not tuple else (
					# tuple() case
					'NOTIFY "' + x[0].replace('"', '""') + """",'""" + \
					x[1].replace("'", "''") + "'"
				)
				for x in channels
			))
			notifies += ';'
		if channel_and_payload:
			notifies += ';'.join((
				'NOTIFY "' + channel.replace('"', '""') + """",'""" + \
				payload.replace("'", "''") + "'"
				for channel, payload in channel_and_payload.items()
			))
			notifies += ';'
		return self.execute(notifies)

	def listening_channels(self):
		if self.version_info[:2] > (8,4):
			return self.sys.listening_channels()
		else:
			return self.sys.listening_relations()

	def listen(self, *channels, len = len):
		qstr = ''
		for x in channels:
			# XXX: hardcoded identifier length?
			if len(x) > 63:
				raise ValueError("channel name too long: " + x)
			qstr += '; LISTEN ' + x.replace('"', '""')
		return self.execute(qstr)

	def unlisten(self, *channels, len = len):
		qstr = ''
		for x in channels:
			# XXX: hardcoded identifier length?
			if len(x) > 63:
				raise ValueError("channel name too long: " + x)
			qstr += '; UNLISTEN ' + x.replace('"', '""')
		return self.execute(qstr)

	def iternotifies(self, timeout = None):
		nm = NotificationManager(self, timeout = timeout)
		for x in nm:
			if x is None:
				yield None
			else:
				for y in x[1]:
					yield y

	def __init__(self, connector, *args, **kw):
		"""
		Create a connection based on the given connector.
		"""
		self.connector = connector
		# raw notify messages
		self._notifies = []
		self.fileno = -1
		self.typio = self.connector.driver.typio(self)
		self.typio.set_encoding('ascii')
		self.settings = Settings(self)
# class Connection

class Connector(pg_api.Connector):
	"""
	All arguments to Connector are keywords. At the very least, user,
	and socket, may be provided. If socket, unix, or process is not
	provided, host and port must be.
	"""
	@property
	def _pq_iri(self):
		return pg_iri.serialize(
			{
				k : v for k,v in self.__dict__.items()
				if v is not None and not k.startswith('_') and k not in (
					'driver', 'category'
				)
			},
			obscure_password = True
		)

	def _e_metas(self):
		yield (None, '[' + self.__class__.__name__ + '] ' + self._pq_iri)

	def __repr__(self):
		keywords = (',' + os.linesep + ' ').join([
			'%s = %r' %(k, getattr(self, k, None)) for k in self.__dict__
			if not k.startswith('_') and getattr(self, k, None) is not None
		])
		return '{mod}.{name}({keywords})'.format(
			mod = type(self).__module__,
			name = type(self).__name__,
			keywords = os.linesep + ' ' + keywords if keywords else ''
		)

	@abstractmethod
	def socket_factory_sequence(self):
		"""
		Generate a list of callables that will be used to attempt to make the
		connection to the server. It is assumed that each factory will produce
		an object with a socket interface that is ready for reading and writing
		data.

		The callables in the sequence must take a timeout parameter.
		"""

	def __init__(self,
		connect_timeout : int = None,
		server_encoding : "server encoding hint for driver" = None,
		sslmode : ('allow', 'prefer', 'require', 'disable') = None,
		sslcrtfile : "filepath" = None,
		sslkeyfile : "filepath" = None,
		sslrootcrtfile : "filepath" = None,
		sslrootcrlfile : "filepath" = None,
		driver = None,
		**kw
	):
		super().__init__(**kw)
		self.driver = driver

		self.server_encoding = server_encoding
		self.connect_timeout = connect_timeout
		self.sslmode = sslmode
		self.sslkeyfile = sslkeyfile
		self.sslcrtfile = sslcrtfile
		self.sslrootcrtfile = sslrootcrtfile
		self.sslrootcrlfile = sslrootcrlfile

		if self.sslrootcrlfile is not None:
			pg_exc.IgnoredClientParameterWarning(
				"certificate revocation lists are *not* checked",
				creator = self,
			).emit()

		# Startup message parameters.
		tnkw = {
			'client_min_messages' : 'WARNING',
		}
		if self.settings:
			s = dict(self.settings)
			if 'search_path' in self.settings:
				sp = s.get('search_path')
				if sp is None:
					self.settings.pop('search_path')
				elif not isinstance(sp, str):
					s['search_path'] = ','.join(
						pg_str.quote_ident(x) for x in sp
					)
			tnkw.update(s)

		tnkw['user'] = self.user
		if self.database is not None:
			tnkw['database'] = self.database

		se = self.server_encoding or 'utf-8'
		##
		# Attempt to accommodate for literal treatment of startup data.
		##
		self._startup_parameters = tuple([
			# All keys go in utf-8. However, ascii would probably be good enough.
			(
				k.encode('utf-8'),
			# If it's a str(), encode in the hinted server_encoding.
			# Otherwise, convert the object(int, float, bool, etc) into a string
			# and treat it as utf-8.
				v.encode(se) if type(v) is str else str(v).encode('utf-8')
			)
			for k, v in tnkw.items()
		])
		self._password = (self.password or '').encode(se)
		self._socket_secure = {
			'keyfile' : self.sslkeyfile,
			'certfile' : self.sslcrtfile,
			'ca_certs' : self.sslrootcrtfile,
		}
# class Connector

class SocketConnector(Connector):
	'abstract connector for using `socket` and `ssl`'
	@abstractmethod
	def socket_factory_sequence(self):
		"""
		Return a sequence of `SocketFactory`s for a connection to use to connect
		to the target host.
		"""

	def create_socket_factory(self, **params):
		return SocketFactory(**params)

class IPConnector(SocketConnector):
	def socket_factory_sequence(self):
		return self._socketcreators

	def socket_factory_params(self, host, port, ipv, **kw):
		if ipv != self.ipv:
			raise TypeError("'ipv' keyword must be '%d'" % self.ipv)
		if host is None:
			raise TypeError("'host' is a required keyword and cannot be 'None'")
		if port is None:
			raise TypeError("'port' is a required keyword and cannot be 'None'")

		return {'socket_create': (self.address_family, socket.SOCK_STREAM),
				'socket_connect': (host, int(port))}

	def __init__(self, host, port, ipv, **kw):
		params = self.socket_factory_params(host, port, ipv, **kw)
		self.host, self.port = params['socket_connect']
		# constant socket connector
		self._socketcreator = self.create_socket_factory(**params)
		self._socketcreators = (self._socketcreator,)
		super().__init__(**kw)

class IP4(IPConnector):
	'Connector for establishing IPv4 connections'
	ipv = 4
	address_family = socket.AF_INET

	def __init__(self,
		host : "IPv4 Address (str)" = None,
		port : int = None,
		ipv = 4,
		**kw
	):
		super().__init__(host, port, ipv, **kw)

class IP6(IPConnector):
	'Connector for establishing IPv6 connections'
	ipv = 6
	address_family = socket.AF_INET6

	def __init__(self,
		host : "IPv6 Address (str)" = None,
		port : int = None,
		ipv = 6,
		**kw
	):
		super().__init__(host, port, ipv, **kw)

class Unix(SocketConnector):
	'Connector for establishing unix domain socket connections'
	def socket_factory_sequence(self):
		return self._socketcreators

	def socket_factory_params(self, unix):
		if unix is None:
			raise TypeError("'unix' is a required keyword and cannot be 'None'")

		return {'socket_create': (socket.AF_UNIX, socket.SOCK_STREAM),
				'socket_connect': unix}

	def __init__(self, unix = None, **kw):
		params = self.socket_factory_params(unix)
		self.unix = params['socket_connect']
		# constant socket connector
		self._socketcreator = self.create_socket_factory(**params)
		self._socketcreators = (self._socketcreator,)
		super().__init__(**kw)

class Host(SocketConnector):
	"""
	Connector for establishing hostname based connections.

	This connector exercises socket.getaddrinfo.
	"""
	def socket_factory_sequence(self):
		"""
		Return a list of `SocketCreator`s based on the results of
		`socket.getaddrinfo`.
		"""
		return [
			# (AF, socktype, proto), (IP, Port)
			self.create_socket_factory(**(self.socket_factory_params(x[0:3], x[4][:2],
																	self._socket_secure)))
			for x in socket.getaddrinfo(
				self.host, self.port, self._address_family, socket.SOCK_STREAM
			)
		]

	def socket_factory_params(self, socktype, address, sslparams):
		return {'socket_create': socktype,
				'socket_connect': address,
				'socket_secure': sslparams}

	def __init__(self,
		host : str = None,
		port : (str, int) = None,
		ipv : int = None,
		address_family : "address family to use(AF_INET,AF_INET6)" = None,
		**kw
	):
		if host is None:
			raise TypeError("'host' is a required keyword")
		if port is None:
			raise TypeError("'port' is a required keyword")

		if address_family is not None and ipv is not None:
			raise TypeError("'ipv' and 'address_family' on mutually exclusive")

		if ipv is None:
			self._address_family = address_family or socket.AF_UNSPEC
		elif ipv == 4:
			self._address_family = socket.AF_INET
		elif ipv == 6:
			self._address_family = socket.AF_INET6
		else:
			raise TypeError("unknown IP version selected: 'ipv' = " + repr(ipv))
		self.host = host
		self.port = port
		super().__init__(**kw)

class Driver(pg_api.Driver):
	def _e_metas(self):
		yield (None, type(self).__module__ + '.' + type(self).__name__)

	def ip4(self, **kw):
		return IP4(driver = self, **kw)

	def ip6(self, **kw):
		return IP6(driver = self, **kw)

	def host(self, **kw):
		return Host(driver = self, **kw)

	def unix(self, **kw):
		return Unix(driver = self, **kw)

	def fit(self,
		unix = None,
		host = None,
		port = None,
		**kw
	) -> Connector:
		"""
		Create the appropriate `postgresql.api.Connector` based on the
		parameters.

		This also protects against mutually exclusive parameters.
		"""
		if unix is not None:
			if host is not None:
				raise TypeError("'unix' and 'host' keywords are exclusive")
			if port is not None:
				raise TypeError("'unix' and 'port' keywords are exclusive")
			return self.unix(unix = unix, **kw)
		else:
			if host is None or port is None:
				raise TypeError("'host' and 'port', or 'unix' must be supplied")
			# We have a host and a port.
			# If it's an IP address, IP4 or IP6 should be selected.
			if ':' in host:
				# There's a ':' in host, good chance that it's IPv6.
				try:
					socket.inet_pton(socket.AF_INET6, host)
					return self.ip6(host = host, port = port, **kw)
				except (socket.error, NameError):
					pass

			# Not IPv6, maybe IPv4...
			try:
				socket.inet_aton(host)
				# It's IP4
				return self.ip4(host = host, port = port, **kw)
			except socket.error:
				pass

			# neither host, nor port are None, probably a hostname.
			return self.host(host = host, port = port, **kw)

	def connect(self, **kw) -> Connection:
		"""
		For information on acceptable keywords, see:

			`postgresql.documentation.driver`:Connection Keywords
		"""
		c = self.fit(**kw)()
		c.connect()
		return c

	def __init__(self, connection = Connection, typio = TypeIO):
		self.connection = connection
		self.typio = typio