From: Daniele Varrazzo Date: Tue, 14 Dec 2021 23:57:11 +0000 (+0100) Subject: Fix prepared statements clearing X-Git-Tag: pool-3.1~72 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a714448def03e21f26b0d41902a0304d3877a1d1;p=thirdparty%2Fpsycopg.git Fix prepared statements clearing In the conditions illustrated by the test, i.e. with some statements already prepared and a multiple statements query containing a DROP, the check for multiple statements wouldn't have been triggered, and the query would have eventually been asked for preparation, failing because containing multiple statements. --- diff --git a/psycopg/psycopg/_preparing.py b/psycopg/psycopg/_preparing.py index 783a25e4f..14008ee16 100644 --- a/psycopg/psycopg/_preparing.py +++ b/psycopg/psycopg/_preparing.py @@ -182,6 +182,7 @@ class PrepareManager: Clear the internal state and prepare a command to clear the state of the server. """ + self._counts.clear() if self._names: self._names.clear() self._maint_commands.clear() diff --git a/tests/test_prepared.py b/tests/test_prepared.py index fec835547..fb3da6e99 100644 --- a/tests/test_prepared.py +++ b/tests/test_prepared.py @@ -92,6 +92,16 @@ def test_no_prepare_multi(conn): assert res == [0] * 10 +def test_no_prepare_multi_with_drop(conn): + conn.execute("select 1", prepare=True) + + for i in range(10): + conn.execute("drop table if exists noprep; create table noprep()") + + cur = conn.execute("select count(*) from pg_prepared_statements") + assert cur.fetchone() == (0,) + + def test_no_prepare_error(conn): conn.autocommit = True for i in range(10):