-
Notifications
You must be signed in to change notification settings - Fork 20
/
powa--5.0.0.sql
7369 lines (6695 loc) · 293 KB
/
powa--5.0.0.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
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
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION powa" to load this file. \quit
SET LOCAL statement_timeout = 0;
SET LOCAL client_encoding = 'UTF8';
SET LOCAL standard_conforming_strings = on;
SET LOCAL client_min_messages = warning;
SET LOCAL search_path = pg_catalog;
-- This table cannot be dumped as postgres doesn't offer a way to restore only
-- some columns. Even if it was possible, we couldn't assume that users would
-- choose to restore the roles too.
CREATE TABLE @[email protected]_roles (
powa_role name NOT NULL PRIMARY KEY,
rolname name,
CHECK (powa_role IN ('powa_admin', 'powa_read_all_data',
'powa_read_all_metrics', 'powa_write_all_data',
'powa_snapshot', 'powa_signal_backend'))
);
INSERT INTO @[email protected]_roles
(powa_role, rolname) VALUES
('powa_admin', NULL),
('powa_read_all_data', NULL),
('powa_read_all_metrics', NULL),
('powa_write_all_data', NULL),
('powa_snapshot', NULL),
('powa_signal_backend', NULL);
CREATE FUNCTION @[email protected]_powa_roles(
IN reuse_existing_role bool default FALSE,
IN admin_role text DEFAULT 'powa_admin',
IN read_all_data_role text DEFAULT 'powa_read_all_data',
IN read_all_metrics_role text DEFAULT 'powa_read_all_metrics',
IN write_all_data_role text DEFAULT 'powa_write_all_data',
IN snapshot_role text DEFAULT 'powa_snapshot',
IN signal_backend_role text DEFAULT 'powa_signal_backend'
) RETURNS void
AS $$
DECLARE
v_roles name[];
v_role name;
v_rec record;
v_nb integer;
BEGIN
v_roles = ARRAY [admin_role, read_all_data_role, read_all_metrics_role,
write_all_data_role, snapshot_role, signal_backend_role];
-- Preliminary sanity checks for the reuse_existing_role case
IF (reuse_existing_role) THEN
SELECT count(*) INTO v_nb
FROM @[email protected]_roles
WHERE rolname IS NOT NULL;
IF (v_nb != 0) THEN
RAISE EXCEPTION 'Cannot chang existing roles';
END IF;
FOR v_rec in SELECT *
FROM pg_catalog.pg_roles
WHERE rolname = ANY (v_roles)
LOOP
IF v_rec.rolsuper THEN
RAISE EXCEPTION 'Existing role % is a superuser', v_rec.rolname;
ELSIF v_rec.rolcreaterole THEN
RAISE EXCEPTION 'Existing role % can create role', v_rec.rolname;
ELSIF v_rec.rolcreatedb THEN
RAISE EXCEPTION 'Existing role % can create db', v_rec.rolname;
ELSIF v_rec.rolcanlogin THEN
RAISE EXCEPTION 'Existing role % can login', v_rec.rolname;
ELSIF v_rec.rolreplication THEN
RAISE EXCEPTION 'Existing role % is a replication role', v_rec.rolname;
ELSIF v_rec.rolbypassrls THEN
RAISE EXCEPTION 'Existing role % can bypass RLS', v_rec.rolname;
END IF;
END LOOP;
END IF;
-- Update the powa_roles information. This will be rollbacked if any issue
-- is later found with it
UPDATE @[email protected]_roles
SET rolname = CASE
WHEN powa_role = 'powa_admin' THEN admin_role
WHEN powa_role = 'powa_read_all_data' THEN read_all_data_role
WHEN powa_role = 'powa_read_all_metrics' THEN read_all_metrics_role
WHEN powa_role = 'powa_write_all_data' THEN write_all_data_role
WHEN powa_role = 'powa_snapshot' THEN snapshot_role
WHEN powa_role = 'powa_signal_backend' THEN signal_backend_role
END;
IF (reuse_existing_role) THEN
SELECT count(*) INTO v_nb
FROM @[email protected]_roles p
LEFT JOIN pg_catalog.pg_roles r USING (rolname)
WHERE r.rolname IS NULL;
IF (v_nb != 0) THEN
RAISE EXCEPTION 'Cannot reuse existing powa roles unless all roles already exist';
END IF;
ELSE
SELECT count(*) INTO v_nb
FROM pg_catalog.pg_roles
WHERE rolname = ANY (v_roles);
IF v_nb != 0 THEN
RAISE EXCEPTION 'Some roles already exists';
END IF;
FOREACH v_role IN ARRAY v_roles LOOP
EXECUTE format('CREATE ROLE %I NOLOGIN', v_role);
END LOOP;
END IF;
-- Final add all the required ACL based on the up-to-date powa_roles table
PERFORM @[email protected]_grant();
END;
$$ LANGUAGE plpgsql STRICT
SET search_path = pg_catalog; /* end if setup_powa_roles */
CREATE TABLE @[email protected]_servers (
id serial PRIMARY KEY,
hostname text NOT NULL,
alias text,
port integer NOT NULL,
username text NOT NULL,
password text,
dbname text NOT NULL,
frequency integer NOT NULL default 300 CHECK (frequency = -1 OR frequency >= 5),
powa_coalesce integer NOT NULL default 100 CHECK (powa_coalesce >= 5),
retention interval NOT NULL default '1 day'::interval,
allow_ui_connection boolean NOT NULL default true,
version text,
UNIQUE (hostname, port),
UNIQUE(alias)
);
INSERT INTO @[email protected]_servers VALUES (0, '', '<local>', 0, '', NULL, '', -1, 100, '0 second');
CREATE TABLE @[email protected]_extensions (
extname text NOT NULL PRIMARY KEY,
external boolean NOT NULL default true,
added_manually boolean NOT NULL default true
);
INSERT INTO @[email protected]_extensions
(extname, external, added_manually) VALUES
('powa', false, false),
('hypopg', false, false),
('pg_stat_statements', false, false),
('pg_qualstats', false, false),
('pg_stat_kcache', false, false),
('pg_track_settings', true, false),
('pg_wait_sampling', false, false);
CREATE TABLE @[email protected]_extension_functions (
extname text NOT NULL,
operation text NOT NULL,
function_name text NOT NULL,
query_source text default NULL,
query_cleanup text default NULL,
added_manually boolean NOT NULL default true,
priority numeric NOT NULL default 10,
PRIMARY KEY (extname, operation, function_name),
CHECK (operation IN ('snapshot','aggregate','purge','reset')),
FOREIGN KEY (extname) REFERENCES @[email protected]_extensions(extname)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO @[email protected]_extension_functions
(extname, operation, function_name, query_source, query_cleanup, added_manually, priority) VALUES
('pg_stat_statements', 'snapshot', 'powa_databases_snapshot', 'powa_databases_src', NULL, false, -3),
('pg_stat_statements', 'snapshot', 'powa_statements_snapshot', 'powa_statements_src', NULL, false, -2),
('pg_stat_statements', 'aggregate', 'powa_statements_aggregate', NULL, NULL, false, default),
('pg_stat_statements', 'purge', 'powa_statements_purge', NULL, NULL, false, default),
('pg_stat_statements', 'purge', 'powa_databases_purge', NULL, NULL, false, default),
('pg_stat_statements', 'reset', 'powa_statements_reset', NULL, NULL, false, default),
('pg_qualstats', 'snapshot', 'powa_qualstats_snapshot', 'powa_qualstats_src', 'SELECT {pg_qualstats}.pg_qualstats_reset()', false, default),
('pg_qualstats', 'aggregate', 'powa_qualstats_aggregate', NULL, NULL, false, default),
('pg_qualstats', 'purge', 'powa_qualstats_purge', NULL, NULL, false, default),
('pg_qualstats', 'reset', 'powa_qualstats_reset', NULL, NULL, false, default),
('pg_stat_kcache', 'snapshot', 'powa_kcache_snapshot', 'powa_kcache_src', NULL, false, -1),
('pg_stat_kcache', 'aggregate', 'powa_kcache_aggregate', NULL, NULL, false, default),
('pg_stat_kcache', 'purge', 'powa_kcache_purge', NULL, NULL, false, default),
('pg_stat_kcache', 'reset', 'powa_kcache_reset', NULL, NULL, false, default),
('pg_track_settings', 'snapshot', 'pg_track_settings_snapshot_settings', 'pg_track_settings_settings_src', NULL, false, default),
('pg_track_settings', 'snapshot', 'pg_track_settings_snapshot_rds', 'pg_track_settings_rds_src', NULL, false, default),
('pg_track_settings', 'snapshot', 'pg_track_settings_snapshot_reboot', 'pg_track_settings_reboot_src', NULL, false, default),
('pg_track_settings', 'reset', 'pg_track_settings_reset', NULL, NULL, false, default),
('pg_wait_sampling', 'snapshot', 'powa_wait_sampling_snapshot', 'powa_wait_sampling_src', NULL, false, default),
('pg_wait_sampling', 'aggregate', 'powa_wait_sampling_aggregate', NULL, NULL, false, default),
('pg_wait_sampling', 'purge', 'powa_wait_sampling_purge', NULL, NULL, false, default),
('pg_wait_sampling', 'reset', 'powa_wait_sampling_reset', NULL, NULL, false, default);
CREATE TABLE @[email protected]_extension_config (
srvid integer NOT NULL,
extname text NOT NULL,
version text,
enabled bool NOT NULL default true,
added_manually boolean NOT NULL default true,
PRIMARY KEY (srvid, extname),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (extname) REFERENCES @[email protected]_extensions(extname)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO @[email protected]_extension_config (srvid, extname, added_manually) VALUES
(0, 'powa', false),
(0, 'pg_stat_statements', false);
-- This is for cluster-wide core views, we don't support custom datasources
-- here.
CREATE TABLE @[email protected]_modules (
module text NOT NULL PRIMARY KEY,
min_version integer NOT NULL DEFAULT 0
);
-- we only manually insert data in this table (and the other related tables)
-- for modules with a custom implementation. For the rest of the modules, this
-- is done automatically with calls to powa_generic_module_setup()
INSERT INTO @[email protected]_modules (module) VALUES
('pg_database'),
('pg_role');
CREATE TABLE @[email protected]_module_config (
srvid integer NOT NULL,
module text NOT NULL,
enabled bool NOT NULL default true,
added_manually boolean NOT NULL default true,
PRIMARY KEY (srvid, module),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (module) REFERENCES @[email protected]_modules(module)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO @[email protected]_module_config (srvid, module, added_manually) VALUES
(0, 'pg_database', false),
(0, 'pg_role', false);
CREATE TABLE @[email protected]_module_functions (
module text NOT NULL,
operation text NOT NULL,
function_name text NOT NULL,
query_source text default NULL,
PRIMARY KEY (module, operation),
CHECK (operation IN ('snapshot','aggregate','purge','reset')),
FOREIGN KEY (module) REFERENCES @[email protected]_modules (module)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO @[email protected]_module_functions (module, operation, function_name, query_source) VALUES
('pg_database', 'snapshot', 'powa_catalog_database_snapshot', 'powa_catalog_database_src'),
('pg_database', 'reset', 'powa_catalog_database_reset', NULL),
('pg_role', 'snapshot', 'powa_catalog_role_snapshot', 'powa_catalog_role_src'),
('pg_role', 'reset', 'powa_catalog_role_reset', NULL);
CREATE VIEW @[email protected]_functions AS
SELECT srvid, 'extension' AS kind, extname AS name, operation, external,
function_name, query_source, query_cleanup, enabled, priority
FROM @[email protected]_extensions e
JOIN @[email protected]_extension_functions f USING (extname)
JOIN @[email protected]_extension_config c USING (extname)
UNION ALL
SELECT srvid, 'module' AS kind, module AS name, operation, false,
function_name, query_source, NULL, enabled, 100
FROM @[email protected]_modules m
JOIN @[email protected]_module_functions f USING (module)
JOIN @[email protected]_module_config c USING (module)
WHERE current_setting('server_version_num')::int >= m.min_version;
CREATE TABLE @[email protected]_db_modules (
db_module text NOT NULL PRIMARY KEY,
tmp_table text NOT NULL,
external boolean NOT NULL default true,
added_manually boolean NOT NULL default true
);
INSERT INTO @[email protected]_db_modules (db_module, tmp_table, external, added_manually) VALUES
('pg_stat_all_indexes', '@[email protected]_all_indexes_src_tmp', false, false),
('pg_stat_all_tables', '@[email protected]_all_tables_src_tmp', false, false),
('pg_stat_user_functions', '@[email protected]_user_functions_src_tmp', false, false);
-- No default rows for this table as this is a remote-server only feature.
-- A NULL dbnames means that the module is activated for all databases,
-- otherwise the module is only activated for the specified database names.
CREATE TABLE @[email protected]_db_module_config (
srvid integer NOT NULL,
db_module text NOT NULL,
dbnames text[],
enabled boolean NOT NULL default true,
PRIMARY KEY (srvid, db_module),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (db_module) REFERENCES @[email protected]_db_modules(db_module)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_db_module_functions (
db_module text NOT NULL,
operation text NOT NULL,
function_name text NOT NULL,
added_manually boolean NOT NULL default true,
priority numeric NOT NULL default 10,
CHECK (operation IN ('snapshot','aggregate','purge','reset')),
PRIMARY KEY (db_module, operation),
FOREIGN KEY (db_module) REFERENCES @[email protected]_db_modules(db_module)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO @[email protected]_db_module_functions
(db_module, operation, function_name, added_manually) VALUES
('pg_stat_all_tables', 'snapshot', 'powa_all_tables_snapshot', false),
('pg_stat_all_tables', 'aggregate', 'powa_all_tables_aggregate', false),
('pg_stat_all_tables', 'purge', 'powa_all_tables_purge', false),
('pg_stat_all_tables', 'reset', 'powa_all_tables_reset', false),
('pg_stat_all_indexes', 'snapshot', 'powa_all_indexes_snapshot', false),
('pg_stat_all_indexes', 'aggregate', 'powa_all_indexes_aggregate', false),
('pg_stat_all_indexes', 'purge', 'powa_all_indexes_purge', false),
('pg_stat_all_indexes', 'reset', 'powa_all_indexes_reset', false),
('pg_stat_user_functions', 'snapshot', 'powa_user_functions_snapshot', false),
('pg_stat_user_functions', 'aggregate', 'powa_user_functions_aggregate', false),
('pg_stat_user_functions', 'purge', 'powa_user_functions_purge', false),
('pg_stat_user_functions', 'reset', 'powa_user_functions_reset', false);
CREATE TABLE @[email protected]_db_module_src_queries (
db_module text NOT NULL,
min_version integer NOT NULL,
added_manually boolean NOT NULL default true,
query_source text NOT NULL,
PRIMARY KEY (db_module, min_version),
FOREIGN KEY (db_module) REFERENCES @[email protected]_db_modules(db_module)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO @[email protected]_db_module_src_queries
(db_module, min_version, added_manually, query_source) VALUES
-- pg_stat_all_tables
('pg_stat_all_tables', 0, false,
'SELECT relid, pg_table_size(relid) AS tbl_size,
seq_scan, NULL AS last_seq_scan, seq_tup_read,
idx_scan, NULL AS last_idx_scan, idx_tup_fetch,
n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, 0 AS n_tup_newpage_upd,
n_live_tup, n_dead_tup, n_mod_since_analyze, 0 AS n_ins_since_vacuum,
last_vacuum, last_autovacuum, last_analyze, last_autoanalyze,
vacuum_count, autovacuum_count, analyze_count, autoanalyze_count,
heap_blks_read, heap_blks_hit, idx_blks_read, idx_blks_hit,
toast_blks_read, toast_blks_hit, tidx_blks_read, tidx_blks_hit
FROM pg_catalog.pg_stat_all_tables st
JOIN pg_catalog.pg_statio_all_tables sit USING (relid)'),
-- pg_stat_all_tables pg13+, n_ins_since_vacuum added
('pg_stat_all_tables', 130000, false,
'SELECT relid, pg_table_size(relid) AS tbl_size,
seq_scan, NULL AS last_seq_scan, seq_tup_read,
idx_scan, NULL AS last_idx_scan, idx_tup_fetch,
n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, 0 AS n_tup_newpage_upd,
n_live_tup, n_dead_tup, n_mod_since_analyze, n_ins_since_vacuum,
last_vacuum, last_autovacuum, last_analyze, last_autoanalyze,
vacuum_count, autovacuum_count, analyze_count, autoanalyze_count,
heap_blks_read, heap_blks_hit, idx_blks_read, idx_blks_hit,
toast_blks_read, toast_blks_hit, tidx_blks_read, tidx_blks_hit
FROM pg_catalog.pg_stat_all_tables st
JOIN pg_catalog.pg_statio_all_tables sit USING (relid)'),
-- pg_stat_all_tables pg16+, last_seq_scan, last_idx_scan and
-- n_tup_newpage_upd added
('pg_stat_all_tables', 160000, false,
'SELECT relid, pg_table_size(relid) AS tbl_size,
seq_scan, last_seq_scan, seq_tup_read,
idx_scan, last_idx_scan, idx_tup_fetch,
n_tup_ins, n_tup_upd, n_tup_del, n_tup_hot_upd, n_tup_newpage_upd,
n_live_tup, n_dead_tup, n_mod_since_analyze, n_ins_since_vacuum,
last_vacuum, last_autovacuum, last_analyze, last_autoanalyze,
vacuum_count, autovacuum_count, analyze_count, autoanalyze_count,
heap_blks_read, heap_blks_hit, idx_blks_read, idx_blks_hit,
toast_blks_read, toast_blks_hit, tidx_blks_read, tidx_blks_hit
FROM pg_catalog.pg_stat_all_tables st
JOIN pg_catalog.pg_statio_all_tables sit USING (relid)'),
-- pg_stat_all_indexes
('pg_stat_all_indexes', 0, false,
'SELECT si.relid, indexrelid, pg_table_size(indexrelid) AS idx_size,
idx_scan, NULL AS last_idx_scan,
idx_tup_read, idx_tup_fetch, idx_blks_read, idx_blks_hit
FROM pg_catalog.pg_stat_all_indexes si
JOIN pg_catalog.pg_statio_all_indexes sit USING (indexrelid)'),
-- pg_stat_all_indexes pg16+
('pg_stat_all_indexes', 160000, false,
'SELECT si.relid, indexrelid, pg_table_size(indexrelid) AS idx_size,
idx_scan, last_idx_scan,
idx_tup_read, idx_tup_fetch, idx_blks_read, idx_blks_hit
FROM pg_catalog.pg_stat_all_indexes si
JOIN pg_catalog.pg_statio_all_indexes sit USING (indexrelid)'),
-- pg_stat_user_functions
('pg_stat_user_functions', 0, false,
'SELECT funcid, calls, total_time, self_time
FROM pg_catalog.pg_stat_user_functions');
CREATE FUNCTION @[email protected]_db_functions(_srvid integer,
_server_version_num integer)
RETURNS TABLE (srvid integer, db_module text, operation text,
function_name text, dbnames text[], query_source text,
tmp_table text, enabled bool, priority numeric)
AS $_$
SELECT srvid, db_module, operation, function_name, dbnames, query_source,
CASE WHEN f.operation = 'snapshot'
THEN tmp_table
ELSE NULL
END AS tmp_table, enabled, priority
FROM @[email protected]_db_modules m
JOIN @[email protected]_db_module_functions f USING (db_module)
JOIN @[email protected]_db_module_config c USING (db_module)
LEFT JOIN LATERAL (
SELECT query_source
FROM @[email protected]_db_module_src_queries q
WHERE q.db_module = m.db_module
AND min_version <= _server_version_num
ORDER BY min_version DESC
LIMIT 1
) q ON f.operation = 'snapshot'
WHERE srvid = _srvid;
$_$ LANGUAGE sql
SET search_path = pg_catalog;
CREATE VIEW @[email protected]_all_functions AS
SELECT *
FROM @[email protected]_functions
UNION ALL
SELECT srvid, 'db_module' AS kind, db_module AS name, operation, external,
function_name, NULL as query_source, NULL AS query_cleanup, enabled,
priority
FROM @[email protected]_db_modules pdm
JOIN @[email protected]_db_module_config pdmc USING (db_module)
JOIN @[email protected]_db_module_functions pdmf USING (db_module);
CREATE TABLE @[email protected]_catalogs (
catname text NOT NULL PRIMARY KEY,
tmp_table text NOT NULL,
priority numeric NOT NULL default 10
);
-- pg_class is last as we use it to record the last_refresh timestamp
INSERT INTO @[email protected]_catalogs
(catname, tmp_table, priority) VALUES
('pg_class', 'powa_catalog_class_src_tmp', 99),
('pg_attribute', 'powa_catalog_attribute_src_tmp', DEFAULT),
('pg_namespace', 'powa_catalog_namespace_src_tmp', DEFAULT),
('pg_type', 'powa_catalog_type_src_tmp', DEFAULT),
('pg_collation', 'powa_catalog_collation_src_tmp', DEFAULT),
('pg_proc', 'powa_catalog_proc_src_tmp', DEFAULT),
('pg_language', 'powa_catalog_language_src_tmp', DEFAULT)
;
CREATE TABLE @[email protected]_catalog_src_queries (
catname text NOT NULL,
min_version integer NOT NULL,
query_source text NOT NULL,
PRIMARY KEY (catname, min_version),
FOREIGN KEY (catname) REFERENCES @[email protected]_catalogs(catname)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
-- we exclude all temporary objects as they're unlikely to be helpful and might
-- bloat the underlying tables.
INSERT INTO @[email protected]_catalog_src_queries
(catname, min_version, query_source) VALUES
-- pg_class
('pg_class', 0,
$$SELECT oid, relname::text AS relname, relnamespace, relpages, reltuples,
reltoastrelid, relisshared, relpersistence, relkind, relnatts,
false AS relrowsecurity, false AS relforcerowsecurity, relreplident,
false AS relispartition,
reloptions,
NULL::text AS relpartbound
FROM pg_catalog.pg_class
WHERE relpersistence != 't'$$),
-- pg_class 9.5+, relrowsecurity and relforcerowsecurity added
('pg_class', 90500,
$$SELECT oid, relname::text AS relname, relnamespace, relpages, reltuples,
reltoastrelid, relisshared, relpersistence, relkind, relnatts,
relrowsecurity, relforcerowsecurity, relreplident,
false AS relispartition,
reloptions,
NULL::text AS relpartbound
FROM pg_catalog.pg_class
WHERE relpersistence != 't'$$),
-- pg_class pg10+, relispartition and repartbound added
('pg_class', 100000,
$$SELECT oid, relname::text AS relname, relnamespace, relpages, reltuples,
reltoastrelid, relisshared, relpersistence, relkind, relnatts,
relrowsecurity, relforcerowsecurity, relreplident,
relispartition,
reloptions,
pg_get_expr(relpartbound, oid) AS relpartbound
FROM pg_catalog.pg_class
WHERE relpersistence != 't'$$),
-- pg_attribute
('pg_attribute', 0,
$$SELECT attrelid, attname::text AS attname, atttypid, attlen, attnum,
''::"char" AS attcompression, attnotnull, atthasdef,
false AS atthasmissing, ''::"char" AS attidentity,
''::"char" AS attgenerated, attstattarget, attcollation, attoptions,
attfdwoptions
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON c.oid = a.attrelid
WHERE a.attnum > 0
AND NOT attisdropped
AND c.relpersistence != 't'$$),
-- pg_attribute pg10+, attidentity added
('pg_attribute', 100000,
$$SELECT attrelid, attname::text AS attname, atttypid, attlen, attnum,
''::"char" AS attcompression, attnotnull, atthasdef,
false AS atthasmissing, attidentity,
''::"char" AS attgenerated, attstattarget, attcollation, attoptions,
attfdwoptions
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON c.oid = a.attrelid
WHERE a.attnum > 0
AND NOT attisdropped
AND c.relpersistence != 't'$$),
-- pg_attribute pg11+, atthasmissing added
('pg_attribute', 110000,
$$SELECT attrelid, attname::text AS attname, atttypid, attlen, attnum,
''::"char" AS attcompression, attnotnull, atthasdef,
atthasmissing, attidentity,
''::"char" AS attgenerated, attstattarget, attcollation, attoptions,
attfdwoptions
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON c.oid = a.attrelid
WHERE a.attnum > 0
AND NOT attisdropped
AND c.relpersistence != 't'$$),
-- pg_attribute pg12+, attgenerated added
('pg_attribute', 120000,
$$SELECT attrelid, attname::text AS attname, atttypid, attlen, attnum,
''::"char" AS attcompression, attnotnull, atthasdef,
atthasmissing, attidentity,
attgenerated, attstattarget, attcollation, attoptions,
attfdwoptions
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON c.oid = a.attrelid
WHERE a.attnum > 0
AND NOT attisdropped
AND c.relpersistence != 't'$$),
-- pg_attribute pg14+, attcompression added
('pg_attribute', 140000,
$$SELECT attrelid, attname::text AS attname, atttypid, attlen, attnum,
attcompression, attnotnull, atthasdef,
atthasmissing, attidentity,
attgenerated, attstattarget, attcollation, attoptions,
attfdwoptions
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON c.oid = a.attrelid
WHERE a.attnum > 0
AND NOT attisdropped
AND c.relpersistence != 't'$$),
-- pg_namespace
('pg_namespace', 0,
$$SELECT oid, nspname::text AS nspname
FROM pg_catalog.pg_namespace$$),
-- pg_type
('pg_type', 0,
$$SELECT oid, typname::text AS typname, typnamespace, typlen, typbyval,
typtype, typcategory, typispreferred, typisdefined, typdelim,
typrelid, typelem, typarray, typalign, typstorage, typnotnull,
typbasetype, typtypmod, typndims, typcollation, typdefault
FROM pg_catalog.pg_type$$),
-- pg_collation
('pg_collation', 0,
$$SELECT oid, collname::text AS collname, collnamespace,
CASE WHEN collname = 'default' THEN 'd' ELSE 'c' END AS collprovider,
true AS collisdeterministic,
pg_encoding_to_char(collencoding) AS collencoding, collcollate,
collctype,
NULL::text AS colllocale, NULL::text AS collicurules,
NULL::text AS collversion
FROM pg_catalog.pg_collation$$),
-- pg_collation pg10+, collprovider and collversion added
('pg_collation', 100000,
$$SELECT oid, collname::text AS collname, collnamespace, collprovider,
true AS collisdeterministic,
pg_encoding_to_char(collencoding) AS collencoding, collcollate,
collctype,
NULL::text AS colllocale, NULL::text AS collicurules,
collversion
FROM pg_catalog.pg_collation$$),
-- pg_collation pg12+, collisdeterministic added
('pg_collation', 120000,
$$SELECT oid, collname::text AS collname, collnamespace, collprovider,
collisdeterministic,
pg_encoding_to_char(collencoding) AS collencoding, collcollate,
collctype,
NULL::text AS colllocale, NULL::text AS collicurules,
collversion
FROM pg_catalog.pg_collation$$),
-- pg_collation pg15+, colliculocale added
('pg_collation', 150000,
$$SELECT oid, collname::text AS collname, collnamespace, collprovider,
collisdeterministic,
pg_encoding_to_char(collencoding) AS collencoding, collcollate,
collctype,
colliculocale AS colllocale, NULL::text AS collicurules,
collversion
FROM pg_catalog.pg_collation$$),
-- pg_collation pg16+, collicurules added
('pg_collation', 160000,
$$SELECT oid, collname::text AS collname, collnamespace, collprovider,
collisdeterministic,
pg_encoding_to_char(collencoding) AS collencoding, collcollate,
collctype,
colliculocale AS colllocale, collicurules,
collversion
FROM pg_catalog.pg_collation$$),
-- pg_collation pg16+, colliculocale renamed to colllocale
('pg_collation', 170000,
$$SELECT oid, collname::text AS collname, collnamespace, collprovider,
collisdeterministic,
pg_encoding_to_char(collencoding) AS collencoding, collcollate,
collctype,
colllocale, collicurules,
collversion
FROM pg_catalog.pg_collation$$),
-- pg_proc
('pg_proc', 0,
$$SELECT oid, proname::text AS proname, oid::regprocedure AS regprocedure,
pronamespace, prolang, procost,
prorows, provariadic,
CASE
WHEN proisagg THEN 'a'::"char"
WHEN proiswindow THEN 'w'::"char"
ELSE 'f'::"char"
END AS prokind, prosecdef, proleakproof, proisstrict, proretset,
provolatile, 'u'::"char" AS proparallel, pronargs, prorettype,
proargtypes, prosrc, proconfig
FROM pg_catalog.pg_proc$$),
-- pg_proc pg 9.6+, proparallel added
('pg_proc', 90600,
$$SELECT oid, proname::text AS proname, oid::regprocedure AS regprocedure,
pronamespace, prolang, procost,
prorows, provariadic,
CASE
WHEN proisagg THEN 'a'::"char"
WHEN proiswindow THEN 'w'::"char"
ELSE 'f'::"char"
END AS prokind, prosecdef, proleakproof, proisstrict, proretset,
provolatile, proparallel, pronargs, prorettype,
proargtypes,
prosrc,
proconfig
FROM pg_catalog.pg_proc$$),
-- pg_proc pg11+, prokind added replacing proisagg and proiswindow
('pg_proc', 110000,
$$SELECT oid, proname::text AS proname, oid::regprocedure AS regprocedure,
pronamespace, prolang, procost,
prorows, provariadic,
prokind, prosecdef, proleakproof, proisstrict, proretset,
provolatile, proparallel, pronargs, prorettype,
proargtypes,
prosrc,
proconfig
FROM pg_catalog.pg_proc$$),
-- pg_proc pg14+, prosqlbody added
('pg_proc', 140000,
$$SELECT oid, proname::text AS proname, oid::regprocedure AS regprocedure,
pronamespace, prolang, procost,
prorows, provariadic,
prokind, prosecdef, proleakproof, proisstrict, proretset,
provolatile, proparallel, pronargs, prorettype,
proargtypes,
CASE WHEN prosqlbody IS NOT NULL THEN
pg_catalog.pg_get_function_sqlbody(oid)
ELSE
prosrc
END AS prosrc,
proconfig
FROM pg_catalog.pg_proc$$),
-- pg_language
('pg_language', 0,
$$SELECT oid, lanname::text AS lanname, lanispl, lanpltrusted
FROM pg_catalog.pg_language$$)
;
CREATE FUNCTION @[email protected]_catalog_src_query(_catname text,
_server_version_num integer)
RETURNS text
AS $_$
SELECT query_source
FROM @[email protected]_catalog_src_queries
WHERE catname = _catname
AND min_version <= _server_version_num
ORDER BY min_version DESC
LIMIT 1;
$_$ LANGUAGE sql
SET search_path = pg_catalog;
CREATE TABLE @[email protected]_catalog_databases (
srvid integer NOT NULL,
oid oid NOT NULL,
datname text NOT NULL,
last_refresh timestamp with time zone,
PRIMARY KEY (srvid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_catalog_roles (
srvid integer NOT NULL,
oid oid NOT NULL,
rolname text NOT NULL,
rolsuper boolean NOT NULL,
rolinherit boolean NOT NULL,
rolcreaterole boolean NOT NULL,
rolcreatedb boolean NOT NULL,
rolcanlogin boolean NOT NULL,
rolreplication boolean NOT NULL,
rolbypassrls boolean NOT NULL,
PRIMARY KEY (srvid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE FUNCTION @[email protected]_catalog_functions(_srvid integer,
_server_version_num integer, _refresh_interval interval DEFAULT '1 year')
RETURNS TABLE (catname text, query_source text, tmp_table text,
excluded_dbnames text[])
AS $_$
SELECT catname,
src_query AS query_source,
'@extschema@.'
|| quote_ident('powa_catalog_'
|| replace(catname, 'pg_', '') || '_src_tmp') AS tmp_table,
coalesce(array_agg(datname)
FILTER (WHERE (last_refresh + _refresh_interval) >= now()), '{}')
AS excluded_dbnames
FROM @[email protected]_catalogs c
LEFT JOIN @[email protected]_catalog_databases d ON d.srvid = _srvid
LEFT JOIN LATERAL (
SELECT @[email protected]_catalog_src_query(catname, _server_version_num)
) f(src_query) ON (true)
GROUP BY catname, src_query;
$_$ LANGUAGE sql
SET search_path = pg_catalog;
CREATE TABLE @[email protected]_catalog_class (
srvid integer NOT NULL,
dbid oid NOT NULL,
oid oid NOT NULL,
relname text NOT NULL,
relnamespace oid NOT NULL,
relpages integer NOT NULL,
reltuples real NOT NULL,
reltoastrelid oid NOT NULL,
relisshared bool NOT NULL,
relpersistence "char" NOT NULL,
relkind "char" NOT NULL,
relnatts smallint NOT NULL,
relrowsecurity boolean NOT NULL,
relforcerowsecurity boolean NOT NULL,
relreplident "char" NOT NULL,
relispartition boolean NOT NULL,
reloptions text[],
relpartbound text,
PRIMARY KEY (srvid, dbid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES @[email protected]_catalog_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_catalog_attribute (
srvid integer NOT NULL,
dbid oid NOT NULL,
attrelid oid NOT NULL,
attname text NOT NULL,
atttypid oid NOT NULL,
attlen smallint NOT NULL,
attnum smallint NOT NULL,
attcompression "char" NOT NULL,
attnotnull boolean NOT NULL,
atthasdef boolean NOT NULL,
atthasmissing boolean NOT NULL,
attidentity "char" NOT NULL,
attgenerated "char" NOT NULL,
attstattarget smallint,
attcollation oid NOT NULL,
attoptions text[],
attfdwoptions text[],
PRIMARY KEY (srvid, dbid, attrelid, attnum),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES @[email protected]_catalog_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_catalog_namespace (
srvid integer NOT NULL,
dbid oid NOT NULL,
oid oid NOT NULL,
nspname text NOT NULL,
PRIMARY KEY (srvid, dbid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES @[email protected]_catalog_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_catalog_type (
srvid integer NOT NULL,
dbid oid NOT NULL,
oid oid NOT NULL,
typname text NOT NULL,
typnamespace oid NOT NULL,
typlen smallint NOT NULL,
typbyval boolean NOT NULL,
typtype "char" NOT NULL,
typcategory "char" NOT NULL,
typispreferred boolean NOT NULL,
typisdefined boolean NOT NULL,
typdelim "char" NOT NULL,
typrelid oid NOT NULL,
typelem oid NOT NULL,
typarray oid NOT NULL,
typalign "char" NOT NULL,
typstorage "char" NOT NULL,
typnotnull boolean NOT NULL,
typbasetype oid NOT NULL,
typtypmod integer NOT NULL,
typndims integer NOT NULL,
typcollation oid NOT NULL,
typdefault text,
PRIMARY KEY (srvid, dbid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES @[email protected]_catalog_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_catalog_collation (
srvid integer NOT NULL,
dbid oid NOT NULL,
oid oid NOT NULL,
collname text NOT NULL,
collnamespace oid NOT NULL,
collprovider "char" NOT NULL,
collisdeterministic boolean NOT NULL,
collencoding text NOT NULL,
collcollate text,
collctype text,
colllocale text,
collicurules text,
collversion text,
PRIMARY KEY (srvid, dbid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES @[email protected]_catalog_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
-- we also store oid::regprocedure for an easy way to have an unambiguous
-- identifier
CREATE TABLE @[email protected]_catalog_proc (
srvid integer NOT NULL,
dbid oid NOT NULL,
oid oid NOT NULL,
proname text NOT NULL,
regprocedure text NOT NULL,
pronamespace oid NOT NULL,
prolang oid NOT NULL,
procost real NOT NULL,
prorows real NOT NULL,
provariadic oid NOT NULL,
prokind "char" NOT NULL,
prosecdef boolean NOT NULL,
proleakproof boolean NOT NULL,
proisstrict boolean NOT NULL,
proretset boolean NOT NULL,
provolatile "char" NOT NULL,
proparallel "char" NOT NULL,
pronargs smallint NOT NULL,
prorettype oid NOT NULL,
proargtypes oidvector NOT NULL,
prosrc text NOT NULL,
proconfig text[],
PRIMARY KEY (srvid, dbid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES @[email protected]_catalog_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_catalog_language (
srvid integer NOT NULL,
dbid oid NOT NULL,
oid oid NOT NULL,
lanname text NOT NULL,
lanispl boolean NOT NULL,
lanpltrusted boolean NOT NULL,
PRIMARY KEY (srvid, dbid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES @[email protected]_catalog_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_snapshot_metas (
srvid integer PRIMARY KEY,
coalesce_seq bigint NOT NULL default (1),
snapts timestamp with time zone NOT NULL default '-infinity'::timestamptz,
aggts timestamp with time zone NOT NULL default '-infinity'::timestamptz,
purgets timestamp with time zone NOT NULL default '-infinity'::timestamptz,
errors text[],
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO @[email protected]_snapshot_metas (srvid) VALUES (0);
CREATE TABLE @[email protected]_databases (
srvid integer NOT NULL,
oid oid,
datname name,
dropped timestamp with time zone,
PRIMARY KEY (srvid, oid),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE @[email protected]_statements (
srvid integer NOT NULL,
queryid bigint NOT NULL,
dbid oid NOT NULL,
userid oid NOT NULL,
query text NOT NULL,
last_present_ts timestamptz NULL DEFAULT now(),
FOREIGN KEY (srvid) REFERENCES @[email protected]_servers(id)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (srvid, dbid) REFERENCES @[email protected]_databases(srvid, oid)
MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
);
ALTER TABLE ONLY @[email protected]_statements
ADD CONSTRAINT powa_statements_pkey PRIMARY KEY (srvid, queryid, dbid, userid);
CREATE INDEX powa_statements_dbid_idx ON @[email protected]_statements(srvid, dbid);
CREATE INDEX powa_statements_userid_idx ON @[email protected]_statements(userid);
CREATE INDEX powa_statements_mru_idx ON @[email protected]_statements (last_present_ts);
CREATE FUNCTION @[email protected]_stat_user_functions(IN dbid oid, OUT funcid oid,
OUT calls bigint,
OUT total_time double precision,
OUT self_time double precision)
RETURNS SETOF record STABLE
LANGUAGE c COST 100
AS '$libdir/powa', 'powa_stat_user_functions';
CREATE FUNCTION @[email protected]_stat_all_rel(IN dbid oid,
OUT relid oid,
OUT numscan bigint,
OUT tup_returned bigint,
OUT tup_fetched bigint,
OUT n_tup_ins bigint,
OUT n_tup_upd bigint,
OUT n_tup_del bigint,
OUT n_tup_hot_upd bigint,
OUT n_liv_tup bigint,
OUT n_dead_tup bigint,
OUT n_mod_since_analyze bigint,
OUT blks_read bigint,
OUT blks_hit bigint,
OUT last_vacuum timestamp with time zone,
OUT vacuum_count bigint,
OUT last_autovacuum timestamp with time zone,
OUT autovacuum_count bigint,
OUT last_analyze timestamp with time zone,
OUT analyze_count bigint,
OUT last_autoanalyze timestamp with time zone,
OUT autoanalyze_count bigint)
RETURNS SETOF record STABLE
LANGUAGE c COST 100
AS '$libdir/powa', 'powa_stat_all_rel';
-------------------------------
-- data sources generic support
-------------------------------
CREATE FUNCTION @[email protected]_generic_datatype_setup(
_datasource text,
_cols text[],
_extra jsonb DEFAULT '{}',
_need_operators boolean default true
)
RETURNS void AS
$$
DECLARE
i integer;
v_prefix text;
v_sql text;
v_record_name text;
v_colname text;
v_coltype text;
v_extra text;
v_kind text;
c_no_agg text[];
v_has_no_agg_col bool;
c_no_minmax text[];