@haathie/pgmb 0.2.15 → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haathie/pgmb",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "PG message broker, with a type-safe typescript client with built-in webhook & SSE support.",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org",
@@ -130,6 +130,8 @@ RETURNS VOID AS $$
130
130
  $$ LANGUAGE sql VOLATILE PARALLEL UNSAFE SECURITY DEFINER
131
131
  SET search_path TO pgmb;
132
132
 
133
+ DROP FUNCTION IF EXISTS maintain_events_table(timestamptz);
134
+
133
135
  CREATE OR REPLACE PROCEDURE maintain_events_table(
134
136
  current_ts timestamptz DEFAULT NOW()
135
137
  ) AS $$
@@ -154,12 +156,10 @@ BEGIN
154
156
  RETURN;
155
157
  END IF;
156
158
 
157
- SELECT cron.schedule(
159
+ PERFORM cron.schedule(
158
160
  'pgmb_maintain_table_partitions',
159
161
  get_config_value('pg_cron_partition_maintenance_cron'),
160
162
  $CMD$ CALL pgmb.maintain_events_table(); $CMD$
161
163
  );
162
164
  END
163
165
  $$;
164
-
165
- DROP FUNCTION IF EXISTS maintain_events_table(timestamptz);
@@ -0,0 +1,57 @@
1
+ SET search_path TO pgmb;
2
+
3
+ CREATE OR REPLACE FUNCTION prepare_poll_for_events_fn(
4
+ sql_statements TEXT[]
5
+ ) RETURNS VOID AS $$
6
+ DECLARE
7
+ tmpl_proc_name constant TEXT :=
8
+ 'poll_for_events_tmpl';
9
+ tmpl_proc_placeholder constant TEXT :=
10
+ 'TRUE -- CONDITIONS_SQL_PLACEHOLDER --';
11
+ condition_sql TEXT;
12
+ proc_src TEXT;
13
+ BEGIN
14
+ IF sql_statements = '{}' THEN
15
+ -- no subscriptions, so just use 'FALSE' to avoid any matches
16
+ sql_statements := ARRAY['FALSE'];
17
+ END IF;
18
+ -- build the condition SQL
19
+ condition_sql := FORMAT(
20
+ '('
21
+ || array_to_string(
22
+ ARRAY(
23
+ SELECT
24
+ '(' || stmt || ') AND s.conditions_sql = %L'
25
+ FROM unnest(sql_statements) AS arr(stmt)
26
+ ),
27
+ ') OR ('
28
+ )
29
+ || ')',
30
+ VARIADIC sql_statements
31
+ );
32
+ condition_sql := FORMAT('/* updated at %s */', NOW()) || condition_sql;
33
+
34
+ -- fetch the source of the template procedure
35
+ select pg_get_functiondef(oid) INTO proc_src
36
+ from pg_proc where proname = tmpl_proc_name and
37
+ pronamespace = 'pgmb'::regnamespace;
38
+ IF proc_src IS NULL THEN
39
+ RAISE EXCEPTION 'Template procedure % not found', tmpl_proc_name;
40
+ END IF;
41
+
42
+ -- replace the placeholder with the actual condition SQL
43
+ proc_src := REPLACE(proc_src, tmpl_proc_placeholder, condition_sql);
44
+ proc_src := REPLACE(proc_src, tmpl_proc_name, 'poll_for_events');
45
+
46
+ -- the new poll_for_events function will be created with
47
+ -- the pgmb_reader role, to avoid a bad "conditions_sql"
48
+ -- from having any destructive access to the database.
49
+ EXECUTE proc_src;
50
+ -- changing the owner will ensure that the function is executed with
51
+ -- the pgmb_reader's permissions.
52
+ -- https://www.postgresql.org/docs/current/sql-alterfunction.html
53
+ EXECUTE 'ALTER FUNCTION poll_for_events() OWNER TO pgmb_reader';
54
+ END;
55
+ $$ LANGUAGE plpgsql VOLATILE STRICT PARALLEL UNSAFE
56
+ SET search_path TO pgmb
57
+ SECURITY INVOKER;
package/sql/pgmb.sql CHANGED
@@ -589,9 +589,11 @@ BEGIN
589
589
  -- the new poll_for_events function will be created with
590
590
  -- the pgmb_reader role, to avoid a bad "conditions_sql"
591
591
  -- from having any destructive access to the database.
592
- SET ROLE pgmb_reader;
593
592
  EXECUTE proc_src;
594
- RESET ROLE;
593
+ -- changing the owner will ensure that the function is executed with
594
+ -- the pgmb_reader's permissions.
595
+ -- https://www.postgresql.org/docs/current/sql-alterfunction.html
596
+ EXECUTE 'ALTER FUNCTION poll_for_events() OWNER TO pgmb_reader';
595
597
  END;
596
598
  $$ LANGUAGE plpgsql VOLATILE STRICT PARALLEL UNSAFE
597
599
  SET search_path TO pgmb