-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from cybertec-postgresql/394_started_at_columns
[+] add `started_at` column to `active_session` and `active_chain tables`, closes #394
- Loading branch information
Showing
4 changed files
with
59 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
ALTER TABLE timetable.active_session | ||
ADD COLUMN started_at TIMESTAMPTZ DEFAULT now(); | ||
|
||
ALTER TABLE timetable.active_chain | ||
ADD COLUMN started_at TIMESTAMPTZ DEFAULT now(); | ||
|
||
CREATE OR REPLACE FUNCTION timetable.try_lock_client_name(worker_pid BIGINT, worker_name TEXT) | ||
RETURNS bool AS | ||
$CODE$ | ||
BEGIN | ||
IF pg_is_in_recovery() THEN | ||
RAISE NOTICE 'Cannot obtain lock on a replica. Please, use the primary node'; | ||
RETURN FALSE; | ||
END IF; | ||
-- remove disconnected sessions | ||
DELETE | ||
FROM timetable.active_session | ||
WHERE server_pid NOT IN ( | ||
SELECT pid | ||
FROM pg_catalog.pg_stat_activity | ||
WHERE application_name = 'pg_timetable' | ||
); | ||
DELETE | ||
FROM timetable.active_chain | ||
WHERE client_name NOT IN ( | ||
SELECT client_name FROM timetable.active_session | ||
); | ||
-- check if there any active sessions with the client name but different client pid | ||
PERFORM 1 | ||
FROM timetable.active_session s | ||
WHERE | ||
s.client_pid <> worker_pid | ||
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()); | ||
RETURN TRUE; | ||
END; | ||
$CODE$ | ||
STRICT | ||
LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters