]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(copy): fix count of chars to escape
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 29 May 2024 19:45:57 +0000 (21:45 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 30 May 2024 20:38:54 +0000 (22:38 +0200)
We missed to reset the number of chars to escape at every field. As a
consequence, we end up resizing and scanning all the fields after the
first one requiring an escape and allocating a bit more memory than
needed.

docs/news.rst
psycopg_c/psycopg_c/_psycopg/copy.pyx

index 3e4161f75ab447ee99c2ca57b0cf2d812954eb8a..4824bb5a5d7c1d21eb99d62c85fbe432f4043f85 100644 (file)
@@ -16,6 +16,8 @@ Psycopg 3.1.20 (unreleased)
 - Use the simple query protocol to execute COMMIT/ROLLBACK when possible.
   This should make easier to connect the PgBouncer admin database
   (:ticket:`#820`).
+- Avoid unneeded escaping checks and memory over-allocation in text copy
+  (:ticket:`#829`).
 
 
 Current release
index e1ca8191f91bc00fcced5cef8d508ea52a1936fb..c138b7f4470c3fa74cbe1684c2908b3afc4289d4 100644 (file)
@@ -114,7 +114,7 @@ def format_row_text(
     cdef char *buf
     cdef int i, j
     cdef unsigned char *target
-    cdef int nesc = 0
+    cdef int nesc
     cdef int with_tab
     cdef PyObject *fmt = <PyObject *>PG_TEXT
     cdef PyObject *row_dumper
@@ -157,6 +157,7 @@ def format_row_text(
 
         # Now from pos to pos + size there is a textual representation: it may
         # contain chars to escape. Scan to find how many such chars there are.
+        nesc = 0
         for j in range(size):
             if copy_escape_lut[target[j]]:
                 nesc += 1