From 7479e6b31e6ed520c9db3814623629ef49e79932 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 20 May 2021 14:00:39 +0200 Subject: [PATCH] Add note about sending multiple statements in the same query Close #1214 --- doc/src/usage.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/doc/src/usage.rst b/doc/src/usage.rst index 0677a5bf..39b6b72b 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -783,6 +783,20 @@ A few other transaction properties can be set session-wide by the change the isolation level. See the `~connection.set_session()` method for all the details. +.. note:: + + When multiple statements are executed in the same query, separated by + semicolon, PostgreSQL wraps them in a transaction. This means that + statements which cannot run in a transaction must be executed separately:: + + # This doesn't work + cur.execute("DROP DATABASE foo; DROP DATABASE bar;") + + If you are wondering why the above works in :program:`psql`, that's + because :program:`psql` parses the queries and splits them on semicolon + before sending them. `!psycopg2` doesn't parse the query, so these + statements must be executed on their own. + .. index:: single: with statement