]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(copy): avoid sending copy data if an error is set binary-copy
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 1 Sep 2025 16:06:43 +0000 (18:06 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 1 Sep 2025 16:06:43 +0000 (18:06 +0200)
It would result in a partial message. The server seems to do the right
thing anyway, but let's not waste its precious time.

psycopg/psycopg/_copy.py
psycopg/psycopg/_copy_async.py

index 0934a63a2e42714acebb5c68821a27e197aa0268..02c92dab7d3ce8c95fe96fa1e545c7d0f696d629 100644 (file)
@@ -136,8 +136,9 @@ class Copy(BaseCopy["Connection[Any]"]):
         using the `Copy` object outside a block.
         """
         if self._direction == COPY_IN:
-            if data := self.formatter.end():
-                self._write(data)
+            if not exc:
+                if data := self.formatter.end():
+                    self._write(data)
             self.writer.finish(exc)
             self._finished = True
         else:
index 05ec0f98fb3e30a5d3a6dbb0ccd6410fb8c4a0a8..95a8922fcef9b17c925545af870937e6e60cfb3a 100644 (file)
@@ -133,8 +133,9 @@ class AsyncCopy(BaseCopy["AsyncConnection[Any]"]):
         using the `Copy` object outside a block.
         """
         if self._direction == COPY_IN:
-            if data := self.formatter.end():
-                await self._write(data)
+            if not exc:
+                if data := self.formatter.end():
+                    await self._write(data)
             await self.writer.finish(exc)
             self._finished = True
         else: