From 913ddbc5cc5fcc0cf953dde8358abc132c891c97 Mon Sep 17 00:00:00 2001 From: Tanzeel Khan <140405735+tanscorpio7@users.noreply.github.com> Date: Mon, 6 Jan 2025 17:02:58 +0530 Subject: [PATCH 1/2] Sessions terminated using kill command still leave behind entries in dm_exec_sessions (#3350) https://github.com/babelfish-for-postgresql/babelfish_extensions/pull/3329 was not enough to fix abandoned rows in sys.dm_exec_sessions. Sessions terminated using kill command still leave behind an entry in sys.dm_exec_sessions. Root cause was not executing the shmem exit hook when session terminated because of SIGTERM. As a fix make `tds_stats_shmem_shutdown` completely similar to `pgstat_beshutdown_hook`. Task: [BABEL-5414] Signed-off-by: Tanzeel Khan --- contrib/babelfishpg_tds/src/backend/tds/tds.c | 10 ++++------ test/JDBC/expected/kill-vu-verify.out | 13 +++++++++++++ test/JDBC/expected/single_db/kill-vu-verify.out | 13 +++++++++++++ test/JDBC/input/kill-vu-verify.mix | 10 +++++++++- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/contrib/babelfishpg_tds/src/backend/tds/tds.c b/contrib/babelfishpg_tds/src/backend/tds/tds.c index 3bbe8f70e82..260e3b8abce 100644 --- a/contrib/babelfishpg_tds/src/backend/tds/tds.c +++ b/contrib/babelfishpg_tds/src/backend/tds/tds.c @@ -429,19 +429,17 @@ tds_status_shmem_startup(void) static void tds_stats_shmem_shutdown(int code, Datum arg) { - /* Don't try to save the outlines during a crash. */ - if (code) - return; + volatile TdsStatus *myTdsStatusEntry = MyTdsStatusEntry; /* Safety check ... shouldn't get here unless shmem is set up. */ if (TdsStatusArray == NULL || MyTdsStatusEntry == NULL) return; - PGSTAT_BEGIN_WRITE_ACTIVITY(MyTdsStatusEntry); + PGSTAT_BEGIN_WRITE_ACTIVITY(myTdsStatusEntry); - MyTdsStatusEntry->st_procpid = 0; /* mark invalid */ + myTdsStatusEntry->st_procpid = 0; /* mark invalid */ - PGSTAT_END_WRITE_ACTIVITY(MyTdsStatusEntry); + PGSTAT_END_WRITE_ACTIVITY(myTdsStatusEntry); MyTdsStatusEntry = NULL; diff --git a/test/JDBC/expected/kill-vu-verify.out b/test/JDBC/expected/kill-vu-verify.out index ec919836f76..15fa3ad64a9 100644 --- a/test/JDBC/expected/kill-vu-verify.out +++ b/test/JDBC/expected/kill-vu-verify.out @@ -3,6 +3,8 @@ create table tab_kill_spid(spid int) go create login victim_user_tds with password = '12345678'; go +select count(*) as count into #kill_temp_table from sys.dm_exec_sessions +go -- tsql user=victim_user_tds password=12345678 select 1 @@ -763,3 +765,14 @@ int 0 ~~END~~ + +IF ((SELECT COUNT(*) FROM sys.dm_exec_sessions) = (SELECT count FROM #kill_temp_table)) + SELECT 'SUCESS'; +ELSE + SELECT 'FAILURE expected count ==> ', (SELECT count FROM #kill_temp_table), ' actual count ==> ', count(*) from sys.dm_exec_sessions b; +GO +~~START~~ +varchar +SUCESS +~~END~~ + diff --git a/test/JDBC/expected/single_db/kill-vu-verify.out b/test/JDBC/expected/single_db/kill-vu-verify.out index f88e73fb6a5..799bd24059d 100644 --- a/test/JDBC/expected/single_db/kill-vu-verify.out +++ b/test/JDBC/expected/single_db/kill-vu-verify.out @@ -3,6 +3,8 @@ create table tab_kill_spid(spid int) go create login victim_user_tds with password = '12345678'; go +select count(*) as count into #kill_temp_table from sys.dm_exec_sessions +go -- tsql user=victim_user_tds password=12345678 select 1 @@ -863,3 +865,14 @@ int 0 ~~END~~ + +IF ((SELECT COUNT(*) FROM sys.dm_exec_sessions) = (SELECT count FROM #kill_temp_table)) + SELECT 'SUCESS'; +ELSE + SELECT 'FAILURE expected count ==> ', (SELECT count FROM #kill_temp_table), ' actual count ==> ', count(*) from sys.dm_exec_sessions b; +GO +~~START~~ +varchar +SUCESS +~~END~~ + diff --git a/test/JDBC/input/kill-vu-verify.mix b/test/JDBC/input/kill-vu-verify.mix index 784f86c86ff..9db55855e94 100644 --- a/test/JDBC/input/kill-vu-verify.mix +++ b/test/JDBC/input/kill-vu-verify.mix @@ -4,6 +4,8 @@ create table tab_kill_spid(spid int) go create login victim_user_tds with password = '12345678'; go +select count(*) as count into #kill_temp_table from sys.dm_exec_sessions +go -- tsql user=victim_user_tds password=12345678 select 1 @@ -403,4 +405,10 @@ SELECT count(*) from sys.dm_exec_sessions where login_name = 'test_kill' GO SELECT COUNT(*) from pg_stat_activity where usename = 'test_kill' -GO \ No newline at end of file +GO + +IF ((SELECT COUNT(*) FROM sys.dm_exec_sessions) = (SELECT count FROM #kill_temp_table)) + SELECT 'SUCESS'; +ELSE + SELECT 'FAILURE expected count ==> ', (SELECT count FROM #kill_temp_table), ' actual count ==> ', count(*) from sys.dm_exec_sessions b; +GO From 3c5e6f60476baa127771f33fe75497c231e1f970 Mon Sep 17 00:00:00 2001 From: Tanzeel Khan Date: Fri, 10 Jan 2025 11:46:41 +0000 Subject: [PATCH 2/2] empty commit Signed-off-by: Tanzeel Khan