On Wed, May 15, 2019 at 08:44:49AM -0500, Justin Pryzby wrote:
> Also, as of PG12 (unreleased), there'll be this new interface:
> https://www.postgresql.org/docs/devel/release-12.html#id-1.3.7
> |Add libpq function to report the memory size of the query result (Lars 
> Kanis, Tom Lane)
> |The function is PQresultMemorySize(). 
> 
> I'd propose to include that with the first release following PG12...although I
> think you'd have to check PG_VERSION_NUM or similar, which I see isn't
> currently being done.

Find attached minimal patch.

$ PYTHONPATH=build/lib.linux-x86_64-2.7/ python2.7 -c "import pg; print 
pg.DB('postgres').query('SELECT generate_series(1,9999)').memsize()"
372952
Index: pgmodule.c
===================================================================
--- pgmodule.c	(revision 1019)
+++ pgmodule.c	(working copy)
@@ -17,6 +17,7 @@
 
 #include <libpq-fe.h>
 #include <libpq/libpq-fs.h>
+#include <pg_config.h>
 
 /* The type definitions from <server/catalog/pg_type.h> */
 #include "pgtypes.h"
 
Index: pgquery.c
===================================================================
--- pgquery.c	(revision 1019)
+++ pgquery.c	(working copy)
@@ -11,6 +11,20 @@
  *
  */
 
+static char query_memsize__doc__[] =
+"memsize() -- return number of bytes allocated by query";
+static PyObject *
+query_memsize(queryObject *self)
+{
+#if PG_VERSION_NUM >= 120000 /* Compile time version */
+    if (PQlibVersion() >= 120000) /* Runtime version */
+        return PyLong_FromSize_t(PQresultMemorySize(self->result));
+    else
+#endif
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
 /* Deallocate the query object. */
 static void
 query_dealloc(queryObject *self)
@@ -707,13 +788,21 @@
         METH_VARARGS, query_fieldnum__doc__},
     {"listfields", (PyCFunction) query_listfields,
         METH_NOARGS, query_listfields__doc__},
     {"ntuples", (PyCFunction) query_ntuples,
         METH_NOARGS, query_ntuples__doc__},
+    {"memsize", (PyCFunction) query_memsize,
+        METH_NOARGS, query_memsize__doc__},
     {NULL, NULL}
 };
 
 static char query__doc__[] = "PyGreSQL query object";
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo/pygresql

Reply via email to