forked from ssinger/slony1-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slony1_funcs.sql.in
executable file
·1549 lines (1378 loc) · 42.6 KB
/
slony1_funcs.sql.in
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- ----------------------------------------------------------------------
-- slony1_funcs.sql
--
-- Declaration of replication support functions.
--
-- Copyright (c) 2003-2004, PostgreSQL Global Development Group
-- Author: Jan Wieck, Afilias USA INC.
--
-- $Id: slony1_funcs.sql.in,v 1.25 2004-03-13 21:53:21 wieck Exp $
-- ----------------------------------------------------------------------
-- **********************************************************************
-- * C functions in src/backend/slony1_base.c
-- **********************************************************************
-- ----------------------------------------------------------------------
-- FUNCTION createEvent (cluster_name, ev_type [, ev_data [...]])
--
-- Create an sl_event entry
-- ----------------------------------------------------------------------
create function @NAMESPACE@.createEvent (name, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
create function @NAMESPACE@.createEvent (name, text, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
create function @NAMESPACE@.createEvent (name, text, text, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
create function @NAMESPACE@.createEvent (name, text, text, text, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
create function @NAMESPACE@.createEvent (name, text, text, text, text, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
create function @NAMESPACE@.createEvent (name, text, text, text, text, text, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
create function @NAMESPACE@.createEvent (name, text, text, text, text, text, text, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
create function @NAMESPACE@.createEvent (name, text, text, text, text, text, text, text, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
create function @NAMESPACE@.createEvent (name, text, text, text, text, text, text, text, text, text)
returns bigint
as '$libdir/slony1_funcs', '_Slony_I_createEvent'
language C
called on null input;
-- ----------------------------------------------------------------------
-- FUNCTION getLocalNodeId (name)
--
--
-- ----------------------------------------------------------------------
create function @NAMESPACE@.getLocalNodeId (name) returns int4
as '$libdir/slony1_funcs', '_Slony_I_getLocalNodeId'
language C
security definer;
-- ----------------------------------------------------------------------
-- FUNCTION setSessionRole (name, role)
--
--
-- ----------------------------------------------------------------------
create function @NAMESPACE@.setSessionRole (name, text) returns text
as '$libdir/slony1_funcs', '_Slony_I_setSessionRole'
language C
security definer;
-- ----------------------------------------------------------------------
-- FUNCTION getSessionRole (name, role)
--
--
-- ----------------------------------------------------------------------
create function @NAMESPACE@.getSessionRole (name) returns text
as '$libdir/slony1_funcs', '_Slony_I_getSessionRole'
language C
security definer;
-- ----------------------------------------------------------------------
-- FUNCTION logTrigger ()
--
--
-- ----------------------------------------------------------------------
create function @NAMESPACE@.logTrigger () returns trigger
as '$libdir/slony1_funcs', '_Slony_I_logTrigger'
language C
security definer;
-- **********************************************************************
-- * PL/pgSQL functions for administrative tasks
-- **********************************************************************
-- ----------------------------------------------------------------------
-- FUNCTION initializeLocalNode (no_id, no_comment)
--
-- Initializes a new node.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.initializeLocalNode (int4, text)
returns int4
as '
declare
p_local_node_id alias for $1;
p_comment alias for $2;
v_old_node_id int4;
v_first_log_no int4;
v_event_seq int8;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- Make sure this node is uninitialized or got reset
-- ----
select last_value::int4 into v_old_node_id from @NAMESPACE@.sl_local_node_id;
if v_old_node_id != -1 then
raise exception ''Slony-I: This node is already initialized'';
end if;
-- ----
-- Set sl_local_node_id to the requested value and add our
-- own system to sl_node.
-- ----
perform setval(''@NAMESPACE@.sl_local_node_id'', p_local_node_id);
perform @NAMESPACE@.storeNode_int (p_local_node_id, p_comment);
return p_local_node_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION resetLocalNode ()
--
-- Reset the local node to appear uninitialized.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.resetLocalNode ()
returns int4
as '
begin
-- **** TODO ****
raise exception ''Slony-I: resetLocalNode() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION storeNode (no_id, no_comment)
--
-- Generate the STORE_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.storeNode (int4, text)
returns int4
as '
declare
p_no_id alias for $1;
p_no_comment alias for $2;
begin
perform @NAMESPACE@.storeNode_int (p_no_id, p_no_comment);
perform @NAMESPACE@.createEvent(''_@CLUSTERNAME@'', ''STORE_NODE'',
p_no_id, p_no_comment);
return p_no_id;
end;
' language plpgsql
called on null input;
-- ----------------------------------------------------------------------
-- FUNCTION storeNode_int (no_id, no_comment)
--
-- Process the STORE_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.storeNode_int (int4, text)
returns int4
as '
declare
p_no_id alias for $1;
p_no_comment alias for $2;
v_old_row record;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- Check if the node exists
-- ----
select * into v_old_row
from @NAMESPACE@.sl_node
where no_id = p_no_id
for update;
if found then
-- ----
-- Node exists, update the existing row.
-- ----
update @NAMESPACE@.sl_node
set no_comment = p_no_comment
where no_id = p_no_id;
else
-- ----
-- New node, insert the sl_node row
-- ----
insert into @NAMESPACE@.sl_node
(no_id, no_active, no_comment) values
(p_no_id, ''f'', p_no_comment);
end if;
return p_no_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION enableNode (no_id)
--
-- Generate the ENABLE_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.enableNode (int4)
returns int4
as '
declare
p_no_id alias for $1;
v_local_node_id int4;
v_node_row record;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- Check that we are the node to activate and that we are
-- currently disabled.
-- ----
v_local_node_id := @NAMESPACE@.getLocalNodeId(''_@CLUSTERNAME@'');
select * into v_node_row
from @NAMESPACE@.sl_node
where no_id = p_no_id
for update;
if not found then
raise exception ''Slony-I: node % not found'', p_no_id;
end if;
if v_node_row.no_active then
raise exception ''Slony-I: node % is already active'', p_no_id;
end if;
-- ----
-- Activate this node and generate the ENABLE_NODE event
-- ----
perform @NAMESPACE@.enableNode_int (p_no_id);
perform @NAMESPACE@.createEvent(''_@CLUSTERNAME@'', ''ENABLE_NODE'',
p_no_id);
return p_no_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION enableNode_int (no_id)
--
-- Process the ENABLE_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.enableNode_int (int4)
returns int4
as '
declare
p_no_id alias for $1;
v_local_node_id int4;
v_node_row record;
v_sub_row record;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- Check that the node is inactive
-- ----
select * into v_node_row
from @NAMESPACE@.sl_node
where no_id = p_no_id
for update;
if not found then
raise exception ''Slony-I: node % not found'', p_no_id;
end if;
if v_node_row.no_active then
return p_no_id;
end if;
-- ----
-- Activate the node and generate sl_confirm status rows for it.
-- ----
update @NAMESPACE@.sl_node
set no_active = ''t''
where no_id = p_no_id;
insert into @NAMESPACE@.sl_confirm
(con_origin, con_received, con_seqno)
select no_id, p_no_id, 0 from @NAMESPACE@.sl_node
where no_id != p_no_id
and no_active;
insert into @NAMESPACE@.sl_confirm
(con_origin, con_received, con_seqno)
select p_no_id, no_id, 0 from @NAMESPACE@.sl_node
where no_id != p_no_id
and no_active;
-- ----
-- Generate ENABLE_SUBSCRIPTION events for all sets that
-- origin here and are subscribed by the just enabled node.
-- ----
v_local_node_id := @NAMESPACE@.getLocalNodeId(''_@CLUSTERNAME@'');
for v_sub_row in select SUB.sub_set from
@NAMESPACE@.sl_set S,
@NAMESPACE@.sl_subscribe SUB
where S.set_origin = v_local_node_id
and S.set_id = SUB.sub_set
and SUB.sub_receiver = p_no_id
for update of S
loop
perform @NAMESPACE@.enableSubscription (v_sub_row.sub_set, p_no_id);
end loop;
return p_no_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION disableNode (no_id)
--
-- Generate the DISABLE_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.disableNode (int4)
returns int4
as '
declare
p_no_id alias for $1;
begin
-- **** TODO ****
raise exception ''Slony-I: disableNode() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION disableNode_int (no_id)
--
-- Process the DISABLE_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.disableNode_int (int4)
returns int4
as '
declare
p_no_id alias for $1;
begin
-- **** TODO ****
raise exception ''Slony-I: disableNode_int() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION dropNode (no_id)
--
-- Generate the DROP_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.dropNode (int4)
returns int4
as '
declare
p_no_id alias for $1;
begin
-- **** TODO ****
raise exception ''Slony-I: dropNode() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION dropNode_int (no_id)
--
-- Process the DROP_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.dropNode_int (int4)
returns int4
as '
declare
p_no_id alias for $1;
begin
-- **** TODO ****
raise exception ''Slony-I: dropNode_int() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION storePath (pa_server, pa_client, pa_conninfo, pa_connretry)
--
-- Generate the STORE_PATH event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.storePath (int4, int4, text, int4)
returns int4
as '
declare
p_pa_server alias for $1;
p_pa_client alias for $2;
p_pa_conninfo alias for $3;
p_pa_connretry alias for $4;
begin
perform @NAMESPACE@.storePath_int(p_pa_server, p_pa_client,
p_pa_conninfo, p_pa_connretry);
perform @NAMESPACE@.createEvent(''_@CLUSTERNAME@'', ''STORE_PATH'',
p_pa_server, p_pa_client, p_pa_conninfo, p_pa_connretry);
return 0;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION storePath_int (pa_server, pa_client, pa_conninfo, pa_connretry)
--
-- Process the STORE_PATH event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.storePath_int (int4, int4, text, int4)
returns int4
as '
declare
p_pa_server alias for $1;
p_pa_client alias for $2;
p_pa_conninfo alias for $3;
p_pa_connretry alias for $4;
v_dummy int4;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- Check if the path already exists
-- ----
select 1 into v_dummy
from @NAMESPACE@.sl_path
where pa_server = p_pa_server
and pa_client = p_pa_client
for update;
if found then
-- ----
-- Path exists, update pa_conninfo
-- ----
update @NAMESPACE@.sl_path
set pa_conninfo = p_pa_conninfo,
pa_connretry = p_pa_connretry
where pa_server = p_pa_server
and pa_client = p_pa_client;
else
-- ----
-- New path
--
-- In case we receive STORE_PATH events before we know
-- about the nodes involved in this, we generate those nodes
-- as pending.
-- ----
if not exists (select 1 from @NAMESPACE@.sl_node
where no_id = p_pa_server) then
perform @NAMESPACE@.storeNode_int (p_pa_server, ''<event pending>'');
end if;
if not exists (select 1 from @NAMESPACE@.sl_node
where no_id = p_pa_client) then
perform @NAMESPACE@.storeNode_int (p_pa_client, ''<event pending>'');
end if;
insert into @NAMESPACE@.sl_path
(pa_server, pa_client, pa_conninfo, pa_connretry) values
(p_pa_server, p_pa_client, p_pa_conninfo, p_pa_connretry);
end if;
return 0;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION dropPath (pa_server, pa_client)
--
-- Generate the DROP_PATH event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.dropPath (int4, int4)
returns int4
as '
declare
p_pa_server alias for $1;
p_pa_client alias for $2;
begin
-- **** TODO ****
raise exception ''Slony-I: dropPath() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION dropPath_int (pa_server, pa_client)
--
-- Process the DROP_NODE event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.dropPath_int (int4, int4)
returns int4
as '
declare
p_pa_server alias for $1;
p_pa_client alias for $2;
begin
-- **** TODO ****
raise exception ''Slony-I: dropPath_int() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION storeListen (li_origin, li_provider, li_receiver)
--
-- Generate the STORE_LISTEN event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.storeListen (int4, int4, int4)
returns int4
as '
declare
p_li_origin alias for $1;
p_li_provider alias for $2;
p_li_receiver alias for $3;
begin
perform @NAMESPACE@.storeListen_int (p_li_origin, p_li_provider, p_li_receiver);
perform @NAMESPACE@.createEvent (''_@CLUSTERNAME@'', ''STORE_LISTEN'',
p_li_origin, p_li_provider, p_li_receiver);
return 0;
end;
' language plpgsql
called on null input;
-- ----------------------------------------------------------------------
-- FUNCTION storeListen_int (li_origin, li_provider, li_receiver)
--
-- Process the STORE_LISTEN event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.storeListen_int (int4, int4, int4)
returns int4
as '
declare
p_li_origin alias for $1;
p_li_provider alias for $2;
p_li_receiver alias for $3;
v_exists int4;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
select 1 into v_exists
from @NAMESPACE@.sl_listen
where li_origin = p_li_origin
and li_provider = p_li_provider
and li_receiver = p_li_receiver;
if not found then
-- ----
-- In case we receive STORE_LISTEN events before we know
-- about the nodes involved in this, we generate those nodes
-- as pending.
-- ----
if not exists (select 1 from @NAMESPACE@.sl_node
where no_id = p_li_origin) then
perform @NAMESPACE@.storeNode_int (p_li_origin, ''<event pending>'');
end if;
if not exists (select 1 from @NAMESPACE@.sl_node
where no_id = p_li_provider) then
perform @NAMESPACE@.storeNode_int (p_li_provider, ''<event pending>'');
end if;
if not exists (select 1 from @NAMESPACE@.sl_node
where no_id = p_li_receiver) then
perform @NAMESPACE@.storeNode_int (p_li_receiver, ''<event pending>'');
end if;
insert into @NAMESPACE@.sl_listen
(li_origin, li_provider, li_receiver) values
(p_li_origin, p_li_provider, p_li_receiver);
end if;
return 0;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION dropListen (li_origin, li_provider, li_receiver)
--
-- Generate the DROP_LISTEN event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.dropListen (int4, int4, int4)
returns int4
as '
declare
p_li_origin alias for $1;
p_li_provider alias for $2;
p_li_receiver alias for $3;
v_retval int4;
begin
v_retval := @NAMESPACE@.dropListen_int(p_li_origin,
p_li_provider, p_li_receiver);
perform @NAMESPACE@.createEvent (''_@CLUSTERNAME@'', ''DROP_LISTEN'',
p_li_origin, p_li_provider, p_li_receiver);
return v_retval;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION dropListen_int (li_origin, li_provider, li_receiver)
--
-- Process the DROP_LISTEN event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.dropListen_int (int4, int4, int4)
returns int4
as '
declare
p_li_origin alias for $1;
p_li_provider alias for $2;
p_li_receiver alias for $3;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
delete from @NAMESPACE@.sl_listen
where li_origin = p_li_origin
and li_provider = p_li_provider
and li_receiver = p_li_receiver;
if found then
return 1;
else
return 0;
end if;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION storeSet (set_id, set_comment)
--
-- Generate the STORE_SET event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.storeSet (int4, text)
returns int4
as '
declare
p_set_id alias for $1;
p_set_comment alias for $2;
v_local_node_id int4;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
v_local_node_id := @NAMESPACE@.getLocalNodeId(''_@CLUSTERNAME@'');
insert into @NAMESPACE@.sl_set
(set_id, set_origin, set_comment) values
(p_set_id, v_local_node_id, p_set_comment);
perform @NAMESPACE@.createEvent(''_@CLUSTERNAME@'', ''STORE_SET'',
p_set_id, v_local_node_id, p_set_comment);
return p_set_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION storeSet_int (set_id, set_origin, set_comment)
--
-- Process the STORE_SET event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.storeSet_int (int4, int4, text)
returns int4
as '
declare
p_set_id alias for $1;
p_set_origin alias for $2;
p_set_comment alias for $3;
v_dummy int4;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
select 1 into v_dummy
from @NAMESPACE@.sl_set
where set_id = p_set_id
for update;
if found then
update @NAMESPACE@.sl_set
set set_comment = p_set_comment
where set_id = p_set_id;
else
if not exists (select 1 from @NAMESPACE@.sl_node
where no_id = p_set_origin) then
perform @NAMESPACE@.storeNode_int (p_set_origin, ''<event pending>'');
end if;
insert into @NAMESPACE@.sl_set
(set_id, set_origin, set_comment) values
(p_set_id, p_set_origin, p_set_comment);
end if;
return p_set_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION moveSet (set_id)
--
-- Generate the MOVE_SET event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.moveSet (int4)
returns int4
as '
declare
p_set_id alias for $1;
v_local_node_id int4;
v_set_row record;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- Check that the set exists and that it is currently remote
-- ----
v_local_node_id := @NAMESPACE@.getLocalNodeId(''_@CLUSTERNAME@'');
select * into v_set_row from @NAMESPACE@.sl_set
where set_id = p_set_id
for update;
if not found then
raise exception ''Slony-I: set % not found'', p_set_id;
end if;
if v_set_row.set_origin = v_local_node_id then
raise exception ''Slony-I: set % originates on local node'',
p_set_id;
end if;
-- ----
-- Take over the set and generate the message
-- ----
update @NAMESPACE@.sl_set
set set_origin = v_local_node_id
where set_id = p_set_id;
perform @NAMESPACE@.createEvent(''_@CLUSTERNAME@'', ''MOVE_SET'',
p_set_id, v_local_node_id);
return p_set_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION moveSet_int (set_id, new_set_origin)
--
-- Process the MOVE_SET event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.moveSet_int (int4, int4)
returns int4
as '
declare
p_set_id alias for $1;
p_new_set_origin alias for $2;
v_set_row record;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- Check that the set exists.
-- ----
select * into v_set_row from @NAMESPACE@.sl_set
where set_id = p_set_id
for update;
if not found then
raise exception ''Slony-I: set % not found'', p_set_id;
end if;
-- ----
-- Change the origin of the set
-- ----
update @NAMESPACE@.sl_set
set set_origin = p_new_set_origin
where set_id = p_set_id;
return p_set_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION dropSet (set_id)
--
-- Generate the DROP_SET event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.dropSet (int4)
returns int4
as '
declare
p_set_id alias for $1;
begin
-- **** TODO ****
raise exception ''Slony-I: dropSet() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION dropSet_int (set_id)
--
-- Process the DROP_SET event.
-- ----------------------------------------------------------------------
create function @NAMESPACE@.dropSet_int (int4)
returns int4
as '
declare
p_set_id alias for $1;
begin
-- **** TODO ****
raise exception ''Slony-I: dropSet_int() not implemented'';
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION setAddTable (set_id, tab_id, tab_fqname, tab_attkind, tab_comment)
-- ----------------------------------------------------------------------
create function @NAMESPACE@.setAddTable(int4, int4, text, text, text)
returns int4
as '
declare
p_set_id alias for $1;
p_tab_id alias for $2;
p_fqname alias for $3;
p_tab_attkind alias for $4;
p_tab_comment alias for $5;
v_set_origin int4;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- Check that we are the origin of the set
-- ----
select set_origin into v_set_origin
from @NAMESPACE@.sl_set
where set_id = p_set_id;
if not found then
raise exception ''Slony-I: setAddTable(): set % not found'', p_set_id;
end if;
if v_set_origin != @NAMESPACE@.getLocalNodeId(''_@CLUSTERNAME@'') then
raise exception ''Slony-I: setAddTable(): set % has remote origin'', p_set_id;
end if;
-- ----
-- Add the table to the set and generate the SET_ADD_TABLE event
-- ----
perform @NAMESPACE@.setAddTable_int(p_set_id, p_tab_id, p_fqname,
p_tab_attkind, p_tab_comment);
perform @NAMESPACE@.createEvent(''_@CLUSTERNAME@'', ''SET_ADD_TABLE'',
p_set_id, p_tab_id, p_fqname, p_tab_attkind, p_tab_comment);
return p_tab_id;
end;
' language plpgsql;
-- ----------------------------------------------------------------------
-- FUNCTION setAddTable_int (set_id, tab_id, tab_fqname, tab_attkind, tab_comment)
-- ----------------------------------------------------------------------
create function @NAMESPACE@.setAddTable_int(int4, int4, text, text, text)
returns int4
as '
declare
p_set_id alias for $1;
p_tab_id alias for $2;
p_fqname alias for $3;
p_tab_attkind alias for $4;
p_tab_comment alias for $5;
v_local_node_id int4;
v_set_origin int4;
v_sub_provider int4;
v_relkind char;
v_tab_reloid oid;
begin
-- ----
-- Grab the central configuration lock
-- ----
lock table @NAMESPACE@.sl_config_lock;
-- ----
-- For sets with a remote origin, check that we are subscribed
-- to that set. Otherwise we ignore the table because it might
-- not even exist in our database.
-- ----
v_local_node_id := @NAMESPACE@.getLocalNodeId(''_@CLUSTERNAME@'');
select set_origin into v_set_origin
from @NAMESPACE@.sl_set
where set_id = p_set_id;
if not found then
raise exception ''Slony-I: setAddTable_int(): set % not found'',
p_set_id;
end if;
if v_set_origin != v_local_node_id then
select sub_provider into v_sub_provider
from @NAMESPACE@.sl_subscribe
where sub_set = p_set_id
and sub_receiver = @NAMESPACE@.getLocalNodeId(''_@CLUSTERNAME@'');
if not found then
return 0;
end if;
end if;
-- ----
-- Get the tables OID and check that it is a real table
-- ----
select PGC.oid, PGC.relkind into v_tab_reloid, v_relkind
from "pg_catalog".pg_class PGC, "pg_catalog".pg_namespace PGN
where PGC.relnamespace = PGN.oid
and p_fqname = "pg_catalog".quote_ident(PGN.nspname) ||
''.'' || "pg_catalog".quote_ident(PGC.relname);
if not found then
raise exception ''Slony-I: setAddTable(): table % not found'',
p_fqname;
end if;
if v_relkind != ''r'' then
raise exception ''Slony-I: setAddTable(): % is not a regular table'',
p_fqname;
end if;
-- ----
-- Add the table to sl_table and create the trigger on it.
-- ----
insert into @NAMESPACE@.sl_table
(tab_id, tab_reloid, tab_set, tab_attkind, tab_comment) values
(p_tab_id, v_tab_reloid, p_set_id, p_tab_attkind, p_tab_comment);
perform @NAMESPACE@.tableAddTrigger(p_tab_id, p_fqname, p_tab_attkind);