Attached is a patch to assert there is no duplicated exit callbacks.

Along the way, I downgrade the runtime "enough room" check to an
assertion: the callbacks are registered in pretty fixed initialization
code path, thus assertion is good enough to prevent overlook there. If
we don't agree with this, then the duplication check shall also bump
to runtime checks.

Regards,
Qingqing
diff --git a/src/backend/storage/ipc/ipc.c b/src/backend/storage/ipc/ipc.c
index 6bc0b06..bfc7764 100644
--- a/src/backend/storage/ipc/ipc.c
+++ b/src/backend/storage/ipc/ipc.c
@@ -291,10 +291,12 @@ atexit_callback(void)
 void
 on_proc_exit(pg_on_exit_callback function, Datum arg)
 {
-       if (on_proc_exit_index >= MAX_ON_EXITS)
-               ereport(FATAL,
-                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                                errmsg_internal("out of on_proc_exit slots")));
+       int     i;
+       
+       /* check no duplicates and there are rooms */
+       for (i = 0; i < on_proc_exit_index; i++)
+               Assert (on_proc_exit_list[i] != function);
+       Assert (on_proc_exit_index < MAX_ON_EXITS);
 
        on_proc_exit_list[on_proc_exit_index].function = function;
        on_proc_exit_list[on_proc_exit_index].arg = arg;
@@ -319,10 +321,12 @@ on_proc_exit(pg_on_exit_callback function, Datum arg)
 void
 before_shmem_exit(pg_on_exit_callback function, Datum arg)
 {
-       if (before_shmem_exit_index >= MAX_ON_EXITS)
-               ereport(FATAL,
-                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                                errmsg_internal("out of before_shmem_exit 
slots")));
+       int             i;
+       
+       /* check no duplicated callbacks and there are rooms */
+       for (i = 0; i < before_shmem_exit_index; i++)
+               Assert (before_shmem_exit_list[i] != function);
+       Assert (before_shmem_exit_index < MAX_ON_EXITS);
 
        before_shmem_exit_list[before_shmem_exit_index].function = function;
        before_shmem_exit_list[before_shmem_exit_index].arg = arg;
@@ -347,10 +351,12 @@ before_shmem_exit(pg_on_exit_callback function, Datum arg)
 void
 on_shmem_exit(pg_on_exit_callback function, Datum arg)
 {
-       if (on_shmem_exit_index >= MAX_ON_EXITS)
-               ereport(FATAL,
-                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-                                errmsg_internal("out of on_shmem_exit 
slots")));
+       int     i;
+       
+       /* check no duplicates and there are rooms */
+       for (i = 0; i < on_shmem_exit_index; i++)
+               Assert (on_shmem_exit_list[i] != function);
+       Assert (on_shmem_exit_index < MAX_ON_EXITS);
 
        on_shmem_exit_list[on_shmem_exit_index].function = function;
        on_shmem_exit_list[on_shmem_exit_index].arg = arg;
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to