+static void php_pqconn_wakeup(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC)
+{
+}
+
+static int apply_unlisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+{
+ php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *);
+ char *quoted_channel = PQescapeIdentifier(obj->intern->conn, key->arKey, key->nKeyLength - 1);
+
+ if (quoted_channel) {
+ PGresult *res;
+ char *cmd;
+
+ spprintf(&cmd, 0, "UNLISTEN %s", quoted_channel);
+ if ((res = PQexec(obj->intern->conn, cmd))) {
+ PHP_PQclear(res);
+ }
+
+ efree(cmd);
+ PQfreemem(quoted_channel);
+ }
+
+ return ZEND_HASH_APPLY_REMOVE;
+}
+
+static void php_pqconn_notice_ignore(void *p, const PGresult *res)
+{
+}
+
+static void php_pqconn_retire(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC)
+{
+ php_pqconn_event_data_t *evdata = PQinstanceData(*handle, php_pqconn_event);
+ PGresult *res;
+
+ /* go away */
+ PQsetInstanceData(*handle, php_pqconn_event, NULL);
+
+ /* ignore notices */
+ PQsetNoticeReceiver(*handle, php_pqconn_notice_ignore, NULL);
+
+ /* clean up async results */
+ while ((res = PQgetResult(*handle))) {
+ PHP_PQclear(res);
+ }
+
+ /* clean up transaction & session */
+ switch (PQtransactionStatus(*handle)) {
+ case PQTRANS_IDLE:
+ res = PQexec(*handle, "RESET ALL");
+ break;
+ default:
+ res = PQexec(*handle, "ROLLBACK; RESET ALL");
+ break;
+ }
+
+ if (res) {
+ PHP_PQclear(res);
+ }
+
+ if (evdata) {
+ /* clean up notify listeners */
+ zend_hash_apply_with_arguments(&evdata->obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, evdata->obj);
+
+ /* release instance data */
+ memset(evdata, 0, sizeof(*evdata));
+ efree(evdata);
+ }
+}
+