Skip to content

Commit

Permalink
Sessions terminated using kill command still leave behind entries in …
Browse files Browse the repository at this point in the history
…dm_exec_sessions (#3390)

#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.

Cherry Picked From
#3350

Issues Resolved
[BABEL-5414]

Sign Off
Signed-off-by: Tanzeel Khan tzlkhan@amazon.com
  • Loading branch information
tanscorpio7 authored Jan 10, 2025
1 parent 1924718 commit fb39d3b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
10 changes: 4 additions & 6 deletions contrib/babelfishpg_tds/src/backend/tds/tds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 13 additions & 0 deletions test/JDBC/expected/kill-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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~~

13 changes: 13 additions & 0 deletions test/JDBC/expected/single_db/kill-vu-verify.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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~~

10 changes: 9 additions & 1 deletion test/JDBC/input/kill-vu-verify.mix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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

0 comments on commit fb39d3b

Please sign in to comment.