unilogic / bucardo forked from bucardo/bucardo

Bucardo

bucardo / bucardo.schema
100644 2226 lines (1960 sloc) 81.739 kb
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
 
-- Schema for the main Bucardo database
 
-- Should be run as a superuser
 
\set ON_ERROR_STOP off
 
CREATE USER bucardo SUPERUSER;
CREATE DATABASE bucardo OWNER bucardo;
 
\c bucardo bucardo
 
-- plpgsql and plperlu are loaded, but just in case:
SET client_min_messages = 'FATAL';
CREATE LANGUAGE plpgsql;
CREATE LANGUAGE plperlu;
SET client_min_messages = 'ERROR';
 
\set ON_ERROR_STOP on
 
CREATE SCHEMA bucardo;
CREATE SCHEMA freezer;
SET search_path TO bucardo;
 
SET client_min_messages = 'WARNING';
SET escape_string_warning = 'OFF';
 
--
-- Main bucardo configuration information
--
CREATE TABLE bucardo_config (
  setting TEXT NOT NULL, -- short unique name, maps to %config inside Bucardo
  value TEXT NOT NULL,
  about TEXT NULL, -- long description
  type TEXT NULL, -- sync or goat
  name TEXT NULL, -- which specific sync or goat
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
 
CREATE UNIQUE INDEX bucardo_config_unique ON bucardo_config(setting) WHERE name IS NULL;
 
CREATE UNIQUE INDEX bucardo_config_unique_name ON bucardo_config(setting,name,type) WHERE name IS NOT NULL;
 
ALTER TABLE bucardo_config ADD CONSTRAINT valid_config_type
  CHECK (type IN ('sync','goat'));
 
CREATE FUNCTION check_bucardo_config()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $bc$
BEGIN
NEW.setting = LOWER(NEW.setting);
 
IF (NEW.type IS NOT NULL and NEW.name IS NULL) THEN
  RAISE EXCEPTION 'Must provide a specific %', NEW.type;
END IF;
 
IF (NEW.name IS NOT NULL and NEW.type IS NULL) THEN
  RAISE EXCEPTION 'Must provide a type if giving a name';
END IF;
 
IF (NEW.setting = 'sync' OR NEW.setting = 'goat') THEN
  RAISE EXCEPTION 'Invalid setting name';
END IF;
 
RETURN NEW;
END;
$bc$;
 
CREATE TRIGGER check_bucardo_config
  BEFORE INSERT OR UPDATE ON bucardo_config
  FOR EACH ROW EXECUTE PROCEDURE check_bucardo_config();
 
 
-- Sleep times (all in seconds)
COPY bucardo_config(setting,value,about)
FROM STDIN
WITH DELIMITER '|';
kick_sleep|0.2|How long do we sleep while waiting for a kick response?
mcp_loop_sleep|0.1|How long does the main MCP daemon sleep between loops?
mcp_dbproblem_sleep|15|How many seconds to sleep before trying to respawn
ctl_nothingfound_sleep|0.2|How long does the controller loop sleep if nothing is found?
kid_nothingfound_sleep|0.3|How long does a kid sleep if nothing is found?
kid_nodeltarows_sleep|0.8|How long do kids sleep if no delta rows are found?
kid_serial_sleep|10|How long to sleep in seconds if we hit a serialization error
endsync_sleep|1.0|How long do we sleep when custom code requests an endsync?
\.
 
 
-- Various timeouts (times are in seconds)
COPY bucardo_config(setting,value,about)
FROM STDIN
WITH DELIMITER '|';
mcp_pingtime|60|How often do we ping check the MCP?
ctl_pingtime|600|How often do we ping check the CTL?
kid_pingtime|60|How often do we ping check the KID?
ctl_checkonkids_time|10|How often does the controller check on the kids health?
ctl_checkabortedkids_time|30|How often does the controller check the q table for aborted children?
ctl_createkid_time|0.5|How long do we sleep to allow kids-on-demand to get on their feet?
tcp_keepalives_idle|10|How long to wait between each keepalive probe.
tcp_keepalives_interval|5|How long to wait for a response to a keepalive probe.
tcp_keepalives_count|2|How many probes to send. 0 indicates sticking with system defaults.
\.
 
-- Debug output
COPY bucardo_config(setting,value,about)
FROM STDIN
WITH DELIMITER '|';
audit_pid|1|Do we populate the audit_pid table or not?
log_showpid|0|Show PID in the log output?
log_showtime|1|Show timestamp in the log output? 0=off 1=seconds since epoch 2=scalar gmtime 3=scalar localtime
log_showline|0|Show line number in the log output?
log_conflict_details|0|Log detailed conflict data?
log_conflict_file|bucardo_conflict.log|Name of the conflict detail log file
\.
 
-- Versioning
COPY bucardo_config(setting,value,about)
FROM STDIN
WITH DELIMITER '|';
bucardo_version|3.2.7|Bucardo version this schema was created with
bucardo_current_version|3.2.7|Current version of Bucardo
\.
 
-- Other settings:
COPY bucardo_config(setting,value,about)
FROM STDIN
WITH DELIMITER '|';
default_email_from|nobody@example.com|Who the alert emails are sent as
default_email_to|nobody@example.com|Who to send alert emails to
default_email_host|localhost|Which host to send email through
kid_abort_limit|3|How many times we will restore an aborted kid before giving up?
max_delete_clause|200|Maximum number of items to delete inside of IN() clauses
max_select_clause|500|Maximum number of items to select inside of IN() clauses
piddir|/var/run/bucardo|Directory holding Bucardo PID files
pidfile|bucardo.pid|Name of the main Bucardo pid file
reason_file|/home/bucardo/restart.reason|File to hold reasons for stopping and starting
stats_script_url|http://www.bucardo.org/|Location of the stats script
stopfile|fullstopbucardo|Name of the semaphore file used to stop Bucardo processes
syslog_facility|LOG_LOCAL1|Which syslog facility level to use
upsert_attempts|3|How many times do we try out the upsert loop?
\.
 
--
-- Keep track of every database we need to connect to
--
CREATE TABLE db (
  name TEXT NOT NULL, -- local name for convenience, not necessarily database name
                CONSTRAINT db_name_pk PRIMARY KEY (name),
  dbhost TEXT NOT NULL DEFAULT '',
  dbport TEXT NOT NULL DEFAULT 5432,
  dbname TEXT NOT NULL,
  dbuser TEXT NOT NULL,
  dbpass TEXT NULL,
  dbconn TEXT NOT NULL DEFAULT '', -- string to add to the generated dsn
  dbservice TEXT NULL,
  pgpass TEXT NULL, -- local file with connection info same as pgpass
  status TEXT NOT NULL DEFAULT 'active',
  sourcelimit SMALLINT NOT NULL DEFAULT 0, -- maximum concurrent read connections to this database
  targetlimit SMALLINT NOT NULL DEFAULT 0, -- maximum concurrent write connections to this database
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX "db_dsn_unique" ON db(dbhost,dbport,dbname,dbuser) WHERE NOT name ~ '^bctest';
ALTER TABLE db ADD CONSTRAINT db_status CHECK (status IN ('active','inactive'));
 
--
-- Databases can belong to zero or more named groups
--
CREATE TABLE dbgroup (
  name TEXT NOT NULL,
              CONSTRAINT dbgroup_name_pk PRIMARY KEY (name),
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
 
CREATE TABLE dbmap (
  db TEXT NOT NULL,
             CONSTRAINT dbmap_db_fk FOREIGN KEY (db) REFERENCES db(name) ON DELETE CASCADE,
  dbgroup TEXT NOT NULL,
             CONSTRAINT dbmap_dbgroup_fk FOREIGN KEY (dbgroup) REFERENCES dbgroup(name) ON DELETE CASCADE,
  priority SMALLINT NOT NULL DEFAULT 0,
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX dbmap_unique ON dbmap(db,dbgroup);
 
 
--
-- Track status information about each database
--
CREATE TABLE db_connlog (
  db TEXT NOT NULL,
                CONSTRAINT db_connlog_dbid_fk FOREIGN KEY (db) REFERENCES db(name) ON DELETE CASCADE,
  conndate TIMESTAMPTZ NOT NULL DEFAULT now(), -- when we first connected to it
  connstring TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'unknown',
                CONSTRAINT db_connlog_status CHECK (status IN ('unknown', 'good', 'down', 'unreachable')),
  version TEXT NULL
);
 
 
--
-- We need to track each item we want to replicate from or replicate to
--
CREATE SEQUENCE goat_id_seq;
CREATE TABLE goat (
  id INTEGER NOT NULL DEFAULT nextval('goat_id_seq'),
                       CONSTRAINT goat_id_pk PRIMARY KEY (id),
  db TEXT NOT NULL,
                       CONSTRAINT goat_db_fk FOREIGN KEY (db) REFERENCES db(name) ON DELETE RESTRICT,
  schemaname TEXT NOT NULL,
  tablename TEXT NOT NULL,
  reltype TEXT NOT NULL DEFAULT 'table',
  pkey TEXT NULL,
  qpkey TEXT NULL,
  pkeytype TEXT NULL,
  has_delta BOOLEAN NOT NULL DEFAULT 'false',
  ping BOOLEAN NULL, -- overrides sync-level ping
  customselect TEXT NULL,
  makedelta BOOLEAN NULL,
  rebuild_index BOOLEAN NULL, -- overrides sync-level rebuild_index
  ghost BOOLEAN NOT NULL DEFAULT 'false', -- only drop triggers, do not replicate
  standard_conflict TEXT NULL,
  analyze_after_copy BOOLEAN NOT NULL DEFAULT 'true',
  strict_checking BOOLEAN NOT NULL DEFAULT 'true',
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
ALTER TABLE goat ADD CONSTRAINT has_schemaname CHECK (length(schemaname) > 1);
 
ALTER TABLE goat ADD CONSTRAINT custom_needs_pkey CHECK (customselect IS NULL OR length(pkey) > 1);
 
ALTER TABLE goat ADD CONSTRAINT pkey_needs_type CHECK (pkey = '' OR pkeytype IS NOT NULL);
 
ALTER TABLE goat ADD CONSTRAINT standard_conflict CHECK (standard_conflict IS NULL
  OR standard_conflict IN ('source','target','skip','random','latest','abort'));
 
-- CREATE UNIQUE INDEX goats_r_unique ON goat(db,schemaname,tablename,reltype);
 
--
-- Set of filters for each goat.
--
CREATE SEQUENCE bucardo_custom_trigger_id_seq;
CREATE TABLE bucardo_custom_trigger (
id INTEGER NOT NULL DEFAULT nextval('bucardo_custom_trigger_id_seq'),
CONSTRAINT bucardo_custom_trigger_id_pk PRIMARY KEY (id),
goat INTEGER NOT NULL,
CONSTRAINT bucardo_custom_trigger_goat_fk FOREIGN KEY (goat) REFERENCES goat(id) ON DELETE CASCADE,
trigger_name TEXT NOT NULL,
trigger_type TEXT NOT NULL,
trigger_language TEXT NOT NULL DEFAULT 'plpgsql',
trigger_body TEXT NOT NULL,
trigger_level TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'active',
cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
 
ALTER TABLE bucardo_custom_trigger ADD CONSTRAINT type_is_delta_or_trigger CHECK (trigger_type IN ('delta', 'triggerkick'));
ALTER TABLE bucardo_custom_trigger ADD CONSTRAINT level_is_row_statement CHECK (trigger_level IN ('ROW', 'STATEMENT'));
CREATE UNIQUE INDEX bucardo_custom_trigger_goat_type_unique ON bucardo_custom_trigger(goat, trigger_type);
 
--
-- A group of goats. Ideally arranged in some sort of tree.
--
CREATE TABLE herd (
  name TEXT NOT NULL,
               CONSTRAINT herd_name_pk PRIMARY KEY (name),
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
 
--
-- Goats belong to zero or more herds. In most cases, they will
-- belong to a single herd if they are being replicated.
--
CREATE TABLE herdmap (
  herd TEXT NOT NULL,
              CONSTRAINT herdmap_herd_fk FOREIGN KEY (herd) REFERENCES herd(name) ON DELETE CASCADE,
  goat INTEGER NOT NULL,
              CONSTRAINT herdmap_goat_fk FOREIGN KEY (goat) REFERENCES goat(id) ON DELETE CASCADE,
  priority SMALLINT NOT NULL DEFAULT 0,
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
 
CREATE VIEW goats_in_herd AS
  SELECT hm.herd, g.*
  FROM goat g
  JOIN herdmap hm ON hm.goat = g.id;
 
CREATE FUNCTION herdcheck()
RETURNS TRIGGER
LANGUAGE plperlu
AS
$bc$
    use strict; use warnings;
    ## Make sure that all goats in a herd are from the same database
    my $new = $_TD->{new};
    my $herdname = $new->{herd};
    $herdname =~ s/'/''/go;
    my $SQL = qq{
        SELECT 1
        FROM bucardo.herdmap h, bucardo.goat g
        WHERE h.goat = g.id
        AND h.herd='$herdname'
        AND g.db != (SELECT db FROM bucardo.goat WHERE id = $new->{goat})
    };
    elog(DEBUG, "Running $SQL");
    my $count = spi_exec_query($SQL)->{processed};
    if ($count >= 1) {
        elog(ERROR, "Cannot have goats from different databases in the same herd ($count)");
    }
 
    ## Make sure that a herd contains at most one schemaname/tablename combination
    $SQL = qq{
        SELECT count(*) AS goats
        FROM bucardo.herdmap h, bucardo.goat g
        WHERE h.goat = g.id
        AND g.id != $new->{goat}
        AND h.herd = '$herdname'
        AND g.tablename = (SELECT tablename FROM bucardo.goat WHERE id = $new->{goat})
        AND g.schemaname = (SELECT schemaname FROM bucardo.goat WHERE id = $new->{goat})
    };
    elog(DEBUG, "Running $SQL");
    $count = spi_exec_query($SQL)->{rows}[0]{goats};
    if ($count >= 1) {
        elog(ERROR, "Cannot have two goats with the same schema and table inside a herd (herd=$herdname) (goat=$new->{goat}) (count=$count)");
    }
 
    return;
$bc$;
 
CREATE TRIGGER herdcheck
  AFTER INSERT OR UPDATE ON herdmap
  FOR EACH ROW EXECUTE PROCEDURE herdcheck();
 
 
--
-- We need to know who is replicating to who, and how
--
CREATE TABLE sync (
  name TEXT NOT NULL UNIQUE,
                       CONSTRAINT sync_name_pk PRIMARY KEY (name),
  source TEXT NOT NULL,
                       CONSTRAINT sync_source_herd_fk FOREIGN KEY (source) REFERENCES herd(name) ON DELETE RESTRICT,
  targetdb TEXT NULL,
                       CONSTRAINT sync_targetdb_fk FOREIGN KEY (targetdb) REFERENCES db(name) ON DELETE RESTRICT,
  targetgroup TEXT NULL,
                       CONSTRAINT sync_targetgroup_fk FOREIGN KEY (targetgroup) REFERENCES dbgroup(name) ON DELETE RESTRICT,
  synctype TEXT NOT NULL,
  stayalive BOOLEAN NOT NULL DEFAULT 'true', -- Does the sync controller stay connected?
  kidsalive BOOLEAN NOT NULL DEFAULT 'true', -- Do the children stay connected?
  usecustomselect BOOLEAN NOT NULL DEFAULT 'false',
  copytype TEXT NOT NULL DEFAULT 'copy',
  copyextra TEXT NOT NULL DEFAULT '', -- e.g. WITH OIDS
  deletemethod TEXT NOT NULL DEFAULT 'delete',
  limitdbs SMALLINT NOT NULL DEFAULT 0, -- How many databases can sync at once? 0=all
  ping BOOLEAN NOT NULL DEFAULT true, -- Are we issuing NOTICES via triggers?
  do_listen BOOLEAN NOT NULL DEFAULT false, -- LISTEN for kicks on source/target database if ! ping
  checktime INTERVAL NULL, -- How often to check if we've not heard anything?
  status TEXT NOT NULL DEFAULT 'active', -- Possibly CHECK / FK ('stopped','paused','b0rken')
  makedelta BOOLEAN NOT NULL DEFAULT 'false',
  rebuild_index BOOLEAN NOT NULL DEFAULT 'false', -- Load without indexes and then REINDEX table
  priority SMALLINT NOT NULL DEFAULT 0, -- Higher is better
  txnmode TEXT NOT NULL DEFAULT 'SERIALIZABLE',
  analyze_after_copy BOOLEAN NOT NULL DEFAULT 'true',
  strict_checking BOOLEAN NOT NULL DEFAULT 'true',
  overdue INTERVAL NOT NULL DEFAULT '0 seconds'::interval,
  expired INTERVAL NOT NULL DEFAULT '0 seconds'::interval,
  track_rates BOOLEAN NOT NULL DEFAULT 'false',
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
 
 
ALTER TABLE sync ADD CONSTRAINT sync_type CHECK (synctype IN ('pushdelta','fullcopy','swap'));
 
ALTER TABLE sync ADD CONSTRAINT sync_copytype CHECK (copytype IN ('insert','copy'));
 
ALTER TABLE sync ADD CONSTRAINT sync_deletemethod CHECK (deletemethod IN ('truncate', 'delete'));
 
CREATE UNIQUE INDEX sync_source_targetdb_type ON sync(source, targetdb, synctype);
 
CREATE UNIQUE INDEX sync_source_targetgroup_type ON sync(source, targetgroup, synctype);
 
ALTER TABLE sync ADD CONSTRAINT sync_validtarget CHECK
  ((targetdb IS NULL AND targetgroup IS NOT NULL)
    OR
   (targetdb IS NOT NULL AND targetgroup IS NULL));
 
ALTER TABLE sync ADD CONSTRAINT sync_swap_nogroup CHECK
  (synctype <> 'swap' or targetdb IS NOT NULL);
 
-- Because NOTIFY is broke, make sure our names are simple:
ALTER TABLE db ADD CONSTRAINT db_name_sane CHECK (name ~ E'^[a-zA-Z]\\w*$');
ALTER TABLE dbgroup ADD CONSTRAINT dbgroup_name_sane CHECK (name ~ E'^[a-zA-Z]\\w*$');
ALTER TABLE sync ADD CONSTRAINT sync_name_sane CHECK (name ~ E'^[a-zA-Z]\\w*$');
 
 
--
-- Track our children
--
CREATE SEQUENCE audit_pid_id_seq;
CREATE TABLE audit_pid (
  id INTEGER NOT NULL DEFAULT nextval('audit_pid_id_seq'),
  parentid INTEGER NULL, -- CTL or MCP id
  familyid INTEGER NULL, -- the MCP id
  type TEXT NOT NULL,
  sync TEXT NOT NULL,
  source TEXT NULL,
  target TEXT NULL,
  master_backend INT NOT NULL DEFAULT pg_backend_pid(),
  source_backend INT NULL,
  target_backend INT NULL,
  ppid INTEGER NOT NULL,
  pid INTEGER NOT NULL,
  birthdate TIMESTAMPTZ NOT NULL DEFAULT now(),
  killdate TIMESTAMPTZ NULL,
  birth TEXT NULL,
  death TEXT NULL
);
 
CREATE TABLE freezer.old_audit_pid AS SELECT * FROM audit_pid LIMIT 0;
 
--
-- Traffic control for children
--
CREATE TABLE q (
  sync TEXT NULL,
              CONSTRAINT q_sync_fk FOREIGN KEY (sync) REFERENCES sync(name) ON DELETE SET NULL,
  sourcedb TEXT NULL,
              CONSTRAINT q_sdb_fk FOREIGN KEY (sourcedb) REFERENCES db(name) ON DELETE SET NULL,
  targetdb TEXT NULL,
              CONSTRAINT q_tdb_fk FOREIGN KEY (targetdb) REFERENCES db(name) ON DELETE SET NULL,
  ppid INTEGER NOT NULL,
  pid INTEGER NULL,
  synctype TEXT NULL,
  updates BIGINT NULL,
  inserts BIGINT NULL,
  deletes BIGINT NULL,
  started TIMESTAMPTZ NULL,
  aborted TIMESTAMPTZ NULL,
  whydie TEXT NULL,
  ended TIMESTAMPTZ NULL,
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- insert constraint - db and sync not null
-- Can only be one unstarted source->target per sync
CREATE UNIQUE INDEX "q_unique" ON q (sync,sourcedb,targetdb) WHERE started IS NULL;
 
CREATE INDEX q_ppid ON q (ppid,pid) WHERE ended IS NULL AND aborted IS NULL;
 
CREATE INDEX q_aborted ON q(sync) WHERE started IS NOT NULL AND aborted IS NOT NULL AND ended IS NULL;
 
CREATE INDEX q_cleanup ON q(cdate) WHERE ended IS NOT NULL;
 
CREATE INDEX q_stathelper ON q(cdate, sync) WHERE ended IS NOT NULL;
 
 
CREATE FUNCTION bucardo.bucardo_q()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $bc$
BEGIN
EXECUTE 'NOTIFY "bucardo_q_'||NEW.sync||'_'||NEW.targetdb||'"';
RETURN NEW;
END;
$bc$;
 
CREATE TRIGGER bucardo_q
  AFTER INSERT ON bucardo.q
  FOR EACH ROW EXECUTE PROCEDURE bucardo.bucardo_q();
 
CREATE TABLE freezer.master_q AS SELECT * FROM q LIMIT 0;
GRANT SELECT ON freezer.master_q TO PUBLIC;
 
CREATE FUNCTION bucardo_purge_q_table(interval)
RETURNS BIGINT
SECURITY DEFINER
LANGUAGE plpgsql
AS $bc$
DECLARE
  numrows BIGINT;
  qcount BIGINT;
BEGIN
  RAISE DEBUG 'Purging q table of finished items older than %', $1;
  INSERT INTO freezer.master_q SELECT * FROM bucardo.q
    WHERE (ended IS NOT NULL OR aborted IS NOT NULL) AND cdate <= now() - $1;
  DELETE FROM bucardo.q WHERE (ended IS NOT NULL OR aborted IS NOT NULL) AND cdate <= now() - $1;
  GET DIAGNOSTICS numrows := row_count;
  SELECT count(*) FROM q INTO qcount;
  RAISE NOTICE 'Rows left in q table: %', qcount;
  RETURN numrows;
END;
$bc$;
 
CREATE FUNCTION bucardo_purge_q_table(text)
RETURNS BIGINT
LANGUAGE SQL
AS $bc$
SELECT bucardo_purge_q_table($1::interval);
$bc$;
 
CREATE FUNCTION bucardo_purge_q_table()
RETURNS BIGINT
LANGUAGE SQL
AS $bc$
SELECT bucardo_purge_q_table('2 hours'::interval);
$bc$;
 
CREATE FUNCTION bucardo_purge_q_table(integer)
RETURNS BIGINT
LANGUAGE plpgsql
IMMUTABLE
AS $bc$
BEGIN
RAISE EXCEPTION 'Please use an time interval such as ''2 hours''';
END;
$bc$;
 
 
CREATE FUNCTION table_exists(text,text)
RETURNS BOOLEAN
LANGUAGE plpgsql
AS $bc$
BEGIN
  PERFORM 1
    FROM pg_catalog.pg_class c, pg_namespace n
    WHERE c.relnamespace = n.oid
    AND n.nspname = $1
    AND c.relname = $2;
  IF FOUND THEN RETURN true; END IF;
  RETURN false;
END;
$bc$;
 
-- Called on update to master_q. Creates and populates child tables, empties master_q.
CREATE OR REPLACE FUNCTION populate_child_q_table()
RETURNS TRIGGER
LANGUAGE plpgsql
VOLATILE
SECURITY DEFINER
AS
$bc$
DECLARE
  myrec RECORD;
  myst TEXT;
  needindex BOOL = false;
  tablename TEXT;
BEGIN
 
  -- Make sure we have all child tables
  FOR myrec IN SELECT DISTINCT TO_CHAR(cdate, 'YYYYMMDD') AS t FROM freezer.master_q LOOP
    tablename = 'child_q_' || myrec.t;
    RAISE DEBUG 'Found %', tablename;
    PERFORM 1 FROM pg_class c JOIN pg_namespace n ON (n.oid = c.relnamespace)
    WHERE n.nspname = 'freezer' AND c.relname = tablename;
    IF NOT FOUND THEN
      myst = 'CREATE TABLE freezer.' || tablename ||'() INHERITS (freezer.master_q)';
      EXECUTE myst;
      myst = 'GRANT SELECT ON freezer.' || tablename ||' TO public';
      EXECUTE myst;
      needindex = TRUE;
    END IF;
 
    IF needindex IS FALSE THEN
      PERFORM 1 FROM pg_class c JOIN pg_namespace n ON (n.oid = c.relnamespace)
      WHERE n.nspname = 'freezer' AND c.relname = tablename||'_daterange';
      IF NOT FOUND THEN
        needindex = TRUE;
      END IF;
    END IF;
    IF needindex IS TRUE THEN
      myst = 'CREATE INDEX '
             || tablename
             || '_daterange ON freezer.'
             || tablename
             || '(cdate)';
      EXECUTE myst;
    END IF;
 
    -- Move all the rows over!
    myst = 'INSERT INTO freezer.'
           || tablename
           || $$ SELECT * FROM ONLY freezer.master_q WHERE TO_CHAR(cdate,'YYYYMMDD') = $$
           || quote_literal(myrec.t);
    EXECUTE myst;
    myst = $$DELETE FROM ONLY freezer.master_q WHERE TO_CHAR(cdate, 'YYYYMMDD') = $$
           || quote_literal(myrec.t);
    EXECUTE myst;
  END LOOP;
  RETURN NULL;
 
END;
$bc$;
 
CREATE TRIGGER populate_child_q_table
  AFTER INSERT OR UPDATE ON freezer.master_q
  FOR EACH STATEMENT EXECUTE PROCEDURE bucardo.populate_child_q_table();
 
--
-- Return a created connection string from the db table
--
CREATE OR REPLACE FUNCTION bucardo.db_getconn(text)
RETURNS TEXT
LANGUAGE plperlu
SECURITY DEFINER
AS $bc$
 
## Given the name of a db, return a connection string, username, and password
 
use strict; use warnings; use DBI;
my ($name, $SQL, $rv, $row, %db);
 
$name = shift;
 
$name =~ s/'/''/go;
$SQL = "SELECT * FROM db WHERE name = '$name'";
$rv = spi_exec_query($SQL);
if (!$rv->{processed}) {
    elog(ERROR, qq{Error: Could not find a database with a name of $name\n});
}
$row = $rv->{rows}[0];
 
## If there is a dbfile and it exists, it overrides the rest
## Format = hostname:port:database:username:password
## http://www.postgresql.org/docs/current/static/libpq-pgpass.html
 
## We also check for one if no password is given
if (!defined $row->{dbpass}) {
    my $passfile = $row->{pgpass} || '';
    if (open my $pass, "<", $passfile) {
        ## We only do complete matches
        my $match = "$row->{dbhost}:$row->{dbport}:$row->{dbname}:$row->{dbuser}";
        while (<$pass>) {
            if (/^$match:(.+)/) {
                $row->{dbpass} = $1;
                elog(DEBUG, "Found password in pgpass file $passfile for $match");
                last;
            }
        }
    }
}
 
for (qw(host port name user pass conn)) {
$db{$_} = exists $row->{"db$_"} ? $row->{"db$_"} : '';
}
 
## Check that the port is numeric
if (defined $db{port} and length $db{port} and $db{port} !~ /^\d+$/) {
    elog(ERROR, qq{Database port must be numeric, but got "$db{port}"\n});
}
 
length $db{name} or elog(ERROR, qq{Database name is mandatory\n});
length $db{user} or elog(ERROR, qq{Database username is mandatory\n});
 
my $connstring = "dbi:Pg:dbname=$db{name}";
$db{host} ||= ''; $db{port} ||= ''; $db{pass} ||= '';
length $db{host} and $connstring .= ";host=$db{host}";
length $db{port} and $connstring .= ";port=$db{port}";
length $db{conn} and $connstring .= ";$db{conn}";
 
return "$connstring\n$db{user}\n$db{pass}";
 
$bc$;
 
 
--
-- Test a database connection, and log to the db_connlog table
--
CREATE FUNCTION bucardo.db_testconn(text)
RETURNS TEXT
LANGUAGE plperlu
SECURITY DEFINER
AS
$bc$
 
## Given the name of a db connection, construct the connection
## string for it and then connect to it and log the attempt
 
use strict; use warnings; use DBI;
my ($name, $SQL, $rv, $row, $dbh, %db, $version, $found);
 
$name = shift;
 
$name =~ s/'/''/g;
$SQL = "SELECT bucardo.db_getconn('$name') AS bob";
$rv = spi_exec_query($SQL);
if (!$rv->{processed}) {
    elog(ERROR, qq{Error: Could not find a database with an name of $name\n});
}
$row = $rv->{rows}[0]{bob};
($db{dsn},$db{user},$db{pass}) = split /\n/ => $row;
 
elog(DEBUG, "Connecting as $db{dsn} user=$db{user} $$");
 
$dbh = DBI->connect($db{dsn}, $db{user}, $db{pass},
    {AutoCommit=>1, RaiseError=>1, PrintError=>0});
 
if (!$dbh) {
    elog(ERROR, qq{Database connection "$db{dsn}" failed: $DBI::errstr\n});
}
 
$version = $dbh->{pg_server_version};
 
# Install plpgsql if not there already
$SQL = q{SELECT 1 FROM pg_language WHERE lanname = 'plpgsql'};
my $sth = $dbh->prepare($SQL);
my $count = $sth->execute();
$sth->finish();
if ($count < 1) {
   $dbh->do("CREATE LANGUAGE plpgsql");
   $dbh->commit();
}
 
$dbh->disconnect();
 
my $safeconn = "$db{dsn} user=$db{user}"; ## No password for now
$safeconn =~ s/'/''/go;
$name =~ s/'/''/go;
$SQL = "INSERT INTO db_connlog (db,connstring,status,version) VALUES ('$name','$safeconn','good',$version)";
spi_exec_query($SQL);
 
return "Database connection successful";
 
$bc$;
 
 
--
-- Check the database connection if anything changes in the db table
--
CREATE FUNCTION db_change()
RETURNS TRIGGER
LANGUAGE plperlu
SECURITY DEFINER
AS
$bc$
 
return if $_TD->{new}{status} eq 'inactive';
 
## Test connection to the database specified
my $name = $_TD->{new}{name};
$name =~ s/'/''/g;
spi_exec_query("SELECT bucardo.db_testconn('$name')");
return;
 
$bc$;
 
CREATE TRIGGER db_change AFTER INSERT OR UPDATE ON db
  FOR EACH ROW EXECUTE PROCEDURE db_change();
 
--
-- Setup the goat table after any change
--
CREATE OR REPLACE FUNCTION validate_goat()
RETURNS TRIGGER
LANGUAGE plperlu
SECURITY DEFINER
AS
$bc$
 
## If a row in goat has changed, re-validate and set things up for that table
elog(DEBUG, "Running validate_goat");
use strict; use warnings; use DBI;
 
my ($SQL, $rv, $row, %db, $dbh, $sth, $count, $oid);
 
my $old = $_TD->{event} eq 'UPDATE' ? $_TD->{old} : 0;
my $new = $_TD->{new};
 
if (!defined $new->{db}) {
   die qq{Must provide a db\n};
}
if (!defined $new->{tablename}) {
   die qq{Must provide a tablename\n};
}
if (!defined $new->{schemaname}) {
   die qq{Must provide a schemaname\n};
}
 
my ($dbname,$schema,$table,$pkey) =
   ($new->{db}, $new->{schemaname}, $new->{tablename}, $new->{pkey});
 
## Do not allow pkeytype or qpkey to be set manually.
if (defined $new->{pkeytype} and (!$old or $new->{pkeytype} ne $old->{pkeytype})) {
    die qq{Cannot set pkeytype manually\n};
}
if (defined $new->{qpkey} and (!$old or $new->{qpkey} ne $old->{qpkey})) {
    die qq{Cannot set qpkey manually\n};
}
 
## If this is an update, we only continue if certain fields have changed
if ($old
    and $old->{db} eq $new->{db}
    and $old->{schemaname} eq $new->{schemaname}
    and $old->{tablename} eq $new->{tablename}
    and (defined $new->{pkey} and $new->{pkey} eq $old->{pkey})
    ) {
    #return;
}
 
(my $safedbname = $dbname) =~ s/'/''/go;
$SQL = "SELECT bucardo.db_getconn('$safedbname') AS apple";
$rv = spi_exec_query($SQL);
if (!$rv->{processed}) {
    elog(ERROR, qq{Error: Could not find a database with an name of $dbname\n});
}
$row = $rv->{rows}[0]{apple};
($db{dsn},$db{user},$db{pass}) = split /\n/ => $row;
 
elog(DEBUG, "Connecting in validate_goat as $db{dsn} user=$db{user} pid=$$ for table $schema.$table");
 
$dbh = DBI->connect($db{dsn}, $db{user}, $db{pass},
    {AutoCommit=>0, RaiseError=>1, PrintError=>0});
 
$dbh or elog(ERROR, qq{Database connection "$db{dsn}" as user $db{user} failed: $DBI::errstr\n});
 
## Get column information for this table (and verify it exists)
$SQL = q{
SELECT c.oid, attnum, attname, quote_ident(attname) AS qattname, typname, atttypid
FROM pg_attribute a, pg_type t, pg_class c, pg_namespace n
WHERE c.relnamespace = n.oid
AND nspname = ? AND relname = ?
AND a.attrelid = c.oid
AND a.atttypid = t.oid
AND attnum > 0
};
$sth = $dbh->prepare($SQL);
$count = $sth->execute($schema,$table);
if ($count < 1) {
   $sth->finish();
   $dbh->disconnect();
   die qq{Table not found at $db{dsn}: $schema.$table\n};
}
my $col = $sth->fetchall_hashref('attnum');
$oid = $col->{each %$col}{oid};
 
## Find all usable unique constraints for this table
$SQL = q{
SELECT indisprimary, indkey
FROM pg_index i
WHERE indisunique AND indpred IS NULL AND indexprs IS NULL AND indrelid = ?
ORDER BY indexrelid DESC
};
## DESC because we choose the "newest" index in case of a tie below
$sth = $dbh->prepare($SQL);
$count = 0+$sth->execute($oid);
my $cons = $sth->fetchall_arrayref({});
$dbh->rollback();
$dbh->disconnect();
 
elog(DEBUG, "Valid unique constraints found: $count\n");
if ($count < 1) {
    ## We have no usable constraints. The entries must be blank.
    my $orignew = $new->{pkey};
    $new->{pkey} = $new->{qpkey} = $new->{pkeytype} = '';
 
    if (!$old) { ## This was an insert: just go
        elog(DEBUG, "No usable constraints, setting pkey et. al. to blank");
        return 'MODIFY';
    }
 
    ## If pkey has been set to NULL, this was a specific reset request, so return
    ## If pkey ended up blank (no change, or changed to blank), just return
    if (!defined $orignew or $orignew eq '') {
        return 'MODIFY';
    }
 
    ## The user has tried to change it something not blank, but this is not possible.
    die qq{Cannot set pkey for table $schema.$table: no unique constraint found\n};
}
 
## Pick the best possible one. Primary keys are always the best choice.
my ($primary) = grep { $_->{indisprimary} } @$cons;
my $uniq;
if (defined $primary) {# and !$old and defined $new->{pkey}) {
    $uniq = $primary;
}
else {
    my (@foo) = grep { ! $_->{indisprimary} } @$cons;
    $count = @foo;
    ## Pick the one with the smallest number of columns.
    ## In case of a tie, choose the one with the smallest column footprint
    if ($count < 2) {
        $uniq = $foo[0];
    }
    else {
        my $lowest = 10_000;
        for (@foo) {
            my $cc = $_->{indkey} =~ y/ / /;
            if ($cc < $lowest) {
                $lowest = $cc;
                $uniq = $_;
            }
        }
    }
}
 
## This should not happen:
if (!defined $uniq) {
   die "Could not find a suitable unique index for table $schema.$table\n";
}
 
## If the user is not trying a manual override, set the best one and leave
if ((!defined $new->{pkey} or !length $new->{pkey}) or ($old and $new->{pkey} eq $old->{pkey})) {
    ($new->{pkey} = $uniq->{indkey}) =~ s/(\d+)(\s+)?/$col->{$1}{attname} . ($2 ? '|' : '')/ge;
    ($new->{qpkey} = $uniq->{indkey}) =~ s/(\d+)(\s+)?/$col->{$1}{qattname} . ($2 ? '|' : '')/ge;
    ($new->{pkeytype} = $uniq->{indkey}) =~ s/(\d+)(\s+)?/$col->{$1}{typname} . ($2 ? '|' : '')/ge;
    return 'MODIFY';
}
 
## They've attempted a manual override of pkey. Make sure it is valid.
for (@$cons) {
    (my $name = $_->{indkey}) =~ s/(\d+)(\s+)?/$col->{$1}{attname} . ($2 ? '|' : '')/ge;
    next unless $name eq $new->{pkey};
    ($new->{qpkey} = $uniq->{indkey}) =~ s/(\d+)(\s+)?/$col->{$1}{qattname} . ($2 ? '|' : '')/ge;
    ($new->{pkeytype} = $uniq->{indkey}) =~ s/(\d+)(\s+)?/$col->{$1}{typname} . ($2 ? '|' : '')/ge;
    return 'MODIFY';
}
 
die qq{Could not find a matching unique constraint that provides those columns\n};
 
$bc$; -- End of validate_goat()
 
CREATE TRIGGER validate_goat
  BEFORE INSERT OR UPDATE ON goat
  FOR EACH ROW EXECUTE PROCEDURE validate_goat();
 
--
-- Check that the goat tables are ready and compatible
--
 
CREATE OR REPLACE FUNCTION validate_sync(text,integer)
RETURNS TEXT
LANGUAGE plperlu
SECURITY DEFINER
AS
$bc$
    use strict; use warnings; use DBI;
 
    my $syncname = shift;
    my $force = shift || 0;
    my ($rv,$SQL,%cache);
 
    elog(NOTICE, "Starting validate_sync for $syncname");
 
    ## Connect to source and target(s) and verify that tables exist,
    ## and are identical. Setup the delta stuff as needed.
 
    ## Grab information about this sync from the database
    my $safename = $syncname;
    $safename =~ s/'/''/go;
    $SQL = "SELECT * FROM sync WHERE name = '$safename'";
    $rv = spi_exec_query($SQL);
    if (!$rv->{processed}) {
        elog(ERROR, "No such sync: $syncname");
    }
 
    my $info = $rv->{rows}[0];
    my $source = $info->{source};
    $source =~ s/'/''/go;
    ## Source is always a herd
 
    ## Prepare a list of all databases and tables involved in this sync
    ## We need to verify that the databases are reachable, that the tables exists,
    ## that the columns match up, and that the delta stuff is set up as needed.
    my %database;
 
    ## Does this herd exist?
    $SQL = qq{SELECT 1 FROM herd WHERE name = '$source'};
    $rv = spi_exec_query($SQL);
    if (!$rv->{processed}) {
       elog(ERROR, "No such herd: $source");
    }
 
    ## Process the source herd
    $SQL = qq{
        SELECT id, db, schemaname, tablename, pkey, pkeytype,
               standard_conflict, ping AS goatping,
               pg_catalog.quote_ident(db) AS safedb,
               pg_catalog.quote_ident(schemaname) AS safeschema,
               pg_catalog.quote_ident(tablename) AS safetable,
               pg_catalog.quote_ident(pkey) AS safepkey
        FROM goat g, herdmap h
        WHERE g.id = h.goat
        AND h.herd = '$source'
    };
    $rv = spi_exec_query($SQL);
    if (!$rv->{processed}) {
        elog(WARNING, "Herd has no members: $source");
        return qq{Herd "$source" for sync "$syncname" has no members: cannot validate};
    }
 
    ## All the same database, so we can extract it outside of the loop
    my $sourcedb = $rv->{rows}[0]{db};
    elog(DEBUG, "Got a sourcedb of $sourcedb for herd of $source");
    my %sourcetable;
    my %goat;
    for my $x (@{$rv->{rows}}) {
        $sourcetable{$x->{schemaname}}{$x->{tablename}} = $x;
    }
 
    ## Now to get all the target databases (will use schemas/tables from above)
    my $targetdb;
    if ($info->{targetdb}) {
        $targetdb->{$info->{targetdb}} = 'target';
    }
    elsif ($info->{targetgroup}) {
        my $group = $info->{targetgroup};
        $group =~ s/'/''/go;
        $SQL = qq{
            SELECT db, pg_catalog.quote_ident(db) AS safedb
            FROM dbmap
            WHERE dbgroup = '$group'
        };
        $rv = spi_exec_query($SQL);
        if (!@{$rv->{rows}}) {
            elog(ERROR, qq{Could not find a target database group of $info->{targetgroup}});
        }
        for (@{$rv->{rows}}) {
            $targetdb->{$_->{db}} = 'target';
        }
    }
    else {
        elog(ERROR, "Could not figure out the target for this sync!");
    }
    if (! keys %$targetdb) {
        elog(NOTICE, "No target databases found");
        return "No target databases foud for this sync";
    }
 
    ## We only want to check active databases
    my %dbstatus;
    $SQL = "SELECT name, status FROM db";
    $rv = spi_exec_query($SQL);
    for (@{$rv->{rows}}) {
        $dbstatus{$_->{name}} = $_->{status};
    }
    for (keys %$targetdb) {
        if ($dbstatus{$_} ne 'active') {
            elog(NOTICE, qq{Skipping inactive target database "$_". This may affect pruning of bucardo_delta});
            delete $targetdb->{$_};
        }
    }
 
    my $dbs = join "," => map { s/'/''/go; $_; } keys %$targetdb;
    if (!length $dbs) {
        elog(NOTICE, "No active databases found for this sync");
        return "No target databases found";
    }
    elog(DEBUG, "Target dbs: $dbs");
 
    ## If any of these are the source database, bail
    if (grep { $_ eq $sourcedb } keys %$targetdb) {
        elog(ERROR, "Source and target databases cannot be the same: $sourcedb");
    }
 
    ## Loop through and check each table in turn, setting up as needed.
 
    my %badtable;
    for my $schema (sort keys %sourcetable) {
        TABLE: for my $table (sort keys %{$sourcetable{$schema}}) {
 
            my $tinfo = $sourcetable{$schema}{$table};
            $tinfo->{synctype} = $info->{synctype};
            my $delta = $info->{synctype} =~ /pushdelta|swap/ ? 1 : 0;
            my $ping = $info->{ping} eq 't' ? 1 : 0;
            my $columns = bucardo_validate_table(1,$sourcedb,$tinfo,$delta,$ping,$syncname,\%cache,$source,$force);
            if ($columns == -1) {
                $badtable{$schema}{$table}=1;
                next TABLE;
            }
 
            $delta = $info->{synctype} =~ /swap/ ? 1 : 0;
            $ping = $delta;
            for my $tdb (sort keys %$targetdb) {
                my $tcolumns = bucardo_validate_table(0,$tdb,$tinfo,$delta,$ping,$syncname,\%cache,$source,$force);
                if ($columns == -1) {
                    $badtable{$schema}{$table}=1;
                    next TABLE;
                }
            } ## end each target database
 
 
        } ## end each source table
 
    } ## end each source schema
 
    ## Remove any tables that were removed from the herd by bucardo_validate_table
    for my $schema (keys %badtable) {
        for my $table (keys %{$badtable{$schema}}) {
            delete $sourcetable{$schema}{$table};
        }
    }
 
    ## Update the bucardo_delta_targets table as needed
    my $check = "SELECT 1 FROM bucardo.bucardo_delta_targets WHERE tablename=? AND targetdb=?";
    my $add = "INSERT INTO bucardo.bucardo_delta_targets(tablename,targetdb) VALUES (?,?)";
    if ($info->{synctype} eq 'swap' or $info->{synctype} eq 'pushdelta') {
       ## Add all targets to the source
       my $dbh = $cache{dbh}{$sourcedb};
       my $sthc = $dbh->prepare($check);
       my $stha = $dbh->prepare($add);
       for my $tdb (sort keys %$targetdb) {
           for my $schema (sort keys %sourcetable) {
               for my $table (sort keys %{$sourcetable{$schema}}) {
                   my $toid = $cache{oid}{$sourcedb}{$schema}{$table};
                   my $ot = $cache{oid}{$tdb}{$schema}{$table};
                   elog(DEBUG, "Adding oid $toid to source, other is $ot for $schema.$table");
                   my $count = $sthc->execute($toid,$tdb);
                   $sthc->finish();
                   elog(DEBUG, "Checking on $tdb, count was $count");
                   if ($count != 1) {
                       $stha->execute($toid,$tdb);
                       $dbh->commit();
                   }
               }
           }
       }
    }
    if ($info->{synctype} eq 'swap') {
        ## Add source to the target(s)
        for my $tdb (sort keys %$targetdb) {
            my $dbh = $cache{dbh}{$tdb};
            my $sthc = $dbh->prepare($check);
            for my $schema (sort keys %sourcetable) {
                for my $table (sort keys %{$sourcetable{$schema}}) {
                    my $oid = $cache{oid}{$tdb}{$schema}{$table};
                    my $count = $sthc->execute($oid,$sourcedb);
                    $sthc->finish();
                    elog(DEBUG, "Checking on source $sourcedb, count was $count");
                    if ($count != 1) {
                        my $stha = $dbh->prepare($add);
                        $stha->execute($oid,$sourcedb);
                        $dbh->commit();
                    }
                }
            }
        }
    }
 
    sub run_sql {
        my ($sql,$dbh) = @_;
        if ($sql =~ /^(\s+)/m) {
            my $leading = length($1);
            $sql =~ s/^\s{$leading}//gsm;
            $sql =~ s/\t/ /gsm;
        }
        elog(DEBUG, "SQL: $sql");
        $dbh->do($sql);
    }
 
    sub bucardo_validate_table {
 
        my ($is_source, $db, $info, $delta, $ping, $syncname, $cache, $source, $force) = @_;
        my ($sth,$SQL);
 
        my ($schema, $safeschema, $table, $safetable, $pkey, $safepkey, $safedb, $pkeytype) = @$info{qw(
           schemaname safeschema tablename safetable pkey safepkey safedb pkeytype)};
 
        elog(DEBUG, "Validate_table for db $db $schema.$table, delta=$delta ping=$ping");
 
        $db =~ s/'/''/go;
        my $rv = spi_exec_query("SELECT bucardo.db_getconn('$db') AS conn");
        $rv->{processed} or elog(ERROR, qq{Error: Could not find a database named "$db"\n});
        my ($dsn,$user,$pass) = split /\n/ => $rv->{rows}[0]{conn};
        elog(DEBUG, "Connecting to $dsn as $user inside bucardo_validate_sync");
        my $dbh;
        if (exists $cache->{dbh}{$db}) {
            $dbh = $cache->{dbh}{$db};
            elog(DEBUG, 'Connected to cached version');
        }
        else {
            $cache->{dbh}{$db} = $dbh = DBI->connect($dsn, $user, $pass, {AutoCommit=>0, RaiseError=>1, PrintError=>0});
        }
 
        if ($info->{synctype} =~ /swap|delta/) {
            if (! $pkey) {
                if (!$force) {
                    elog(ERROR, qq{Table "$schema.$table" must specify a primary key when using a sync of '$info->{synctype}'});
                }
                warn qq{Table "$schema.$table" must specify a primary key when using a sync of '$info->{synctype}'};
                warn qq{REMOVING TABLE "$schema.$table" from herd "$source"\n};
                (my $safesource = $source) =~ s/'/''/g;
                $SQL = "DELETE FROM herdmap WHERE herd='$safesource' AND goat IN (SELECT id FROM goat ".
                "WHERE schemaname='$safeschema' AND tablename='$safetable')";
                $rv = spi_exec_query($SQL);
                return -1;
            }
            if (! $pkeytype) {
                elog(ERROR, qq{Table "$schema.$table" must specify a pkeytype when using a sync of '$info->{synctype}'});
            }
            if ($info->{synctype} =~ /swap/) {
 
                ## Grab a list of any custom conflict handlers
                $SQL = "SELECT 1 FROM customcode c, customcode_map m ".
                        "WHERE c.id=m.code AND m.goat=$info->{id} AND active IS TRUE ".
                        "AND whenrun = 'conflict'";
                elog(DEBUG, "Running $SQL");
                my $count = spi_exec_query($SQL)->{processed};
                if (! $info->{standard_conflict} and ! $count) {
                    elog(ERROR, qq{Table "$schema.$table" must specify a way to handle conflicts});
                }
            }
        }
 
        ## Get an inventory of supporting objects
        my %found;
 
        ## Make sure this schema/table combo exists!
        my $oid;
        $SQL = qq{
            SELECT c.oid
            FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
            WHERE c.relnamespace = n.oid
            AND c.relkind = 'r'
            AND n.nspname = ?
            AND c.relname = ?
        };
        elog(DEBUG, "SQL: $SQL Args: $schema, $table");
        $sth = $dbh->prepare($SQL);
        my $count = $sth->execute($schema,$table);
        if ($count < 1) {
            $sth->finish();
            elog(ERROR, qq{No such table found for database $db: "$schema.$table"});
        }
        $oid = $sth->fetchall_arrayref()->[0][0];
 
        $cache->{oid}{$db}{$schema}{$table} = $oid;
 
        ## Make sure it does not have any verboten columns
        $SQL = qq{
            SELECT count(*)
            FROM pg_catalog.pg_attribute
            WHERE attrelid = $oid
            AND NOT attisdropped
            AND attname ~ '^BUCARDO';
        };
        $count = $dbh->selectall_arrayref($SQL)->[0][0];
        if ($count) {
            elog(ERROR, qq{Table "$schema.$table" contains a column starting with 'BUCARDO'});
        }
 
        $SQL = qq{
            SELECT count(*)
            FROM pg_catalog.pg_namespace
            WHERE nspname = ?
        };
        $sth = $dbh->prepare($SQL);
        $sth->execute('bucardo');
        $found{bc_schema} = $sth->fetchall_arrayref()->[0][0];
 
## If we are not a delta sync, we can jump to the ping part
        my $func0 = length($syncname) <= 42 ? "bucardo_triggerkick_$syncname" : "bkick_$syncname";
        my $trigger0 = "bucardo_triggerkick_$syncname";
$delta or goto PINGCHECK;
 
        ## (if bc_schema exists) Do these triggers exist on this table?
        my $trigger1 = "bucardo_add_delta";
        my $trigger2 = "bucardo_add_delta_binary";
 
        if (!$found{bc_schema}) {
            $found{trigger0} = $found{trigger1} = $found{trigger2} = 0;
        }
        else {
            $SQL = qq{
                SELECT max(t0), max(t1), max(t2)
                FROM (
                SELECT
                  CASE WHEN t.tgname = ? THEN 1 ELSE 0 END AS t0,
                  CASE WHEN t.tgname = ? THEN 1 ELSE 0 END AS t1,
                  CASE WHEN t.tgname = ? THEN 1 ELSE 0 END AS t2
                FROM pg_catalog.pg_trigger t, pg_catalog.pg_class c, pg_catalog.pg_namespace n
                WHERE c.relnamespace = n.oid AND t.tgrelid = c.oid AND c.relkind = 'r'
                AND n.nspname = ?
                AND c.relname = ?
                AND t.tgname IN (?,?,?)
              ) foo
            };
            elog(DEBUG, "Preparing to check triggers: $SQL");
            $sth = $dbh->prepare($SQL);
            $sth->execute($trigger0,$trigger1,$trigger2,$schema,$table,$trigger0,$trigger1,$trigger2);
            my $x = 0;
            my $row = $sth->fetchall_arrayref()->[0];
            for my $val (@$row) {
                $found{"trigger$x"} = $val;
                $x++;
            }
        }
 
        ## (if bc_schema exists) Do these supporting functions exist?
        (my $noquotepkey = $safepkey) =~ s/^"(.+)"$/$1/;
        my $func1 = length($noquotepkey) <= 45 ? "bucardo_add_delta_$noquotepkey" : "bad_$noquotepkey";
        my $func2 = length($noquotepkey) <= 38 ? "bucardo_add_delta_binary_$noquotepkey" : "badb_$noquotepkey";
        my $func3 = "bucardo_purge_delta";
        my $func4 = "bucardo_compress_delta";
        if ($safepkey =~ /^"/) {
           $func1 = qq{"$func1"};
           $func2 = qq{"$func2"};
        }
 
        if (!$found{bc_schema}) {
            $found{func0} = $found{func1} = $found{func2} = $found{func3} = $found{func4} = 0;
        }
        else {
            $SQL = qq{
                SELECT max(f0), max(f1), max(f2), max(f3)
                FROM (
                SELECT
                  CASE WHEN p.proname = ? THEN 1 ELSE 0 END AS f0,
                  CASE WHEN p.proname = ? THEN 1 ELSE 0 END AS f1,
                  CASE WHEN p.proname = ? THEN 1 ELSE 0 END AS f2,
                  CASE WHEN p.proname = ? THEN 1 ELSE 0 END AS f3,
                  CASE WHEN p.proname = ? THEN 1 ELSE 0 END AS f4
                FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n
                WHERE p.pronamespace = n.oid
                AND n.nspname = ?
                AND p.proname IN (?,?,?,?,?)
            ) foo
            };
            elog(DEBUG, "Preparing to check functions: $SQL");
            $sth = $dbh->prepare($SQL);
            $sth->execute($func0,$func1,$func2,$func3,$func4,'bucardo',$func0,$func1,$func2,$func3,$func4);
            my $x = 0;
            my $row = $sth->fetchall_arrayref()->[0];
            for my $val (@$row) {
                $found{"func$x"} = $val;
                $x++;
            }
        }
 
        ## (if bc_schema exists) Does this table exist?
 
        if (!$found{bc_schema}) {
            $found{bucardo_delta} = $found{bucardo_track} = $found{bucardo_delta_targets} = 0;
        }
        else {
            $SQL = qq{
                SELECT max(t1), max(t2), max(t3)
                FROM (
                SELECT
                  CASE WHEN c.relname = 'bucardo_delta' THEN 1 ELSE 0 END AS t1,
                  CASE WHEN c.relname = 'bucardo_track' THEN 1 ELSE 0 END AS t2,
                  CASE WHEN c.relname = 'bucardo_delta_targets' THEN 1 ELSE 0 END AS t3
                FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
                WHERE c.relnamespace = n.oid
                AND c.relkind = 'r'
                AND n.nspname = 'bucardo'
                AND c.relname IN ('bucardo_delta','bucardo_track','bucardo_delta_targets')
                ) foo
            };
            elog(DEBUG, "Preparing to check tables: $SQL");
            $sth = $dbh->prepare($SQL);
            $sth->execute();
            my $row = $sth->fetchall_arrayref()->[0];
            $found{'bucardo_delta'} = $row->[0];
            $found{'bucardo_track'} = $row->[1];
            $found{'bucardo_delta_targets'} = $row->[2];
        }
 
        ## (if bucardo_delta exists) Do these indexes exist?
        my $index1 = "bucardo_delta_${schema}_${table}_txn";
        my $l = length $index1;
        if ($l > 63) {
            if ($l <= 67) {
                $index1 = "bucardo_d_${schema}_${table}_txn";
            }
            elsif ($l <= 69) {
                $index1 = "bucardo_d_${schema}_${table}_t";
            }
            elsif ($l <= 76) {
                $index1 = "b_d_${schema}_${table}_t";
            }
            else {
                $index1 = "bucardo_delta_" . int(rand 9999999999);
            }
        }
        my $index2 = "bucardo_delta_${schema}_${table}_rowid";
        $l = length $index2;
        if ($l > 63) {
            if ($l <= 67) {
                $index2 = "bucardo_d_${schema}_${table}_rowid";
            }
            elsif ($l <= 71) {
                $index2 = "bucardo_d_${schema}_${table}_r";
            }
            elsif ($l <= 78) {
                $index2 = "b_d_${schema}_${table}_r";
            }
            else {
                $index2 = "bucardo_delta_" . int(rand 9999999999);
            }
        }
        my $index3 = "bucardo_track_target";
        my $index4 = "bucardo_delta_targetdb_unique";
        my $index5 = "bucardo_delta_txntime";
        my $safeindex1 = $index1;
        if ($safeindex1 =~ s/"/""/g or $safeindex1 =~ / /) {
            $safeindex1 = qq{"$safeindex1"};
        }
 
        if (!$found{bucardo_delta}) {
            $found{index1} = $found{index2} = $found{index3} = $found{index4} = $found{index5} = 0;
        }
        else {
            $SQL = qq{
                SELECT max(i1), max(i2), max(i3), max(i4), max(i5)
                FROM (
                SELECT
                  CASE WHEN c.relname = ? THEN 1 ELSE 0 END AS i1,
                  CASE WHEN c.relname = ? THEN 1 ELSE 0 END AS i2,
                  CASE WHEN c.relname = ? THEN 1 ELSE 0 END AS i3,
                  CASE WHEN c.relname = ? THEN 1 ELSE 0 END AS i4,
                  CASE WHEN c.relname = ? THEN 1 ELSE 0 END AS i5
                FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
                WHERE c.relnamespace = n.oid
                AND c.relkind = 'i'
                AND n.nspname = ?
                AND c.relname IN (?,?,?,?,?)
                ) foo
            };
            elog(DEBUG, "Preparing to check indexes: $SQL");
            $sth = $dbh->prepare($SQL);
            $sth->execute($index1,$index2,$index3,$index4,$index5,'bucardo',$index1,$index2,$index3,$index4,$index5);
            my $x = 1;
            my $row = $sth->fetchall_arrayref()->[0];
            for my $val (@$row) {
                $found{"index$x"} = $val;
                $x++;
            }
        }
 
      PINGCHECK:
        ## Goat-level ping overrides the sync-level ping - source only
        if (defined $info->{goatping} and $is_source) {
            $ping = $info->{goatping} eq 't' ? 1 : 0;
            elog(DEBUG, "Got goatping as $info->{goatping}, force ping to $ping");
        }
 
        ## If this is not set as ping, remove the trigger - the only one safe to remove
        if (!$ping and $found{trigger0}) {
            run_sql(qq{DROP TRIGGER "$trigger0" ON $safeschema.$safetable}, $dbh);
        }
 
        ## Schema is needed for both ping and delta
        if (($ping or $delta) and !$found{bc_schema}) {
            run_sql('CREATE SCHEMA bucardo', $dbh);
        }
 
        ## Ping needs a special function and trigger
if ($ping) {
my $custom_trigger_level;
my $custom_function_name;
$SQL = qq{ SELECT trigger_language,trigger_body,trigger_level FROM bucardo_custom_trigger
WHERE goat=$info->{id}
AND status='active'
AND trigger_type='triggerkick' };
elog(DEBUG, "Running $SQL");
$rv = spi_exec_query($SQL);
 
            if (!$found{func0} or $force or $rv->{processed}) {
 
if($rv->{processed}) {
$custom_function_name = $func0 . "_" . $info->{tablename};
$custom_trigger_level = $rv->{rows}[0]{trigger_level};
$SQL = qq{
CREATE OR REPLACE FUNCTION bucardo."$custom_function_name"()
RETURNS TRIGGER
LANGUAGE $rv->{rows}[0]{trigger_language}
AS \$notify\$
};
$SQL .= qq{ $rv->{rows}[0]{trigger_body} };
$SQL .= qq{ \$notify\$; };
}
else {
                 $SQL = qq{
CREATE OR REPLACE FUNCTION bucardo."$func0"()
RETURNS TRIGGER
LANGUAGE plpgsql
AS \$notify\$
BEGIN
EXECUTE 'NOTIFY "bucardo_kick_sync_$syncname"';
 
RETURN NEW;
END;
\$notify\$;
};
}
                run_sql($SQL,$dbh);
            }
            if (!$found{trigger0}) {
if($custom_trigger_level && $custom_function_name) {
                 $SQL = qq{
CREATE TRIGGER "$trigger0"
AFTER INSERT OR UPDATE OR DELETE ON $safeschema.$safetable
FOR EACH $custom_trigger_level EXECUTE PROCEDURE bucardo."$custom_function_name"()
};
} else
{
$SQL = qq{
CREATE TRIGGER "$trigger0"
AFTER INSERT OR UPDATE OR DELETE ON $safeschema.$safetable
FOR EACH STATEMENT EXECUTE PROCEDURE bucardo."$func0"()
};
}
run_sql($SQL,$dbh);
            }
        }
 
        ## If this is not a delta, clean up what we can
        if (!$delta) {
            if ($found{bucardo_delta}) {
                elog(DEBUG, "Warning! NOT removing rows from bucardo.bucardo_delta where tablename=$oid ($table)");
            }
            if ($found{'bucardo_track'}) {
                elog(DEBUG, "Warning! NOT removing rows from bucardo.bucardo_track where tablename=$oid ($table)");
            }
            $found{index1} and elog(DEBUG, "Warning! NOT removing index bucardo.$safeindex1");
            $found{trigger1} and elog(DEBUG, "Warning! NOT removing trigger $trigger1 from $safeschema.$safetable");
            $found{trigger2} and elog(DEBUG, "Warning! NOT removing trigger $trigger2 from $safeschema.$safetable");
 
            ## Clean up the functions if nobody else is using them
            if ($found{func1} or $found{func2}) {
                $SQL = qq{
                    SELECT count(*)
                    FROM pg_catalog.pg_trigger
                    WHERE tgfoid = (
                        SELECT oid
                        FROM pg_proc
                        WHERE proname = ?
                        AND pronamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = ?)
                    )
                };
                elog(DEBUG, "SQL: $SQL");
                $sth = $dbh->prepare($SQL);
                if ($found{func1}) {
                    $sth->execute($func1,'bucardo');
                    if (! $sth->fetchall_arrayref()->[0][0]) {
                        elog(DEBUG, "Warning! NOT removing function bucardo.$func1()");
                    }
                }
                if ($found{func2}) {
                    $sth = $dbh->prepare($SQL);
                    $sth->execute($func2,'bucardo');
                    if (! $sth->fetchall_arrayref()->[0][0]) {
                        elog(DEBUG, "Warning! NOT removing function bucardo.$func2()");
                    }
                }
            }
        } ## end if not delta
        else {
 
            my @pkeys = split (/\|/ => $pkey);
            my $numpkeys = @pkeys;
 
            if (! $found{bucardo_delta}) {
                $SQL = qq{
                    CREATE TABLE bucardo.bucardo_delta (
                        tablename OID NOT NULL,
                        rowid TEXT NOT NULL,
                        txntime TIMESTAMPTZ NOT NULL DEFAULT now()
                    );
                };
                run_sql($SQL,$dbh);
                for (2..$numpkeys) {
                    $SQL = "ALTER TABLE bucardo.bucardo_delta ADD rowid$_ TEXT NULL";
                    run_sql($SQL,$dbh);
                }
            }
 
            if (! $found{'bucardo_track'}) {
                $SQL = qq{
                    CREATE TABLE bucardo.bucardo_track (
                        txntime TIMESTAMPTZ NOT NULL,
                        tablename OID NOT NULL,
                        targetdb TEXT NOT NULL
                    );
                };
                run_sql($SQL,$dbh);
            }
            if (! $found{bucardo_delta_targets}) {
                $SQL = qq{
                    CREATE TABLE bucardo.bucardo_delta_targets (
                        tablename OID NOT NULL,
                        targetdb TEXT NOT NULL,
                        cdate TIMESTAMPTZ NOT NULL DEFAULT now()
                    );
                };
                run_sql($SQL,$dbh);
            }
            if ($force) {
                if ($found{index1}) {
                    elog(DEBUG, qq{Dropping index "$safeindex1"});
                    $dbh->do(qq{DROP INDEX bucardo."$safeindex1"});
                    $found{index1} = 0;
                }
                if ($found{index2}) {
                    elog(DEBUG, qq{Dropping index "$index2"});
                    $dbh->do(qq{DROP INDEX bucardo."$index2"});
                    $found{index2} = 0;
                }
            }
            if (! $found{index1}) {
                $dbh->do(qq{CREATE INDEX "$safeindex1" ON bucardo.bucardo_delta(txntime) WHERE tablename = $oid});
            }
            for (2..$numpkeys) {
                my $colname = "rowid$_";
                $SQL = "SELECT count(*) FROM pg_catalog.pg_attribute a, pg_class c, pg_namespace n ".
                    "WHERE c.relnamespace = n.oid AND n.nspname = 'bucardo' AND a.attrelid = c.oid ".
                        "AND c.relname = 'bucardo_delta' AND a.attname = '$colname'";
                $count = $dbh->selectall_arrayref($SQL)->[0][0];
                if ($count != 1) {
                    $SQL = "ALTER TABLE bucardo.bucardo_delta ADD $colname TEXT NULL";
                    $dbh->do($SQL);
                }
            }
            if (! $found{index2}) {
                my @pkeytypes = split (/\|/ => $pkeytype);
                if (1 == @pkeytypes) {
                    my $safepkeytype = $pkeytype =~ /timestamp|timestamptz|date|bytea/ ? 'text' : $pkeytype;
                    elog(DEBUG, "Create index $index2 on bucardo_delta(rowid::$safepkeytype)");
                    $dbh->do(qq{CREATE INDEX "$index2" ON bucardo.bucardo_delta((rowid::$safepkeytype)) WHERE tablename = $oid});
                }
                else {
                    my $x = 0;
                    my $multicol = join ',' => map { s{timestamp|timestamptz|date|bytea}{text}; $x++;
                        sprintf "(rowid%s::$_)", $x<2 ? '' : $x; } split (/\|/ => $pkeytype);
                    elog(DEBUG, "Creating index on $multicol");
                    $dbh->do(qq{CREATE INDEX "$index2" ON bucardo.bucardo_delta($multicol) WHERE tablename = $oid});
                }
            }
            if (! $found{index3}) {
                $dbh->do(qq{CREATE INDEX "$index3" ON bucardo.bucardo_track(tablename,txntime,targetdb)});
            }
            if (! $found{index4}) {
                $dbh->do(qq{CREATE UNIQUE INDEX "$index4" ON bucardo.bucardo_delta_targets(tablename,targetdb)});
            }
            if (! $found{index5}) {
                $dbh->do(qq{CREATE INDEX "$index5" ON bucardo.bucardo_delta(txntime)});
            }
            my $rowids = 'rowid';
            for (2..$numpkeys) {
                $rowids .= ",rowid$_";
            }
            my $new = join ',' => map { qq{NEW."$_"} } @pkeys;
            my $old = join ',' => map { qq{OLD."$_"} } @pkeys;
            my $clause = join ' OR ' => map { qq{OLD."$_" <> NEW."$_"} } @pkeys;
 
$SQL = qq{ SELECT trigger_language,trigger_body FROM bucardo_custom_trigger
WHERE goat=$info->{id}
AND status='active'
AND trigger_type='delta' };
elog(DEBUG, "Running $SQL");
$rv = spi_exec_query($SQL);
 
if(! $found{func1} and $rv->{processed}) {
$SQL = qq{
CREATE OR REPLACE FUNCTION bucardo.$func1()
                    RETURNS TRIGGER
                    LANGUAGE $rv->{rows}[0]{trigger_language}
SECURITY DEFINER
                    VOLATILE
                    AS
                    \$clone\$
};
$SQL .= qq{ $rv->{rows}[0]{trigger_body} };
$SQL .= qq{ \$clone\$; };
run_sql($SQL,$dbh);
}
elsif (! $found{func1} and $pkeytype ne 'bytea') {
                $SQL = qq{
                    CREATE OR REPLACE FUNCTION bucardo.$func1()
                    RETURNS TRIGGER
                    LANGUAGE plpgsql
                    SECURITY DEFINER
                    VOLATILE
                    AS
                    \$clone\$
                    BEGIN
                        IF (TG_OP = 'INSERT') THEN
                          INSERT INTO bucardo.bucardo_delta(tablename,$rowids) VALUES (TG_RELID, $new);
                        ELSIF (TG_OP = 'UPDATE') THEN
                          INSERT INTO bucardo.bucardo_delta(tablename,$rowids) VALUES (TG_RELID, $old);
                          IF ($clause) THEN
                            INSERT INTO bucardo.bucardo_delta(tablename,$rowids) VALUES (TG_RELID, $new);
                          END IF;
                        ELSE
                          INSERT INTO bucardo.bucardo_delta(tablename,$rowids) VALUES (TG_RELID, $old);
                        END IF;
                        RETURN NULL;
                    END;
                    \$clone\$;
                };
                run_sql($SQL,$dbh);
            }
            elsif (! $found{func2} and $pkeytype eq 'bytea') {
                $SQL = qq{
                    CREATE OR REPLACE FUNCTION bucardo.$func2()
                    RETURNS TRIGGER
                    LANGUAGE plpgsql
                    SECURITY DEFINER
                    VOLATILE
                    AS
                    \$clone\$
                    BEGIN
                        IF (TG_OP = 'INSERT') THEN
                          INSERT INTO bucardo.bucardo_delta(tablename,rowid) VALUES (TG_RELID,
                                   ENCODE(NEW.${safepkey},'base64'));
                        ELSIF (TG_OP = 'UPDATE') THEN
                          INSERT INTO bucardo.bucardo_delta(tablename,rowid) VALUES (TG_RELID,
                                   ENCODE(OLD.$safepkey,'base64'));
                          IF (ENCODE(OLD.$safepkey,'base64') <> ENCODE(NEW.$safepkey,'base64')) THEN
                            INSERT INTO bucardo.bucardo_delta(tablename,rowid) VALUES (TG_RELID,
                                   ENCODE(NEW.$safepkey,'base64'));
                          END IF;
                        ELSE
                          INSERT INTO bucardo.bucardo_delta(tablename,rowid) VALUES (TG_RELID,
                                   ENCODE(OLD.$safepkey,'base64'));
                        END IF;
                        RETURN NULL;
                    END;
                    \$clone\$;
                };
                run_sql($SQL,$dbh);
            }
            if (! $found{func3}) {
                $SQL = qq{
                    CREATE OR REPLACE FUNCTION bucardo.bucardo_purge_delta(interval)
                    RETURNS TEXT
                    LANGUAGE plpgsql
                    VOLATILE
                    SECURITY DEFINER
                    AS
                    \$clone\$
                    DECLARE
                        drows BIGINT;
                        trows BIGINT;
                    BEGIN
                        DELETE FROM bucardo.bucardo_delta
                        USING
                        (SELECT b.tablename AS tn, b.txntime AS tt FROM
                        (SELECT tablename, count(*) FROM bucardo.bucardo_delta_targets GROUP BY 1) AS a,
                        (SELECT tablename, txntime, count(*) FROM bucardo.bucardo_track GROUP BY 1,2) AS b
                        WHERE a.tablename = b.tablename
                        AND a.count=b.count) AS foo
                        WHERE tablename = tn AND txntime = tt
                        AND txntime < now()-\$1;
 
                        GET DIAGNOSTICS drows := row_count;
 
                        DELETE FROM bucardo.bucardo_track
                        WHERE NOT EXISTS (SELECT 1 FROM bucardo.bucardo_delta d WHERE d.txntime = bucardo_track.txntime);
 
                        GET DIAGNOSTICS trows := row_count;
 
                        RETURN 'Rows deleted from bucardo_delta: '||drows||
                          ' Rows deleted from bucardo_track: '||trows;
 
                  END;
                    \$clone\$;
                };
                run_sql($SQL,$dbh);
            }
            if (! $found{func4}) {
                $SQL = qq{
                  CREATE OR REPLACE FUNCTION bucardo.bucardo_compress_delta(text, text)
                  RETURNS TEXT
                  LANGUAGE plpgsql
                  SECURITY DEFINER
                  AS \$clone\$
                  DECLARE
                    mymode TEXT;
                    myoid OID;
                    myst TEXT;
                    got2 bool;
                    drows BIGINT = 0;
                    trows BIGINT = 0;
                    rnames TEXT;
                    rname TEXT;
                    ids_where TEXT;
                    ids_sel TEXT;
                    ids_grp TEXT;
                    idnum TEXT;
                  BEGIN
                  
                  -- Are we running in serializable mode?
                  SELECT INTO mymode current_setting('transaction_isolation');
                  IF (mymode <> 'serializable') THEN
                    RAISE EXCEPTION 'This function must be run in serializable mode';
                  END IF;
                  
                  -- Grab the oid of this schema/table combo
                  SELECT INTO myoid
                    c.oid FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
                    WHERE nspname = \$1 AND relname = \$2;
                  
                  IF NOT FOUND THEN
                    RAISE EXCEPTION 'No such table: %.%', \$1, \$2;
                  END IF;
                  
                  ids_where = 'COALESCE(rowid,''NULL'') = COALESCE(id, ''NULL'')';
                  ids_sel = 'rowid AS id';
                  ids_grp = 'rowid';
                  FOR rname IN SELECT attname FROM pg_attribute WHERE attrelid =
                          (SELECT oid FROM pg_class WHERE relname = 'bucardo_delta'
                          AND relnamespace =
                          (SELECT oid FROM pg_namespace WHERE
                          nspname = 'bucardo') AND attname ~ '^rowid'
                      ) LOOP
                      rnames = COALESCE(rnames || ' ', '') || rname ;
                      SELECT INTO idnum SUBSTRING(rname FROM '[[:digit:]]+');
                      IF idnum IS NOT NULL THEN
                        ids_where = ids_where
                          || ' AND ('
                          || rname
                          || ' = id'
                          || idnum
                          || ' OR ('
                          || rname
                          || ' IS NULL AND id'
                          || idnum
                          || ' IS NULL))';
                        ids_sel = ids_sel
                          || ', '
                          || rname
                          || ' AS id'
                          || idnum;
                        ids_grp = ids_grp
                          || ', '
                          || rname;
                      END IF;
                  END LOOP;
                  
                  myst = 'DELETE FROM bucardo.bucardo_delta
                  USING (SELECT MAX(txntime) AS maxt, '||ids_sel||'
                         FROM bucardo.bucardo_delta
                         WHERE tablename = '||myoid||'
                         GROUP BY ' || ids_grp || ') AS foo
                  WHERE tablename = '|| myoid || ' AND ' || ids_where ||' AND txntime <> maxt';
                  RAISE DEBUG 'Running %', myst;
                  EXECUTE myst;
                  
                  GET DIAGNOSTICS drows := row_count;
                  
                  myst = 'DELETE FROM bucardo.bucardo_track'
                    || ' WHERE NOT EXISTS (SELECT 1 FROM bucardo.bucardo_delta d WHERE d.txntime = bucardo_track.txntime)';
                  EXECUTE myst;
                  
                  GET DIAGNOSTICS trows := row_count;
                  
                  RETURN 'Compressed '||\$1||'.'||\$2||'. Rows deleted from bucardo_delta: '||drows||
                    ' Rows deleted from bucardo_track: '||trows;
                  
                  END;
                  \$clone\$;
                };
                run_sql($SQL,$dbh);
 
                $SQL = qq{
                  CREATE OR REPLACE FUNCTION bucardo.bucardo_compress_delta(text)
                  RETURNS TEXT
                  LANGUAGE SQL
                  SECURITY DEFINER
                  AS \$clone\$
                   SELECT bucardo.bucardo_compress_delta(n.nspname, c.relname) FROM pg_class c
                   JOIN pg_namespace n ON n.oid = c.relnamespace
                   WHERE relname = \$1 AND pg_table_is_visible(c.oid);
                  \$clone\$;
                };
                run_sql($SQL,$dbh);
                  
                $SQL = qq{
                  CREATE OR REPLACE FUNCTION bucardo.bucardo_compress_delta(oid)
                  RETURNS TEXT
                  LANGUAGE SQL
                  SECURITY DEFINER
                  AS \$clone\$
                   SELECT bucardo.bucardo_compress_delta(n.nspname, c.relname) FROM pg_class c
                   JOIN pg_namespace n ON n.oid = c.relnamespace
                   WHERE c.oid = \$1;
                  \$clone\$;
                };
                run_sql($SQL,$dbh);
                  
                $SQL = qq{
                  CREATE OR REPLACE FUNCTION bucardo.bucardo_compress_delta()
                  RETURNS SETOF TEXT
                  LANGUAGE SQL
                  SECURITY DEFINER
                  AS \$clone\$
                   SELECT bucardo.bucardo_compress_delta(n.nspname, c.relname) FROM pg_class c
                   JOIN pg_namespace n ON n.oid = c.relnamespace
                   WHERE c.oid IN (SELECT DISTINCT tablename FROM bucardo.bucardo_delta);
                  \$clone\$;
                };
                run_sql($SQL,$dbh);
            }
                  
            if (! $found{trigger1} and $pkeytype ne 'bytea') {
                $SQL = qq{
                    CREATE TRIGGER "$trigger1"
                    AFTER INSERT OR UPDATE OR DELETE ON $safeschema.$safetable
                    FOR EACH ROW EXECUTE PROCEDURE bucardo.$func1()
                };
                run_sql($SQL,$dbh);
            }
            if (! $found{trigger2} and $pkeytype eq 'bytea') {
                $SQL = qq{
                    CREATE TRIGGER "$trigger2"
                    AFTER INSERT OR UPDATE OR DELETE ON $safeschema.$safetable
                    FOR EACH ROW EXECUTE PROCEDURE bucardo.$func2()
                };
                run_sql($SQL,$dbh);
            }
 
        } ## end if delta
 
        $dbh->commit();
        return 1;
 
    } ## end of bucardo_validate_table
 
    for (values %{$cache{dbh}}) {
        $_->disconnect();
    }
 
    return 'MODIFY';
$bc$;
 
CREATE OR REPLACE FUNCTION bucardo.validate_sync(text)
RETURNS TEXT
LANGUAGE SQL
AS
$bc$
SELECT bucardo.validate_sync($1,0);
$bc$;
 
CREATE OR REPLACE FUNCTION bucardo.validate_all_syncs(integer)
RETURNS INTEGER
LANGUAGE plpgsql
AS
$bc$
DECLARE count INTEGER = 0; myrec RECORD;
BEGIN
FOR myrec IN SELECT name FROM sync ORDER BY name LOOP
  PERFORM validate_sync(myrec.name, $1);
  count = count + 1;
END LOOP;
RETURN count;
END;
$bc$;
 
CREATE OR REPLACE FUNCTION bucardo.validate_all_syncs()
RETURNS INTEGER
LANGUAGE SQL
AS
$bc$
SELECT validate_all_syncs(0);
$bc$;
 
CREATE FUNCTION validate_sync()
RETURNS TRIGGER
LANGUAGE plperlu
SECURITY DEFINER
AS $bc$
 
    use strict; use warnings;
 
    elog(DEBUG, "Starting validate_sync trigger");
    my $new = $_TD->{new};
    my $found=0;
 
    ## If insert, we always do the full validation:
 
    if ($_TD->{event} eq 'INSERT') {
        elog(DEBUG, "Found insert, will call validate_sync");
        $found = 1;
    }
    else {
        my $old = $_TD->{old};
        for my $x (qw(name source targetdb targetgroup synctype ping)) {
            elog(DEBUG, "Checking on $x");
            if (! defined $old->{$x}) {
                next if ! defined $new->{$x};
            }
            elsif (defined $new->{$x} and $new->{$x} eq $old->{$x}) {
                next;
            }
            $found=1;
            last;
        }
    }
    if ($found) {
        spi_exec_query("SELECT validate_sync('$new->{name}')");
    }
    return;
$bc$;
 
CREATE TRIGGER validate_sync
  AFTER INSERT OR UPDATE ON sync
  FOR EACH ROW EXECUTE PROCEDURE validate_sync();
 
CREATE FUNCTION bucardo_delete_sync()
RETURNS TRIGGER
LANGUAGE plperlu
SECURITY DEFINER
AS $bc$
 
    use strict; use warnings;
 
    elog(DEBUG, "Starting delete_sync trigger");
 
    my $old = $_TD->{old};
 
    ## If this sync was fullcopy, we don't need to worry about it
    return if $old->{synctype} eq 'fullcopy';
 
    my ($SQL, $rv, $sth, $count);
 
    ## Gather up a list of tables used in this sync, as well as the source database handle
 
    (my $herd = $old->{source}) =~ s/'/''/go;
    $SQL = qq{
        SELECT db, pg_catalog.quote_ident(schemaname) AS safeschema,
                   pg_catalog.quote_ident(tablename) AS safetable
        FROM goat g, herdmap h
        WHERE g.id = h.goat
        AND h.herd = '$herd'
    };
    $rv = spi_exec_query($SQL);
    $rv->{processed} or die qq{No such herd: $herd};
    my $sourcedb = $rv->{rows}[0]{db};
    elog(DEBUG, "Got a sourcedb of $sourcedb for herd of $herd");
    my %relation;
    for (@{$rv->{rows}}) {
        $relation{$_->{safeschema}}{$_->{safetable}} = $_;
    }
    $rv = spi_exec_query("SELECT bucardo.db_getconn('$sourcedb') AS conn");
    $rv->{processed} or die qq{Could not find a database named "$sourcedb"};
    my ($dsn,$user,$pass) = split /\n/ => $rv->{rows}[0]{conn};
    elog(DEBUG, "Connecting to $dsn as $user inside bucardo_delete_sync");
    my $sdbh = DBI->connect($dsn, $user, $pass, {AutoCommit=>0, RaiseError=>1, PrintError=>0});
 
    ## Get the list of target databases
 
    my %target;
    if ($old->{targetdb}) {
       $target{$old->{targetdb}} = 1;
    }
    else {
        my $group = $old->{targetgroup};
        $group =~ s/'/''/g;
        $SQL = "SELECT db FROM dbmap WHERE dbgroup = '$group'";
        $rv = spi_exec_query($SQL);
        $rv->{processed} or die qq{Could not find the dbgroup $group};
        for (@{$rv->{rows}}) {
            $target{$_->{db}} = 1;
        }
    }
 
    ## Try and delete each combo
    $SQL = "DELETE FROM bucardo.bucardo_delta_targets WHERE targetdb = ? AND tablename = ".
        "(SELECT c.oid FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n WHERE c.relname = ? ".
        "AND c.relnamespace = n.oid AND n.nspname = ?)";
    $sth = $sdbh->prepare($SQL);
    for my $schema (sort keys %relation) {
        for my $table (sort keys %{$relation{$schema}}) {
            for my $target (keys %target) {
                $count = $sth->execute($target,$table,$schema);
                elog(DEBUG,"Tried to remove $schema:$table for $target, got $count");
            }
        }
    }
    $sdbh->commit();
 
    return if $old->{synctype} eq 'pushdelta';
 
    ## Finally, remove from the target database
    (my $targetdb = $old->{targetdb}) =~ s/'/''/go;
    $rv = spi_exec_query("SELECT bucardo.db_getconn('$targetdb') AS conn");
    $rv->{processed} or die qq{Could not find a database named "$targetdb"};
    ($dsn,$user,$pass) = split /\n/ => $rv->{rows}[0]{conn};
    elog(DEBUG, "Connecting to $dsn as $user inside bucardo_delete_sync");
    my $tdbh = DBI->connect($dsn, $user, $pass, {AutoCommit=>0, RaiseError=>1, PrintError=>0});
 
    $sth = $tdbh->prepare($SQL);
    for my $schema (sort keys %relation) {
        for my $table (sort keys %{$relation{$schema}}) {
            $count = $sth->execute($sourcedb,$table,$schema);
            elog(DEBUG,"Tried to remove $schema:$table for $sourcedb, got $count");
        }
    }
    $tdbh->commit();
 
    return;
 
$bc$;
 
CREATE TRIGGER bucardo_delete_sync
  AFTER DELETE ON sync
  FOR EACH ROW EXECUTE PROCEDURE bucardo_delete_sync();
 
CREATE SEQUENCE customcode_id_seq;
CREATE TABLE customcode (
  id INTEGER NOT NULL DEFAULT nextval('customcode_id_seq'),
             CONSTRAINT customcode_id_pk PRIMARY KEY (id),
  name TEXT NOT NULL UNIQUE,
  about TEXT NULL,
  whenrun TEXT NOT NULL,
  getdbh BOOLEAN NOT NULL DEFAULT 'true',
  getrows BOOLEAN NOT NULL DEFAULT 'false',
  src_code TEXT NOT NULL,
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
ALTER TABLE customcode ADD CONSTRAINT customcode_whenrun
CHECK (whenrun IN (
'before_txn',
'before_check_rows',
'before_trigger_drop',
'after_trigger_drop',
'after_table_sync',
'exception',
'conflict',
'before_trigger_enable',
'after_trigger_enable',
'after_txn',
'before_sync',
'after_sync'
));
 
CREATE TABLE customcode_map (
  code INTEGER NOT NULL,
             CONSTRAINT customcode_map_code_fk FOREIGN KEY (code) REFERENCES customcode(id) ON DELETE CASCADE,
  sync TEXT NULL,
             CONSTRAINT customcode_map_sync_fk FOREIGN KEY (sync) REFERENCES sync(name) ON DELETE SET NULL,
  goat INTEGER NULL,
             CONSTRAINT customcode_map_goat_fk FOREIGN KEY (goat) REFERENCES goat(id) ON DELETE SET NULL,
  active BOOLEAN NOT NULL DEFAULT 'true',
  priority SMALLINT NOT NULL DEFAULT 0,
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
ALTER TABLE customcode_map ADD CONSTRAINT customcode_map_syncgoat
CHECK (sync IS NULL OR goat IS NULL);
 
CREATE UNIQUE INDEX customcode_map_unique_sync ON customcode_map(code,sync) WHERE sync IS NOT NULL;
CREATE UNIQUE INDEX customcode_map_unique_goat ON customcode_map(code,goat) WHERE goat IS NOT NULL;
 
 
CREATE OR REPLACE FUNCTION find_unused_goats()
RETURNS SETOF text
LANGUAGE plpgsql
AS $bc$
DECLARE
  myrec RECORD;
BEGIN
  FOR myrec IN
    SELECT quote_ident(db) || '.' || quote_ident(schemaname) || '.' || quote_ident(tablename) AS t
      FROM goat g
      WHERE NOT EXISTS (SELECT 1 FROM herdmap h WHERE h.goat = g.id)
      ORDER BY schemaname, tablename
    LOOP
      RETURN NEXT 'Not used in any herds: ' || myrec.t;
  END LOOP;
 
  FOR myrec IN
    SELECT quote_ident(db) || '.' || quote_ident(schemaname) || '.' || quote_ident(tablename) AS t
      FROM goat g
      JOIN herdmap h ON h.goat = g.id
      WHERE NOT EXISTS (SELECT 1 FROM sync WHERE source = h.herd)
      ORDER BY schemaname, tablename
    LOOP
      RETURN NEXT 'Not used in source herd: ' || myrec.t;
  END LOOP;
 
  FOR myrec IN
    SELECT quote_ident(db) || '.' || quote_ident(schemaname) || '.' || quote_ident(tablename) AS t
      FROM goat g
      JOIN herdmap h ON h.goat = g.id
      WHERE NOT EXISTS (SELECT 1 FROM sync WHERE source = h.herd AND status = 'active')
      ORDER BY schemaname, tablename
    LOOP
      RETURN NEXT 'Not used in source herd of active sync: ' || myrec.t;
  END LOOP;
 
  RETURN;
END;
$bc$;
 
-- Monitor how long data takes to move over, from commit to commit
CREATE TABLE bucardo_rate (
  sync TEXT NOT NULL,
  goat INTEGER NOT NULL,
  target TEXT NULL,
  mastercommit TIMESTAMPTZ NOT NULL,
  slavecommit TIMESTAMPTZ NOT NULL,
  total INTEGER NOT NULL
);
CREATE INDEX bucardo_rate_sync ON bucardo_rate(sync);
 
-- Keep track of any upgrades as we go along
CREATE TABLE upgrade_log (
  action TEXT NOT NULL,
  summary TEXT NOT NULL,
  version TEXT NOT NULL,
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
 
-- Allow users to insert messages in the Bucardo logs
 
CREATE FUNCTION bucardo_log_message_notify()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $bc$
BEGIN
  EXECUTE 'NOTIFY "bucardo_log_message"';
  RETURN NULL;
END;
$bc$;
 
CREATE TABLE bucardo_log_message (
  msg TEXT NOT NULL,
  cdate TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TRIGGER bucardo_log_message_trigger
  AFTER INSERT ON bucardo_log_message
  FOR EACH STATEMENT EXECUTE PROCEDURE bucardo_log_message_notify();
 
 
--
-- END OF THE SCHEMA
--