From dab41c699a3e20a3577ad52529d879741185df13 Mon Sep 17 00:00:00 2001 From: Oleksandr Shulgin Date: Thu, 2 Jul 2015 14:34:09 +0200 Subject: [PATCH] Fix PQconsumeInput usage. Only call when no data is available in the internal buffer. --- psycopg/pqpath.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 04789d35..ed8b37f3 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -1542,14 +1542,19 @@ pq_read_replication_message(cursorObject *curs, int decode) Dprintf("pq_read_replication_message(decode=%d)", decode); retry: - if (!PQconsumeInput(curs->conn->pgconn)) { - goto none; - } - Py_BEGIN_ALLOW_THREADS; len = PQgetCopyData(curs->conn->pgconn, &buffer, 1 /* async */); Py_END_ALLOW_THREADS; + if (len == 0) { + /* We should only try reading more data into the internal buffer when + * there is nothing available at the moment. Otherwise, with a really + * highly loaded server we might be reading a number of messages for + * every single one we process, thus overgrowing the internal buffer + * until the system runs out of memory. */ + if (PQconsumeInput(curs->conn->pgconn)) { + goto retry; + } goto none; }