forked from BrandwatchLtd/trunkfingerprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrunkfingerprint--1.0.0.sql
656 lines (636 loc) · 37.3 KB
/
trunkfingerprint--1.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
-- this thing calculates a fingerprint of DB structure
create schema if not exists @extschema@;
--=================================================================================================
create or replace function _get_catalog_columns(p_catalog regclass) returns table (
catalog_oid oid,
column_list text
) as $f$
begin
return query
select attrelid,
string_agg(
case
-- do not show certain columns, as they contain non-universal ids or are unreliable in some kind
when (relname, attname) in (
('pg_depend', 'objsubid'), -- tracked in objid
('pg_depend', 'refobjsubid'), -- tracked in refobjid
('pg_description', 'objsubid'), -- tracked in objoid
('pg_class', 'reltoastrelid'),
('pg_class', 'relfrozenxid'),
('pg_class', 'relminmxid'),
('pg_class', 'relnatts'),
('pg_class', 'reltuples'),
('pg_class', 'relpages'),
('pg_class', 'relallvisible'),
('pg_attribute', 'attnum'), -- we don't check attributes order
('pg_attribute', 'attndims'),-- this is not enforced by postgres; create table like does not copy it
('pg_attrdef', 'adsrc'), -- shows old values
('pg_constraint', 'consrc'), -- shows old values
('pg_class', 'relhaspkey'), -- shows old values
('pg_class', 'relhasrules'), -- shows old values
('pg_class', 'relhastriggers'), -- shows old values
('pg_class', 'relhassubclass') -- shows old values
)
then 'null::int'
-- show object names instead of oids
when atttypid = 'oid'::regtype then
case when attname like '%namespace' then '(select nspname from pg_namespace where oid = foo.' || attname || ')'
when attname like '%owner'
or attname like '%role'
or attname like '%user' then '(select rolname from pg_roles where oid = foo.' || attname || ')'
when attname like '%collation' then '(select collname from pg_collation where oid = foo.' || attname || ')'
when attname like '%server' then '(select srvname from pg_foreign_server where oid = foo.' || attname || ')'
when attname like '%fdw' then '(select fdwname from pg_foreign_data_wrapper where oid = foo.' || attname || ')'
when attname like '%lang' then '(select lanname from pg_language where oid = foo.' || attname || ')'
when attname like '%constraint'then '(select conname from pg_constraint where oid = foo.' || attname || ')'
when attname like '%am'
or attname like '%method' then '(select amname from pg_am where oid = foo.' || attname || ')'
when attname like '%family' then '(select opfname from pg_opfamily where oid = foo.' || attname || ')'
when attname like '%opr'
or attname like '%op'
or attname = any('{oprcom,oprnegate}')
then '(select oprname from pg_operator where oid = foo.' || attname || ')'
when attname like '%opc' then '(select opcname from pg_opclass where oid = foo.' || attname || ')'
when attname like '%tablespace'then '(select spcname from pg_tablespace where oid = foo.' || attname || ')'
when attname like '%relid'
or attname like '%indid'
or attname like '%classid'
or attname like '%classoid'
or attname like '%@_class' escape '@'
or attname = any('{inhparent}')
or (relname, attname) = ('pg_class', 'oid')
then attname || '::regclass::text'
when attname like '%typid'
or attname like '%type'
or attname = any('{typelem,typarray,typbasetype,provariadic,oprleft,oprright,oprresult,
castsource,casttarget}')
or (relname, attname) = ('pg_type', 'oid')
then attname || '::regtype::text'
when attname like '%foid'
or attname like '%func'
or attname like '%validator'
or attname = any('{laninline,fdwhandler}')
or (relname, attname) = ('pg_proc', 'oid')
then attname || '::regprocedure::text'
when (relname, attname) = ('pg_class', 'relfilenode')
then 'null::int'
when attname = 'oid' -- ignore those as they are not used elsewhere
and relname = any('{pg_attrdef,pg_user_mapping,pg_constraint,pg_language,pg_rewrite,pg_extension,
pg_foreign_data_wrapper,pg_foreign_server,pg_foreign_table,pg_policy,pg_default_acl,
pg_trigger,pg_event_trigger,pg_cast,pg_enum,pg_namespace,pg_conversion,pg_transform,
pg_operator,pg_opfamily,pg_opclass,pg_am,pg_amop,pg_amproc,pg_collation}'::text[])
then 'null::int'
when
(relname = 'pg_depend' and attname = any('{refobjid,objid}'))
or
(relname = 'pg_description' and attname = 'objoid')
then format($$
case %1$s::regclass::text
when 'pg_am' then (
select amname::text
from pg_am
where oid = %2$s
)
when 'pg_amop' then (
select (amname, opfname, nspname, amoplefttype::regtype, amoprighttype::regtype, amoppurpose)::text
from pg_amop
join pg_opfamily on pg_opfamily.oid = amopfamily
join pg_am on pg_am.oid = opfmethod
join pg_namespace on pg_namespace.oid = opfnamespace
where pg_amop.oid = %2$s
)
when 'pg_amproc' then (
select (amname, opfname, nspname, amproclefttype::regtype, amprocrighttype::regtype, amprocnum)::text
from pg_amproc
join pg_opfamily on pg_opfamily.oid = amprocfamily
join pg_am on pg_am.oid = opfmethod
join pg_namespace on pg_namespace.oid = opfnamespace
where pg_amproc.oid = %2$s
)
when 'pg_attrdef' then (
select (attrelid::regclass::text, attname)::text
from pg_attribute
join pg_attrdef on attnum = adnum
and attrelid = adrelid
where pg_attrdef.oid = %2$s
)
when 'pg_authid' then (
select rolname::text
from pg_authid
where oid = %2$s
)
when 'pg_cast' then (
select (castsource::regtype::text, casttarget::regtype::text)::text
from pg_cast
where oid = %2$s
)
when 'pg_class' then
case %3$s
when 0 then %2$s::regclass::text
else (
select (%2$s::regclass, attname)::text
from pg_attribute
where attrelid = %2$s
and attnum = %3$s
)
end
when 'pg_collation' then (
select (nspname, collname, collencoding)::text
from pg_collation
join pg_namespace on pg_namespace.oid = collnamespace
where pg_collation.oid = %2$s
)
when 'pg_constraint' then (
select (conrelid::regclass::text, conname)::text -- is it unique?
from pg_constraint
where oid = %2$s
)
when 'pg_conversion' then (
select (nspname, conname)::text
from pg_conversion
join pg_namespace on pg_namespace.oid = connamespace
where pg_conversion.oid = %2$s
)
when 'pg_database' then (
select datname::text
from pg_database
where oid = %2$s
)
when 'pg_default_acl' then (
select (rolname, nspname, defaclobjtype)::text
from pg_default_acl
join pg_namespace on pg_namespace.oid = defaclnamespace
join pg_roles on pg_roles.oid = defaclrole
where pg_default_acl.oid = %2$s
)
when 'pg_event_trigger' then (
select evtname::text
from pg_event_trigger
where oid = %2$s
)
when 'pg_extension' then (
select extname::text
from pg_extension
where oid = %2$s
)
when 'pg_foreign_data_wrapper' then (
select fdwname::text
from pg_foreign_data_wrapper
where oid = %2$s
)
when 'pg_foreign_server' then (
select srvname::text
from pg_foreign_server
where oid = %2$s
)
when 'pg_language' then (
select lanname::text
from pg_language
where oid = %2$s
)
when 'pg_namespace' then (
select nspname::text
from pg_namespace
where oid = %2$s
)
when 'pg_opclass' then (
select (amname, opcname, nspname)::text
from pg_opclass
join pg_am on pg_am.oid = opcmethod
join pg_namespace on pg_namespace.oid = opcnamespace
where pg_opclass.oid = %2$s
)
when 'pg_opfamily' then (
select (amname, opfname, nspname)::text
from pg_opfamily
join pg_am on pg_am.oid = opfmethod
join pg_namespace on pg_namespace.oid = opfnamespace
where pg_opfamily.oid = %2$s
)
when 'pg_operator' then (
select (oprname, oprleft::regtype, oprright::regtype, nspname)::text
from pg_operator
join pg_namespace on pg_namespace.oid = oprnamespace
where pg_operator.oid = %2$s
)
when 'pg_proc' then %2$s::regprocedure::text
when 'pg_rewrite' then (
select (ev_class::regclass, rulename)::text
from pg_rewrite
where oid = %2$s
)
when 'pg_tablespace' then (
select spcname::text
from pg_tablespace
where oid = %2$s
)
when 'pg_trigger' then (
select (tgrelid::regclass, tgname)::text
from pg_trigger
where oid = %2$s
)
when 'pg_ts_config' then %2$s::regconfig::text
when 'pg_ts_dict' then %2$s::regdictionary::text
when 'pg_ts_parser' then (
select (nspname, prsname)::text
from pg_ts_parser
join pg_namespace on pg_namespace.oid = prsnamespace
where pg_ts_parser.oid = %2$s
)
when 'pg_ts_template' then (
select (nspname, tmplname)::text
from pg_ts_template
join pg_namespace on pg_namespace.oid = tmplnamespace
where pg_ts_template.oid = %2$s
)
when 'pg_type' then %2$s::regtype::text
when 'pg_user_mapping' then (
select (rolname, srvname)::text
from pg_user_mapping
join pg_roles on pg_roles.oid = umuser
join pg_foreign_server on pg_foreign_server.oid = umserver
where pg_user_mapping.oid = %2$s
)
when '-' then '-'::text
else 1/(1 - %2$s::int/%2$s::int)
|| '%% pg_depend tracking for this kind of objects not implemented %%'
end
$$,
case attname
when 'objoid' then 'classoid'
when 'objid' then 'classid'
when 'refobjid' then 'refclassid'
end /*$1%s*/,
attname /*$2%s*/,
case attname
when 'objoid' then 'objsubid'
when 'objid' then 'objsubid'
when 'refobjid' then 'refobjsubid'
end /*$3%s*/
)
else '% attempt to return bare oid % ' || attname
end
-- show object names arrays instead of oidvectors
when atttypid = any('{oidvector,oid[]}'::regtype[]) then
case when attname like '%collation' then '(select array_agg(collname order by ord)
from unnest(' || attname || ') with ordinality as foo(colloid,ord)
join pg_collation on pg_collation.oid = colloid)'
when attname = 'indclass' then '(select array_agg(opcname order by ord)
from unnest(' || attname || ') with ordinality as foo(opcoid,ord)
join pg_opclass on pg_opclass.oid = opcoid)'
when attname like '%op' then '(select array_agg(oprname order by ord)
from unnest(' || attname || ') with ordinality as foo(oproid,ord)
join pg_operator on pg_operator.oid = oproid)'
when attname like '%roles'
or attname = 'pg_group' then '(select array_agg(rolname order by ord)
from unnest(' || attname || ') with ordinality as foo(roloid,ord)
join pg_roles on pg_roles.oid = roloid)'
when attname like '%types' then attname || '::regtype[]::text'
when attname = 'extconfig' then attname || '::regclass[]::text'
else '% attempt to return bare oidvector % ' || attname
end
-- show something pretty-formatted instead of pg_node_trees
when atttypid = 'pg_node_tree'::regtype then
case relname || '.' || attname
when 'pg_attrdef.adbin' then 'pg_get_expr(adbin, adrelid, true)'
when 'pg_constraint.conbin' then 'pg_get_constraintdef(oid, true)'
when 'pg_index.indexprs' then 'pg_get_expr(indexprs, indrelid, true)'
when 'pg_index.indpred' then 'pg_get_expr(indpred, indrelid, true)'
when 'pg_proc.proargdefaults' then 'pg_catalog.pg_get_function_arguments(oid)'
when 'pg_rewrite.ev_action' then 'pg_catalog.pg_get_ruledef(oid, true)' -- NB: this function call takes most of the time
when 'pg_rewrite.ev_qual' then 'null::int' -- already tracked one line above
when 'pg_trigger.tgqual' then 'pg_get_triggerdef(oid, true)'
when 'pg_type.typdefaultbin' then 'null::int' -- already tracked in pg_type.typedefault
else '% attempt to return bare pg_node_tree % ' || attname
end
-- get certain arrays sorted
when atttypid = 'aclitem[]'::regtype
or (atttypid = 'text[]'::regtype and (attname like '%options' or attname like '%config'))
or (relname, attname) = ('pg_event_trigger', 'evttags')
then format('(select array_agg(item::text order by item::text) from unnest(%I) item)', attname)
-- show column names instead of attnums
when (relname, attname) = ('pg_attrdef', 'adnum')
then '(select attname from pg_attribute where attnum = adnum and attrelid = adrelid)'
when (relname, attname) = ('pg_index', 'indkey')
then '(select array_agg(attname order by ord) from unnest(indkey::int2[]) with ordinality as foo(attn,ord)
join pg_attribute on attnum = attn and attrelid = indrelid)'
when (relname, attname) = ('pg_trigger', 'tgattr')
then '(select array_agg(attname order by ord) from unnest(tgattr::int2[]) with ordinality as foo(attn,ord)
join pg_attribute on attnum = attn and attrelid = tgrelid)'
when (relname, attname) = ('pg_constraint', 'conkey')
then '(select array_agg(attname order by ord) from unnest(conkey) with ordinality as foo(attn,ord)
join pg_attribute on attnum = attn and attrelid = conrelid)'
when (relname, attname) = ('pg_constraint', 'confkey')
then '(select array_agg(attname order by ord) from unnest(confkey) with ordinality as foo(attn,ord)
join pg_attribute on attnum = attn and attrelid = confrelid)'
-- show all the rest as is
else attname::text end,
', '
order by attnum
)
from pg_attribute
join pg_class on pg_class.oid = pg_attribute.attrelid
join pg_namespace on pg_namespace.oid = relnamespace
where nspname = 'pg_catalog'
and relkind = 'r'
and relname <> all('{pg_statistic,pg_largeobject,pg_largeobject_metadata}'::name[] || -- those are rather data than structure
'{pg_seclabel}'::name[] -- TBD
)
and relname not like 'pg@_ts@_%' escape '@' -- TBD: FTS-related
and (attnum > 0 or attname = 'oid')
and not relisshared
and (p_catalog = attrelid) is not false
group by attrelid,
relname
order by relname;
end;
$f$ language plpgsql set search_path to pg_catalog, @extschema@, pg_temp;
comment on function _get_catalog_columns(regclass)
is 'Provides a list of expressions to select from it for each system catalog.';
--=================================================================================================
create or replace function _get_excluded_objects(p_exclude_schemas name[]) returns table (
classid oid,
objid oid
) as $f$
with
excluded_namespace as (
select tableoid classid, oid objid
from pg_namespace
where nspname = any(p_exclude_schemas)
or nspname like 'pg\_temp\_%'
or nspname like 'pg\_toast\_temp\_%'
),
excluded_proc as (
select tableoid classid, oid objid
from pg_proc
where pronamespace in (select objid from excluded_namespace)
),
excluded_opfamily as (
select tableoid classid, oid objid
from pg_opfamily
where opfnamespace in (select objid from excluded_namespace)
),
excluded_class as (
select tableoid classid, oid objid
from pg_class
where relnamespace in (select objid from excluded_namespace)
),
excluded_ts_config as (
select tableoid classid, oid objid
from pg_ts_config
where cfgnamespace in (select objid from excluded_namespace)
),
excluded_type as (
select tableoid classid, oid objid
from pg_type
where typnamespace in (select objid from excluded_namespace)
)
table excluded_namespace
union all
table excluded_proc
union all
table excluded_opfamily
union all
table excluded_class
union all
table excluded_ts_config
union all
table excluded_type
union all
select tableoid classid, oid objid
from pg_conversion
where connamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_opclass
where opcnamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_operator
where oprnamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_collation
where collnamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_default_acl
where defaclnamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_extension
where extnamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_ts_dict
where dictnamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_ts_parser
where prsnamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_ts_template
where tmplnamespace in (select objid from excluded_namespace)
union all
select tableoid classid, oid objid
from pg_cast
where castfunc in (select objid from excluded_proc)
union all
select tableoid classid, oid objid
from pg_amop
where amopfamily in (select objid from excluded_opfamily)
union all
select tableoid classid, oid objid
from pg_amproc
where amprocfamily in (select objid from excluded_opfamily)
union all
select tableoid classid, oid objid
from pg_rewrite
where ev_class in (select objid from excluded_class)
union all
select tableoid classid, oid objid
from pg_constraint
where conrelid in (select objid from excluded_class)
union all
select tableoid classid, oid objid
from pg_trigger
where tgrelid in (select objid from excluded_class)
union all
select tableoid classid, oid objid
from pg_attrdef
where adrelid in (select objid from excluded_class)
union all
select tableoid classid, oid objid
from pg_enum
where enumtypid in (select objid from excluded_type);
$f$ language sql set search_path to pg_catalog, @extschema@, pg_temp;
comment on function _get_excluded_objects(name[])
is 'Given a list of schemas to exclude, this function retuns all the objects kind of in these schemas';
--=================================================================================================
create or replace function _get_not_restricted_condition_for_catalog(p_oid_column text, p_catalog_oid regclass)
returns text
as $f$
select $$foo.$$ || p_oid_column || $$ not in (
select e.objid
from excluded_objects e
where e.classid = $$ || p_catalog_oid::oid || $$
) $$;
$f$ language sql set search_path to pg_catalog, @extschema@, pg_temp;
comment on function _get_not_restricted_condition_for_catalog(text, regclass)
is 'Helper function for building SQL';
--=================================================================================================
create or replace function _get_single_catalog_sql(p_level int, p_catalog_oid oid, p_column_list text)
returns text as $f$
declare
c_not_in_all_excluded_objects_sql text := $$
not in (
select e.classid,
e.objid
from excluded_objects e
)
$$;
c_notoast text := $$
not in (
select pg_class.oid
from pg_class
join pg_namespace on pg_namespace.oid = relnamespace
where nspname = 'pg_toast'
)$$;
begin
return
-- select list
case when p_level = 2 then $$
select ($$ || p_column_list || $$)
$$ else $$
select hash_array(coalesce(
array_agg(
hashtext(($$ || p_column_list || $$)::text)
order by
hashtext(($$ || p_column_list || $$)::text)
),
'{}'
))::text
$$ end || $$
-- from
from $$ || p_catalog_oid::regclass || $$ foo
-- conditions:
where $$ ||
-- condition for not being in restricted schema
case
-- all-objects catalogs
when p_catalog_oid = 'pg_depend'::regclass
then $$ (classid, objid) $$ || c_not_in_all_excluded_objects_sql
when p_catalog_oid = any('{pg_description,pg_seclabel}'::regclass[])
then $$ (classoid, objoid) $$ || c_not_in_all_excluded_objects_sql
-- catalogs without oid column (how on the earth they exist!)
when p_catalog_oid = 'pg_aggregate'::regclass
then _get_not_restricted_condition_for_catalog('aggfnoid', 'pg_proc')
when p_catalog_oid = 'pg_index'::regclass
then _get_not_restricted_condition_for_catalog('indexrelid', 'pg_class')
when p_catalog_oid = 'pg_foreign_table'::regclass
then _get_not_restricted_condition_for_catalog('ftrelid', 'pg_class')
when p_catalog_oid = 'pg_inherits'::regclass
then _get_not_restricted_condition_for_catalog('inhrelid', 'pg_class')
when p_catalog_oid = 'pg_attribute'::regclass
then _get_not_restricted_condition_for_catalog('attrelid', 'pg_class')
when p_catalog_oid = 'pg_ts_config_map'::regclass
then _get_not_restricted_condition_for_catalog('mapcfg', 'pg_ts_config')
when p_catalog_oid = 'pg_range'::regclass
then _get_not_restricted_condition_for_catalog('rngtypid', 'pg_type')
-- ordinary catalogs
else
_get_not_restricted_condition_for_catalog('oid', p_catalog_oid)
end ||
-- other catalog-specific conditions
case p_catalog_oid
when 'pg_namespace'::regclass -- exclude temp schemas
then $$ and nspname not like 'pg@_temp@_%' escape '@'
and nspname not like 'pg@_toast@_temp@_%' escape '@' $$
when 'pg_trigger'::regclass -- exclude internal trigger, as their names are autogenerated
then $$ and not tgisinternal $$
when 'pg_attribute'::regclass -- exclude dropped columns and columns of toasted tables and indexes
then $$ and not attisdropped
and attrelid not in (
select pg_class.oid
from pg_class
join pg_namespace on pg_namespace.oid = relnamespace
where nspname = 'pg_toast'
or relkind = 'i'
) $$
when 'pg_attrdef'::regclass -- exclude dropped columns defaults
then $$ and (adrelid, adnum)
not in (select attrelid, attnum from pg_attribute where attisdropped) $$
when 'pg_class'::regclass -- exclude toast tables
then $$ and not relisshared
and oid $$ || c_notoast
when 'pg_index'::regclass -- exclude toast tables indices
then $$ and indrelid $$ || c_notoast
when 'pg_type'::regclass -- exclude toast table types
then $$ and typrelid $$ || c_notoast
when 'pg_depend'::regclass -- exclude rule-depends-on-column links
then $$ and not (
classid = 'pg_rewrite'::regclass and
refclassid = 'pg_class'::regclass and
refobjsubid <> 0
) and (
classid <> 'pg_class'::regclass or objid $$ || c_notoast || $$
) and (
refclassid <> 'pg_class'::regclass or refobjid $$ || c_notoast || $$
) and (
deptype not in ('i', 'p')
)$$
else $$ $$
end ||
-- ordering
case
when p_level = 2
then $$ order by ($$ || p_column_list || $$)$$
else $$ $$
end;
end;
$f$ language plpgsql set search_path to pg_catalog, @extschema@, pg_temp;
comment on function _get_single_catalog_sql(int, oid, text)
is 'Build an SQL for a single catalog';
--=================================================================================================
create or replace function get_db_structure_hash(
p_level int default 0,
p_catalog regclass default null,
p_exclude_schemas name[] default '{}'
)
returns setof varchar as $f$
declare
catalog_oid oid;
column_list text;
one_hash text;
hashes text := '';
single_catalog_sql text;
begin
create temp table excluded_objects on commit drop as
select * from _get_excluded_objects(p_exclude_schemas);
for catalog_oid, column_list in select * from _get_catalog_columns(p_catalog) loop
if p_catalog is null and p_level = 2 then
return next catalog_oid::regclass::text;
end if;
if p_level = 2 then
return next column_list;
end if;
single_catalog_sql := _get_single_catalog_sql(p_level, catalog_oid, column_list);
raise debug '%: running %', clock_timestamp(), single_catalog_sql;
for one_hash in execute single_catalog_sql loop
if p_level >= 1 then
return next catalog_oid::regclass || ' ' || one_hash;
else
hashes := hashes || one_hash;
end if;
end loop;
end loop;
if p_level = 0 then
return next md5(hashes);
end if;
drop table excluded_objects;
end;
$f$ language plpgsql set search_path to pg_catalog, @extschema@, pg_temp;
comment on function get_db_structure_hash(int, regclass, name[])
is 'Main function to call, p_level could be 0 (single fingerprint), '
'1 (one value for each catalog) or 2 (a line per each catalog object)';