diff --git a/psycopg/connection.h b/psycopg/connection.h index baed6a2b..90dc1291 100644 --- a/psycopg/connection.h +++ b/psycopg/connection.h @@ -36,6 +36,9 @@ extern "C" { #define CONN_STATUS_SYNC 3 #define CONN_STATUS_ASYNC 4 +/* Hard limit on the notices stored by the Python connection */ +#define CONN_NOTICES_LIMIT 50 + extern PyTypeObject connectionType; typedef struct { diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index 6995451f..fb4018c3 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -45,8 +45,13 @@ conn_notice_callback(void *args, const char *message) if (self->protocol < 3 && strncmp(message, "ERROR", 5) == 0) pq_set_critical(self, message); - else + else { PyList_Append(self->notice_list, PyString_FromString(message)); + + /* Remove the oldest item if the queue is getting too long. */ + if (PyList_GET_SIZE(self->notice_list) > CONN_NOTICES_LIMIT) + PySequence_DelItem(self->notice_list, 0); + } } /* conn_connect - execute a connection to the dataabase */