From 92d3185dab4e0da685f2e1784637f9afd12d7c24 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 29 May 2024 21:45:57 +0200 Subject: [PATCH] fix(copy): fix count of chars to escape 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 | 2 ++ psycopg_c/psycopg_c/_psycopg/copy.pyx | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/news.rst b/docs/news.rst index 2e3c62d2c..d19bd2dc4 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -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 diff --git a/psycopg_c/psycopg_c/_psycopg/copy.pyx b/psycopg_c/psycopg_c/_psycopg/copy.pyx index e1ca8191f..c138b7f44 100644 --- a/psycopg_c/psycopg_c/_psycopg/copy.pyx +++ b/psycopg_c/psycopg_c/_psycopg/copy.pyx @@ -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 = 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 -- 2.47.2