2010-02-09 07:58:28 +03:00
|
|
|
The ``connection`` class
|
|
|
|
========================
|
|
|
|
|
|
|
|
.. sectionauthor:: Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
|
|
|
|
2010-02-13 19:06:39 +03:00
|
|
|
.. testsetup::
|
|
|
|
|
|
|
|
from pprint import pprint
|
|
|
|
import psycopg2.extensions
|
|
|
|
|
2010-02-14 14:58:23 +03:00
|
|
|
drop_test_table('foo')
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. class:: connection
|
|
|
|
|
|
|
|
Handles the connection to a PostgreSQL database instance. It encapsulates
|
|
|
|
a database session.
|
|
|
|
|
|
|
|
Connections are created using the factory function
|
2010-02-26 03:17:52 +03:00
|
|
|
`~psycopg2.connect()`.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
Connections are thread safe and can be shared among many thread. See
|
|
|
|
:ref:`thread-safety` for details.
|
|
|
|
|
|
|
|
.. method:: cursor([name] [, cursor_factory])
|
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
Return a new `cursor` object using the connection.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
If `name` is specified, the returned cursor will be a *server
|
2010-02-09 07:58:28 +03:00
|
|
|
side* (or *named*) cursor. Otherwise the cursor will be *client side*.
|
|
|
|
See :ref:`server-side-cursors` for further details.
|
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
The `cursor_factory` argument can be used to create non-standard
|
2010-02-09 07:58:28 +03:00
|
|
|
cursors. The class returned should be a subclass of
|
2010-02-26 03:17:52 +03:00
|
|
|
`psycopg2.extensions.cursor`. See :ref:`subclassing-cursor` for
|
2010-02-11 06:15:14 +03:00
|
|
|
details.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. extension::
|
|
|
|
|
2010-02-18 04:08:44 +03:00
|
|
|
The `name` and `cursor_factory` parameters are Psycopg
|
2010-02-11 06:15:14 +03:00
|
|
|
extensions to the |DBAPI|.
|
2010-02-09 17:18:28 +03:00
|
|
|
|
2010-02-13 08:43:40 +03:00
|
|
|
|
2010-02-09 18:32:24 +03:00
|
|
|
.. index::
|
|
|
|
pair: Transaction; Commit
|
2010-02-09 17:18:28 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: commit()
|
|
|
|
|
|
|
|
Commit any pending transaction to the database. Psycopg can be set to
|
|
|
|
perform automatic commits at each operation, see
|
2010-02-26 03:17:52 +03:00
|
|
|
`~connection.set_isolation_level()`.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
|
2010-02-09 18:32:24 +03:00
|
|
|
.. index::
|
|
|
|
pair: Transaction; Rollback
|
2010-02-09 17:18:28 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: rollback()
|
|
|
|
|
|
|
|
Roll back to the start of any pending transaction. Closing a
|
|
|
|
connection without committing the changes first will cause an implicit
|
|
|
|
rollback to be performed.
|
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: close()
|
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
Close the connection now (rather than whenever `__del__()` is
|
2010-02-11 06:15:14 +03:00
|
|
|
called). The connection will be unusable from this point forward; an
|
2010-02-26 03:17:52 +03:00
|
|
|
`~psycopg2.InterfaceError` will be raised if any operation is
|
2010-02-11 06:15:14 +03:00
|
|
|
attempted with the connection. The same applies to all cursor objects
|
|
|
|
trying to use the connection. Note that closing a connection without
|
2010-08-05 04:18:11 +04:00
|
|
|
committing the changes first will cause any pending change to be
|
|
|
|
discarded as if a :sql:`ROLLBACK` was performed (unless a different
|
|
|
|
isolation level has been selected: see
|
2010-02-26 03:17:52 +03:00
|
|
|
`~connection.set_isolation_level()`).
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-08-05 04:18:11 +04:00
|
|
|
.. index::
|
|
|
|
single: PgBouncer; unclean server
|
|
|
|
|
|
|
|
.. versionchanged:: 2.2
|
|
|
|
previously an explicit :sql:`ROLLBACK` was issued by Psycopg on
|
|
|
|
`!close()`. The command could have been sent to the backend at an
|
|
|
|
inappropriate time, so Psycopg currently relies on the backend to
|
|
|
|
implicitly discard uncommitted changes. Some middleware are known
|
|
|
|
to behave incorrectly though when the connection is closed during
|
|
|
|
a transaction (when `~connection.status` is
|
|
|
|
`~psycopg2.extensions.STATUS_IN_TRANSACTION`), e.g. PgBouncer_
|
|
|
|
reports an ``unclean server`` and discards the connection. To
|
|
|
|
avoid this problem you can ensure to terminate the transaction
|
|
|
|
with a `~connection.commit()`/`~connection.rollback()` before
|
|
|
|
closing.
|
|
|
|
|
|
|
|
.. _PgBouncer: http://pgbouncer.projects.postgresql.org/
|
2010-02-09 17:18:28 +03:00
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
.. index::
|
|
|
|
single: Exceptions; In the connection class
|
|
|
|
|
2010-08-05 02:26:52 +04:00
|
|
|
.. rubric:: Exceptions as connection class attributes
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
The `!connection` also exposes as attributes the same exceptions
|
|
|
|
available in the `psycopg2` module. See :ref:`dbapi-exceptions`.
|
2010-02-10 02:09:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
.. extension::
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
The above methods are the only ones defined by the |DBAPI| protocol.
|
2010-02-10 02:09:48 +03:00
|
|
|
The Psycopg connection objects exports the following additional
|
|
|
|
methods and attributes.
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. attribute:: closed
|
|
|
|
|
|
|
|
Read-only attribute reporting whether the database connection is open
|
|
|
|
(0) or closed (1).
|
|
|
|
|
2010-02-10 02:09:48 +03:00
|
|
|
|
2010-02-13 08:43:40 +03:00
|
|
|
.. method:: reset
|
|
|
|
|
|
|
|
Reset the connection to the default.
|
|
|
|
|
|
|
|
The method rolls back an eventual pending transaction and executes the
|
|
|
|
PostgreSQL |RESET|_ and |SET SESSION AUTHORIZATION|__ to revert the
|
|
|
|
session to the default values.
|
|
|
|
|
|
|
|
.. |RESET| replace:: :sql:`RESET`
|
2010-09-24 02:12:54 +04:00
|
|
|
.. _RESET: http://www.postgresql.org/docs/9.0/static/sql-reset.html
|
2010-02-13 08:43:40 +03:00
|
|
|
|
|
|
|
.. |SET SESSION AUTHORIZATION| replace:: :sql:`SET SESSION AUTHORIZATION`
|
2010-09-24 02:12:54 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/9.0/static/sql-set-session-authorization.html
|
2010-02-13 08:43:40 +03:00
|
|
|
|
|
|
|
.. versionadded:: 2.0.12
|
|
|
|
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: dsn
|
|
|
|
|
|
|
|
Read-only string containing the connection string used by the
|
|
|
|
connection.
|
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
|
2010-02-09 18:32:24 +03:00
|
|
|
.. index::
|
|
|
|
pair: Transaction; Autocommit
|
2010-02-09 17:18:28 +03:00
|
|
|
pair: Transaction; Isolation level
|
|
|
|
|
2010-02-11 06:15:14 +03:00
|
|
|
.. _autocommit:
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: isolation_level
|
|
|
|
.. method:: set_isolation_level(level)
|
|
|
|
|
|
|
|
Read or set the `transaction isolation level`_ for the current session.
|
|
|
|
The level defines the different phenomena that can happen in the
|
|
|
|
database between concurrent transactions.
|
|
|
|
|
|
|
|
The value set or read is an integer: symbolic constants are defined in
|
2010-02-26 03:17:52 +03:00
|
|
|
the module `psycopg2.extensions`: see
|
2010-02-09 07:58:28 +03:00
|
|
|
:ref:`isolation-level-constants` for the available values.
|
|
|
|
|
2010-02-18 06:51:17 +03:00
|
|
|
The default level is :sql:`READ COMMITTED`: at this level a
|
|
|
|
transaction is automatically started the first time a database command
|
|
|
|
is executed. If you want an *autocommit* mode, switch to
|
2010-02-26 03:17:52 +03:00
|
|
|
`~psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT` before
|
2010-02-18 06:51:17 +03:00
|
|
|
executing any command::
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
>>> conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
|
|
|
|
2010-02-18 06:51:17 +03:00
|
|
|
See also :ref:`transactions-control`.
|
2010-02-09 17:18:28 +03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Client; Encoding
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: encoding
|
|
|
|
.. method:: set_client_encoding(enc)
|
|
|
|
|
|
|
|
Read or set the client encoding for the current session. The default
|
|
|
|
is the encoding defined by the database. It should be one of the
|
|
|
|
`characters set supported by PostgreSQL`__
|
|
|
|
|
2010-09-24 02:12:54 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/9.0/static/multibyte.html
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
|
2010-02-09 17:03:44 +03:00
|
|
|
.. index::
|
2010-02-09 17:18:28 +03:00
|
|
|
pair: Client; Logging
|
2010-02-09 17:03:44 +03:00
|
|
|
|
|
|
|
.. attribute:: notices
|
|
|
|
|
|
|
|
A list containing all the database messages sent to the client during
|
2010-02-13 19:06:39 +03:00
|
|
|
the session.
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
:options: NORMALIZE_WHITESPACE
|
2010-02-09 17:03:44 +03:00
|
|
|
|
|
|
|
>>> cur.execute("CREATE TABLE foo (id serial PRIMARY KEY);")
|
2010-02-13 19:06:39 +03:00
|
|
|
>>> pprint(conn.notices)
|
2010-02-09 17:03:44 +03:00
|
|
|
['NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey" for table "foo"\n',
|
|
|
|
'NOTICE: CREATE TABLE will create implicit sequence "foo_id_seq" for serial column "foo.id"\n']
|
|
|
|
|
|
|
|
To avoid a leak in case excessive notices are generated, only the last
|
|
|
|
50 messages are kept.
|
|
|
|
|
|
|
|
You can configure what messages to receive using `PostgreSQL logging
|
|
|
|
configuration parameters`__ such as ``log_statement``,
|
|
|
|
``client_min_messages``, ``log_min_duration_statement`` etc.
|
|
|
|
|
2010-09-24 02:12:54 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/9.0/static/runtime-config-logging.html
|
2010-02-09 17:03:44 +03:00
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
|
2010-02-09 20:52:41 +03:00
|
|
|
.. attribute:: notifies
|
|
|
|
|
|
|
|
List containing asynchronous notifications received by the session.
|
|
|
|
|
|
|
|
Received notifications have the form of a 2 items tuple
|
2010-02-11 06:15:14 +03:00
|
|
|
:samp:`({pid},{name})`, where :samp:`{pid}` is the PID of the backend
|
|
|
|
that sent the notification and :samp:`{name}` is the signal name
|
|
|
|
specified in the :sql:`NOTIFY` command.
|
2010-02-09 20:52:41 +03:00
|
|
|
|
|
|
|
For other details see :ref:`async-notify`.
|
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
.. index::
|
|
|
|
pair: Backend; PID
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: get_backend_pid()
|
|
|
|
|
|
|
|
Returns the process ID (PID) of the backend server process handling
|
|
|
|
this connection.
|
|
|
|
|
|
|
|
Note that the PID belongs to a process executing on the database
|
|
|
|
server host, not the local host!
|
|
|
|
|
|
|
|
.. seealso:: libpq docs for `PQbackendPID()`__ for details.
|
|
|
|
|
2010-09-24 02:12:54 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/9.0/static/libpq-status.html#LIBPQ-PQBACKENDPID
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-13 08:49:34 +03:00
|
|
|
.. versionadded:: 2.0.8
|
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Server; Parameters
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: get_parameter_status(parameter)
|
|
|
|
|
|
|
|
Look up a current parameter setting of the server.
|
|
|
|
|
|
|
|
Potential values for ``parameter`` are: ``server_version``,
|
|
|
|
``server_encoding``, ``client_encoding``, ``is_superuser``,
|
|
|
|
``session_authorization``, ``DateStyle``, ``TimeZone``,
|
|
|
|
``integer_datetimes``, and ``standard_conforming_strings``.
|
|
|
|
|
|
|
|
If server did not report requested parameter, return ``None``.
|
|
|
|
|
|
|
|
.. seealso:: libpq docs for `PQparameterStatus()`__ for details.
|
|
|
|
|
2010-09-24 02:12:54 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/9.0/static/libpq-status.html#LIBPQ-PQPARAMETERSTATUS
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-13 08:49:34 +03:00
|
|
|
.. versionadded:: 2.0.12
|
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Transaction; Status
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. method:: get_transaction_status()
|
|
|
|
|
|
|
|
Return the current session transaction status as an integer. Symbolic
|
2010-02-09 16:33:31 +03:00
|
|
|
constants for the values are defined in the module
|
2010-02-26 03:17:52 +03:00
|
|
|
`psycopg2.extensions`: see :ref:`transaction-status-constants`
|
2010-02-09 07:58:28 +03:00
|
|
|
for the available values.
|
|
|
|
|
|
|
|
.. seealso:: libpq docs for `PQtransactionStatus()`__ for details.
|
|
|
|
|
2010-09-24 02:12:54 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/9.0/static/libpq-status.html#LIBPQ-PQTRANSACTIONSTATUS
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Protocol; Version
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: protocol_version
|
|
|
|
|
2010-02-09 16:33:31 +03:00
|
|
|
A read-only integer representing frontend/backend protocol being used.
|
2010-02-09 07:58:28 +03:00
|
|
|
It can be 2 or 3.
|
|
|
|
|
|
|
|
.. seealso:: libpq docs for `PQprotocolVersion()`__ for details.
|
|
|
|
|
2010-09-24 02:12:54 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/9.0/static/libpq-status.html#LIBPQ-PQPROTOCOLVERSION
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-13 08:49:34 +03:00
|
|
|
.. versionadded:: 2.0.12
|
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
pair: Server; Version
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: server_version
|
|
|
|
|
|
|
|
A read-only integer representing the backend version.
|
|
|
|
|
|
|
|
The number is formed by converting the major, minor, and revision
|
|
|
|
numbers into two-decimal-digit numbers and appending them together.
|
2010-02-11 06:15:14 +03:00
|
|
|
For example, version 8.1.5 will be returned as ``80105``.
|
2010-02-09 07:58:28 +03:00
|
|
|
|
|
|
|
.. seealso:: libpq docs for `PQserverVersion()`__ for details.
|
|
|
|
|
2010-09-24 02:12:54 +04:00
|
|
|
.. __: http://www.postgresql.org/docs/9.0/static/libpq-status.html#LIBPQ-PQSERVERVERSION
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-13 08:49:34 +03:00
|
|
|
.. versionadded:: 2.0.12
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-09 17:18:28 +03:00
|
|
|
.. index::
|
|
|
|
pair: Connection; Status
|
|
|
|
|
2010-02-09 07:58:28 +03:00
|
|
|
.. attribute:: status
|
|
|
|
|
|
|
|
A read-only integer representing the status of the connection.
|
|
|
|
Symbolic constants for the values are defined in the module
|
2010-02-26 03:17:52 +03:00
|
|
|
`psycopg2.extensions`: see :ref:`connection-status-constants`
|
2010-02-09 07:58:28 +03:00
|
|
|
for the available values.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: lobject([oid [, mode [, new_oid [, new_file [, lobject_factory]]]]])
|
|
|
|
|
2010-02-14 22:42:15 +03:00
|
|
|
Return a new database large object. See :ref:`large-objects` for an
|
|
|
|
overview.
|
|
|
|
|
|
|
|
:param oid: The OID of the object to read or write. 0 to create
|
|
|
|
a new large object and and have its OID assigned automatically.
|
|
|
|
:param mode: Access mode to the object: can be ``r``, ``w``,
|
|
|
|
``rw`` or ``n`` (meaning don't open it).
|
|
|
|
:param new_oid: Create a new object using the specified OID. The
|
2010-02-26 03:17:52 +03:00
|
|
|
function raises `OperationalError` if the OID is already in
|
2010-02-14 22:42:15 +03:00
|
|
|
use. Default is 0, meaning assign a new one automatically.
|
|
|
|
:param new_file: The name of a file to be imported in the the database
|
|
|
|
(using the |lo_import|_ function)
|
|
|
|
:param lobject_factory: Subclass of
|
2010-02-26 03:17:52 +03:00
|
|
|
`~psycopg2.extensions.lobject` to be instantiated.
|
|
|
|
:rtype: `~psycopg2.extensions.lobject`
|
2010-02-14 22:42:15 +03:00
|
|
|
|
2010-02-26 03:17:52 +03:00
|
|
|
.. |lo_import| replace:: `!lo_import()`
|
2010-09-24 02:12:54 +04:00
|
|
|
.. _lo_import: http://www.postgresql.org/docs/9.0/static/lo-interfaces.html#LO-IMPORT
|
2010-02-09 07:58:28 +03:00
|
|
|
|
2010-02-13 08:49:34 +03:00
|
|
|
.. versionadded:: 2.0.8
|
2010-04-08 16:22:55 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. rubric:: Methods related to asynchronous support.
|
|
|
|
|
2010-04-20 15:30:41 +04:00
|
|
|
.. versionadded:: 2.2.0
|
|
|
|
|
2010-04-04 06:10:18 +04:00
|
|
|
.. seealso:: :ref:`async-support` and :ref:`green-support`.
|
2010-04-08 16:22:55 +04:00
|
|
|
|
|
|
|
|
2010-04-20 03:44:42 +04:00
|
|
|
.. attribute:: async
|
2010-04-08 16:22:55 +04:00
|
|
|
|
2010-09-24 02:17:32 +04:00
|
|
|
Read only attribute: 1 if the connection is asynchronous, 0 otherwise.
|
2010-04-08 16:22:55 +04:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: poll()
|
|
|
|
|
2010-04-20 02:49:14 +04:00
|
|
|
Used during an asynchronous connection attempt, or when a cursor is
|
|
|
|
executing a query on an asynchronous connection, make communication
|
2010-04-08 16:22:55 +04:00
|
|
|
proceed if it wouldn't block.
|
|
|
|
|
|
|
|
Return one of the constants defined in :ref:`poll-constants`. If it
|
2010-04-20 02:49:14 +04:00
|
|
|
returns `~psycopg2.extensions.POLL_OK` then the connection has been
|
|
|
|
estabilished or the query results are available on the client.
|
2010-04-04 06:10:18 +04:00
|
|
|
Otherwise wait until the file descriptor returned by `fileno()` is
|
|
|
|
ready to read or to write, as explained in :ref:`async-support`.
|
|
|
|
`poll()` should be also used by the function installed by
|
|
|
|
`~psycopg2.extensions.set_wait_callback()` as explained in
|
|
|
|
:ref:`green-support`.
|
2010-04-08 16:22:55 +04:00
|
|
|
|
2010-04-20 15:30:41 +04:00
|
|
|
`poll()` is also used to receive asynchronous notifications from the
|
|
|
|
database: see :ref:`async-notify` from further details.
|
|
|
|
|
2010-04-08 16:22:55 +04:00
|
|
|
|
|
|
|
.. method:: fileno()
|
|
|
|
|
2010-04-20 02:49:14 +04:00
|
|
|
Return the file descriptor underlying the connection: useful to read
|
|
|
|
its status during asynchronous communication.
|
2010-04-08 16:22:55 +04:00
|
|
|
|
|
|
|
|
2010-04-20 03:50:34 +04:00
|
|
|
.. method:: isexecuting()
|
2010-04-08 16:22:55 +04:00
|
|
|
|
|
|
|
Return `True` if the connection is executing an asynchronous operation.
|
|
|
|
|
2010-04-20 02:51:42 +04:00
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
:hide:
|
|
|
|
|
|
|
|
conn.rollback()
|