From: Daniele Varrazzo Date: Fri, 17 May 2024 00:42:32 +0000 (+0200) Subject: docs: improve docs about query protocols and PgBouncer X-Git-Tag: 3.2.0~27 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=ef8d6a307244521ed7ea528fc1c1929e43a3b68a;p=thirdparty%2Fpsycopg.git docs: improve docs about query protocols and PgBouncer --- diff --git a/docs/advanced/cursors.rst b/docs/advanced/cursors.rst index fd4191c80..40e4a4222 100644 --- a/docs/advanced/cursors.rst +++ b/docs/advanced/cursors.rst @@ -126,22 +126,6 @@ as argument. cur = psycopg.ClientCursor(conn) -You can also use `~ClientCursor` with autocommit mode to disable Extended -Query Protocol and use Simple Query Protocol instead. This is useful for -example when issuing `SHOW STATS` to a `pgbouncer` for example, since the -`Pgbouncer Admin Console `_ -only support Simple Query Protocol. - -.. code:: python - - from psycopg import connect, ClientCursor - - conn = psycopg.connect(DSN, cursor_factory=ClientCursor, autocommit=True) - cur = conn.cursor() - cur.execute("SHOW STATS") - cur.fetchall() - - .. warning:: Client-side cursors don't support :ref:`binary parameters and return @@ -159,6 +143,51 @@ only support Simple Query Protocol. to parametrize tables and fields names too, or entirely generic SQL snippets. + +.. index:: + single: PgBouncer + double: Query protocol; simple + +.. _simple-query-protocol: + +Simple query protocol +^^^^^^^^^^^^^^^^^^^^^ + +Using the `!ClientCursor` should ensure that psycopg will always use the +`simple query protocol`__ for querying. In most cases, the choice of the +fronted/backend protocol used is transparent on PostgreSQL. However, in some +case using the simple query protocol is mandatory. This is the case querying +the `PgBouncer admin console`__ for instance, which doesn't support the +extended query protocol. + +.. __: https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-SIMPLE-QUERY +.. __: https://www.pgbouncer.org/usage.html#admin-console + +.. code:: python + + from psycopg import connect, ClientCursor + + conn = psycopg.connect(ADMIN_DSN, cursor_factory=ClientCursor) + cur = conn.cursor() + cur.execute("SHOW STATS") + cur.fetchall() + +.. versionchanged:: 3.1.20 + While querying using the `!ClientCursor` works well with PgBouncer, the + connection's COMMIT and ROLLBACK commands are only ensured to be executed + using the simple query protocol starting from Psycopg 3.1.20. + + In previous versions you should use an autocommit connection in order to + query the PgBouncer admin console: + + .. code:: python + + from psycopg import connect, ClientCursor + + conn = psycopg.connect(ADMIN_DSN, cursor_factory=ClientCursor, autocommit=True) + ... + + .. index:: double: Cursor; Server-side single: Portal diff --git a/docs/basic/from_pg2.rst b/docs/basic/from_pg2.rst index c449c1031..6f5e375af 100644 --- a/docs/basic/from_pg2.rst +++ b/docs/basic/from_pg2.rst @@ -98,6 +98,27 @@ use the `psycopg.sql` module:: ... .format(sql.Identifier(username), password)) +.. index:: + single: PgBouncer + double: Query protocol; advanced + +.. _advanced-query-protocol: + +Extended query Protocol +----------------------- + +In order to use :ref:`server-side-binding`, psycopg normally uses the +`extended query protocol`__ to communicate with the backend. + +In certain context outside pure PostgreSQL, the extended query protocol is not +supported, for instance to query the `PgBouncer admin console`__. In this case +you should probably use a `ClientCursor`. See :ref:`simple-query-protocol` for +details. + +.. __: https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY +.. __: https://www.pgbouncer.org/usage.html#admin-console + + .. _multi-statements: Multiple statements in the same query