Skip to content

Commit

Permalink
Update ddl.sql
Browse files Browse the repository at this point in the history
client_pid(worker_pid)

The server running pg_timetable may be different, but coincidentally the pid may be the same.

Duplicate execution can be prevented by adding the client_addr column as shown below.

---------  -------------------------------------------------------------------
SQL> select * from timetable.active_sessions
client_id 
---------  -------------------------------------------------------------------
14	pg_timetable	28417	167.71.214.34	2022-08-07 14:12:24.333 +0900
14	pg_timetable	28418	167.71.214.34	2022-08-07 14:12:24.366 +0900
14	pg_timetable	28422	128.199.132.200	2022-08-07 14:12:29.563 +0900
14	pg_timetable	28423	128.199.132.200	2022-08-07 14:12:29.589 +0900
  • Loading branch information
hellower committed Aug 7, 2022
1 parent 1982b4b commit f982823
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions internal/pgengine/sql/ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ CREATE UNLOGGED TABLE timetable.active_session(
client_pid BIGINT NOT NULL,
client_name TEXT NOT NULL,
server_pid BIGINT NOT NULL,
client_addr INET NOT NULL,
started_at TIMESTAMPTZ DEFAULT now()
);

Expand Down Expand Up @@ -187,15 +188,15 @@ BEGIN
PERFORM 1
FROM timetable.active_session s
WHERE
s.client_pid <> worker_pid
s.client_addr <> inet_client_addr()
AND s.client_name = worker_name
LIMIT 1;
IF FOUND THEN
RAISE NOTICE 'Another client is already connected to server with name: %', worker_name;
RETURN FALSE;
END IF;
-- insert current session information
INSERT INTO timetable.active_session(client_pid, client_name, server_pid) VALUES (worker_pid, worker_name, pg_backend_pid());
INSERT INTO timetable.active_session(client_pid, client_name, server_pid, client_addr) VALUES (worker_pid, worker_name, pg_backend_pid(), inet_client_addr());
RETURN TRUE;
END;
$CODE$
Expand Down

0 comments on commit f982823

Please sign in to comment.