-
Notifications
You must be signed in to change notification settings - Fork 4
/
pglogical_ticker--1.2.sql
647 lines (545 loc) · 16.8 KB
/
pglogical_ticker--1.2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/* pglogical_ticker--1.0.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pglogical_ticker" to load this file. \quit
CREATE FUNCTION pglogical_ticker._launch(oid)
RETURNS pg_catalog.INT4 STRICT
AS 'MODULE_PATHNAME', 'pglogical_ticker_launch'
LANGUAGE C;
CREATE FUNCTION pglogical_ticker.launch()
RETURNS pg_catalog.INT4 STRICT
AS $BODY$
SELECT pglogical_ticker._launch(oid)
FROM pg_database
WHERE datname = current_database()
--This should be improved in the future but should do
--the job for now.
AND NOT EXISTS
(SELECT 1
FROM pg_stat_activity psa
WHERE NOT pid = pg_backend_pid()
AND query = 'SELECT pglogical_ticker.tick();');
$BODY$
LANGUAGE SQL;
CREATE FUNCTION pglogical_ticker.dependency_update()
RETURNS VOID AS
$DEPS$
/*****
This handles the rename of pglogical.replication_set_relation to pglogical_ticker.rep_set_table_wrapper from version 1 to 2
*/
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'rep_set_table_wrapper' AND table_schema = 'pglogical_ticker') THEN
PERFORM pglogical_ticker.drop_ext_object('VIEW','pglogical_ticker.rep_set_table_wrapper');
DROP VIEW pglogical_ticker.rep_set_table_wrapper;
END IF;
IF (SELECT extversion FROM pg_extension WHERE extname = 'pglogical') ~* '^1.*' THEN
CREATE VIEW pglogical_ticker.rep_set_table_wrapper AS
SELECT *
FROM pglogical.replication_set_relation;
ELSE
CREATE VIEW pglogical_ticker.rep_set_table_wrapper AS
SELECT *
FROM pglogical.replication_set_table;
END IF;
END;
$DEPS$
LANGUAGE plpgsql;
SELECT pglogical_ticker.dependency_update();
CREATE OR REPLACE FUNCTION pglogical_ticker.add_ext_object
(p_type text
, p_full_obj_name text)
RETURNS VOID AS
$BODY$
BEGIN
PERFORM pglogical_ticker.toggle_ext_object(p_type, p_full_obj_name, 'ADD');
END;
$BODY$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION pglogical_ticker.drop_ext_object
(p_type text
, p_full_obj_name text)
RETURNS VOID AS
$BODY$
BEGIN
PERFORM pglogical_ticker.toggle_ext_object(p_type, p_full_obj_name, 'DROP');
END;
$BODY$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION pglogical_ticker.toggle_ext_object
(p_type text
, p_full_obj_name text
, p_toggle text)
RETURNS VOID AS
$BODY$
DECLARE
c_valid_types TEXT[] = ARRAY['EVENT TRIGGER','FUNCTION','VIEW','TABLE'];
c_valid_toggles TEXT[] = ARRAY['ADD','DROP'];
BEGIN
IF NOT (SELECT ARRAY[upper(p_type)] && c_valid_types) THEN
RAISE EXCEPTION 'Must pass one of % as 1st arg.', array_to_string(c_valid_types,',');
END IF;
IF NOT (SELECT ARRAY[upper(p_toggle)] && c_valid_toggles) THEN
RAISE EXCEPTION 'Must pass one of % as 3rd arg.', array_to_string(c_valid_toggles,',');
END IF;
EXECUTE 'ALTER EXTENSION pglogical_ticker '||p_toggle||' '||p_type||' '||p_full_obj_name;
/*EXCEPTION
WHEN undefined_function THEN
RETURN;
WHEN undefined_object THEN
RETURN;
WHEN object_not_in_prerequisite_state THEN
RETURN;
*/
END;
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION pglogical_ticker.deploy_ticker_tables()
RETURNS INT AS
$BODY$
/****
This will create the main table on both provider and
all subscriber(s) for in use replication sets.
It assumes this extension is installed both places.
*/
DECLARE
v_row_count INT;
BEGIN
PERFORM pglogical.replicate_ddl_command($$
CREATE TABLE IF NOT EXISTS pglogical_ticker.$$||quote_ident(set_name)||$$ (
provider_name NAME PRIMARY KEY,
source_time TIMESTAMPTZ
);$$, ARRAY[set_name])
FROM pglogical.replication_set;
PERFORM pglogical_ticker.add_ext_object('TABLE', 'pglogical_ticker.'||quote_ident(set_name))
FROM pglogical.replication_set;
GET DIAGNOSTICS v_row_count = ROW_COUNT;
RETURN v_row_count;
END;
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION pglogical_ticker.all_repset_tickers()
RETURNS TABLE (provider_name NAME, set_name NAME, source_time TIMESTAMPTZ)
AS
$BODY$
DECLARE v_sql TEXT;
BEGIN
SELECT COALESCE(
string_agg(
format(
'SELECT provider_name, %s::NAME AS set_name, source_time FROM %s',
quote_literal(rs.set_name),
relid::REGCLASS::TEXT
),
E'\nUNION ALL\n'
),
'SELECT NULL::NAME, NULL::NAME, NULL::TIMESTAMPTZ') INTO v_sql
FROM pg_stat_user_tables st
INNER JOIN pglogical.replication_set rs ON rs.set_name = st.relname
WHERE schemaname = 'pglogical_ticker';
RETURN QUERY EXECUTE v_sql;
END;
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION pglogical_ticker.all_subscription_tickers()
RETURNS TABLE (provider_name NAME, set_name NAME, source_time TIMESTAMPTZ)
AS
$BODY$
DECLARE v_sql TEXT;
BEGIN
WITH sub_rep_sets AS (
SELECT DISTINCT unnest(sub_replication_sets) AS set_name
FROM pglogical.subscription
)
SELECT COALESCE(
string_agg(
format(
'SELECT provider_name, %s::NAME AS set_name, source_time FROM %s',
quote_literal(srs.set_name),
relid::REGCLASS::TEXT
),
E'\nUNION ALL\n'
),
'SELECT NULL::NAME, NULL::NAME, NULL::TIMESTAMPTZ') INTO v_sql
FROM pg_stat_user_tables st
INNER JOIN sub_rep_sets srs ON srs.set_name = st.relname
WHERE schemaname = 'pglogical_ticker';
RETURN QUERY EXECUTE v_sql;
END;
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION pglogical_ticker.add_ticker_tables_to_replication()
RETURNS INT AS
$BODY$
DECLARE v_row_count INT;
BEGIN
/****
This will add all ticker tables
to replication if not done already.
It assumes of course pglogical_ticker.deploy_ticker_tables()
has been run.
*/
PERFORM rs.set_name, pglogical.replication_set_add_table(
set_name:=rs.set_name
,relation:=('pglogical_ticker.'||quote_ident(set_name))::REGCLASS
--default synchronize_data is false
,synchronize_data:=false
)
FROM pglogical.replication_set rs
WHERE NOT EXISTS
(SELECT 1
FROM pglogical_ticker.rep_set_table_wrapper rsr
WHERE rsr.set_reloid = ('pglogical_ticker.'||quote_ident(set_name))::REGCLASS
AND rsr.set_id = rs.set_id);
GET DIAGNOSTICS v_row_count = ROW_COUNT;
RETURN v_row_count;
END;
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION pglogical_ticker.tick_rep_set(p_set_name name)
RETURNS INT AS
$BODY$
DECLARE
v_sql TEXT;
BEGIN
v_sql:=$$
INSERT INTO pglogical_ticker.$$||quote_ident(p_set_name)||$$ (provider_name, source_time)
SELECT ni.if_name, now() AS source_time
FROM pglogical.replication_set rs
INNER JOIN pglogical.node n ON n.node_id = rs.set_nodeid
INNER JOIN pglogical.node_interface ni ON ni.if_nodeid = n.node_id
WHERE EXISTS (SELECT 1
FROM pglogical_ticker.rep_set_table_wrapper rsr
WHERE rsr.set_id = rs.set_id)
ON CONFLICT (provider_name, replication_set_name)
DO UPDATE
SET source_time = now();
$$;
EXECUTE v_sql;
END;
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION pglogical_ticker.tick()
RETURNS VOID AS
$BODY$
DECLARE
v_record RECORD;
v_sql TEXT;
v_row_count INT;
BEGIN
FOR v_record IN SELECT set_name FROM pglogical.replication_set ORDER BY set_name
LOOP
v_sql:=$$
INSERT INTO pglogical_ticker.$$||quote_ident(v_record.set_name)||$$ (provider_name, source_time)
SELECT ni.if_name, now() AS source_time
FROM pglogical.replication_set rs
INNER JOIN pglogical.node n ON n.node_id = rs.set_nodeid
INNER JOIN pglogical.node_interface ni ON ni.if_nodeid = n.node_id
WHERE rs.set_name = '$$||quote_ident(v_record.set_name)||$$'
ON CONFLICT (provider_name)
DO UPDATE
SET source_time = now();
$$;
EXECUTE v_sql;
END LOOP;
END;
$BODY$
LANGUAGE plpgsql;
REVOKE EXECUTE ON ALL FUNCTIONS IN SCHEMA pglogical_ticker FROM PUBLIC;
/* pglogical_ticker--1.0--1.1.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pglogical_ticker" to load this file. \quit
--This must be done AFTER we update the function def
SELECT pglogical_ticker.drop_ext_object('FUNCTION','pglogical_ticker.dependency_update()');
DROP FUNCTION pglogical_ticker.dependency_update();
SELECT pglogical_ticker.drop_ext_object('VIEW','pglogical_ticker.rep_set_table_wrapper');
DROP VIEW IF EXISTS pglogical_ticker.rep_set_table_wrapper;
CREATE OR REPLACE FUNCTION pglogical_ticker.toggle_ext_object(p_type text, p_full_obj_name text, p_toggle text)
RETURNS void
LANGUAGE plpgsql
AS $function$
DECLARE
c_valid_types TEXT[] = ARRAY['EVENT TRIGGER','FUNCTION','VIEW','TABLE'];
c_valid_toggles TEXT[] = ARRAY['ADD','DROP'];
BEGIN
IF NOT (SELECT ARRAY[upper(p_type)] && c_valid_types) THEN
RAISE EXCEPTION 'Must pass one of % as 1st arg.', array_to_string(c_valid_types,',');
END IF;
IF NOT (SELECT ARRAY[upper(p_toggle)] && c_valid_toggles) THEN
RAISE EXCEPTION 'Must pass one of % as 3rd arg.', array_to_string(c_valid_toggles,',');
END IF;
EXECUTE 'ALTER EXTENSION pglogical_ticker '||p_toggle||' '||p_type||' '||p_full_obj_name;
EXCEPTION
WHEN undefined_function THEN
RETURN;
WHEN undefined_object THEN
RETURN;
WHEN object_not_in_prerequisite_state THEN
RETURN;
END;
$function$
;
CREATE OR REPLACE FUNCTION pglogical_ticker.rep_set_table_wrapper()
RETURNS TABLE (set_id OID, set_reloid REGCLASS)
LANGUAGE plpgsql
AS $function$
/*****
This handles the rename of pglogical.replication_set_relation to pglogical_ticker.rep_set_table_wrapper from version 1 to 2
*/
BEGIN
IF EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'pglogical' AND tablename = 'replication_set_table') THEN
RETURN QUERY
SELECT r.set_id, r.set_reloid
FROM pglogical.replication_set_table r;
ELSEIF EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'pglogical' AND tablename = 'replication_set_relation') THEN
RETURN QUERY
SELECT r.set_id, r.set_reloid
FROM pglogical.replication_set_relation r;
ELSE
RAISE EXCEPTION 'No table pglogical.replication_set_relation or pglogical.replication_set_table found';
END IF;
END;
$function$
;
CREATE OR REPLACE FUNCTION pglogical_ticker.add_ticker_tables_to_replication()
RETURNS integer
LANGUAGE plpgsql
AS $function$
DECLARE v_row_count INT;
BEGIN
/****
This will add all ticker tables
to replication if not done already.
It assumes of course pglogical_ticker.deploy_ticker_tables()
has been run.
*/
PERFORM rs.set_name, pglogical.replication_set_add_table(
set_name:=rs.set_name
,relation:=('pglogical_ticker.'||quote_ident(set_name))::REGCLASS
--default synchronize_data is false
,synchronize_data:=false
)
FROM pglogical.replication_set rs
WHERE NOT EXISTS
(SELECT 1
FROM pglogical_ticker.rep_set_table_wrapper() rsr
WHERE rsr.set_reloid = ('pglogical_ticker.'||quote_ident(set_name))::REGCLASS
AND rsr.set_id = rs.set_id);
GET DIAGNOSTICS v_row_count = ROW_COUNT;
RETURN v_row_count;
END;
$function$
;
CREATE OR REPLACE FUNCTION pglogical_ticker.tick_rep_set(p_set_name name)
RETURNS integer
LANGUAGE plpgsql
AS $function$
DECLARE
v_sql TEXT;
BEGIN
v_sql:=$$
INSERT INTO pglogical_ticker.$$||quote_ident(p_set_name)||$$ (provider_name, source_time)
SELECT ni.if_name, now() AS source_time
FROM pglogical.replication_set rs
INNER JOIN pglogical.node n ON n.node_id = rs.set_nodeid
INNER JOIN pglogical.node_interface ni ON ni.if_nodeid = n.node_id
WHERE EXISTS (SELECT 1
FROM pglogical_ticker.rep_set_table_wrapper() rsr
WHERE rsr.set_id = rs.set_id)
ON CONFLICT (provider_name, replication_set_name)
DO UPDATE
SET source_time = now();
$$;
EXECUTE v_sql;
END;
$function$
;
CREATE OR REPLACE FUNCTION pglogical_ticker.launch()
RETURNS integer
LANGUAGE sql
STRICT
AS $function$
SELECT pglogical_ticker._launch(oid)
FROM pg_database
WHERE datname = current_database()
--This should be improved in the future but should do
--the job for now.
AND NOT EXISTS
(SELECT 1
FROM pg_stat_activity psa
WHERE NOT pid = pg_backend_pid()
AND query = 'SELECT pglogical_ticker.tick();')
AND NOT pg_is_in_recovery();
$function$
;
CREATE OR REPLACE FUNCTION pglogical_ticker.launch_if_repset_tables()
RETURNS integer
LANGUAGE sql
AS $function$
SELECT pglogical_ticker.launch()
WHERE EXISTS (SELECT 1 FROM pglogical_ticker.rep_set_table_wrapper());
$function$
;
/* pglogical_ticker--1.1--1.2.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pglogical_ticker" to load this file. \quit
DROP FUNCTION pglogical_ticker.deploy_ticker_tables();
DROP FUNCTION pglogical_ticker.add_ticker_tables_to_replication();
CREATE OR REPLACE FUNCTION pglogical_ticker.eligible_tickers
(
/***
"Eligible tickers" are defined as replication sets and tables
that are eligible to be created or added to replication, either
because the replication sets exist, or with cascading replication,
the tables already exist to add to a specified replication set
p_cascade_to_set_name as cascaded tickers.
***/
p_cascade_to_set_name NAME = NULL
)
RETURNS TABLE (set_name name, tablename name)
LANGUAGE plpgsql
AS $function$
/****
It assumes this extension is installed both places!
*/
BEGIN
RETURN QUERY
--In the generic case, always tablename = set_name
SELECT rs.set_name, rs.set_name AS tablename
FROM pglogical.replication_set rs
WHERE p_cascade_to_set_name IS NULL
UNION
--For cascading replication, we override set_name
SELECT p_cascade_to_set_name AS set_name_out, relname AS tablename
FROM pg_class c
INNER JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE p_cascade_to_set_name IS NOT NULL
AND n.nspname = 'pglogical_ticker'
AND c.relkind = 'r'
AND EXISTS (
SELECT 1
FROM pglogical.replication_set rsi
WHERE rsi.set_name = p_cascade_to_set_name
);
END;
$function$
;
CREATE OR REPLACE FUNCTION pglogical_ticker.deploy_ticker_tables(
--For use with cascading replication, you can pass
--a set_name in order to add all current subscription tickers
--to this replication set
p_cascade_to_set_name NAME = NULL
)
RETURNS integer
LANGUAGE plpgsql
AS $function$
/****
This will create the main table on both provider and
all subscriber(s) for in use replication sets.
It assumes this extension is installed both places.
*/
DECLARE
v_row_count INT;
BEGIN
PERFORM pglogical.replicate_ddl_command($$
CREATE TABLE IF NOT EXISTS pglogical_ticker.$$||quote_ident(tablename)||$$ (
provider_name NAME PRIMARY KEY,
source_time TIMESTAMPTZ
);
SELECT pglogical_ticker.add_ext_object(
'TABLE',
format('%s.%s',
'pglogical_ticker',
quote_ident($$||quote_literal(tablename)||$$)
)
);
$$, ARRAY[set_name])
FROM pglogical_ticker.eligible_tickers(p_cascade_to_set_name);
GET DIAGNOSTICS v_row_count = ROW_COUNT;
RETURN v_row_count;
END;
$function$
;
CREATE OR REPLACE FUNCTION pglogical_ticker.add_ticker_tables_to_replication(
--For use with cascading replication, you can pass
--a set_name in order to add all current subscription tickers
--to this replication set
p_cascade_to_set_name NAME = NULL
)
RETURNS integer
LANGUAGE plpgsql
AS $function$
DECLARE v_row_count INT;
BEGIN
/****
This will add all ticker tables
to replication if not done already.
It assumes of course pglogical_ticker.deploy_ticker_tables()
has been run.
*/
PERFORM et.set_name, pglogical.replication_set_add_table(
set_name:=et.set_name
,relation:=('pglogical_ticker.'||quote_ident(et.tablename))::REGCLASS
--default synchronize_data is false
,synchronize_data:=false
)
FROM pglogical_ticker.eligible_tickers(p_cascade_to_set_name) et
WHERE NOT EXISTS
(SELECT 1
FROM pglogical_ticker.rep_set_table_wrapper() rsr
INNER JOIN pglogical.replication_set rs ON rs.set_id = rsr.set_id
WHERE rsr.set_reloid = ('pglogical_ticker.'||quote_ident(et.tablename))::REGCLASS
AND et.set_name = rs.set_name);
GET DIAGNOSTICS v_row_count = ROW_COUNT;
RETURN v_row_count;
END;
$function$
;
CREATE OR REPLACE FUNCTION pglogical_ticker.tick()
RETURNS void
LANGUAGE plpgsql
AS $function$
DECLARE
v_record RECORD;
v_sql TEXT;
v_row_count INT;
BEGIN
FOR v_record IN
SELECT rs.set_name
FROM pglogical.replication_set rs
/***
Don't try to tick tables that don't yet exist. This will allow
us to create replication sets without worrying about adding a ticker table
immediately.
***/
WHERE EXISTS
(SELECT 1
FROM pg_class c
INNER JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'pglogical_ticker'
AND c.relname = rs.set_name
/***
Also avoid uselessly ticking tables that are not in any replication set
(regardless of which one)
***/
AND EXISTS
(SELECT 1
FROM pglogical_ticker.rep_set_table_wrapper() rst
WHERE c.oid = rst.set_reloid)
)
ORDER BY rs.set_name
LOOP
v_sql:=$$
INSERT INTO pglogical_ticker.$$||quote_ident(v_record.set_name)||$$ (provider_name, source_time)
SELECT ni.if_name, now() AS source_time
FROM pglogical.replication_set rs
INNER JOIN pglogical.node n ON n.node_id = rs.set_nodeid
INNER JOIN pglogical.node_interface ni ON ni.if_nodeid = n.node_id
WHERE rs.set_name = '$$||quote_ident(v_record.set_name)||$$'
ON CONFLICT (provider_name)
DO UPDATE
SET source_time = now();
$$;
EXECUTE v_sql;
END LOOP;
END;
$function$
;