forked from lacanoid/pgddl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddlx.sql
2859 lines (2673 loc) · 96.8 KB
/
ddlx.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
--
-- DDL eXtractor functions
-- version 0.23 [email protected]
--
---------------------------------------------------
SET client_min_messages = warning;
---------------------------------------------------
---------------------------------------------------
-- Helpers for digesting system catalogs
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_identify(
IN oid,
OUT oid oid, OUT classid regclass,
OUT name name, OUT namespace name,
OUT owner name, OUT sql_kind text,
OUT sql_identifier text, OUT acl aclitem[])
RETURNS record LANGUAGE sql AS $function$
WITH
rel_kind(k,v) AS (
VALUES ('r','TABLE'),
('p','TABLE'),
('v','VIEW'),
('i','INDEX'),
('I','INDEX'),
('S','SEQUENCE'),
('s','SPECIAL'),
('m','MATERIALIZED VIEW'),
('c','TYPE'),
('t','TOAST'),
('f','FOREIGN TABLE')
),
typ_type(k,v,v2) AS (
VALUES ('b','BASE','TYPE'),
('c','COMPOSITE','TYPE'),
('d','DOMAIN','DOMAIN'),
('e','ENUM','TYPE'),
('p','PSEUDO','TYPE'),
('r','RANGE','TYPE')
)
SELECT coalesce(t.oid,c.oid),
case when t.oid is not null then 'pg_type'::regclass
else 'pg_class'::regclass end,
c.relname AS name, n.nspname AS namespace,
pg_get_userbyid(c.relowner) AS owner,
coalesce(cc.v,c.relkind::text) AS sql_kind,
cast($1::regclass AS text) AS sql_identifier,
relacl as acl
FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace
LEFT JOIN pg_type t ON t.typrelid=c.oid AND t.typtype='c' AND c.relkind='c'
LEFT JOIN rel_kind AS cc on cc.k = c.relkind
WHERE c.oid = $1
UNION ALL
SELECT p.oid,'pg_proc'::regclass,
p.proname AS name, n.nspname AS namespace, pg_get_userbyid(p.proowner) AS owner,
#if 11
case p.prokind
when 'f' then 'FUNCTION'
when 'a' then 'AGGREGATE'
when 'p' then 'PROCEDURE'
when 'w' then 'WINDOW FUNCTION'
end
#else
case
when p.proisagg then 'AGGREGATE'
else 'FUNCTION'
end
#end
AS sql_kind,
cast($1::regprocedure AS text) AS sql_identifier,
proacl as acl
FROM pg_proc p JOIN pg_namespace n ON n.oid=p.pronamespace
WHERE p.oid = $1
UNION ALL
SELECT coalesce(c.oid,t.oid),
case when c.oid is not null then 'pg_class'::regclass
else 'pg_type'::regclass end,
t.typname AS name, n.nspname AS namespace, pg_get_userbyid(t.typowner) AS owner,
coalesce(cc.v,tt.v2,t.typtype::text) AS sql_kind,
format_type($1,null) AS sql_identifier,
#if 9.2
typacl as acl
#else
null as acl
#end
FROM pg_type t JOIN pg_namespace n ON n.oid=t.typnamespace
LEFT JOIN typ_type AS tt ON tt.k = t.typtype
LEFT JOIN pg_class AS c ON c.oid = t.typrelid AND t.typtype='c' AND c.relkind<>'c'
LEFT JOIN rel_kind AS cc ON cc.k = c.relkind
WHERE t.oid = $1
UNION ALL
SELECT r.oid,'pg_roles'::regclass,
r.rolname as name, null as namespace, null as owner,
'ROLE' as sql_kind,
quote_ident(r.rolname) as sql_identifier,
null as acl
FROM pg_roles r
WHERE r.oid = $1
UNION ALL
SELECT r.oid,'pg_rewrite'::regclass,
r.rulename as name, null as namespace, null as owner,
'RULE' as sql_kind,
quote_ident(r.rulename)||' ON '||
cast(c.oid::regclass as text) sql_identifier,
null as acl
FROM pg_rewrite r JOIN pg_class c on (c.oid = r.ev_class)
WHERE r.oid = $1
UNION ALL
SELECT n.oid,'pg_namespace'::regclass,
n.nspname as name, current_database() as namespace, pg_get_userbyid(n.nspowner) AS owner,
'SCHEMA' as sql_kind,
quote_ident(n.nspname) as sql_identifier,
nspacl as acl
FROM pg_namespace n join pg_roles r on r.oid = n.nspowner
WHERE n.oid = $1
UNION ALL
SELECT con.oid,'pg_constraint'::regclass,
con.conname as name,
c.relname as namespace, null as owner, 'CONSTRAINT' as sql_kind,
quote_ident(con.conname)
||coalesce(' ON '||cast(c.oid::regclass as text),'') as sql_identifier,
null as acl
FROM pg_constraint con
left JOIN pg_class c ON (con.conrelid=c.oid)
LEFT join (
values ('f','FOREIGN KEY'), ('c','CHECK'), ('x','EXCLUDE'),
('u','UNIQUE'), ('p','PRIMARY KEY'), ('t','TRIGGER'))
as tt on tt.column1 = con.contype
WHERE con.oid = $1
UNION ALL
SELECT t.oid,'pg_trigger'::regclass,
t.tgname as name, c.relname as namespace, null as owner,
'TRIGGER' as sql_kind,
format('%I ON %s',t.tgname,cast(c.oid::regclass as text)) as sql_identifier,
null as acl
FROM pg_trigger t join pg_class c on (t.tgrelid=c.oid)
WHERE t.oid = $1
UNION ALL
SELECT ad.oid,'pg_attrdef'::regclass,
a.attname as name, c.relname as namespace, null as owner,
'DEFAULT' as sql_kind,
format('%s.%I',cast(c.oid::regclass as text),a.attname) as sql_identifier,
null as acl
FROM pg_attrdef ad
JOIN pg_class c ON (ad.adrelid=c.oid)
JOIN pg_attribute a ON (c.oid = a.attrelid and a.attnum=ad.adnum)
WHERE ad.oid = $1
UNION ALL
SELECT op.oid,'pg_operator'::regclass,
op.oprname as name, n.nspname as namespace, pg_get_userbyid(op.oprowner) as owner,
'OPERATOR' as sql_kind,
cast(op.oid::regoperator as text) as sql_identifier,
null as acl
FROM pg_operator op JOIN pg_namespace n ON n.oid=op.oprnamespace
WHERE op.oid = $1
UNION ALL
SELECT cfg.oid,'pg_ts_config'::regclass,
cfg.cfgname as name, n.nspname as namespace, pg_get_userbyid(cfg.cfgowner) as owner,
'TEXT SEARCH CONFIGURATION' as sql_kind,
cast(cfg.oid::regconfig as text) as sql_identifier,
null as acl
FROM pg_ts_config cfg JOIN pg_namespace n ON n.oid=cfg.cfgnamespace
WHERE cfg.oid = $1
UNION ALL
SELECT dict.oid,'pg_ts_dict'::regclass,
dict.dictname as name, n.nspname as namespace, pg_get_userbyid(dict.dictowner) as owner,
'TEXT SEARCH DICTIONARY' as sql_kind,
cast(dict.oid::regdictionary as text) as sql_identifier,
null as acl
FROM pg_ts_dict dict JOIN pg_namespace n ON n.oid=dict.dictnamespace
WHERE dict.oid = $1
UNION ALL
SELECT prs.oid,'pg_ts_parser'::regclass,
prs.prsname as name, n.nspname as namespace, null as owner,
'TEXT SEARCH PARSER' as sql_kind,
format('%s%I',
quote_ident(nullif(n.nspname,current_schema()))||'.',prs.prsname)
as sql_identifier,
null as acl
FROM pg_ts_parser prs JOIN pg_namespace n ON n.oid=prs.prsnamespace
WHERE prs.oid = $1
UNION ALL
SELECT tmpl.oid,'pg_ts_template'::regclass,
tmpl.tmplname as name, n.nspname as namespace, null as owner,
'TEXT SEARCH TEMPLATE' as sql_kind,
format('%s%I',
quote_ident(nullif(n.nspname,current_schema()))||'.',tmpl.tmplname)
as sql_identifier,
null as acl
FROM pg_ts_template tmpl JOIN pg_namespace n ON n.oid=tmpl.tmplnamespace
WHERE tmpl.oid = $1
UNION ALL
SELECT fdw.oid,'pg_foreign_data_wrapper'::regclass,
fdw.fdwname as name, null as namespace, pg_get_userbyid(fdw.fdwowner) as owner,
'FOREIGN DATA WRAPPER' as sql_kind,
quote_ident(fdw.fdwname) as sql_identifier,
fdwacl as acl
FROM pg_foreign_data_wrapper fdw
WHERE fdw.oid = $1
UNION ALL
SELECT srv.oid,'pg_foreign_server'::regclass,
srv.srvname as name, null as namespace, pg_get_userbyid(srv.srvowner) as owner,
'SERVER' as sql_kind,
quote_ident(srv.srvname) as sql_identifier,
srvacl as acl
FROM pg_foreign_server srv
WHERE srv.oid = $1
UNION ALL
SELECT ums.umid,'pg_user_mapping'::regclass,
null as name, null as namespace, null as owner, 'USER MAPPING' as sql_kind,
'FOR '||quote_ident(ums.usename)||
' SERVER '||quote_ident(ums.srvname) as sql_identifier,
null as acl
FROM pg_user_mappings ums
WHERE ums.umid = $1
UNION ALL
SELECT ca.oid,'pg_cast'::regclass,
null as name, null as namespace, null as owner,
'CAST' as sql_kind,
format('(%s AS %s)',
format_type(ca.castsource,null),format_type(ca.casttarget,null))
as sql_identifier,
null as acl
FROM pg_cast ca
WHERE ca.oid = $1
UNION ALL
SELECT co.oid,'pg_collation'::regclass,
co.collname as name, n.nspname as namespace, pg_get_userbyid(co.collowner) as owner,
'COLLATION' as sql_kind,
format('%s%I',
quote_ident(nullif(n.nspname,current_schema()))||'.',co.collname)
as sql_identifier,
null as acl
FROM pg_collation co JOIN pg_namespace n ON n.oid=co.collnamespace
WHERE co.oid = $1
UNION ALL
SELECT co.oid,'pg_conversion'::regclass,
co.conname as name, n.nspname as namespace, pg_get_userbyid(co.conowner) as owner,
'CONVERSION' as sql_kind,
format('%s%I',
quote_ident(nullif(n.nspname,current_schema()))||'.',co.conname)
as sql_identifier,
null as acl
FROM pg_conversion co JOIN pg_namespace n ON n.oid=co.connamespace
WHERE co.oid = $1
UNION ALL
SELECT lan.oid,'pg_language'::regclass,
lan.lanname as name, null as namespace, pg_get_userbyid(lan.lanowner) as owner,
'LANGUAGE' as sql_kind,
quote_ident(lan.lanname) as sql_identifier,
lan.lanacl as acl
FROM pg_language lan
WHERE lan.oid = $1
UNION ALL
SELECT opf.oid,'pg_opfamily'::regclass,
opf.opfname as name, n.nspname as namespace, pg_get_userbyid(opf.opfowner) as owner,
'OPERATOR FAMILY' as sql_kind,
format('%s%I USING %I',
quote_ident(nullif(n.nspname,current_schema()))||'.',
opf.opfname,
am.amname)
as sql_identifier,
null as acl
FROM pg_opfamily opf JOIN pg_namespace n ON n.oid=opf.opfnamespace
JOIN pg_am am on (am.oid=opf.opfmethod)
WHERE opf.oid = $1
UNION ALL
SELECT dat.oid,'pg_database'::regclass,
dat.datname as name, null as namespace, pg_get_userbyid(dat.datdba) as owner,
'DATABASE' as sql_kind,
quote_ident(dat.datname) as sql_identifier,
dat.datacl as acl
FROM pg_database dat
WHERE dat.oid = $1
UNION ALL
SELECT spc.oid,'pg_tablespace'::regclass,
spc.spcname as name, null as namespace, pg_get_userbyid(spc.spcowner) as owner,
'TABLESPACE' as sql_kind,
quote_ident(spc.spcname) as sql_identifier,
spc.spcacl as acl
FROM pg_tablespace spc
WHERE spc.oid = $1
UNION ALL
SELECT opc.oid,'pg_opclass'::regclass,
opcname as name, n.nspname as namespace, pg_get_userbyid(opc.opcowner) as owner,
'OPERATOR CLASS' as sql_kind,
format('%s%I USING %I',
quote_ident(nullif(n.nspname,current_schema()))||'.',
opc.opcname,
am.amname)
as sql_identifier,
null as acl
FROM pg_opclass opc JOIN pg_namespace n ON n.oid=opc.opcnamespace
JOIN pg_am am ON am.oid=opc.opcmethod
WHERE opc.oid = $1
UNION ALL
SELECT e.oid, 'pg_extension'::regclass,
e.extname AS name, e.extnamespace::text AS namespace, pg_get_userbyid(e.extowner) AS owner,
'EXTENSION'::text AS sql_kind,
e.extname AS sql_identifier,
NULL::aclitem[] AS acl
FROM pg_extension e
WHERE e.oid = $1
#if 9.3
UNION ALL
SELECT evt.oid,'pg_event_trigger'::regclass,
evt.evtname as name, null as namespace, pg_get_userbyid(evt.evtowner) as owner,
'EVENT TRIGGER' as sql_kind,
quote_ident(evt.evtname) as sql_identifier,
null as acl
FROM pg_event_trigger evt
WHERE evt.oid = $1
UNION ALL
SELECT amproc.oid,'pg_amproc'::regclass,
'FUNCTION '||amprocnum, null as namespace, null as owner,
'AMPROC' as sql_kind,
format('FUNCTION %s (%s)',
amprocnum,
array_to_string(array[amproclefttype,amprocrighttype]::regtype[],','))
as sql_identifier,
null as acl
FROM pg_amproc amproc
WHERE amproc.oid = $1
UNION ALL
SELECT amop.oid,'pg_amop'::regclass,
'OPERATOR '||amopstrategy, null as namespace, null as owner,
'AMOP' as sql_kind,
format('OPERATOR %s (%s)',
amopstrategy,
array_to_string(array[amoplefttype,amoprighttype]::regtype[],','))
as sql_identifier,
null as acl
FROM pg_amop amop
WHERE amop.oid = $1
#if 9.5
UNION ALL
SELECT pol.oid,'pg_policy'::regclass,
pol.polname as name, null as namespace, null as owner,
'POLICY' as sql_kind,
format('%I ON %s',
polname,
cast(c.oid::regclass as text))
as sql_identifier,
null as acl
FROM pg_policy pol JOIN pg_class c on (c.oid=pol.polrelid)
WHERE pol.oid = $1
UNION ALL
SELECT trf.oid,'pg_transform'::regclass,
null as name, null as namespace, null as owner,
'TRANSFORM' as sql_kind,
format('FOR %s LANGUAGE %I',
format_type(trf.trftype,null),
l.lanname) as sql_identifier,
null as acl
FROM pg_transform trf JOIN pg_language l on (l.oid=trf.trflang)
WHERE trf.oid = $1
UNION ALL
SELECT am.oid,'pg_am'::regclass,
am.amname as name, NULL as namespace, NULL as owner,
'ACCESS METHOD' as sql_kind,
quote_ident(amname) as sql_identifier,
null as acl
FROM pg_am am
WHERE am.oid = $1
#if 10
UNION ALL
SELECT stx.oid,'pg_statistic_ext'::regclass,
stx.stxname, n.nspname as namespace, pg_get_userbyid(stx.stxowner) as owner,
'STATISTICS' as sql_kind,
format('%s%I',quote_ident(nullif(n.nspname,current_schema()))||'.',stx.stxname)
as sql_identifier,
null as acl
FROM pg_statistic_ext stx join pg_namespace n on (n.oid=stxnamespace)
WHERE stx.oid = $1
UNION ALL
SELECT pub.oid,'pg_publication'::regclass,
pub.pubname, NULL as namespace, pg_get_userbyid(pub.pubowner) as owner,
'PUBLICATION' as sql_kind,
quote_ident(pub.pubname) as sql_identifier,
null as acl
FROM pg_publication pub
WHERE pub.oid = $1
#if 14
UNION ALL
SELECT sub.oid,'pg_subscription'::regclass,
sub.subname, NULL as namespace, pg_get_userbyid(sub.subowner) as owner,
'SUBSCRIPTION' as sql_kind,
quote_ident(sub.subname) as sql_identifier,
null as acl
FROM pg_subscription sub
WHERE sub.oid = $1
#end
$function$ strict;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_describe(
IN regclass,
OUT ord smallint,
OUT name name, OUT type text, OUT size integer, OUT not_null boolean,
OUT "default" text,
OUT ident text, OUT gen text,
OUT comment text, OUT primary_key name,
OUT is_local boolean, OUT storage text, OUT collation text,
OUT namespace name, OUT class_name name, OUT sql_identifier text,
OUT relid oid, OUT options text[], OUT definition text,
OUT sequence regclass)
RETURNS SETOF record LANGUAGE sql AS $function$
SELECT a.attnum AS ord,
a.attname AS name,
format_type(t.oid, NULL::integer) AS type,
CASE
WHEN (a.atttypmod - 4) > 0 THEN a.atttypmod - 4
ELSE NULL::integer
END AS size,
a.attnotnull AS not_null,
pg_get_expr(def.adbin,def.adrelid) AS "default",
#if 10
nullif(a.attidentity::text,''),
#else
null::text,
#end
#if 12
nullif(a.attgenerated::text,''),
#else
null::text,
#end
col_description(c.oid, a.attnum::integer) AS comment,
con.conname AS primary_key,
a.attislocal AS is_local,
case when a.attstorage<>t.typstorage
then case a.attstorage
when 'p' then 'plain'::text
when 'e' then 'external'::text
when 'm' then 'main'::text
when 'x' then 'extended'::text
else a.attstorage::text
end
end as storage,
nullif(col.collcollate::text,'') AS collation,
s.nspname AS namespace,
c.relname AS class_name,
format('%s.%I',text(c.oid::regclass),a.attname) AS sql_identifier,
c.oid as relid,
#if 9.2
attoptions||attfdwoptions as options,
#else
attoptions as options,
#end
format('%I %s%s%s%s%s%s%s',
a.attname::text,
format_type(t.oid, a.atttypmod),
#if 9.2
case
when a.attfdwoptions is not null
then (
select ' OPTIONS ( '||string_agg(
quote_ident(option_name)||' '||quote_nullable(option_value),
', ')||' ) '
from pg_options_to_table(a.attfdwoptions))
end,
#else
null::text,
#end
CASE
WHEN length(col.collcollate) > 0
THEN ' COLLATE ' || quote_ident(col.collcollate::text)
END
,
#if 10
case when a.attnotnull and attidentity not in ('a','d') then ' NOT NULL' end
#else
case when a.attnotnull THEN ' NOT NULL' end
#end
,
coalesce(' DEFAULT ' || pg_get_expr(def.adbin, def.adrelid), ''),
#if 10
case when attidentity in ('a','d')
then format(' GENERATED %s AS IDENTITY',
case attidentity
when 'a' then 'ALWAYS'
when 'd' then 'BY DEFAULT'
end)
end
#else
null::text
#end
,
#if 12
case when a.attgenerated = 's'
then format(' GENERATED ALWAYS AS %s STORED',
pg_get_expr(def.adbin,def.adrelid))
end
#else
null::text
#end
)
AS definition,
pg_get_serial_sequence(c.oid::regclass::text,a.attname)::regclass as sequence
FROM pg_class c
JOIN pg_namespace s ON s.oid = c.relnamespace
JOIN pg_attribute a ON c.oid = a.attrelid
LEFT JOIN pg_attrdef def ON c.oid = def.adrelid AND a.attnum = def.adnum
LEFT JOIN pg_constraint con
ON con.conrelid = c.oid AND (a.attnum = ANY (con.conkey)) AND con.contype = 'p'
LEFT JOIN pg_type t ON t.oid = a.atttypid
LEFT JOIN pg_collation col ON col.oid = a.attcollation
JOIN pg_namespace tn ON tn.oid = t.typnamespace
LEFT JOIN pg_depend d ON def.oid = d.objid AND d.deptype='n'
LEFT JOIN pg_class seq ON seq.oid = d.refobjid AND seq.relkind='S'
WHERE c.relkind IN ('r','v','c','f','p') AND a.attnum > 0 AND NOT a.attisdropped
AND has_table_privilege(c.oid, 'select') AND has_schema_privilege(s.oid, 'usage')
AND c.oid = $1
ORDER BY s.nspname, c.relname, a.attnum;
$function$ strict;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_get_constraints(
regclass default null,
OUT namespace name,
OUT class_name name,
OUT constraint_name name,
OUT constraint_type text,
OUT constraint_definition text,
OUT is_deferrable boolean,
OUT initially_deferred boolean,
OUT regclass oid,
OUT sysid oid,
OUT is_local boolean)
RETURNS SETOF record LANGUAGE sql AS $function$
SELECT nc.nspname AS namespace,
r.relname AS class_name,
c.conname AS constraint_name,
case c.contype
when 'c'::"char" then 'CHECK'::text
when 'f'::"char" then 'FOREIGN KEY'::text
when 'p'::"char" then 'PRIMARY KEY'::text
when 'u'::"char" then 'UNIQUE'::text
when 't'::"char" then 'TRIGGER'::text
when 'x'::"char" then 'EXCLUDE'::text
else c.contype::text
end,
pg_get_constraintdef(c.oid,true) AS constraint_definition,
c.condeferrable AS is_deferrable,
c.condeferred AS initially_deferred,
r.oid as regclass, c.oid AS sysid,
d.refobjid is null AS is_local
FROM pg_constraint c
JOIN pg_class r ON c.conrelid = r.oid
JOIN pg_namespace nc ON nc.oid = c.connamespace
JOIN pg_namespace nr ON nr.oid = r.relnamespace
LEFT JOIN pg_depend d ON d.objid = c.oid AND d.deptype='P'
WHERE $1 IS NULL OR r.oid=$1
$function$;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_get_rules(
regclass default null,
OUT namespace text, OUT class_name text, OUT rule_name text, OUT rule_event text,
OUT is_instead boolean, OUT rule_definition text, OUT regclass regclass)
RETURNS SETOF record LANGUAGE sql AS $function$
SELECT n.nspname::text AS namespace,
c.relname::text AS class_name,
r.rulename::text AS rule_name,
CASE
WHEN r.ev_type = '1'::"char" THEN 'SELECT'::text
WHEN r.ev_type = '2'::"char" THEN 'UPDATE'::text
WHEN r.ev_type = '3'::"char" THEN 'INSERT'::text
WHEN r.ev_type = '4'::"char" THEN 'DELETE'::text
ELSE 'UNKNOWN'::text
END AS rule_event,
r.is_instead,
pg_get_ruledef(r.oid, true) AS rule_definition,
c.oid::regclass AS regclass
FROM pg_rewrite r
JOIN pg_class c ON c.oid = r.ev_class
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE ($1 IS NULL OR c.oid=$1)
AND NOT (r.ev_type = '1'::"char" AND r.rulename = '_RETURN'::name)
ORDER BY r.oid
$function$;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_get_triggers(
regclass default null,
OUT oid oid,
OUT is_constraint text, OUT trigger_name text, OUT action_order text,
OUT event_manipulation text, OUT event_object_sql_identifier text,
OUT action_statement text, OUT action_orientation text,
OUT trigger_definition text, OUT regclass regclass, OUT regprocedure regprocedure,
OUT event_object_schema text, OUT event_object_table text, OUT sql_identifier text)
RETURNS SETOF record LANGUAGE sql AS $function$
SELECT t.oid,
CASE t.tgisinternal
WHEN true THEN 'CONSTRAINT'::text
ELSE NULL::text
END AS is_constraint, t.tgname::text AS trigger_name,
CASE (t.tgtype::integer & 64) <> 0
WHEN true THEN 'INSTEAD'::text
ELSE CASE t.tgtype::integer & 2
WHEN 2 THEN 'BEFORE'::text
WHEN 0 THEN 'AFTER'::text
ELSE NULL::text
END
END AS action_order,
array_to_string(array[
case when (t.tgtype::integer & 4) <> 0 then 'INSERT' end,
case when (t.tgtype::integer & 8) <> 0 then 'DELETE' end,
case when (t.tgtype::integer & 16) <> 0 then 'UPDATE' end,
case when (t.tgtype::integer & 32) <> 0 then 'TRUNCATE' end
],' OR ') AS event_manipulation,
c.oid::regclass::text AS event_object_sql_identifier,
p.oid::regprocedure::text AS action_statement,
CASE t.tgtype::integer & 1
WHEN 1 THEN 'ROW'::text
ELSE 'STATEMENT'::text
END AS action_orientation,
pg_get_triggerdef(t.oid,true) as trigger_definition,
c.oid::regclass AS regclass,
p.oid::regprocedure AS regprocedure,
s.nspname::text AS event_object_schema,
c.relname::text AS event_object_table,
(quote_ident(t.tgname::text) || ' ON ') || c.oid::regclass::text AS sql_identifier
FROM pg_trigger t
LEFT JOIN pg_class c ON c.oid = t.tgrelid
LEFT JOIN pg_namespace s ON s.oid = c.relnamespace
LEFT JOIN pg_proc p ON p.oid = t.tgfoid
LEFT JOIN pg_namespace s1 ON s1.oid = p.pronamespace
WHERE $1 IS NULL OR c.oid=$1
$function$;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_get_indexes(
regclass default null,
OUT oid oid, OUT namespace text, OUT class text, OUT name text,
OUT tablespace text, OUT constraint_name text, OUT is_local boolean)
RETURNS SETOF record LANGUAGE sql AS $function$
SELECT DISTINCT
i.oid AS oid,
n.nspname::text AS namespace,
c.relname::text AS class,
i.relname::text AS name,
NULL::text AS tablespace,
cc.conname::text AS constraint_name,
d2.refobjid IS NULL AS is_local
FROM pg_index x
JOIN pg_class c ON c.oid = x.indrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
JOIN pg_class i ON i.oid = x.indexrelid
JOIN pg_depend d ON d.objid = x.indexrelid
LEFT JOIN pg_depend d2 ON d2.objid = x.indexrelid
AND d2.deptype='P' AND d2.refclassid='pg_class'::regclass
LEFT JOIN pg_constraint cc
ON cc.oid = d.refobjid AND d.refclassid='pg_constraint'::regclass
WHERE c.relkind in ('r','m','p')
AND i.relkind in ('i','I')
AND d.deptype in ('i','a')
AND ($1 IS NULL OR c.oid = $1)
$function$;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_get_functions(
regproc default null,
OUT sysid oid, OUT namespace name, OUT name name, OUT comment text,
OUT owner name, OUT sql_identifier text, OUT language name, OUT attributes text,
OUT retset boolean, OUT is_trigger boolean, OUT returns text, OUT arguments text,
OUT definition text, OUT security text, OUT is_strict text, OUT argtypes oidvector,
OUT cost real, OUT rows real)
RETURNS SETOF record LANGUAGE sql AS $function$
SELECT p.oid AS sysid,
s.nspname AS namespace,
p.proname AS name,
pg_description.description AS comment,
u.rolname AS owner,
p.oid::regprocedure::text AS sql_identifier,
l.lanname AS language,
CASE p.provolatile
WHEN 'i'::"char" THEN 'IMMUTABLE'::text
WHEN 's'::"char" THEN 'STABLE'::text
WHEN 'v'::"char" THEN 'VOLATILE'::text
ELSE NULL::text
END AS attributes,
p.proretset AS retset,
p.prorettype = 'trigger'::regtype::oid AS is_trigger,
text(p.prorettype::regtype) AS returns,
pg_get_function_arguments(p.oid) AS arguments,
-- oidvectortypes(p.proargtypes) AS argtypes,
p.prosrc AS definition,
CASE p.prosecdef
WHEN true THEN 'DEFINER'::text
ELSE 'INVOKER'::text
END AS security,
case p.proisstrict
WHEN true THEN 'STRICT'::text
ELSE NULL
END AS is_strict,
p.proargtypes AS proargtypes,
p.procost as cost,
p.prorows as rows
FROM pg_proc p
LEFT JOIN pg_namespace s ON s.oid = p.pronamespace
LEFT JOIN pg_language l ON l.oid = p.prolang
LEFT JOIN pg_roles u ON p.proowner = u.oid
LEFT JOIN pg_description ON p.oid = pg_description.objoid
WHERE $1 IS NULL OR p.oid = $1
$function$;
-----------------------------------------------------------
-- DDL generator functions for individial object types --
-----------------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_banner(
name text, kind text, namespace text, owner text, extra text default null
)
RETURNS text LANGUAGE sql AS $function$
SELECT format(E'%s-- Type: %s ; Name: %s; Owner: %s\n\n',
E'--\n-- ' || $5 || E'\n',
$2,$1,$4)
$function$;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_comment(oid)
RETURNS text LANGUAGE sql AS $function$
with obj as (select * from ddlx_identify($1))
select format(
E'COMMENT ON %s %s IS %L;\n',
obj.sql_kind, sql_identifier,
case
when obj.classid='pg_database'::regclass
then shobj_description(oid,classid::name)
when obj.classid='pg_tablespace'::regclass
then shobj_description(oid,classid::name)
else obj_description(oid)
end)
from obj
$function$ strict;
---------------------------------------------------
-- forward declarations, will be redefined later
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_create(oid, text[] default '{}') RETURNS text
LANGUAGE sql AS $function$ select null::text $function$;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_alter_owner(oid)
RETURNS text LANGUAGE sql AS $function$
with obj as (select * from ddlx_identify($1))
select
case
when obj.sql_kind = 'INDEX' then ''
else 'ALTER '||sql_kind||' '||sql_identifier||
' OWNER TO '||quote_ident(owner)||E';\n'
end
from obj
$function$ strict;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_table_constraints(regclass, OUT sql text)
RETURNS SETOF text LANGUAGE sql AS $function$
select
('CONSTRAINT ' || quote_ident(constraint_name) || ' ' || constraint_definition) as sql
from @[email protected]_get_constraints($1)
where is_local
order by constraint_type desc, constraint_name;
$function$ strict;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_create_table(regclass, text[] default '{}')
RETURNS text LANGUAGE sql AS $function$
with obj as (select * from ddlx_identify($1))
select
array_to_string(array[
'CREATE '||
case relpersistence
when 'u' then 'UNLOGGED '
when 't' then 'TEMPORARY '
else ''
end
|| obj.sql_kind || ' '
|| case when 'ine' ilike any($2) then 'IF NOT EXISTS ' else '' end
|| obj.sql_identifier
|| case when reloftype>0 then ' OF '||cast(reloftype::regtype as text) else '' end
|| case obj.sql_kind when 'TYPE' then ' AS' else '' end
||
#if 10
case
when c.relispartition
then ' PARTITION OF ' || (SELECT string_agg(i.inhparent::regclass::text,',')
FROM pg_inherits i WHERE i.inhrelid = $1)
else
#end
case when reloftype>0
then ''
else
' (' ||
coalesce(E'\n' ||
array_to_string(
array_cat(
(SELECT array_agg(' '||definition) FROM @[email protected]_describe($1) WHERE is_local),
(SELECT array_agg(' '||sql) FROM @[email protected]_table_constraints($1))
),
E',\n'
), '') || E'\n' || ')'
end
#if 10
end
#end
#if 10
,
case when c.relpartbound is not null
then pg_get_expr(c.relpartbound,c.oid,true)
end
#end
,
#if 10
case
when not c.relispartition
then (SELECT 'INHERITS(' || string_agg(i.inhparent::regclass::text,', ') || E')'
FROM pg_inherits i WHERE i.inhrelid = $1)
end
#else
(SELECT 'INHERITS(' || string_agg(i.inhparent::regclass::text,', ') || E')'
FROM pg_inherits i WHERE i.inhrelid = $1)
#end
#if 10
,
CASE
WHEN p.partstrat IS NOT NULL
THEN 'PARTITION BY ' || pg_get_partkeydef($1)
END
#end
#unless 12
,
CASE relhasoids WHEN true THEN 'WITH OIDS' END
#end
,
E'SERVER '||quote_ident(fs.srvname)||E' OPTIONS (\n'||
(select string_agg(
' '||quote_ident(option_name)||' '||quote_nullable(option_value),
E',\n')
from pg_options_to_table(ft.ftoptions))||E'\n)'
],E'\n ')
||
E';\n'
FROM pg_class c JOIN obj ON (true)
LEFT JOIN pg_foreign_table ft ON (c.oid = ft.ftrelid)
LEFT JOIN pg_foreign_server fs ON (ft.ftserver = fs.oid)
#if 10
LEFT JOIN pg_partitioned_table p ON p.partrelid = c.oid
#end
WHERE c.oid = $1
-- AND relkind in ('r','c')
$function$ strict;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_create_view(regclass, text[] default '{}')
RETURNS text LANGUAGE sql AS $function$
select
'CREATE '||
case relkind
when 'v' THEN 'OR REPLACE VIEW '
when 'm' THEN 'MATERIALIZED VIEW ' ||
case when 'ine' ilike any($2) then 'IF NOT EXISTS ' else '' end
end || (oid::regclass::text) || E' AS\n' ||
trim(';' from pg_catalog.pg_get_viewdef(oid,true)) ||
#if 9.3
case when relkind='m' and not relispopulated
then E'\n WITH NO DATA'
else E''
end ||
#end
E';\n'
FROM pg_class t
WHERE oid = $1
AND relkind in ('v','m')
$function$ strict;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_alter_sequence(regclass)
RETURNS text LANGUAGE sql AS $function$
with obj as (select * from ddlx_identify($1))
select
'ALTER SEQUENCE '||(oid::regclass::text)
||E'\n INCREMENT BY '||increment
||E'\n MINVALUE '||minimum_value
||E'\n MAXVALUE '||maximum_value
||E'\n START WITH '||start_value
||E'\n '|| case cycle_option when 'YES' then 'CYCLE' else 'NO CYCLE' end
||E';\n'
FROM information_schema.sequences s JOIN obj ON (true)
WHERE sequence_schema = obj.namespace
AND sequence_name = obj.name
AND obj.sql_kind = 'SEQUENCE'
$function$ strict;
CREATE OR REPLACE FUNCTION ddlx_create_sequence(regclass, text[] default '{}')
RETURNS text LANGUAGE sql AS $function$
with obj as (select * from ddlx_identify($1)),
seq as (
select sc.oid::regclass,d.refobjid::regclass,a.attname
from pg_class sc
left join pg_depend d on d.objid = sc.oid and d.deptype = 'a'
left join pg_attribute a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid
where relkind='S' and sc.oid = $1
)
select
'CREATE SEQUENCE '
|| case when 'ine' ilike any($2) then 'IF NOT EXISTS ' else '' end
||(obj.oid::regclass::text) || E';\n'
-- || ddlx_alter_sequence($1)
from obj;
$function$ strict;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_create_type_shell(regtype)
RETURNS text LANGUAGE sql AS $function$
select 'CREATE TYPE ' || format_type(oid,null) || ';\n'
from pg_type t
where oid = $1
$function$ strict;
---------------------------------------------------
CREATE OR REPLACE FUNCTION ddlx_create_type_base(regtype)
RETURNS text LANGUAGE sql AS $function$
select 'CREATE TYPE ' || format_type($1,null) || ' (' || E'\n ' ||
array_to_string(array[
'INPUT = ' || cast(t.typinput::regproc as text),
'OUTPUT = ' || cast(t.typoutput::regproc as text),
'SEND = ' || cast(nullif(t.typsend,0)::regproc as text),
'RECEIVE = ' || cast(nullif(t.typreceive,0)::regproc as text),
'TYPMOD_IN = ' || cast(nullif(t.typmodin,0)::regproc as text),
'TYPMOD_OUT = ' || cast(nullif(t.typmodout,0)::regproc as text),
'ANALYZE = ' || cast(nullif(t.typanalyze,0)::regproc as text),
'INTERNALLENGTH = ' ||
case when t.typlen < 0 then 'VARIABLE' else cast(t.typlen as text) end,
case when t.typbyval then 'PASSEDBYVALUE' end,
'ALIGNMENT = ' ||
case t.typalign
when 'c' then 'char'
when 's' then 'int2'
when 'i' then 'int4'
when 'd' then 'double'
end,
'STORAGE = ' ||
case t.typstorage
when 'p' then 'plain'
when 'e' then 'external'
when 'm' then 'main'
when 'x' then 'extended'
end,
'CATEGORY = ' || quote_nullable(t.typcategory::text),
case when t.typispreferred then E'PREFERRED = true' end,
case
when t.typdefault is not null
then E'DEFAULT = ' || quote_nullable(t.typdefault)
end,
case when t.typelem <> 0 then E'ELEMENT = ' || format_type(t.typelem,null) end,
'DELIMITER = ' || quote_nullable(t.typdelim::text),
'COLLATABLE = ' || case when t.typcollation <> 0 then 'true' else 'false' end
], E',\n ')
|| E'\n);\n\n'
from pg_type t
where oid = $1
$function$ strict;
---------------------------------------------------
#if 9.2
CREATE OR REPLACE FUNCTION ddlx_create_type_range(regtype)
RETURNS text LANGUAGE sql AS $function$
select 'CREATE TYPE ' || format_type($1,null) || E' AS RANGE (\n ' ||
array_to_string(array[
'SUBTYPE = ' || format_type(r.rngsubtype,null),
'SUBTYPE_OPCLASS = ' || quote_ident(opc.opcname),
case
when length(col.collcollate) > 0
then 'COLLATION = ' || quote_ident(col.collcollate::text)
end,
'CANONICAL = ' || cast(nullif(r.rngcanonical,0)::regproc as text),
'SUBTYPE_DIFF = ' || cast(nullif(r.rngsubdiff,0)::regproc as text)