-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_timescaledb_catalog_publication.sql
66 lines (58 loc) · 1.82 KB
/
create_timescaledb_catalog_publication.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
CREATE OR REPLACE FUNCTION create_timescaledb_catalog_publication(
publication_name text,
replication_user text
)
RETURNS bool
LANGUAGE plpgsql
SECURITY DEFINER
AS
$$
DECLARE
found bool;
owner oid;
BEGIN
SELECT true, pubowner
FROM pg_catalog.pg_publication
WHERE pubname = publication_name
INTO found, owner;
IF found THEN
SELECT true
FROM pg_catalog.pg_publication_tables
WHERE pubname = publication_name
AND schemaname = '_timescaledb_catalog'
AND tablename = 'hypertable'
INTO found;
IF NOT found THEN
RAISE EXCEPTION
'Publication % already exists but is missing _timescaledb_catalog.hypertable', publication_name;
END IF;
SELECT true
FROM pg_catalog.pg_publication_tables
WHERE pubname = publication_name
AND schemaname = '_timescaledb_catalog'
AND tablename = 'chunk'
INTO found;
IF NOT found THEN
RAISE EXCEPTION
'Publication % already exists but is missing _timescaledb_catalog.chunk', publication_name;
END IF;
SELECT true
FROM (SELECT session_user as username) s
LEFT JOIN pg_catalog.pg_user u ON u.usename = s.username
WHERE u.usesysid = owner
INTO found;
IF NOT found THEN
RAISE EXCEPTION
'Publication % already exists but is not owned by the session user', publication_name;
END IF;
RETURN true;
END IF;
EXECUTE
format(
'CREATE PUBLICATION %I FOR TABLE _timescaledb_catalog.chunk, _timescaledb_catalog.hypertable',
publication_name
);
EXECUTE format('ALTER PUBLICATION %I OWNER TO %s', publication_name, replication_user);
RETURN true;
END;
$$;