/usr/share/doc/python3-postgresql/html/_sources/driver.txt 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 | .. _db_interface:
******
Driver
******
`postgresql.driver` provides a PG-API, `postgresql.api`, interface to a
PostgreSQL server using PQ version 3.0 to facilitate communication. It makes
use of the protocol's extended features to provide binary datatype transmission
and protocol level prepared statements for strongly typed parameters.
`postgresql.driver` currently supports PostgreSQL servers as far back as 8.0.
Prior versions are not tested. While any version of PostgreSQL supporting
version 3.0 of the PQ protocol *should* work, many features may not work due to
absent functionality in the remote end.
For DB-API 2.0 users, the driver module is located at
`postgresql.driver.dbapi20`. The DB-API 2.0 interface extends PG-API. All of the
features discussed in this chapter are available on DB-API connections.
.. warning::
PostgreSQL versions 8.1 and earlier do not support standard conforming
strings. In order to avoid subjective escape methods on connections,
`postgresql.driver.pq3` enables the ``standard_conforming_strings`` setting
by default. Greater care must be taken when working versions that do not
support standard strings.
**The majority of issues surrounding the interpolation of properly quoted literals can be easily avoided by using parameterized statements**.
The following identifiers are regularly used as shorthands for significant
interface elements:
``db``
`postgresql.api.Connection`, a database connection. `Connections`_
``ps``
`postgresql.api.Statement`, a prepared statement. `Prepared Statements`_
``c``
`postgresql.api.Cursor`, a cursor; the results of a prepared statement.
`Cursors`_
``C``
`postgresql.api.Connector`, a connector. `Connectors`_
Establishing a Connection
=========================
There are many ways to establish a `postgresql.api.Connection` to a
PostgreSQL server using `postgresql.driver`. This section discusses those,
connection creation, interfaces.
`postgresql.open`
-----------------
In the root package module, the ``open()`` function is provided for accessing
databases using a locator string and optional connection keywords. The string
taken by `postgresql.open` is a URL whose components make up the client
parameters::
>>> db = postgresql.open("pq://localhost/postgres")
This will connect to the host, ``localhost`` and to the database named
``postgres`` via the ``pq`` protocol. open will inherit client parameters from
the environment, so the user name given to the server will come from
``$PGUSER``, or if that is unset, the result of `getpass.getuser`--the username
of the user running the process. The user's "pgpassfile" will even be
referenced if no password is given::
>>> db = postgresql.open("pq://username:password@localhost/postgres")
In this case, the password *is* given, so ``~/.pgpass`` would never be
referenced. The ``user`` client parameter is also given, ``username``, so
``$PGUSER`` or `getpass.getuser` will not be given to the server.
Settings can also be provided by the query portion of the URL::
>>> db = postgresql.open("pq://user@localhost/postgres?search_path=public&timezone=mst")
The above syntax ultimately passes the query as settings(see the description of
the ``settings`` keyword in `Connection Keywords`). Driver parameters require a
distinction. This distinction is made when the setting's name is wrapped in
square-brackets, '[' and ']':
>>> db = postgresql.open("pq://user@localhost/postgres?[sslmode]=require&[connect_timeout]=5")
``sslmode`` and ``connect_timeout`` are driver parameters. These are never sent
to the server, but if they were not in square-brackets, they would be, and the
driver would never identify them as driver parameters.
The general structure of a PQ-locator is::
protocol://user:password@host:port/database?[driver_setting]=value&server_setting=value
Optionally, connection keyword arguments can be used to override anything given
in the locator::
>>> db = postgresql.open("pq://user:secret@host", password = "thE_real_sekrat")
Or, if the locator is not desired, individual keywords can be used exclusively::
>>> db = postgresql.open(user = 'user', host = 'localhost', port = 6543)
In fact, all arguments to `postgresql.open` are optional as all arguments are
keywords; ``iri`` is merely the first keyword argument taken by
`postgresql.open`. If the environment has all the necessary parameters for a
successful connection, there is no need to pass anything to open::
>>> db = postgresql.open()
For a complete list of keywords that `postgresql.open` can accept, see
`Connection Keywords`_.
For more information about the environment variables, see :ref:`pg_envvars`.
For more information about the ``pgpassfile``, see :ref:`pg_passfile`.
`postgresql.driver.connect`
---------------------------
`postgresql.open` is a high-level interface to connection creation. It provides
password resolution services and client parameter inheritance. For some
applications, this is undesirable as such implicit inheritance may lead to
failures due to unanticipated parameters being used. For those applications,
use of `postgresql.open` is not recommended. Rather, `postgresql.driver.connect`
should be used when explicit parameterization is desired by an application:
>>> import postgresql.driver as pg_driver
>>> db = pg_driver.connect(
... user = 'usename',
... password = 'secret',
... host = 'localhost',
... port = 5432
... )
This will create a connection to the server listening on port ``5432``
on the host ``localhost`` as the user ``usename`` with the password ``secret``.
.. note::
`connect` will *not* inherit parameters from the environment as libpq-based drivers do.
See `Connection Keywords`_ for a full list of acceptable keyword parameters and
their meaning.
Connectors
----------
Connectors are the supporting objects used to instantiate a connection. They
exist for the purpose of providing connections with the necessary abstractions
for facilitating the client's communication with the server, *and to act as a
container for the client parameters*. The latter purpose is of primary interest
to this section.
Each connection object is associated with its connector by the ``connector``
attribute on the connection. This provides the user with access to the
parameters used to establish the connection in the first place, and the means to
create another connection to the same server. The attributes on the connector
should *not* be altered. If parameter changes are needed, a new connector should
be created.
The attributes available on a connector are consistent with the names of the
connection parameters described in `Connection Keywords`_, so that list can be
used as a reference to identify the information available on the connector.
Connectors fit into the category of "connection creation interfaces", so
connector instantiation normally takes the same parameters that the
`postgresql.driver.connect` function takes.
.. note::
Connector implementations are specific to the transport, so keyword arguments
like ``host`` and ``port`` aren't supported by the ``Unix`` connector.
The driver, `postgresql.driver.default` provides a set of connectors for making
a connection:
``postgresql.driver.default.host(...)``
Provides a ``getaddrinfo()`` abstraction for establishing a connection.
``postgresql.driver.default.ip4(...)``
Connect to a single IPv4 addressed host.
``postgresql.driver.default.ip6(...)``
Connect to a single IPv6 addressed host.
``postgresql.driver.default.unix(...)``
Connect to a single unix domain socket. Requires the ``unix`` keyword which
must be an absolute path to the unix domain socket to connect to.
``host`` is the usual connector used to establish a connection::
>>> C = postgresql.driver.default.host(
... user = 'auser',
... host = 'foo.com',
... port = 5432)
>>> # create
>>> db = C()
>>> # establish
>>> db.connect()
If a constant internet address is used, ``ip4`` or ``ip6`` can be used::
>>> C = postgresql.driver.default.ip4(user='auser', host='127.0.0.1', port=5432)
>>> db = C()
>>> db.connect()
Additionally, ``db.connect()`` on ``db.__enter__()`` for with-statement support:
>>> with C() as db:
... ...
Connectors are constant. They have no knowledge of PostgreSQL service files,
environment variables or LDAP services, so changes made to those facilities
will *not* be reflected in a connector's configuration. If the latest
information from any of these sources is needed, a new connector needs to be
created as the credentials have changed.
.. note::
``host`` connectors use ``getaddrinfo()``, so if DNS changes are made,
new connections *will* use the latest information.
Connection Keywords
-------------------
The following is a list of keywords accepted by connection creation
interfaces:
``user``
The user to connect as.
``password``
The user's password.
``database``
The name of the database to connect to. (PostgreSQL defaults it to `user`)
``host``
The hostname or IP address to connect to.
``port``
The port on the host to connect to.
``unix``
The unix domain socket to connect to. Exclusive with ``host`` and ``port``.
Expects a string containing the *absolute path* to the unix domain socket to
connect to.
``settings``
A dictionary or key-value pair sequence stating the parameters to give to the
database. These settings are included in the startup packet, and should be
used carefully as when an invalid setting is given, it will cause the
connection to fail.
``connect_timeout``
Amount of time to wait for a connection to be made. (in seconds)
``server_encoding``
Hint given to the driver to properly encode password data and some information
in the startup packet.
This should only be used in cases where connections cannot be made due to
authentication failures that occur while using known-correct credentials.
``sslmode``
``'disable'``
Don't allow SSL connections.
``'allow'``
Try without SSL first, but if that doesn't work, try with.
``'prefer'``
Try SSL first, then without.
``'require'``
Require an SSL connection.
``sslcrtfile``
Certificate file path given to `ssl.wrap_socket`.
``sslkeyfile``
Key file path given to `ssl.wrap_socket`.
``sslrootcrtfile``
Root certificate file path given to `ssl.wrap_socket`
``sslrootcrlfile``
Revocation list file path. [Currently not checked.]
Connections
===========
`postgresql.open` and `postgresql.driver.connect` provide the means to
establish a connection. Connections provide a `postgresql.api.Database`
interface to a PostgreSQL server; specifically, a `postgresql.api.Connection`.
Connections are one-time objects. Once, it is closed or lost, it can longer be
used to interact with the database provided by the server. If further use of the
server is desired, a new connection *must* be established.
.. note::
Cannot connect failures, exceptions raised on ``connect()``, are also terminal.
In cases where operations are performed on a closed connection, a
`postgresql.exceptions.ConnectionDoesNotExistError` will be raised.
Database Interface Points
-------------------------
After a connection is established::
>>> import postgresql
>>> db = postgresql.open(...)
The methods and properties on the connection object are ready for use:
``Connection.prepare(sql_statement_string)``
Create a `postgresql.api.Statement` object for querying the database.
This provides an "SQL statement template" that can be executed multiple times.
See `Prepared Statements`_ for more information.
``Connection.proc(procedure_id)``
Create a `postgresql.api.StoredProcedure` object referring to a stored
procedure on the database. The returned object will provide a
`collections.Callable` interface to the stored procedure on the server. See
`Stored Procedures`_ for more information.
``Connection.statement_from_id(statement_id)``
Create a `postgresql.api.Statement` object from an existing statement
identifier. This is used in cases where the statement was prepared on the
server. See `Prepared Statements`_ for more information.
``Connection.cursor_from_id(cursor_id)``
Create a `postgresql.api.Cursor` object from an existing cursor identifier.
This is used in cases where the cursor was declared on the server. See
`Cursors`_ for more information.
``Connection.do(language, source)``
Execute a DO statement on the server using the specified language.
*DO statements are available on PostgreSQL 9.0 and greater.*
*Executing this method on servers that do not support DO statements will*
*likely cause a SyntaxError*.
``Connection.execute(sql_statements_string)``
Run a block of SQL on the server. This method returns `None` unless an error
occurs. If errors occur, the processing of the statements will stop and the
error will be raised.
``Connection.xact(isolation = None, mode = None)``
The `postgresql.api.Transaction` constructor for creating transactions.
This method creates a transaction reference. The transaction will not be
started until it's instructed to do so. See `Transactions`_ for more
information.
``Connection.settings``
A property providing a `collections.MutableMapping` interface to the
database's SQL settings. See `Settings`_ for more information.
``Connection.clone()``
Create a new connection object based on the same factors that were used to
create ``db``. The new connection returned will already be connected.
``Connection.msghook(msg)``
By default, the `msghook` attribute does not exist. If set to a callable, any
message that occurs during an operation of the database or an operation of a
database derived object will be given to the callable. See the
`Database Messages`_ section for more information.
``Connection.listen(*channels)``
Start listening for asynchronous notifications in the specified channels.
Sends a batch of ``LISTEN`` statements to the server.
``Connection.unlisten(*channels)``
Stop listening for asynchronous notifications in the specified channels.
Sends a batch of ``UNLISTEN`` statements to the server.
``Connection.listening_channels()``
Return an iterator producing the channel names that are currently being
listened to.
``Connection.notify(*channels, **channel_and_payload)``
NOTIFY the channels with the given payload. Sends a batch of ``NOTIFY``
statements to the server.
Equivalent to issuing "NOTIFY <channel>" or "NOTIFY <channel>, <payload>"
for each item in `channels` and `channel_and_payload`. All NOTIFYs issued
will occur in the same transaction, regardless of auto-commit.
The items in `channels` can either be a string or a tuple. If a string,
no payload is given, but if an item is a `builtins.tuple`, the second item
in the pair will be given as the payload, and the first as the channel.
`channels` offers a means to issue NOTIFYs in guaranteed order::
>>> db.notify('channel1', ('different_channel', 'payload'))
In the above, ``NOTIFY "channel1";`` will be issued first, followed by
``NOTIFY "different_channel", 'payload';``.
The items in `channel_and_payload` are all payloaded NOTIFYs where the
keys are the channels and the values are the payloads. Order is undefined::
>>> db.notify(channel_name = 'payload_data')
`channels` and `channels_and_payload` can be used together. In such cases all
NOTIFY statements generated from `channels_and_payload` will follow those in
`channels`.
``Connection.iternotifies(timeout = None)``
Return an iterator to the NOTIFYs received on the connection. The iterator
will yield notification triples consisting of ``(channel, payload, pid)``.
While iterating, the connection should *not* be used in other threads.
The optional timeout can be used to enable "idle" events in which `None`
objects will be yielded by the iterator.
See :ref:`notifyman` for details.
When a connection is established, certain pieces of information are collected from
the backend. The following are the attributes set on the connection object after
the connection is made:
``Connection.version``
The version string of the *server*; the result of ``SELECT version()``.
``Connection.version_info``
A ``sys.version_info`` form of the ``server_version`` setting. eg.
``(8, 1, 2, 'final', 0)``.
``Connection.security``
`None` if no security. ``'ssl'`` if SSL is enabled.
``Connection.backend_id``
The process-id of the backend process.
``Connection.backend_start``
When backend was started. ``datetime.datetime`` instance.
``Connection.client_address``
The address of the client that the backend is communicating with.
``Connection.client_port``
The port of the client that the backend is communicating with.
``Connection.fileno()``
Method to get the file descriptor number of the connection's socket. This
method will return `None` if the socket object does not have a ``fileno``.
Under normal circumstances, it will return an `int`.
The ``backend_start``, ``client_address``, and ``client_port`` are collected
from pg_stat_activity. If this information is unavailable, the attributes will
be `None`.
Prepared Statements
===================
Prepared statements are the primary entry point for initiating an operation on
the database. Prepared statement objects represent a request that will, likely,
be sent to the database at some point in the future. A statement is a single
SQL command.
The ``prepare`` entry point on the connection provides the standard method for
creating a `postgersql.api.Statement` instance bound to the
connection(``db``) from an SQL statement string::
>>> ps = db.prepare("SELECT 1")
>>> ps()
[(1,)]
Statement objects may also be created from a statement identifier using the
``statement_from_id`` method on the connection. When this method is used, the
statement must have already been prepared or an error will be raised.
>>> db.execute("PREPARE a_statement_id AS SELECT 1;")
>>> ps = db.statement_from_id('a_statement_id')
>>> ps()
[(1,)]
When a statement is executed, it binds any given parameters to a *new* cursor
and the entire result-set is returned.
Statements created using ``prepare()`` will leverage garbage collection in order
to automatically close statements that are no longer referenced. However,
statements created from pre-existing identifiers, ``statement_from_id``, must
be explicitly closed if the statement is to be discarded.
Statement objects are one-time objects. Once closed, they can no longer be used.
Statement Interface Points
--------------------------
Prepared statements can be executed just like functions:
>>> ps = db.prepare("SELECT 'hello, world!'")
>>> ps()
[('hello, world!',)]
The default execution method, ``__call__``, produces the entire result set. It
is the simplest form of statement execution. Statement objects can be executed in
different ways to accommodate for the larger results or random access(scrollable
cursors).
Prepared statement objects have a few execution methods:
``Statement(*parameters)``
As shown before, statement objects can be invoked like a function to get
the statement's results.
``Statement.rows(*parameters)``
Return a iterator to all the rows produced by the statement. This
method will stream rows on demand, so it is ideal for situations where
each individual row in a large result-set must be processed.
``iter(Statement)``
Convenience interface that executes the ``rows()`` method without arguments.
This enables the following syntax:
>>> for table_name, in db.prepare("SELECT table_name FROM information_schema.tables"):
... print(table_name)
``Statement.column(*parameters)``
Return a iterator to the first column produced by the statement. This
method will stream values on demand, and *should* only be used with statements
that have a single column; otherwise, bandwidth will ultimately be wasted as
the other columns will be dropped.
*This execution method cannot be used with COPY statements.*
``Statement.first(*parameters)``
For simple statements, cursor objects are unnecessary.
Consider the data contained in ``c`` from above, 'hello world!'. To get at this
data directly from the ``__call__(...)`` method, it looks something like::
>>> ps = db.prepare("SELECT 'hello, world!'")
>>> ps()[0][0]
'hello, world!'
To simplify access to simple data, the ``first`` method will simply return
the "first" of the result set::
>>> ps.first()
'hello, world!'
The first value.
When the result set consists of a single column, ``first()`` will return
that column in the first row.
The first row.
When the result set consists of multiple columns, ``first()`` will return
that first row.
The first, and only, row count.
When DML--for instance, an INSERT-statement--is executed, ``first()`` will
return the row count returned by the statement as an integer.
.. note::
DML that returns row data, RETURNING, will *not* return a row count.
The result set created by the statement determines what is actually returned.
Naturally, a statement used with ``first()`` should be crafted with these
rules in mind.
``Statement.chunks(*parameters)``
This access point is designed for situations where rows are being streamed out
quickly. It is a method that returns a ``collections.Iterator`` that produces
*sequences* of rows. This is the most efficient way to get rows from the
database. The rows in the sequences are ``builtins.tuple`` objects.
``Statement.declare(*parameters)``
Create a scrollable cursor with hold. This returns a `postgresql.api.Cursor`
ready for accessing random rows in the result-set. Applications that use the
database to support paging can use this method to manage the view.
``Statement.close()``
Close the statement inhibiting further use.
``Statement.load_rows(collections.Iterable(parameters))``
Given an iterable producing parameters, execute the statement for each
iteration. Always returns `None`.
``Statement.load_chunks(collections.Iterable(collections.Iterable(parameters)))``
Given an iterable of iterables producing parameters, execute the statement
for each parameter produced. However, send the all execution commands with
the corresponding parameters of each chunk before reading any results.
Always returns `None`. This access point is designed to be used in conjunction
with ``Statement.chunks()`` for transferring rows from one connection to another with
great efficiency::
>>> dst.prepare(...).load_chunks(src.prepare(...).chunks())
``Statement.clone()``
Create a new statement object based on the same factors that were used to
create ``ps``.
``Statement.msghook(msg)``
By default, the `msghook` attribute does not exist. If set to a callable, any
message that occurs during an operation of the statement or an operation of a
statement derived object will be given to the callable. See the
`Database Messages`_ section for more information.
In order to provide the appropriate type transformations, the driver must
acquire metadata about the statement's parameters and results. This data is
published via the following properties on the statement object:
``Statement.sql_parameter_types``
A sequence of SQL type names specifying the types of the parameters used in
the statement.
``Statement.sql_column_types``
A sequence of SQL type names specifying the types of the columns produced by
the statement. `None` if the statement does not return row-data.
``Statement.pg_parameter_types``
A sequence of PostgreSQL type Oid's specifying the types of the parameters
used in the statement.
``Statement.pg_column_types``
A sequence of PostgreSQL type Oid's specifying the types of the columns produced by
the statement. `None` if the statement does not return row-data.
``Statement.parameter_types``
A sequence of Python types that the statement expects.
``Statement.column_types``
A sequence of Python types that the statement will produce.
``Statement.column_names``
A sequence of `str` objects specifying the names of the columns produced by
the statement. `None` if the statement does not return row-data.
The indexes of the parameter sequences correspond to the parameter's
identifier, N+1: ``sql_parameter_types[0]`` -> ``'$1'``.
>>> ps = db.prepare("SELECT $1::integer AS intname, $2::varchar AS chardata")
>>> ps.sql_parameter_types
('INTEGER','VARCHAR')
>>> ps.sql_column_types
('INTEGER','VARCHAR')
>>> ps.column_names
('intname','chardata')
>>> ps.column_types
(<class 'int'>, <class 'str'>)
Parameterized Statements
------------------------
Statements can take parameters. Using statement parameters is the recommended
way to interrogate the database when variable information is needed to formulate
a complete request. In order to do this, the statement must be defined using
PostgreSQL's positional parameter notation. ``$1``, ``$2``, ``$3``, etc::
>>> ps = db.prepare("SELECT $1")
>>> ps('hello, world!')[0][0]
'hello, world!'
PostgreSQL determines the type of the parameter based on the context of the
parameter's identifier::
>>> ps = db.prepare(
... "SELECT * FROM information_schema.tables WHERE table_name = $1 LIMIT $2"
... )
>>> ps("tables", 1)
[('postgres', 'information_schema', 'tables', 'VIEW', None, None, None, None, None, 'NO', 'NO', None)]
Parameter ``$1`` in the above statement will take on the type of the
``table_name`` column and ``$2`` will take on the type required by the LIMIT
clause(text and int8).
However, parameters can be forced to a specific type using explicit casts:
>>> ps = db.prepare("SELECT $1::integer")
>>> ps.first(-400)
-400
Parameters are typed. PostgreSQL servers provide the driver with the
type information about a positional parameter, and the serialization routine
will raise an exception if the given object is inappropriate. The Python
types expected by the driver for a given SQL-or-PostgreSQL type are listed
in `Type Support`_.
This usage of types is not always convenient. Notably, the `datetime` module
does not provide a friendly way for a user to express intervals, dates, or
times. There is a likely inclination to forego these parameter type
requirements.
In such cases, explicit casts can be made to work-around the type
requirements::
>>> ps = db.prepare("SELECT $1::text::date")
>>> ps.first('yesterday')
datetime.date(2009, 3, 11)
The parameter, ``$1``, is given to the database as a string, which is then
promptly cast into a date. Of course, without the explicit cast as text, the
outcome would be different::
>>> ps = db.prepare("SELECT $1::date")
>>> ps.first('yesterday')
Traceback:
...
postgresql.exceptions.ParameterError
The function that processes the parameter expects a `datetime.date` object, and
the given `str` object does not provide the necessary interfaces for the
conversion, so the driver raises a `postgresql.exceptions.ParameterError` from
the original conversion exception.
Inserting and DML
-----------------
Loading data into the database is facilitated by prepared statements. In these
examples, a table definition is necessary for a complete illustration::
>>> db.execute(
... """
... CREATE TABLE employee (
... employee_name text,
... employee_salary numeric,
... employee_dob date,
... employee_hire_date date
... );
... """
... )
Create an INSERT statement using ``prepare``::
>>> mkemp = db.prepare("INSERT INTO employee VALUES ($1, $2, $3, $4)")
And add "Mr. Johnson" to the table::
>>> import datetime
>>> r = mkemp(
... "John Johnson",
... "92000",
... datetime.date(1950, 12, 10),
... datetime.date(1998, 4, 23)
... )
>>> print(r[0])
INSERT
>>> print(r[1])
1
The execution of DML will return a tuple. This tuple contains the completed
command name and the associated row count.
Using the call interface is fine for making a single insert, but when multiple
records need to be inserted, it's not the most efficient means to load data. For
multiple records, the ``ps.load_rows([...])`` provides an efficient way to load
large quantities of structured data::
>>> from datetime import date
>>> mkemp.load_rows([
... ("Jack Johnson", "85000", date(1962, 11, 23), date(1990, 3, 5)),
... ("Debra McGuffer", "52000", date(1973, 3, 4), date(2002, 1, 14)),
... ("Barbara Smith", "86000", date(1965, 2, 24), date(2005, 7, 19)),
... ])
While small, the above illustrates the ``ps.load_rows()`` method taking an
iterable of tuples that provides parameters for the each execution of the
statement.
``load_rows`` is also used to support ``COPY ... FROM STDIN`` statements::
>>> copy_emps_in = db.prepare("COPY employee FROM STDIN")
>>> copy_emps_in.load_rows([
... b'Emp Name1\t72000\t1970-2-01\t1980-10-22\n',
... b'Emp Name2\t62000\t1968-9-11\t1985-11-1\n',
... b'Emp Name3\t62000\t1968-9-11\t1985-11-1\n',
... ])
Copy data goes in as bytes and come out as bytes regardless of the type of COPY
taking place. It is the user's obligation to make sure the row-data is in the
appropriate encoding.
COPY Statements
---------------
`postgresql.driver` transparently supports PostgreSQL's COPY command. To the
user, COPY will act exactly like other statements that produce tuples; COPY
tuples, however, are `bytes` objects. The only distinction in usability is that
the COPY *should* be completed before other actions take place on the
connection--this is important when a COPY is invoked via ``rows()`` or
``chunks()``.
In situations where other actions are invoked during a ``COPY TO STDOUT``, the
entire result set of the COPY will be read. However, no error will be raised so
long as there is enough memory available, so it is *very* desirable to avoid
doing other actions on the connection while a COPY is active.
In situations where other actions are invoked during a ``COPY FROM STDIN``, a
COPY failure error will occur. The driver manages the connection state in such
a way that will purposefully cause the error as the COPY was inappropriately
interrupted. This not usually a problem as ``load_rows(...)`` and
``load_chunks(...)`` methods must complete the COPY command before returning.
Copy data is always transferred using ``bytes`` objects. Even in cases where the
COPY is not in ``BINARY`` mode. Any needed encoding transformations *must* be
made the caller. This is done to avoid any unnecessary overhead by default::
>>> ps = db.prepare("COPY (SELECT i FROM generate_series(0, 99) AS g(i)) TO STDOUT")
>>> r = ps()
>>> len(r)
100
>>> r[0]
b'0\n'
>>> r[-1]
b'99\n'
Of course, invoking a statement that way will read the entire result-set into
memory, which is not usually desirable for COPY. Using the ``chunks(...)``
iterator is the *fastest* way to move data::
>>> ci = ps.chunks()
>>> import sys
>>> for rowset in ps.chunks():
... sys.stdout.buffer.writelines(rowset)
...
<lots of data>
``COPY FROM STDIN`` commands are supported via
`postgresql.api.Statement.load_rows`. Each invocation to
``load_rows`` is a single invocation of COPY. ``load_rows`` takes an iterable of
COPY lines to send to the server::
>>> db.execute("""
... CREATE TABLE sample_copy (
... sc_number int,
... sc_text text
... );
... """)
>>> copyin = db.prepare('COPY sample_copy FROM STDIN')
>>> copyin.load_rows([
... b'123\tone twenty three\n',
... b'350\ttree fitty\n',
... ])
For direct connection-to-connection COPY, use of ``load_chunks(...)`` is
recommended as it will provide the most efficient transfer method::
>>> copyout = src.prepare('COPY atable TO STDOUT')
>>> copyin = dst.prepare('COPY atable FROM STDIN')
>>> copyin.load_chunks(copyout.chunks())
Specifically, each chunk of row data produced by ``chunks()`` will be written in
full by ``load_chunks()`` before getting another chunk to write.
Cursors
=======
When a prepared statement is declared, ``ps.declare(...)``, a
`postgresql.api.Cursor` is created and returned for random access to the rows in
the result set. Direct use of cursors is primarily useful for applications that
need to implement paging. For situations that need to iterate over the result
set, the ``ps.rows(...)`` or ``ps.chunks(...)`` execution methods should be
used.
Cursors can also be created directly from ``cursor_id``'s using the
``cursor_from_id`` method on connection objects::
>>> db.execute('DECLARE the_cursor_id CURSOR WITH HOLD FOR SELECT 1;')
>>> c = db.cursor_from_id('the_cursor_id')
>>> c.read()
[(1,)]
>>> c.close()
.. hint::
If the cursor that needs to be opened is going to be treated as an iterator,
then a FETCH-statement should be prepared instead using ``cursor_from_id``.
Like statements created from an identifier, cursors created from an identifier
must be explicitly closed in order to destroy the object on the server.
Likewise, cursors created from statement invocations will be automatically
released when they are no longer referenced.
.. note::
PG-API cursors are a direct interface to single result-set SQL cursors. This
is in contrast with DB-API cursors, which have interfaces for dealing with
multiple result-sets. There is no execute method on PG-API cursors.
Cursor Interface Points
-----------------------
For cursors that return row data, these interfaces are provided for accessing
those results:
``Cursor.read(quantity = None, direction = None)``
This method name is borrowed from `file` objects, and are semantically
similar. However, this being a cursor, rows are returned instead of bytes or
characters. When the number of rows returned is less then the quantity
requested, it means that the cursor has been exhausted in the configured
direction. The ``direction`` argument can be either ``'FORWARD'`` or `True`
to FETCH FORWARD, or ``'BACKWARD'`` or `False` to FETCH BACKWARD.
Like, ``seek()``, the ``direction`` *property* on the cursor object effects
this method.
``Cursor.seek(position[, whence = 0])``
When the cursor is scrollable, this seek interface can be used to move the
position of the cursor. See `Scrollable Cursors`_ for more information.
``next(Cursor)``
This fetches the next row in the cursor object. Cursors support the iterator
protocol. While equivalent to ``cursor.read(1)[0]``, `StopIteration` is raised
if the returned sequence is empty. (``__next__()``)
``Cursor.close()``
For cursors opened using ``cursor_from_id()``, this method must be called in
order to ``CLOSE`` the cursor. For cursors created by invoking a prepared
statement, this is not necessary as the garbage collection interface will take
the appropriate steps.
``Cursor.clone()``
Create a new cursor object based on the same factors that were used to
create ``c``.
``Cursor.msghook(msg)``
By default, the `msghook` attribute does not exist. If set to a callable, any
message that occurs during an operation of the cursor will be given to the
callable. See the `Database Messages`_ section for more information.
Cursors have some additional configuration properties that may be modified
during the use of the cursor:
``Cursor.direction``
A value of `True`, the default, will cause read to fetch forwards, whereas a
value of `False` will cause it to fetch backwards. ``'BACKWARD'`` and
``'FORWARD'`` can be used instead of `False` and `True`.
Cursors normally share metadata with the statements that create them, so it is
usually unnecessary for referencing the cursor's column descriptions directly.
However, when a cursor is opened from an identifier, the cursor interface must
collect the metadata itself. These attributes provide the metadata in absence of
a statement object:
``Cursor.sql_column_types``
A sequence of SQL type names specifying the types of the columns produced by
the cursor. `None` if the cursor does not return row-data.
``Cursor.pg_column_types``
A sequence of PostgreSQL type Oid's specifying the types of the columns produced by
the cursor. `None` if the cursor does not return row-data.
``Cursor.column_types``
A sequence of Python types that the cursor will produce.
``Cursor.column_names``
A sequence of `str` objects specifying the names of the columns produced by
the cursor. `None` if the cursor does not return row-data.
``Cursor.statement``
The statement that was executed that created the cursor. `None` if
unknown--``db.cursor_from_id()``.
Scrollable Cursors
------------------
Scrollable cursors are supported for applications that need to implement paging.
When statements are invoked via the ``declare(...)`` method, the returned cursor
is scrollable.
.. note::
Scrollable cursors never pre-fetch in order to provide guaranteed positioning.
The cursor interface supports scrolling using the ``seek`` method. Like
``read``, it is semantically similar to a file object's ``seek()``.
``seek`` takes two arguments: ``position`` and ``whence``:
``position``
The position to scroll to. The meaning of this is determined by ``whence``.
``whence``
How to use the position: absolute, relative, or absolute from end:
absolute: ``'ABSOLUTE'`` or ``0`` (default)
seek to the absolute position in the cursor relative to the beginning of the
cursor.
relative: ``'RELATIVE'`` or ``1``
seek to the relative position. Negative ``position``'s will cause a MOVE
backwards, while positive ``position``'s will MOVE forwards.
from end: ``'FROM_END'`` or ``2``
seek to the end of the cursor and then MOVE backwards by the given
``position``.
The ``whence`` keyword argument allows for either numeric and textual
specifications.
Scrolling through employees::
>>> emps_by_age = db.prepare("""
... SELECT
... employee_name, employee_salary, employee_dob, employee_hire_date,
... EXTRACT(years FROM AGE(employee_dob)) AS age
... ORDER BY age ASC
... """)
>>> c = emps_by_age.declare()
>>> # seek to the end, ``2`` works as well.
>>> c.seek(0, 'FROM_END')
>>> # scroll back one, ``1`` works as well.
>>> c.seek(-1, 'RELATIVE')
>>> # and back to the beginning again
>>> c.seek(0)
Additionally, scrollable cursors support backward fetches by specifying the
direction keyword argument::
>>> c.seek(0, 2)
>>> c.read(1, 'BACKWARD')
Cursor Direction
----------------
The ``direction`` property on the cursor states the default direction for read
and seek operations. Normally, the direction is `True`, ``'FORWARD'``. When the
property is set to ``'BACKWARD'`` or `False`, the read method will fetch
backward by default, and seek operations will be inverted to simulate a
reversely ordered cursor. The following example illustrates the effect::
>>> reverse_c = db.prepare('SELECT i FROM generate_series(99, 0, -1) AS g(i)').declare()
>>> c = db.prepare('SELECT i FROM generate_series(0, 99) AS g(i)').declare()
>>> reverse_c.direction = 'BACKWARD'
>>> reverse_c.seek(0)
>>> c.read() == reverse_c.read()
Furthermore, when the cursor is configured to read backwards, specifying
``'BACKWARD'`` for read's ``direction`` argument will ultimately cause a forward
fetch. This potentially confusing facet of direction configuration is
implemented in order to create an appropriate symmetry in functionality.
The cursors in the above example contain the same rows, but are ultimately in
reverse order. The backward direction property is designed so that the effect
of any read or seek operation on those cursors is the same::
>>> reverse_c.seek(50)
>>> c.seek(50)
>>> c.read(10) == reverse_c.read(10)
>>> c.read(10, 'BACKWARD') == reverse_c.read(10, 'BACKWARD')
And for relative seeks::
>>> c.seek(-10, 1)
>>> reverse_c.seek(-10, 1)
>>> c.read(10, 'BACKWARD') == reverse_c.read(10, 'BACKWARD')
Rows
====
Rows received from PostgreSQL are instantiated into `postgresql.types.Row`
objects. Rows are both a sequence and a mapping. Items accessed with an `int`
are seen as indexes and other objects are seen as keys::
>>> row = db.prepare("SELECT 't'::text AS col0, 2::int4 AS col1").first()
>>> row
('t', 2)
>>> row[0]
't'
>>> row["col0"]
't'
However, this extra functionality is not free. The cost of instantiating
`postgresql.types.Row` objects is quite measurable, so the `chunks()` execution
method will produce `builtins.tuple` objects for cases where performance is
critical.
.. note::
Attributes aren't used to provide access to values due to potential conflicts
with existing method and property names.
Row Interface Points
--------------------
Rows implement the `collections.Mapping` and `collections.Sequence` interfaces.
``Row.keys()``
An iterable producing the column names. Order is not guaranteed. See the
``column_names`` property to get an ordered sequence.
``Row.values()``
Iterable to the values in the row.
``Row.get(key_or_index[, default=None])``
Get the item in the row. If the key doesn't exist or the index is out of
range, return the default.
``Row.items()``
Iterable of key-value pairs. Ordered by index.
``iter(Row)``
Iterable to the values in index order.
``value in Row``
Whether or not the value exists in the row. (__contains__)
``Row[key_or_index]``
If ``key_or_index`` is an integer, return the value at that index. If the
index is out of range, raise an `IndexError`. Otherwise, return the value
associated with column name. If the given key, ``key_or_index``, does not
exist, raise a `KeyError`.
``Row.index_from_key(key)``
Return the index associated with the given key.
``Row.key_from_index(index)``
Return the key associated with the given index.
``Row.transform(*args, **kw)``
Create a new row object of the same length, with the same keys, but with new
values produced by applying the given callables to the corresponding items.
Callables given as ``args`` will be associated with values by their index and
callables given as keywords will be associated with values by their key,
column name.
While the mapping interfaces will provide most of the needed information, some
additional properties are provided for consistency with statement and cursor
objects.
``Row.column_names``
Property providing an ordered sequence of column names. The index corresponds
to the row value-index that the name refers to.
>>> row[row.column_names[i]] == row[i]
Row Transformations
-------------------
After a row is returned, sometimes the data in the row is not in the desired
format. Further processing is needed if the row object is to going to be
given to another piece of code which requires an object of differring
consistency.
The ``transform`` method on row objects provides a means to create a new row
object consisting of the old row's items, but with certain columns transformed
using the given callables::
>>> row = db.prepare("""
... SELECT
... 'XX9301423'::text AS product_code,
... 2::int4 AS quantity,
... '4.92'::numeric AS total
... """).first()
>>> row
('XX9301423', 2, Decimal("4.92"))
>>> row.transform(quantity = str)
('XX9301423', '2', Decimal("4.92"))
``transform`` supports both positional and keyword arguments in order to
assign the callable for a column's transformation::
>>> from operator import methodcaller
>>> row.transform(methodcaller('strip', 'XX'))
('9301423', 2, Decimal("4.92"))
Of course, more than one column can be transformed::
>>> stripxx = methodcaller('strip', 'XX')
>>> row.transform(stripxx, str, str)
('9301423', '2', '4.92')
`None` can also be used to indicate no transformation::
>>> row.transform(None, str, str)
('XX9301423', '2', '4.92')
More advanced usage can make use of lambdas for compound transformations in a
single pass of the row::
>>> strip_and_int = lambda x: int(stripxx(x))
>>> row.transform(strip_and_int)
(9301423, 2, Decimal("4.92"))
Transformations will be, more often than not, applied against *rows* as
opposed to *a* row. Using `operator.methodcaller` with `map` provides the
necessary functionality to create simple iterables producing transformed row
sequences::
>>> import decimal
>>> apply_tax = lambda x: (x * decimal.Decimal("0.1")) + x
>>> transform_row = methodcaller('transform', strip_and_int, None, apply_tax)
>>> r = map(transform_row, [row])
>>> list(r)
[(9301423, 2, Decimal('5.412'))]
And finally, `functools.partial` can be used to create a simple callable::
>>> from functools import partial
>>> transform_rows = partial(map, transform_row)
>>> list(transform_rows([row]))
[(9301423, 2, Decimal('5.412'))]
Queries
=======
Queries in `py-postgresql` are single use prepared statements. They exist primarily for
syntactic convenience, but they also allow the driver to recognize the short lifetime of
the statement.
Single use statements are supported using the ``query`` property on connection
objects, :py:class:`postgresql.api.Connection.query`. The statement object is not
available when using queries as the results, or handle to the results, are directly returned.
Queries have access to all execution methods:
* ``Connection.query(sql, *parameters)``
* ``Connection.query.rows(sql, *parameters)``
* ``Connection.query.column(sql, *parameters)``
* ``Connection.query.first(sql, *parameters)``
* ``Connection.query.chunks(sql, *parameters)``
* ``Connection.query.declare(sql, *parameters)``
* ``Connection.query.load_rows(sql, collections.Iterable(parameters))``
* ``Connection.query.load_chunks(collections.Iterable(collections.Iterable(parameters)))``
In cases where a sequence of one-shot queries needs to be performed, it may be important to
avoid unnecessary repeat attribute resolution from the connection object as the ``query``
property is an interface object created on access. Caching the target execution methods is
recommended::
qrows = db.query.rows
l = []
for x in my_queries:
l.append(qrows(x))
The characteristic of Each execution method is discussed in the prior
`Prepared Statements`_ section.
Stored Procedures
=================
The ``proc`` method on `postgresql.api.Database` objects provides a means to
create a reference to a stored procedure on the remote database.
`postgresql.api.StoredProcedure` objects are used to represent the referenced
SQL routine.
This provides a direct interface to functions stored on the database. It
leverages knowledge of the parameters and results of the function in order
to provide the user with a natural interface to the procedure::
>>> func = db.proc('version()')
>>> func()
'PostgreSQL 8.3.6 on ...'
Stored Procedure Interface Points
---------------------------------
It's more-or-less a function, so there's only one interface point:
``func(*args, **kw)`` (``__call__``)
Stored procedure objects are callable, executing a procedure will return an
object of suitable representation for a given procedure's type signature.
If it returns a single object, it will return the single object produced by
the procedure.
If it's a set returning function, it will return an *iterable* to the values
produced by the procedure.
In cases of set returning function with multiple OUT-parameters, a cursor
will be returned.
Stored Procedure Type Support
-----------------------------
Stored procedures support most types of functions. "Function Types" being set
returning functions, multiple-OUT parameters, and simple single-object returns.
Set-returning functions, SRFs return a sequence::
>>> generate_series = db.proc('generate_series(int,int)')
>>> gs = generate_series(1, 20)
>>> gs
<generator object <genexpr>>
>>> next(gs)
1
>>> list(gs)
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
For functions like ``generate_series()``, the driver is able to identify that
the return is a sequence of *solitary* integer objects, so the result of the
function is just that, a sequence of integers.
Functions returning composite types are recognized, and return row objects::
>>> db.execute("""
... CREATE FUNCTION composite(OUT i int, OUT t text)
... LANGUAGE SQL AS
... $body$
... SELECT 900::int AS i, 'sample text'::text AS t;
... $body$;
... """)
>>> composite = db.proc('composite()')
>>> r = composite()
>>> r
(900, 'sample text')
>>> r['i']
900
>>> r['t']
'sample text'
Functions returning a set of composites are recognized, and the result is a
`postgresql.api.Cursor` object whose column names are consistent with the names
of the OUT parameters::
>>> db.execute("""
... CREATE FUNCTION srfcomposite(out i int, out t text)
... RETURNS SETOF RECORD
... LANGUAGE SQL AS
... $body$
... SELECT 900::int AS i, 'sample text'::text AS t
... UNION ALL
... SELECT 450::int AS i, 'more sample text'::text AS t
... $body$;
... """)
>>> srfcomposite = db.proc('srfcomposite()')
>>> r = srfcomposite()
>>> next(r)
(900, 'sample text')
>>> v = next(r)
>>> v['i'], v['t']
(450, 'more sample text')
Transactions
============
Transactions are managed by creating an object corresponding to a
transaction started on the server. A transaction is a transaction block,
a savepoint, or a prepared transaction. The ``xact(...)`` method on the
connection object provides the standard method for creating a
`postgresql.api.Transaction` object to manage a transaction on the connection.
The creation of a transaction object does not start the transaction. Rather, the
transaction must be explicitly started using the ``start()`` method on the
transaction object. Usually, transactions *should* be managed with the context
manager interfaces::
>>> with db.xact():
... ...
The transaction in the above example is opened, started, by the ``__enter__``
method invoked by the with-statement's usage. It will be subsequently
committed or rolled-back depending on the exception state and the error state
of the connection when ``__exit__`` is called.
**Using the with-statement syntax for managing transactions is strongly
recommended.** By using the transaction's context manager, it allows for Python
exceptions to be properly treated as fatal to the transaction as when an
uncaught exception of any kind occurs within the block, it is unlikely that
the state of the transaction can be trusted. Additionally, the ``__exit__``
method provides a safe-guard against invalid commits. This can occur if a
database error is inappropriately caught within a block without being raised.
The context manager interfaces are higher level interfaces to the explicit
instruction methods provided by `postgresql.api.Transaction` objects.
Transaction Configuration
-------------------------
Keyword arguments given to ``xact()`` provide the means for configuring the
properties of the transaction. Only three points of configuration are available:
``isolation``
The isolation level of the transaction. This must be a string. It will be
interpolated directly into the START TRANSACTION statement. Normally,
'SERIALIZABLE' or 'READ COMMITTED':
>>> with db.xact('SERIALIZABLE'):
... ...
``mode``
A string, 'READ ONLY' or 'READ WRITE'. States the mutability of stored
information in the database. Like ``isolation``, this is interpolated
directly into the START TRANSACTION string.
The specification of any of these transaction properties imply that the transaction
is a block. Savepoints do not take configuration, so if a transaction identified
as a block is started while another block is running, an exception will be
raised.
Transaction Interface Points
----------------------------
The methods available on transaction objects manage the state of the transaction
and relay any necessary instructions to the remote server in order to reflect
that change of state.
>>> x = db.xact(...)
``x.start()``
Start the transaction.
``x.commit()``
Commit the transaction.
``x.rollback()``
Abort the transaction.
These methods are primarily provided for applications that manage transactions
in a way that cannot be formed around single, sequential blocks of code.
Generally, using these methods require additional work to be performed by the
code that is managing the transaction.
If usage of these direct, instructional methods is necessary, it is important to
note that if the database is in an error state when a *transaction block's*
commit() is executed, an implicit rollback will occur. The transaction object
will simply follow instructions and issue the ``COMMIT`` statement, and it will
succeed without exception.
Error Control
-------------
Handling *database* errors inside transaction CMs is generally discouraged as
any database operation that occurs within a failed transaction is an error
itself. It is important to trap any recoverable database errors *outside* of the
scope of the transaction's context manager:
>>> try:
... with db.xact():
... ...
... except postgresql.exceptions.UniqueError:
... pass
In cases where the database is in an error state, but the context exits
without an exception, a `postgresql.exceptions.InFailedTransactionError` is
raised by the driver:
>>> with db.xact():
... try:
... ...
... except postgresql.exceptions.UniqueError:
... pass
...
Traceback (most recent call last):
...
postgresql.exceptions.InFailedTransactionError: invalid block exit detected
CODE: 25P02
SEVERITY: ERROR
Normally, if a ``COMMIT`` is issued on a failed transaction, the command implies a
``ROLLBACK`` without error. This is a very undesirable result for the CM's exit
as it may allow for code to be ran that presumes the transaction was committed.
The driver intervenes here and raises the
`postgresql.exceptions.InFailedTransactionError` to safe-guard against such
cases. This effect is consistent with savepoint releases that occur during an
error state. The distinction between the two cases is made using the ``source``
property on the raised exception.
Settings
========
SQL's SHOW and SET provides a means to configure runtime parameters on the
database("GUC"s). In order to save the user some grief, a
`collections.MutableMapping` interface is provided to simplify configuration.
The ``settings`` attribute on the connection provides the interface extension.
The standard dictionary interface is supported:
>>> db.settings['search_path'] = "$user,public"
And ``update(...)`` is better performing for multiple sets:
>>> db.settings.update({
... 'search_path' : "$user,public",
... 'default_statistics_target' : "1000"
... })
.. note::
The ``transaction_isolation`` setting cannot be set using the ``settings``
mapping. Internally, ``settings`` uses ``set_config``, which cannot adjust
that particular setting.
Settings Interface Points
-------------------------
Manipulation and interrogation of the connection's settings is achieved by
using the standard `collections.MutableMapping` interfaces.
``Connection.settings[k]``
Get the value of a single setting.
``Connection.settings[k] = v``
Set the value of a single setting.
``Connection.settings.update([(k1,v2), (k2,v2), ..., (kn,vn)])``
Set multiple settings using a sequence of key-value pairs.
``Connection.settings.update({k1 : v1, k2 : v2, ..., kn : vn})``
Set multiple settings using a dictionary or mapping object.
``Connection.settings.getset([k1, k2, ..., kn])``
Get a set of a settings. This is the most efficient way to get multiple
settings as it uses a single request.
``Connection.settings.keys()``
Get all available setting names.
``Connection.settings.values()``
Get all setting values.
``Connection.settings.items()``
Get a sequence of key-value pairs corresponding to all settings on the
database.
Settings Management
-------------------
`postgresql.api.Settings` objects can create context managers when called.
This gives the user with the ability to specify sections of code that are to
be ran with certain settings. The settings' context manager takes full
advantage of keyword arguments in order to configure the context manager:
>>> with db.settings(search_path = 'local,public', timezone = 'mst'):
... ...
`postgresql.api.Settings` objects are callable; the return is a context manager
configured with the given keyword arguments representing the settings to use for
the block of code that is about to be executed.
When the block exits, the settings will be restored to the values that they had
before the block entered.
Type Support
============
The driver supports a large number of PostgreSQL types at the binary level.
Most types are converted to standard Python types. The remaining types are
usually PostgreSQL specific types that are converted into objects whose class
is defined in `postgresql.types`.
When a conversion function is not available for a particular type, the driver
will use the string format of the type and instantiate a `str` object
for the data. It will also expect `str` data when parameter of a type without a
conversion function is bound.
.. note::
Generally, these standard types are provided for convenience. If conversions into
these datatypes are not desired, it is recommended that explicit casts into
``text`` are made in statement string.
.. table:: Python types used to represent PostgreSQL types.
================================= ================================== ===========
PostgreSQL Types Python Types SQL Types
================================= ================================== ===========
`postgresql.types.INT2OID` `int` smallint
`postgresql.types.INT4OID` `int` integer
`postgresql.types.INT8OID` `int` bigint
`postgresql.types.FLOAT4OID` `float` float
`postgresql.types.FLOAT8OID` `float` double
`postgresql.types.VARCHAROID` `str` varchar
`postgresql.types.BPCHAROID` `str` char
`postgresql.types.XMLOID` `xml.etree` (cElementTree) xml
`postgresql.types.DATEOID` `datetime.date` date
`postgresql.types.TIMESTAMPOID` `datetime.datetime` timestamp
`postgresql.types.TIMESTAMPTZOID` `datetime.datetime` (tzinfo) timestamptz
`postgresql.types.TIMEOID` `datetime.time` time
`postgresql.types.TIMETZOID` `datetime.time` timetz
`postgresql.types.INTERVALOID` `datetime.timedelta` interval
`postgresql.types.NUMERICOID` `decimal.Decimal` numeric
`postgresql.types.BYTEAOID` `bytes` bytea
`postgresql.types.TEXTOID` `str` text
<contrib_hstore> `dict` hstore
================================= ================================== ===========
The mapping in the above table *normally* goes both ways. So when a parameter
is passed to a statement, the type *should* be consistent with the corresponding
Python type. However, many times, for convenience, the object will be passed
through the type's constructor, so it is not always necessary.
Arrays
------
Arrays of PostgreSQL types are supported with near transparency. For simple
arrays, arbitrary iterables can just be given as a statement's parameter and the
array's constructor will consume the objects produced by the iterator into a
`postgresql.types.Array` instance. However, in situations where the array has
multiple dimensions, `list` objects are used to delimit the boundaries of the
array.
>>> ps = db.prepare("select $1::int[]")
>>> ps.first([(1,2), (2,3)])
Traceback:
...
postgresql.exceptions.ParameterError
In the above case, it is apparent that this array is supposed to have two
dimensions. However, this is not the case for other types:
>>> ps = db.prepare("select $1::point[]")
>>> ps.first([(1,2), (2,3)])
postgresql.types.Array([postgresql.types.point((1.0, 2.0)), postgresql.types.point((2.0, 3.0))])
Lists are used to provide the necessary boundary information:
>>> ps = db.prepare("select $1::int[]")
>>> ps.first([[1,2],[2,3]])
postgresql.types.Array([[1,2],[2,3]])
The above is the appropriate way to define the array from the original example.
.. hint::
The root-iterable object given as an array parameter does not need to be a
list-type as it's assumed to be made up of elements.
Composites
----------
Composites are supported using `postgresql.types.Row` objects to represent
the data. When a composite is referenced for the first time, the driver
queries the database for information about the columns that make up the type.
This information is then used to create the necessary I/O routines for packing
and unpacking the parameters and columns of that type::
>>> db.execute("CREATE TYPE ctest AS (i int, t text, n numeric);")
>>> ps = db.prepare("SELECT $1::ctest")
>>> i = (100, 'text', "100.02013")
>>> r = ps.first(i)
>>> r["t"]
'text'
>>> r["n"]
Decimal("100.02013")
Or if use of a dictionary is desired::
>>> r = ps.first({'t' : 'just-the-text'})
>>> r
(None, 'just-the-text', None)
When a dictionary is given to construct the row, absent values are filled with
`None`.
.. _db_messages:
Database Messages
=================
By default, py-postgresql gives detailed reports of messages emitted by the
database. Often, the verbosity is excessive due to single target processes or
existing application infrastructure for tracing the sources of various events.
Normally, this verbosity is not a significant problem as the driver defaults the
``client_min_messages`` setting to ``'WARNING'`` by default.
However, if ``NOTICE`` or ``INFO`` messages are needed, finer grained control
over message propagation may be desired, py-postgresql's object relationship
model provides a common protocol for controlling message propagation and,
ultimately, display.
The ``msghook`` attribute on elements--for instance, Statements, Connections,
and Connectors--is absent by default. However, when present on an object that
contributed the cause of a message event, it will be invoked with the Message,
`postgresql.message.Message`, object as its sole parameter. The attribute of
the object that is closest to the event is checked first, if present it will
be called. If the ``msghook()`` call returns a `True`
value(specficially, ``bool(x) is True``), the message will *not* be
propagated any further. However, if a `False` value--notably, `None`--is
returned, the next element is checked until the list is exhausted and the
message is given to `postgresql.sys.msghook`. The normal list of elements is
as follows::
Output → Statement → Connection → Connector → Driver → postgresql.sys
Where ``Output`` can be a `postgresql.api.Cursor` object produced by
``declare(...)`` or an implicit output management object used *internally* by
``Statement.__call__()`` and other statement execution methods. Setting the
``msghook`` attribute on `postgresql.api.Statement` gives very fine
control over raised messages. Consider filtering the notice message on create
table statements that implicitly create indexes::
>>> db = postgresql.open(...)
>>> db.settings['client_min_messages'] = 'NOTICE'
>>> ct_this = db.prepare('CREATE TEMP TABLE "this" (i int PRIMARY KEY)')
>>> ct_that = db.prepare('CREATE TEMP TABLE "that" (i int PRIMARY KEY)')
>>> def filter_notices(msg):
... if msg.details['severity'] == 'NOTICE':
... return True
...
>>> ct_that()
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "that_pkey" for table "that"
...
('CREATE TABLE', None)
>>> ct_this.msghook = filter_notices
>>> ct_this()
('CREATE TABLE', None)
>>>
The above illustrates the quality of an installed ``msghook`` that simply
inhibits further propagation of messages with a severity of 'NOTICE'--but, only
notices coming from objects derived from the ``ct_this``
`postgresql.api.Statement` object.
Subsequently, if the filter is installed on the connection's ``msghook``::
>>> db = postgresql.open(...)
>>> db.settings['client_min_messages'] = 'NOTICE'
>>> ct_this = db.prepare('CREATE TEMP TABLE "this" (i int PRIMARY KEY)')
>>> ct_that = db.prepare('CREATE TEMP TABLE "that" (i int PRIMARY KEY)')
>>> def filter_notices(msg):
... if msg.details['severity'] == 'NOTICE':
... return True
...
>>> db.msghook = filter_notices
>>> ct_that()
('CREATE TABLE', None)
>>> ct_this()
('CREATE TABLE', None)
>>>
Any message with ``'NOTICE'`` severity coming from the connection, ``db``, will be
suffocated by the ``filter_notices`` function. However, if a ``msghook`` is
installed on either of those statements, it would be possible for display to
occur depending on the implementation of the hook installed on the statement
objects.
Message Metadata
----------------
PostgreSQL messages, `postgresql.message.Message`, are primarily described in three
parts: the SQL-state code, the main message string, and a mapping containing the
details. The follow attributes are available on message objects:
``Message.message``
The primary message string.
``Message.code``
The SQL-state code associated with a given message.
``Message.source``
The origins of the message. Normally, ``'SERVER'`` or ``'CLIENT'``.
``Message.location``
A terse, textual representation of ``'file'``, ``'line'``, and ``'function'``
provided by the associated ``details``.
``Message.details``
A mapping providing extended information about a message. This mapping
object **can** contain the following keys:
``'severity'``
Any of ``'DEBUG'``, ``'INFO'``, ``'NOTICE'``, ``'WARNING'``, ``'ERROR'``,
``'FATAL'``, or ``'PANIC'``; the latter three are usually associated with a
`postgresql.exceptions.Error` instance.
``'context'``
The CONTEXT portion of the message.
``'detail'``
The DETAIL portion of the message.
``'hint'``
The HINT portion of the message.
``'position'``
A number identifying the position in the statement string that caused a
parse error.
``'file'``
The name of the file that emitted the message.
(*normally* server information)
``'function'``
The name of the function that emitted the message.
(*normally* server information)
``'line'``
The line of the file that emitted the message.
(*normally* server information)
|