CVS User Account cvsuser
Thu Sep 23 18:28:35 PDT 2004
Log Message:
-----------
Added function cleanupListener(), which is called at slon startup
and removes all stale entries from pg_listener.

Jan

Tags:
----
REL_1_0_STABLE

Modified Files:
--------------
    slony1-engine/src/backend:
        slony1_funcs.c (r1.17.2.1 -> r1.17.2.2)
        slony1_funcs.sql (r1.15.2.1 -> r1.15.2.2)
    slony1-engine/src/slon:
        local_listen.c (r1.23 -> r1.23.2.1)

-------------- next part --------------
Index: slony1_funcs.sql
===================================================================
RCS file: /usr/local/cvsroot/slony1/slony1-engine/src/backend/slony1_funcs.sql,v
retrieving revision 1.15.2.1
retrieving revision 1.15.2.2
diff -Lsrc/backend/slony1_funcs.sql -Lsrc/backend/slony1_funcs.sql -u -w -r1.15.2.1 -r1.15.2.2
--- src/backend/slony1_funcs.sql
+++ src/backend/slony1_funcs.sql
@@ -159,6 +159,16 @@
 	language C;
 
 
+-- ----------------------------------------------------------------------
+-- FUNCTION cleanupListener ()
+--
+--	
+-- ----------------------------------------------------------------------
+create or replace function @NAMESPACE at .cleanupListener () returns int4
+    as '$libdir/slony1_funcs', '_Slony_I_cleanupListener'
+	language C;
+
+
 -- **********************************************************************
 -- * PL/pgSQL functions for administrative tasks
 -- **********************************************************************
Index: slony1_funcs.c
===================================================================
RCS file: /usr/local/cvsroot/slony1/slony1-engine/src/backend/slony1_funcs.c,v
retrieving revision 1.17.2.1
retrieving revision 1.17.2.2
diff -Lsrc/backend/slony1_funcs.c -Lsrc/backend/slony1_funcs.c -u -w -r1.17.2.1 -r1.17.2.2
--- src/backend/slony1_funcs.c
+++ src/backend/slony1_funcs.c
@@ -18,6 +18,7 @@
 #include "parser/parse_type.h"
 #include "executor/spi.h"
 #include "commands/trigger.h"
+#include "commands/async.h"
 #include "access/xact.h"
 #include "utils/builtins.h"
 #ifdef HAVE_TYPCACHE
@@ -37,6 +38,7 @@
 PG_FUNCTION_INFO_V1(_Slony_I_denyAccess);
 PG_FUNCTION_INFO_V1(_Slony_I_lockedSet);
 PG_FUNCTION_INFO_V1(_Slony_I_terminateNodeConnections);
+PG_FUNCTION_INFO_V1(_Slony_I_cleanupListener);
 
 Datum           _Slony_I_createEvent(PG_FUNCTION_ARGS);
 Datum           _Slony_I_getLocalNodeId(PG_FUNCTION_ARGS);
@@ -47,6 +49,7 @@
 Datum           _Slony_I_denyAccess(PG_FUNCTION_ARGS);
 Datum           _Slony_I_lockedSet(PG_FUNCTION_ARGS);
 Datum           _Slony_I_terminateNodeConnections(PG_FUNCTION_ARGS);
+Datum           _Slony_I_cleanupListener(PG_FUNCTION_ARGS);
 
 
 #define PLAN_NONE			0
@@ -956,6 +959,55 @@
 }
 
 
+Datum
+_Slony_I_cleanupListener(PG_FUNCTION_ARGS)
+{
+	static void	   *plan = NULL;
+	int				i;
+	int32			pid;
+	char		   *relname;
+	bool			isnull;
+
+
+	if (SPI_connect() < 0)
+		elog(ERROR, "Slony-I: SPI_connect() failed in cleanupListener()");
+
+	if (plan == NULL)
+	{
+		plan = SPI_saveplan(SPI_prepare("select relname, listenerpid "
+				"    from \"pg_catalog\".pg_listener; ",
+				0, NULL));
+		if (plan == NULL)
+			elog(ERROR, "Slony-I: SPI_prepare() failed in cleanupListener()");
+	}
+
+	if (SPI_execp(plan, NULL, NULL, 0) != SPI_OK_SELECT)
+		elog(ERROR, "Slony-I: SPI_execp() failed in cleanupListener()");
+
+	for (i = 0; i < SPI_processed; i++)
+	{
+		pid = DatumGetInt32(SPI_getbinval(SPI_tuptable->vals[i], 
+				SPI_tuptable->tupdesc, 2, &isnull));
+		if (kill(pid, 0) < 0)
+		{
+			if (errno == ESRCH)
+			{
+				relname = SPI_getvalue(SPI_tuptable->vals[i],
+						SPI_tuptable->tupdesc, 1);
+
+				elog(NOTICE, "Slony-I: removing stale pg_listener entry "
+						"for pid %d, relname %s", pid, relname);
+				Async_Unlisten(relname, pid);
+			}
+		}
+	}
+
+	SPI_finish();
+
+	return (Datum)0;
+}
+
+
 static char *
 slon_quote_literal(char *str)
 {
Index: local_listen.c
===================================================================
RCS file: /usr/local/cvsroot/slony1/slony1-engine/src/slon/local_listen.c,v
retrieving revision 1.23
retrieving revision 1.23.2.1
diff -Lsrc/slon/local_listen.c -Lsrc/slon/local_listen.c -u -w -r1.23 -r1.23.2.1
--- src/slon/local_listen.c
+++ src/slon/local_listen.c
@@ -69,8 +69,10 @@
 	 * Listen for local events
 	 */
 	slon_mkquery(&query1, 
+			"select %s.cleanupListener(); "
 			"listen \"_%s_Event\"; "
 			"listen \"_%s_Restart\"; ",
+			rtcfg_namespace,
 			rtcfg_cluster_name, rtcfg_cluster_name);
 	res = PQexec(dbconn, dstring_data(&query1));
 	if (PQresultStatus(res) != PGRES_COMMAND_OK)


More information about the Slony1-commit mailing list