Wonky hack to print stats on every backend exit.
authorRobert Haas <[email protected]>
Thu, 2 Aug 2012 14:28:51 +0000 (14:28 +0000)
committerRobert Haas <[email protected]>
Mon, 13 Oct 2014 17:14:20 +0000 (13:14 -0400)
contrib/hashtest/hashtest--1.0.sql
contrib/hashtest/hashtest.c

index c787532e739e0ee30771a864386ee173948e4d69..e271baff0f90aa773419590a1447298395930a8a 100644 (file)
@@ -26,11 +26,6 @@ RETURNS void
 AS 'MODULE_PATHNAME', 'chash_collision_test'
 LANGUAGE C;
 
-CREATE FUNCTION chash_write_stats_to_log()
-RETURNS void
-AS 'MODULE_PATHNAME', 'chash_write_stats_to_log'
-LANGUAGE C;
-
 CREATE FUNCTION dynahash_insert_test()
 RETURNS void
 AS 'MODULE_PATHNAME', 'dynahash_insert_test'
index 554457df0e9f4e76591811cb32b2918c3f2a1784..9288708ea4848b64dae84e0a9598a40a13505edd 100644 (file)
@@ -6,6 +6,7 @@
 #include "postgres.h"
 
 #include "funcapi.h"
+#include "libpq/auth.h"
 #include "miscadmin.h"
 #include "portability/instr_time.h"
 #include "storage/ipc.h"
@@ -19,7 +20,6 @@ Datum     chash_search_test(PG_FUNCTION_ARGS);
 Datum      chash_delete_test(PG_FUNCTION_ARGS);
 Datum      chash_concurrent_test(PG_FUNCTION_ARGS);
 Datum      chash_collision_test(PG_FUNCTION_ARGS);
-Datum      chash_write_stats_to_log(PG_FUNCTION_ARGS);
 Datum      dynahash_insert_test(PG_FUNCTION_ARGS);
 Datum      dynahash_search_test(PG_FUNCTION_ARGS);
 Datum      dynahash_delete_test(PG_FUNCTION_ARGS);
@@ -57,6 +57,10 @@ static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
 static CHashTable chash;
 static HTAB *dynahash;
 static LWLockId dynahash_lock[DYNAHASH_PARTITIONS];
+static ClientAuthentication_hook_type original_client_auth_hook = NULL;
+
+static void hashtest_client_auth_hook(Port *port, int status);
+static void chash_write_stats_to_log(int code, Datum dummy);
 
 #define dynahash_get_lock(hashcode) \
    (dynahash_lock[(hashcode) % DYNAHASH_PARTITIONS])
@@ -79,6 +83,42 @@ _PG_init(void)
    elog(LOG, "chash: %u bytes; dynahash: %u bytes", (unsigned) cs,
         (unsigned) ds);
    RequestAddinLWLocks(DYNAHASH_PARTITIONS);
+   original_client_auth_hook = ClientAuthentication_hook;
+   ClientAuthentication_hook = hashtest_client_auth_hook;
+   
+}
+
+static void
+hashtest_client_auth_hook(Port *port, int status)
+{
+   if (original_client_auth_hook)
+       original_client_auth_hook(port, status);
+   on_proc_exit(chash_write_stats_to_log, (Datum) 0);
+}
+
+static void
+chash_write_stats_to_log(int code, Datum dummy)
+{
+   uint64  stats[CHS_NumberOfStatistics];
+   CHashStatisticsType i;
+   StringInfoData  buf;
+
+   CHashStatistics(chash, stats);
+   initStringInfo(&buf);
+
+   for (i = 0; i < CHS_NumberOfStatistics; ++i)
+   {
+       if (stats[i] == 0)
+           continue;
+       appendStringInfo(&buf, UINT64_FORMAT " %s; ", stats[i],
+                        CHashStatisticsNames[i]);
+   }
+
+   if (buf.len > 1)
+   {
+       buf.data[buf.len-2] = '\0';
+       elog(LOG, "chash statistics: %s", buf.data);
+   }
 }
 
 static void
@@ -275,35 +315,6 @@ chash_collision_test(PG_FUNCTION_ARGS)
    PG_RETURN_VOID();
 }
 
-Datum
-chash_write_stats_to_log(PG_FUNCTION_ARGS)
-{
-   uint64  stats[CHS_NumberOfStatistics];
-   CHashStatisticsType i;
-   StringInfoData  buf;
-
-   CHashStatistics(chash, stats);
-   initStringInfo(&buf);
-
-   for (i = 0; i < CHS_NumberOfStatistics; ++i)
-   {
-       if (stats[i] == 0)
-           continue;
-       appendStringInfo(&buf, UINT64_FORMAT " %s; ", stats[i],
-                        CHashStatisticsNames[i]);
-   }
-
-   if (buf.len > 1)
-   {
-       buf.data[buf.len-2] = '\0';
-       elog(LOG, "chash statistics: %s", buf.data);
-   }
-   else
-       elog(LOG, "chash statistics: nothing to report");
-
-   PG_RETURN_VOID();
-}
-
 static bool
 dynahash_insert(uint32 key, uint32 val)
 {