]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: only flush the pipeline after executemany if returning
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 29 Mar 2022 12:26:26 +0000 (14:26 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 2 Apr 2022 23:23:22 +0000 (01:23 +0200)
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
psycopg/psycopg/connection_async.py
psycopg/psycopg/cursor.py
tests/test_pipeline.py
tests/test_pipeline_async.py

index bf2c584a47d7e8d0d4e114db2db7d21108debde4..5826b4beb82f5969a43ff7f79ad0d7478fe6ba0b 100644 (file)
@@ -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:
index 7d4bebfb1325641ca3b240e5412caba26f605eb4..246dd3fc837558f3a87ef445802733a9ebd1b57d 100644 (file)
@@ -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:
index 7a982f62a3565e8783e87f7aff1902147f4837ae..b123e7b5e80eed5ac5867f27b2df37d0bcddfaff 100644 (file)
@@ -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)
 
index 0e64b1f2d3f25ff87c07547688ac19e37d98345f..dc68062c302d3dfed9d5245e5d285cc0eac7db21 100644 (file)
@@ -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
index fbb2c9f980da1cb9796f2d877b84b61c93fb4e3a..d11992b6e65e4bd0995be2c550d9d4625b6ab0de 100644 (file)
@@ -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