From 040ee0c4f754296aafbb71996e43e56b10537bcb Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 29 Mar 2022 14:26:26 +0200 Subject: [PATCH] fix: only flush the pipeline after executemany if returning This has the side effect of breaking rowcount, as before, but can send many executemany in the same pipeline. `rowcount` is correct if `returning=True` is set instead, which is a thing we can at least document, and makes sense: "if you want a result, you flush the pipeline, dude". --- psycopg/psycopg/connection.py | 5 +---- psycopg/psycopg/connection_async.py | 5 +---- psycopg/psycopg/cursor.py | 3 +++ tests/test_pipeline.py | 1 - tests/test_pipeline_async.py | 1 - 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index bf2c584a4..5826b4beb 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -882,10 +882,7 @@ class Connection(BaseConnection[Row]): if not pipeline: # No-op re-entered inner pipeline block. - try: - yield self._pipeline - finally: - self._pipeline.sync() + yield self._pipeline return try: diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index 7d4bebfb1..246dd3fc8 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -310,10 +310,7 @@ class AsyncConnection(BaseConnection[Row]): if not pipeline: # No-op re-entered inner pipeline block. - try: - yield self._pipeline - finally: - await self._pipeline.sync() + yield self._pipeline return try: diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index 7a982f62a..b123e7b5e 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -240,6 +240,9 @@ class BaseCursor(Generic[ConnectionType, Row]): self._last_query = query + if returning: + yield from pipeline._sync_gen() + for cmd in self._conn._prepared.get_maintenance_commands(): yield from self._conn._exec_command(cmd) diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index 0e64b1f2d..dc68062c3 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -189,7 +189,6 @@ def test_executemany_no_returning(conn): [(10,), (20,)], returning=False, ) - assert cur.rowcount == 2 with pytest.raises(e.ProgrammingError, match="no result available"): cur.fetchone() assert cur.nextset() is None diff --git a/tests/test_pipeline_async.py b/tests/test_pipeline_async.py index fbb2c9f98..d11992b6e 100644 --- a/tests/test_pipeline_async.py +++ b/tests/test_pipeline_async.py @@ -192,7 +192,6 @@ async def test_executemany_no_returning(aconn): [(10,), (20,)], returning=False, ) - assert cur.rowcount == 2 with pytest.raises(e.ProgrammingError, match="no result available"): await cur.fetchone() assert cur.nextset() is None -- 2.47.2