]> 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:35:56 +0000 (22:35 +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 2e3c62d2c5d99693b2647d32e3c7a13a62a0ea33..d19bd2dc4c01281712f2a11dc2359d1dc6821cff 100644 (file)
@@ -52,6 +52,8 @@ Psycopg 3.1.20 (unreleased)
 - Use the simple query protocol to execute COMMIT/ROLLBACK when possible.
   This should make querying the PgBouncer admin database easier
   (: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