From 2d99d49872b90210eb0158d8c1a18fda2937fb53 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Thu, 2 Aug 2012 14:28:51 +0000 Subject: [PATCH] Wonky hack to print stats on every backend exit. --- contrib/hashtest/hashtest--1.0.sql | 5 --- contrib/hashtest/hashtest.c | 71 +++++++++++++++++------------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/contrib/hashtest/hashtest--1.0.sql b/contrib/hashtest/hashtest--1.0.sql index c787532e73..e271baff0f 100644 --- a/contrib/hashtest/hashtest--1.0.sql +++ b/contrib/hashtest/hashtest--1.0.sql @@ -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' diff --git a/contrib/hashtest/hashtest.c b/contrib/hashtest/hashtest.c index 554457df0e..9288708ea4 100644 --- a/contrib/hashtest/hashtest.c +++ b/contrib/hashtest/hashtest.c @@ -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) { -- 2.39.5