Skip to content

Commit ff0c519

Browse files
authored
Merge pull request #403 from cybertec-postgresql/394_started_at_columns
[+] add `started_at` column to `active_session` and `active_chain tables`, closes #394
2 parents 427dd1c + f6771ef commit ff0c519

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

internal/pgengine/migration.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ var Migrations func() migrator.Option = func() migrator.Option {
9696
return ExecuteMigrationScript(ctx, tx, "00381.sql")
9797
},
9898
},
99+
&migrator.Migration{
100+
Name: "00394 Add started_at column to active_session and active_chain tables",
101+
Func: func(ctx context.Context, tx pgx.Tx) error {
102+
return ExecuteMigrationScript(ctx, tx, "00394.sql")
103+
},
104+
},
99105
// adding new migration here, update "timetable"."migration" in "sql/ddl.sql"
100106
// and "dbapi" variable in main.go!
101107

internal/pgengine/sql/ddl.sql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ VALUES
1818
(2, '00323 Append timetable.delete_job function'),
1919
(3, '00329 Migration required for some new added functions'),
2020
(4, '00334 Refactor timetable.task as plain schema without tree-like dependencies'),
21-
(5, '00381 Rewrite active chain handling');
21+
(5, '00381 Rewrite active chain handling'),
22+
(6, '00394 Add started_at column to active_session and active_chain tables');
2223

2324
CREATE DOMAIN timetable.cron AS TEXT CHECK(
2425
substr(VALUE, 1, 6) IN ('@every', '@after') AND (substr(VALUE, 7) :: INTERVAL) IS NOT NULL
@@ -105,7 +106,8 @@ COMMENT ON TABLE timetable.parameter IS
105106
CREATE UNLOGGED TABLE timetable.active_session(
106107
client_pid BIGINT NOT NULL,
107108
client_name TEXT NOT NULL,
108-
server_pid BIGINT NOT NULL
109+
server_pid BIGINT NOT NULL,
110+
started_at TIMESTAMPTZ DEFAULT now()
109111
);
110112

111113
COMMENT ON TABLE timetable.active_session IS
@@ -150,7 +152,8 @@ COMMENT ON TABLE timetable.execution_log IS
150152

151153
CREATE UNLOGGED TABLE timetable.active_chain(
152154
chain_id BIGINT NOT NULL,
153-
client_name TEXT NOT NULL
155+
client_name TEXT NOT NULL,
156+
started_at TIMESTAMPTZ DEFAULT now()
154157
);
155158

156159
COMMENT ON TABLE timetable.active_chain IS
@@ -189,7 +192,7 @@ BEGIN
189192
RETURN FALSE;
190193
END IF;
191194
-- insert current session information
192-
INSERT INTO timetable.active_session VALUES (worker_pid, worker_name, pg_backend_pid());
195+
INSERT INTO timetable.active_session(client_pid, client_name, server_pid) VALUES (worker_pid, worker_name, pg_backend_pid());
193196
RETURN TRUE;
194197
END;
195198
$CODE$
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
ALTER TABLE timetable.active_session
2+
ADD COLUMN started_at TIMESTAMPTZ DEFAULT now();
3+
4+
ALTER TABLE timetable.active_chain
5+
ADD COLUMN started_at TIMESTAMPTZ DEFAULT now();
6+
7+
CREATE OR REPLACE FUNCTION timetable.try_lock_client_name(worker_pid BIGINT, worker_name TEXT)
8+
RETURNS bool AS
9+
$CODE$
10+
BEGIN
11+
IF pg_is_in_recovery() THEN
12+
RAISE NOTICE 'Cannot obtain lock on a replica. Please, use the primary node';
13+
RETURN FALSE;
14+
END IF;
15+
-- remove disconnected sessions
16+
DELETE
17+
FROM timetable.active_session
18+
WHERE server_pid NOT IN (
19+
SELECT pid
20+
FROM pg_catalog.pg_stat_activity
21+
WHERE application_name = 'pg_timetable'
22+
);
23+
DELETE
24+
FROM timetable.active_chain
25+
WHERE client_name NOT IN (
26+
SELECT client_name FROM timetable.active_session
27+
);
28+
-- check if there any active sessions with the client name but different client pid
29+
PERFORM 1
30+
FROM timetable.active_session s
31+
WHERE
32+
s.client_pid <> worker_pid
33+
AND s.client_name = worker_name
34+
LIMIT 1;
35+
IF FOUND THEN
36+
RAISE NOTICE 'Another client is already connected to server with name: %', worker_name;
37+
RETURN FALSE;
38+
END IF;
39+
-- insert current session information
40+
INSERT INTO timetable.active_session(client_pid, client_name, server_pid) VALUES (worker_pid, worker_name, pg_backend_pid());
41+
RETURN TRUE;
42+
END;
43+
$CODE$
44+
STRICT
45+
LANGUAGE plpgsql;

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var (
4747
commit string = "000000"
4848
version string = "master"
4949
date string = "unknown"
50-
dbapi string = "00381"
50+
dbapi string = "00394"
5151
)
5252

5353
func printVersion() {

0 commit comments

Comments
 (0)