PyGreSQL 6.2.0__tar.gz → 6.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. {pygresql-6.2.0 → pygresql-6.2.2}/LICENSE.txt +1 -1
  2. {pygresql-6.2.0 → pygresql-6.2.2}/PKG-INFO +1 -1
  3. {pygresql-6.2.0 → pygresql-6.2.2}/PyGreSQL.egg-info/PKG-INFO +1 -1
  4. {pygresql-6.2.0 → pygresql-6.2.2}/docs/about.rst +1 -1
  5. {pygresql-6.2.0 → pygresql-6.2.2}/docs/conf.py +1 -1
  6. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/changelog.rst +13 -0
  7. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/connection.rst +2 -1
  8. {pygresql-6.2.0 → pygresql-6.2.2}/docs/copyright.rst +1 -1
  9. {pygresql-6.2.0 → pygresql-6.2.2}/ext/pgconn.c +100 -116
  10. {pygresql-6.2.0 → pygresql-6.2.2}/ext/pginternal.c +70 -1
  11. {pygresql-6.2.0 → pygresql-6.2.2}/ext/pglarge.c +1 -1
  12. {pygresql-6.2.0 → pygresql-6.2.2}/ext/pgmodule.c +14 -3
  13. {pygresql-6.2.0 → pygresql-6.2.2}/ext/pgnotice.c +1 -1
  14. {pygresql-6.2.0 → pygresql-6.2.2}/ext/pgquery.c +2 -5
  15. {pygresql-6.2.0 → pygresql-6.2.2}/ext/pgsource.c +1 -1
  16. {pygresql-6.2.0 → pygresql-6.2.2}/pg/__init__.py +1 -1
  17. {pygresql-6.2.0 → pygresql-6.2.2}/pg/_pg.pyi +2 -1
  18. {pygresql-6.2.0 → pygresql-6.2.2}/pg/db.py +5 -3
  19. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/__init__.py +1 -1
  20. {pygresql-6.2.0 → pygresql-6.2.2}/pyproject.toml +1 -1
  21. {pygresql-6.2.0 → pygresql-6.2.2}/setup.py +34 -23
  22. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_classic_connection.py +26 -9
  23. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_classic_dbwrapper.py +18 -0
  24. {pygresql-6.2.0 → pygresql-6.2.2}/MANIFEST.in +0 -0
  25. {pygresql-6.2.0 → pygresql-6.2.2}/PyGreSQL.egg-info/SOURCES.txt +0 -0
  26. {pygresql-6.2.0 → pygresql-6.2.2}/PyGreSQL.egg-info/dependency_links.txt +0 -0
  27. {pygresql-6.2.0 → pygresql-6.2.2}/PyGreSQL.egg-info/not-zip-safe +0 -0
  28. {pygresql-6.2.0 → pygresql-6.2.2}/PyGreSQL.egg-info/top_level.txt +0 -0
  29. {pygresql-6.2.0 → pygresql-6.2.2}/README.rst +0 -0
  30. {pygresql-6.2.0 → pygresql-6.2.2}/docs/Makefile +0 -0
  31. {pygresql-6.2.0 → pygresql-6.2.2}/docs/_static/favicon.ico +0 -0
  32. {pygresql-6.2.0 → pygresql-6.2.2}/docs/_static/pygresql.png +0 -0
  33. {pygresql-6.2.0 → pygresql-6.2.2}/docs/community/homes.rst +0 -0
  34. {pygresql-6.2.0 → pygresql-6.2.2}/docs/community/index.rst +0 -0
  35. {pygresql-6.2.0 → pygresql-6.2.2}/docs/community/issues.rst +0 -0
  36. {pygresql-6.2.0 → pygresql-6.2.2}/docs/community/mailinglist.rst +0 -0
  37. {pygresql-6.2.0 → pygresql-6.2.2}/docs/community/source.rst +0 -0
  38. {pygresql-6.2.0 → pygresql-6.2.2}/docs/community/support.rst +0 -0
  39. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/examples.rst +0 -0
  40. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/general.rst +0 -0
  41. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/index.rst +0 -0
  42. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/install.rst +0 -0
  43. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/adaptation.rst +0 -0
  44. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/db_types.rst +0 -0
  45. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/db_wrapper.rst +0 -0
  46. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/index.rst +0 -0
  47. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/introduction.rst +0 -0
  48. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/large_objects.rst +0 -0
  49. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/module.rst +0 -0
  50. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/notification.rst +0 -0
  51. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pg/query.rst +0 -0
  52. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pgdb/adaptation.rst +0 -0
  53. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pgdb/connection.rst +0 -0
  54. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pgdb/cursor.rst +0 -0
  55. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pgdb/index.rst +0 -0
  56. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pgdb/introduction.rst +0 -0
  57. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pgdb/module.rst +0 -0
  58. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pgdb/typecache.rst +0 -0
  59. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/pgdb/types.rst +0 -0
  60. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/postgres/advanced.rst +0 -0
  61. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/postgres/basic.rst +0 -0
  62. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/postgres/func.rst +0 -0
  63. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/postgres/index.rst +0 -0
  64. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/postgres/syscat.rst +0 -0
  65. {pygresql-6.2.0 → pygresql-6.2.2}/docs/contents/tutorial.rst +0 -0
  66. {pygresql-6.2.0 → pygresql-6.2.2}/docs/download/download.rst +0 -0
  67. {pygresql-6.2.0 → pygresql-6.2.2}/docs/download/files.rst +0 -0
  68. {pygresql-6.2.0 → pygresql-6.2.2}/docs/download/index.rst +0 -0
  69. {pygresql-6.2.0 → pygresql-6.2.2}/docs/make.bat +0 -0
  70. {pygresql-6.2.0 → pygresql-6.2.2}/docs/requirements.txt +0 -0
  71. {pygresql-6.2.0 → pygresql-6.2.2}/ext/pgtypes.h +0 -0
  72. {pygresql-6.2.0 → pygresql-6.2.2}/pg/adapt.py +0 -0
  73. {pygresql-6.2.0 → pygresql-6.2.2}/pg/attrs.py +0 -0
  74. {pygresql-6.2.0 → pygresql-6.2.2}/pg/cast.py +0 -0
  75. {pygresql-6.2.0 → pygresql-6.2.2}/pg/core.py +0 -0
  76. {pygresql-6.2.0 → pygresql-6.2.2}/pg/error.py +0 -0
  77. {pygresql-6.2.0 → pygresql-6.2.2}/pg/helpers.py +0 -0
  78. {pygresql-6.2.0 → pygresql-6.2.2}/pg/notify.py +0 -0
  79. {pygresql-6.2.0 → pygresql-6.2.2}/pg/py.typed +0 -0
  80. {pygresql-6.2.0 → pygresql-6.2.2}/pg/tz.py +0 -0
  81. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/adapt.py +0 -0
  82. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/cast.py +0 -0
  83. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/connect.py +0 -0
  84. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/connection.py +0 -0
  85. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/constants.py +0 -0
  86. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/cursor.py +0 -0
  87. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/py.typed +0 -0
  88. {pygresql-6.2.0 → pygresql-6.2.2}/pgdb/typecode.py +0 -0
  89. {pygresql-6.2.0 → pygresql-6.2.2}/setup.cfg +0 -0
  90. {pygresql-6.2.0 → pygresql-6.2.2}/tests/__init__.py +0 -0
  91. {pygresql-6.2.0 → pygresql-6.2.2}/tests/config.py +0 -0
  92. {pygresql-6.2.0 → pygresql-6.2.2}/tests/dbapi20.py +0 -0
  93. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_classic.py +0 -0
  94. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_classic_attrdict.py +0 -0
  95. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_classic_functions.py +0 -0
  96. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_classic_largeobj.py +0 -0
  97. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_classic_notification.py +0 -0
  98. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_dbapi20.py +0 -0
  99. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_dbapi20_copy.py +0 -0
  100. {pygresql-6.2.0 → pygresql-6.2.2}/tests/test_tutorial.py +0 -0
  101. {pygresql-6.2.0 → pygresql-6.2.2}/tox.ini +0 -0
@@ -6,7 +6,7 @@ Copyright (c) 1995, Pascal Andre
6
6
 
7
7
  Further modifications copyright (c) 1997-2008 by D'Arcy J.M. Cain
8
8
 
9
- Further modifications copyright (c) 2009-2025 by the PyGreSQL Development Team
9
+ Further modifications copyright (c) 2009-2026 by the PyGreSQL Development Team
10
10
 
11
11
  PyGreSQL is released under the PostgreSQL License, a liberal Open Source
12
12
  license, similar to the BSD or MIT licenses:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyGreSQL
3
- Version: 6.2.0
3
+ Version: 6.2.2
4
4
  Summary: Python PostgreSQL interfaces
5
5
  Home-page: https://pygresql.github.io/
6
6
  Download-URL: https://pygresql.github.io/download/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyGreSQL
3
- Version: 6.2.0
3
+ Version: 6.2.2
4
4
  Summary: Python PostgreSQL interfaces
5
5
  Home-page: https://pygresql.github.io/
6
6
  Download-URL: https://pygresql.github.io/download/
@@ -8,7 +8,7 @@ powerful PostgreSQL features from Python.
8
8
 
9
9
  | This software is copyright © 1995, Pascal Andre.
10
10
  | Further modifications are copyright © 1997-2008 by D'Arcy J.M. Cain.
11
- | Further modifications are copyright © 2009-2025 by the PyGreSQL team.
11
+ | Further modifications are copyright © 2009-2026 by the PyGreSQL team.
12
12
  | For licensing details, see the full :doc:`copyright`.
13
13
 
14
14
  **PostgreSQL** is a highly scalable, SQL compliant, open source
@@ -8,7 +8,7 @@
8
8
 
9
9
  project = 'PyGreSQL'
10
10
  author = 'The PyGreSQL team'
11
- copyright = '2025, ' + author
11
+ copyright = '2026, ' + author
12
12
 
13
13
  def project_version():
14
14
  with open('../pyproject.toml') as f:
@@ -1,11 +1,24 @@
1
1
  ChangeLog
2
2
  =========
3
3
 
4
+ Version 6.2.2 (2026-01-03)
5
+ --------------------------
6
+ - The `inserttable()` method in the `pg` module can now handle rows of
7
+ arbitrary size (this was previously limited to 64 KB) (#79, thanks
8
+ to Justin Pryzby for this contribution).
9
+
10
+ Version 6.2.1 (2025-12-27)
11
+ --------------------------
12
+ - Made setup cleaner and work with a wider range of setuptools versions (#90).
13
+ - The `inserttable()` method in the `pg` module now supports an optional
14
+ `freeze` parameter to optimize initial bulk loading (#81).
15
+
4
16
  Version 6.2.0 (2025-12-21)
5
17
  --------------------------
6
18
  - Added support for Python 3.14 and PostgreSQL 18.
7
19
  - Removed support for Python versions older than 3.8 (released October 2019)
8
20
  and PostgreSQL older than version 12 (released October 2019).
21
+ - Fixed `set_parameter()` for values containing special characters.
9
22
 
10
23
  Version 6.1.0 (2024-12-05)
11
24
  --------------------------
@@ -478,13 +478,14 @@ first, otherwise :meth:`Connection.getnotify` will always return ``None``.
478
478
  inserttable -- insert an iterable into a table
479
479
  ----------------------------------------------
480
480
 
481
- .. method:: Connection.inserttable(table, values, [columns])
481
+ .. method:: Connection.inserttable(table, values, [columns], *, freeze=False)
482
482
 
483
483
  Insert a Python iterable into a database table
484
484
 
485
485
  :param str table: the table name
486
486
  :param list values: iterable of row values, which must be lists or tuples
487
487
  :param list columns: list or tuple of column names
488
+ :param bool freeze: if True, immediately freeze the inserted rows
488
489
  :rtype: int
489
490
  :raises TypeError: invalid connection, bad argument type, or too many arguments
490
491
  :raises MemoryError: insert buffer could not be allocated
@@ -10,7 +10,7 @@ Copyright (c) 1995, Pascal Andre
10
10
  Further modifications copyright (c) 1997-2008 by D'Arcy J.M. Cain
11
11
  (darcy@PyGreSQL.org)
12
12
 
13
- Further modifications copyright (c) 2009-2025 by the PyGreSQL team.
13
+ Further modifications copyright (c) 2009-2026 by the PyGreSQL team.
14
14
 
15
15
  Permission to use, copy, modify, and distribute this software and its
16
16
  documentation for any purpose, without fee, and without a written agreement
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * The connection object - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -532,6 +532,12 @@ conn_describe_prepared(connObject *self, PyObject *args)
532
532
  query_obj->max_row = PQntuples(result);
533
533
  query_obj->num_fields = PQnfields(result);
534
534
  query_obj->col_types = get_col_types(result, query_obj->num_fields);
535
+ if (!query_obj->col_types) {
536
+ PQclear(result);
537
+ Py_DECREF(query_obj);
538
+ Py_DECREF(self);
539
+ return NULL;
540
+ }
535
541
  return (PyObject *)query_obj;
536
542
  }
537
543
  set_error(ProgrammingError, "Cannot describe prepared statement",
@@ -708,28 +714,34 @@ conn_is_non_blocking(connObject *self, PyObject *noargs)
708
714
  static char conn_inserttable__doc__[] =
709
715
  "inserttable(table, data, [columns]) -- insert iterable into table\n\n"
710
716
  "The fields in the iterable must be in the same order as in the table\n"
711
- "or in the list or tuple of columns if one is specified.\n";
717
+ "or in the list or tuple of columns if one is specified.\n\n"
718
+ "If the optional argument 'freeze' is set to True, the inserted rows\n"
719
+ "will be immediately frozen (can be useful for initial bulk loads).\n";
712
720
 
713
721
  static PyObject *
714
- conn_inserttable(connObject *self, PyObject *args)
722
+ conn_inserttable(connObject *self, PyObject *args, PyObject *kwds)
715
723
  {
716
724
  PGresult *result;
717
- char *table, *buffer, *bufpt, *bufmax, *s, *t;
718
- int encoding, ret;
719
- size_t bufsiz;
725
+ char *table, *s, *t;
726
+ struct CharBuffer buffer = {0};
727
+ int freeze = 0, encoding, ret;
720
728
  PyObject *rows, *iter_row, *item, *columns = NULL;
721
729
  Py_ssize_t i, j, m, n;
722
730
 
731
+ static char *kwlist[] = {"table", "data", "columns", "freeze", NULL};
732
+
723
733
  if (!self->cnx) {
724
734
  PyErr_SetString(PyExc_TypeError, "Connection is not valid");
725
735
  return NULL;
726
736
  }
727
737
 
728
738
  /* gets arguments */
729
- if (!PyArg_ParseTuple(args, "sO|O", &table, &rows, &columns)) {
739
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "sO|O$p", kwlist, &table,
740
+ &rows, &columns, &freeze)) {
730
741
  PyErr_SetString(
731
742
  PyExc_TypeError,
732
- "Method inserttable() expects a string and a list as arguments");
743
+ "Method inserttable() expects a string, an iterable, an optional "
744
+ "list/tuple and an optional boolean 'freeze' as arguments");
733
745
  return NULL;
734
746
  }
735
747
 
@@ -769,18 +781,16 @@ conn_inserttable(connObject *self, PyObject *args)
769
781
  n = -1; /* number of columns not yet known */
770
782
  }
771
783
 
772
- /* allocate buffer */
773
- if (!(buffer = PyMem_Malloc(MAX_BUFFER_SIZE))) {
784
+ encoding = PQclientEncoding(self->cnx);
785
+
786
+ /* pre-allocate some memory for the query buffer */
787
+ if (!init_char_buffer(&buffer, 4096)) {
774
788
  Py_DECREF(iter_row);
775
789
  return PyErr_NoMemory();
776
790
  }
777
791
 
778
- encoding = PQclientEncoding(self->cnx);
779
-
780
792
  /* starts query */
781
- bufpt = buffer;
782
- bufmax = bufpt + MAX_BUFFER_SIZE;
783
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "copy ");
793
+ ext_char_buffer_s(&buffer, "copy ");
784
794
 
785
795
  s = table;
786
796
  do {
@@ -788,18 +798,16 @@ conn_inserttable(connObject *self, PyObject *args)
788
798
  if (!t)
789
799
  t = s + strlen(s);
790
800
  table = PQescapeIdentifier(self->cnx, s, (size_t)(t - s));
791
- if (bufpt < bufmax)
792
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "%s", table);
801
+ ext_char_buffer_s(&buffer, table);
793
802
  PQfreemem(table);
794
803
  s = t;
795
- if (*s && bufpt < bufmax)
796
- *bufpt++ = *s++;
804
+ if (*s)
805
+ ext_char_buffer_c(&buffer, *s++);
797
806
  } while (*s);
798
807
 
799
808
  if (columns) {
800
809
  /* adds a string like f" ({','.join(columns)})" */
801
- if (bufpt < bufmax)
802
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), " (");
810
+ ext_char_buffer_s(&buffer, " (");
803
811
  for (j = 0; j < n; ++j) {
804
812
  PyObject *obj = PySequence_Fast_GET_ITEM(columns, j);
805
813
  Py_ssize_t slen;
@@ -811,7 +819,7 @@ conn_inserttable(connObject *self, PyObject *args)
811
819
  else if (PyUnicode_Check(obj)) {
812
820
  obj = get_encoded_string(obj, encoding);
813
821
  if (!obj) {
814
- PyMem_Free(buffer);
822
+ PyMem_Free(buffer.data);
815
823
  Py_DECREF(iter_row);
816
824
  return NULL; /* pass the UnicodeEncodeError */
817
825
  }
@@ -820,33 +828,35 @@ conn_inserttable(connObject *self, PyObject *args)
820
828
  PyErr_SetString(
821
829
  PyExc_TypeError,
822
830
  "The third argument must contain only strings");
823
- PyMem_Free(buffer);
831
+ PyMem_Free(buffer.data);
824
832
  Py_DECREF(iter_row);
825
833
  return NULL;
826
834
  }
827
835
  PyBytes_AsStringAndSize(obj, &col, &slen);
828
836
  col = PQescapeIdentifier(self->cnx, col, (size_t)slen);
829
837
  Py_DECREF(obj);
830
- if (bufpt < bufmax)
831
- bufpt += snprintf(bufpt, (size_t)(bufmax - bufpt), "%s%s", col,
832
- j == n - 1 ? ")" : ",");
838
+ ext_char_buffer_s(&buffer, col);
833
839
  PQfreemem(col);
840
+ ext_char_buffer_c(&buffer, j == n - 1 ? ')' : ',');
834
841
  }
835
842
  }
836
- if (bufpt < bufmax)
837
- snprintf(bufpt, (size_t)(bufmax - bufpt), " from stdin");
838
- if (bufpt >= bufmax) {
839
- PyMem_Free(buffer);
843
+ ext_char_buffer_s(&buffer, " from stdin");
844
+ if (freeze)
845
+ ext_char_buffer_s(&buffer, " freeze");
846
+ ext_char_buffer_c(&buffer, '\0');
847
+
848
+ if (buffer.error) {
849
+ PyMem_Free(buffer.data);
840
850
  Py_DECREF(iter_row);
841
851
  return PyErr_NoMemory();
842
852
  }
843
853
 
844
854
  Py_BEGIN_ALLOW_THREADS
845
- result = PQexec(self->cnx, buffer);
855
+ result = PQexec(self->cnx, buffer.data);
846
856
  Py_END_ALLOW_THREADS
847
857
 
848
858
  if (!result || PQresultStatus(result) != PGRES_COPY_IN) {
849
- PyMem_Free(buffer);
859
+ PyMem_Free(buffer.data);
850
860
  Py_DECREF(iter_row);
851
861
  PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->cnx));
852
862
  return NULL;
@@ -861,8 +871,7 @@ conn_inserttable(connObject *self, PyObject *args)
861
871
 
862
872
  if (!(PyTuple_Check(columns) || PyList_Check(columns))) {
863
873
  PQputCopyEnd(self->cnx, "Invalid arguments");
864
- PyMem_Free(buffer);
865
- Py_DECREF(columns);
874
+ PyMem_Free(buffer.data);
866
875
  Py_DECREF(columns);
867
876
  Py_DECREF(iter_row);
868
877
  PyErr_SetString(
@@ -877,7 +886,7 @@ conn_inserttable(connObject *self, PyObject *args)
877
886
  }
878
887
  else if (j != n) {
879
888
  PQputCopyEnd(self->cnx, "Invalid arguments");
880
- PyMem_Free(buffer);
889
+ PyMem_Free(buffer.data);
881
890
  Py_DECREF(columns);
882
891
  Py_DECREF(iter_row);
883
892
  PyErr_SetString(
@@ -886,65 +895,54 @@ conn_inserttable(connObject *self, PyObject *args)
886
895
  return NULL;
887
896
  }
888
897
 
889
- /* builds insert line */
890
- bufpt = buffer;
891
- bufsiz = MAX_BUFFER_SIZE - 1;
898
+ /* empty buffer while keeping allocated memory */
899
+ buffer.size = 0;
900
+
901
+ /* build insert line */
892
902
 
893
903
  for (j = 0; j < n; ++j) {
894
- if (j) {
895
- *bufpt++ = '\t';
896
- --bufsiz;
897
- }
904
+ if (j)
905
+ ext_char_buffer_c(&buffer, '\t');
898
906
 
899
907
  item = PySequence_Fast_GET_ITEM(columns, j);
900
908
 
901
909
  /* convert item to string and append to buffer */
902
910
  if (item == Py_None) {
903
- if (bufsiz > 2) {
904
- *bufpt++ = '\\';
905
- *bufpt++ = 'N';
906
- bufsiz -= 2;
907
- }
908
- else
909
- bufsiz = 0;
911
+ ext_char_buffer_c(&buffer, '\\');
912
+ ext_char_buffer_c(&buffer, 'N');
910
913
  }
911
914
  else if (PyBytes_Check(item)) {
912
915
  const char *t = PyBytes_AsString(item);
913
916
 
914
- while (*t && bufsiz) {
917
+ while (*t) {
915
918
  switch (*t) {
916
919
  case '\\':
917
- *bufpt++ = '\\';
918
- if (--bufsiz)
919
- *bufpt++ = '\\';
920
+ ext_char_buffer_c(&buffer, '\\');
921
+ ext_char_buffer_c(&buffer, '\\');
920
922
  break;
921
923
  case '\t':
922
- *bufpt++ = '\\';
923
- if (--bufsiz)
924
- *bufpt++ = 't';
924
+ ext_char_buffer_c(&buffer, '\\');
925
+ ext_char_buffer_c(&buffer, '\t');
925
926
  break;
926
927
  case '\r':
927
- *bufpt++ = '\\';
928
- if (--bufsiz)
929
- *bufpt++ = 'r';
928
+ ext_char_buffer_c(&buffer, '\\');
929
+ ext_char_buffer_c(&buffer, '\r');
930
930
  break;
931
931
  case '\n':
932
- *bufpt++ = '\\';
933
- if (--bufsiz)
934
- *bufpt++ = 'n';
932
+ ext_char_buffer_c(&buffer, '\\');
933
+ ext_char_buffer_c(&buffer, '\n');
935
934
  break;
936
935
  default:
937
- *bufpt++ = *t;
936
+ ext_char_buffer_c(&buffer, *t);
938
937
  }
939
938
  ++t;
940
- --bufsiz;
941
939
  }
942
940
  }
943
941
  else if (PyUnicode_Check(item)) {
944
942
  PyObject *s = get_encoded_string(item, encoding);
945
943
  if (!s) {
946
944
  PQputCopyEnd(self->cnx, "Encoding error");
947
- PyMem_Free(buffer);
945
+ PyMem_Free(buffer.data);
948
946
  Py_DECREF(item);
949
947
  Py_DECREF(columns);
950
948
  Py_DECREF(iter_row);
@@ -953,33 +951,28 @@ conn_inserttable(connObject *self, PyObject *args)
953
951
  else {
954
952
  const char *t = PyBytes_AsString(s);
955
953
 
956
- while (*t && bufsiz) {
954
+ while (*t) {
957
955
  switch (*t) {
958
956
  case '\\':
959
- *bufpt++ = '\\';
960
- if (--bufsiz)
961
- *bufpt++ = '\\';
957
+ ext_char_buffer_c(&buffer, '\\');
958
+ ext_char_buffer_c(&buffer, '\\');
962
959
  break;
963
960
  case '\t':
964
- *bufpt++ = '\\';
965
- if (--bufsiz)
966
- *bufpt++ = 't';
961
+ ext_char_buffer_c(&buffer, '\\');
962
+ ext_char_buffer_c(&buffer, '\t');
967
963
  break;
968
964
  case '\r':
969
- *bufpt++ = '\\';
970
- if (--bufsiz)
971
- *bufpt++ = 'r';
965
+ ext_char_buffer_c(&buffer, '\\');
966
+ ext_char_buffer_c(&buffer, '\r');
972
967
  break;
973
968
  case '\n':
974
- *bufpt++ = '\\';
975
- if (--bufsiz)
976
- *bufpt++ = 'n';
969
+ ext_char_buffer_c(&buffer, '\\');
970
+ ext_char_buffer_c(&buffer, '\n');
977
971
  break;
978
972
  default:
979
- *bufpt++ = *t;
973
+ ext_char_buffer_c(&buffer, *t);
980
974
  }
981
975
  ++t;
982
- --bufsiz;
983
976
  }
984
977
  Py_DECREF(s);
985
978
  }
@@ -988,68 +981,59 @@ conn_inserttable(connObject *self, PyObject *args)
988
981
  PyObject *s = PyObject_Str(item);
989
982
  const char *t = PyUnicode_AsUTF8(s);
990
983
 
991
- while (*t && bufsiz) {
992
- *bufpt++ = *t++;
993
- --bufsiz;
994
- }
984
+ ext_char_buffer_s(&buffer, t);
995
985
  Py_DECREF(s);
996
986
  }
997
987
  else {
998
988
  PyObject *s = PyObject_Repr(item);
999
989
  const char *t = PyUnicode_AsUTF8(s);
1000
990
 
1001
- while (*t && bufsiz) {
991
+ while (*t) {
1002
992
  switch (*t) {
1003
993
  case '\\':
1004
- *bufpt++ = '\\';
1005
- if (--bufsiz)
1006
- *bufpt++ = '\\';
994
+ ext_char_buffer_c(&buffer, '\\');
995
+ ext_char_buffer_c(&buffer, '\\');
1007
996
  break;
1008
997
  case '\t':
1009
- *bufpt++ = '\\';
1010
- if (--bufsiz)
1011
- *bufpt++ = 't';
998
+ ext_char_buffer_c(&buffer, '\\');
999
+ ext_char_buffer_c(&buffer, '\t');
1012
1000
  break;
1013
1001
  case '\r':
1014
- *bufpt++ = '\\';
1015
- if (--bufsiz)
1016
- *bufpt++ = 'r';
1002
+ ext_char_buffer_c(&buffer, '\\');
1003
+ ext_char_buffer_c(&buffer, '\r');
1017
1004
  break;
1018
1005
  case '\n':
1019
- *bufpt++ = '\\';
1020
- if (--bufsiz)
1021
- *bufpt++ = 'n';
1006
+ ext_char_buffer_c(&buffer, '\\');
1007
+ ext_char_buffer_c(&buffer, '\n');
1022
1008
  break;
1023
1009
  default:
1024
- *bufpt++ = *t;
1010
+ ext_char_buffer_c(&buffer, *t);
1025
1011
  }
1026
1012
  ++t;
1027
- --bufsiz;
1028
1013
  }
1029
1014
  Py_DECREF(s);
1030
1015
  }
1031
-
1032
- if (bufsiz <= 0) {
1033
- PQputCopyEnd(self->cnx, "Memory error");
1034
- PyMem_Free(buffer);
1035
- Py_DECREF(columns);
1036
- Py_DECREF(iter_row);
1037
- return PyErr_NoMemory();
1038
- }
1039
1016
  }
1040
1017
 
1041
1018
  Py_DECREF(columns);
1042
1019
 
1043
- *bufpt++ = '\n';
1020
+ /* terminate line */
1021
+ ext_char_buffer_c(&buffer, '\n');
1022
+ if (buffer.error) {
1023
+ PQputCopyEnd(self->cnx, "Memory error");
1024
+ PyMem_Free(buffer.data);
1025
+ Py_DECREF(iter_row);
1026
+ return PyErr_NoMemory();
1027
+ }
1044
1028
 
1045
- /* sends data */
1046
- ret = PQputCopyData(self->cnx, buffer, (int)(bufpt - buffer));
1029
+ /* send data */
1030
+ ret = PQputCopyData(self->cnx, buffer.data, (int)buffer.size);
1047
1031
  if (ret != 1) {
1048
1032
  char *errormsg = ret == -1 ? PQerrorMessage(self->cnx)
1049
1033
  : "Data cannot be queued";
1050
1034
  PyErr_SetString(PyExc_IOError, errormsg);
1051
1035
  PQputCopyEnd(self->cnx, errormsg);
1052
- PyMem_Free(buffer);
1036
+ PyMem_Free(buffer.data);
1053
1037
  Py_DECREF(iter_row);
1054
1038
  return NULL;
1055
1039
  }
@@ -1057,7 +1041,7 @@ conn_inserttable(connObject *self, PyObject *args)
1057
1041
 
1058
1042
  Py_DECREF(iter_row);
1059
1043
  if (PyErr_Occurred()) {
1060
- PyMem_Free(buffer);
1044
+ PyMem_Free(buffer.data);
1061
1045
  return NULL; /* pass the iteration error */
1062
1046
  }
1063
1047
 
@@ -1065,11 +1049,11 @@ conn_inserttable(connObject *self, PyObject *args)
1065
1049
  if (ret != 1) {
1066
1050
  PyErr_SetString(PyExc_IOError, ret == -1 ? PQerrorMessage(self->cnx)
1067
1051
  : "Data cannot be queued");
1068
- PyMem_Free(buffer);
1052
+ PyMem_Free(buffer.data);
1069
1053
  return NULL;
1070
1054
  }
1071
1055
 
1072
- PyMem_Free(buffer);
1056
+ PyMem_Free(buffer.data);
1073
1057
 
1074
1058
  Py_BEGIN_ALLOW_THREADS
1075
1059
  result = PQgetResult(self->cnx);
@@ -1753,8 +1737,8 @@ static struct PyMethodDef conn_methods[] = {
1753
1737
  conn_set_notice_receiver__doc__},
1754
1738
  {"getnotify", (PyCFunction)conn_get_notify, METH_NOARGS,
1755
1739
  conn_get_notify__doc__},
1756
- {"inserttable", (PyCFunction)conn_inserttable, METH_VARARGS,
1757
- conn_inserttable__doc__},
1740
+ {"inserttable", (PyCFunction)conn_inserttable,
1741
+ METH_VARARGS | METH_KEYWORDS, conn_inserttable__doc__},
1758
1742
  {"transaction", (PyCFunction)conn_transaction, METH_NOARGS,
1759
1743
  conn_transaction__doc__},
1760
1744
  {"parameter", (PyCFunction)conn_parameter, METH_VARARGS,
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Internal functions - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -1493,3 +1493,72 @@ notice_receiver(void *arg, const PGresult *res)
1493
1493
  }
1494
1494
  PyGILState_Release(gstate);
1495
1495
  }
1496
+
1497
+ /* Pre-allocate some memory for a char buffer and return success status. */
1498
+ static int
1499
+ init_char_buffer(struct CharBuffer *buf, size_t initial_size)
1500
+ {
1501
+ buf->size = 0;
1502
+ buf->data = PyMem_Malloc(initial_size);
1503
+ if (buf->data) {
1504
+ buf->max_size = initial_size;
1505
+ buf->error = 0;
1506
+ }
1507
+ else {
1508
+ buf->max_size = 0;
1509
+ buf->error = 1;
1510
+ }
1511
+ return !buf->error;
1512
+ }
1513
+
1514
+ /* Extend char buffer with given string.
1515
+ Note: We do not assume or guarantee that the buffer is zero-terminated. */
1516
+ static void
1517
+ ext_char_buffer_s(struct CharBuffer *buf, const char *s)
1518
+ {
1519
+ size_t len = strlen(s), need;
1520
+
1521
+ if (!len || buf->error)
1522
+ return;
1523
+
1524
+ if ((need = buf->size + len) >= buf->max_size) {
1525
+ void *tmp;
1526
+
1527
+ if (buf->max_size < 1024 * 1024) {
1528
+ /* allocate powers of two unless it's large */
1529
+ size_t double_size = 2 * buf->max_size;
1530
+ if (double_size >= need) /* overflow check */
1531
+ need = double_size;
1532
+ }
1533
+
1534
+ tmp = PyMem_Realloc(buf->data, need);
1535
+ if (!tmp) {
1536
+ buf->error = 1;
1537
+ return;
1538
+ }
1539
+
1540
+ buf->data = tmp;
1541
+ buf->max_size = need;
1542
+ }
1543
+
1544
+ memcpy(buf->data + buf->size, s, len);
1545
+ buf->size += len;
1546
+ }
1547
+
1548
+ /* Extend char buffer with given character */
1549
+ static void
1550
+ ext_char_buffer_c(struct CharBuffer *buf, char c)
1551
+ {
1552
+ if (buf->error)
1553
+ return;
1554
+
1555
+ if (buf->size >= buf->max_size) { /* buffer is full? */
1556
+ /* slow path dealing with reallocation */
1557
+ char tmp[2] = {c ? c : '\n', '\0'}; /* allow adding a zero-byte */
1558
+ ext_char_buffer_s(buf, tmp);
1559
+ if (!c)
1560
+ buf->data[buf->size - 1] = '\0'; /* fix zero-byte */
1561
+ }
1562
+ else
1563
+ buf->data[buf->size++] = c;
1564
+ }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Large object support - this file is part a of the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * This is the main file for the C extension module.
5
5
  *
6
- * Copyright (c) 2025 by the PyGreSQL Development Team
6
+ * Copyright (c) 2026 by the PyGreSQL Development Team
7
7
  *
8
8
  * Please see the LICENSE.TXT file for specific restrictions.
9
9
  */
@@ -53,8 +53,7 @@ static const char *PyPgVersion = TOSTRING(PYGRESQL_VERSION);
53
53
  #define QUERY_MOVENEXT 3
54
54
  #define QUERY_MOVEPREV 4
55
55
 
56
- #define MAX_BUFFER_SIZE 65536 /* maximum transaction size */
57
- #define MAX_ARRAY_DEPTH 16 /* maximum allowed depth of an array */
56
+ #define MAX_ARRAY_DEPTH 16 /* maximum allowed depth of an array */
58
57
 
59
58
  /* MODULE GLOBAL VARIABLES */
60
59
 
@@ -158,6 +157,18 @@ typedef struct {
158
157
  } largeObject;
159
158
  #define is_largeObject(v) (PyType(v) == &largeType)
160
159
 
160
+ /*
161
+ A buffer for character data with routines to handle resizing.
162
+ This is inspired by libpq's PQExpBufferData.
163
+ The buffer can be extended with the extend_char_buffer_s/x() functions.
164
+ */
165
+ struct CharBuffer {
166
+ char *data; /* actual string data */
167
+ size_t size; /* current size of data */
168
+ size_t max_size; /* allocated size */
169
+ int error; /* error flag (invalid data) */
170
+ };
171
+
161
172
  /* Internal functions */
162
173
  #include "pginternal.c"
163
174