diff -durpN postgresql.3/doc/src/sgml/libpq.sgml postgresql.4/doc/src/sgml/libpq.sgml
--- postgresql.3/doc/src/sgml/libpq.sgml 2012-11-30 09:29:49.703286090 +0100
+++ postgresql.4/doc/src/sgml/libpq.sgml 2013-01-02 16:54:43.783565292 +0100
@@ -588,6 +588,27 @@ void PQfinish(PGconn *conn);
+
+ PQfinishSafePQfinishSafe>>
+
+
+ A macro that calls PQfinish and sets
+ the pointer to NULL afterwards.
+
+#define PQfinishSafe(conn) do { PQfinish(conn); conn = NULL; } while (0)
+
+
+
+
+ The PGconn> pointer (being NULL) is safe to use
+ after this macro. Every function that deals with a
+ PGconn> pointer considers a non-NULL pointer as valid.
+ Using a stale pointer may result in a crash. Setting the pointer
+ to NULL makes calling such functions a no-op instead.
+
+
+
+
PQresetPQreset>>
@@ -2753,6 +2774,29 @@ void PQclear(PGresult *res);
+
+
+ PQclearSafePQclearSafe>>
+
+
+ A macro to call PQclear and set
+ the pointer to NULL afterwards.
+
+#define PQclearSafe(res) do { PQclear(res); res = NULL; } while (0)
+
+
+
+
+ Calling PQclear leaves a stale
+ PGresult pointer. Calling
+ functions that deal with PGresult
+ objects afterwards may result in a crash. Setting the
+ PGresult pointer to NULL makes
+ calling such functions a no-op instead.
+
+
+
+
diff -durpN postgresql.3/src/interfaces/libpq/libpq-fe.h postgresql.4/src/interfaces/libpq/libpq-fe.h
--- postgresql.3/src/interfaces/libpq/libpq-fe.h 2013-01-02 09:19:03.929522614 +0100
+++ postgresql.4/src/interfaces/libpq/libpq-fe.h 2013-01-02 16:52:41.236683245 +0100
@@ -256,6 +256,9 @@ extern PGconn *PQsetdbLogin(const char *
/* close the current connection and free the PGconn data structure */
extern void PQfinish(PGconn *conn);
+/* macro to close the current connection and set the conn pointer to NULL */
+#define PQfinishSafe(conn) do { PQfinish(conn); conn = NULL; } while (0)
+
/* get info about connection options known to PQconnectdb */
extern PQconninfoOption *PQconndefaults(void);
@@ -472,6 +475,9 @@ extern int PQsendDescribePortal(PGconn *
/* Delete a PGresult */
extern void PQclear(PGresult *res);
+/* Macro to delete a PGresult and set the res pointer to NULL */
+#define PQclearSafe(res) do { PQclear(res); res = NULL; } while (0)
+
/* For freeing other alloc'd results, such as PGnotify structs */
extern void PQfreemem(void *ptr);