1
1
if [[ ! -z ${IS_LARGE_DB+x} ]] && [[ ${IS_LARGE_DB} == " 1" ]]; then
2
- MIN_RELPAGES=1000
2
+ MIN_RELPAGES=100
3
3
else
4
4
MIN_RELPAGES=0
5
5
fi
@@ -10,11 +10,14 @@ f_stderr=$(mktemp)
10
10
(${CHECK_HOST_CMD} " ${_PSQL} -f - " << SQL
11
11
do \$ $
12
12
declare
13
+ MIN_RELPAGES int8 = ${MIN_RELPAGES} ; -- skip tables with small number of pages
13
14
rec record;
14
15
out text;
16
+ out1 json;
15
17
i numeric;
16
18
val int8;
17
19
ratio numeric;
20
+ sql text;
18
21
begin
19
22
out := '';
20
23
i := 0;
25
28
nspname as schema_name,
26
29
relname as table_name,
27
30
t.typname,
28
- attname,
29
- (select pg_get_serial_sequence(quote_ident(nspname) || '.' || quote_ident(relname), attname)) as seq
31
+ (select pg_get_serial_sequence(quote_ident(nspname) || '.' || quote_ident(relname), attname)) as seq ,
32
+ min( attname) as attname
30
33
from pg_index i
31
34
join pg_class c on c.oid = i.indrelid
32
35
left join pg_namespace n on n.oid = c.relnamespace
@@ -36,25 +39,35 @@ begin
36
39
join pg_type t on t.oid = atttypid
37
40
where
38
41
i.indisprimary
39
- and (c.relpages > ${ MIN_RELPAGES} or (select pg_get_serial_sequence(quote_ident(nspname) || '.' || quote_ident(relname), attname)) is not null)
42
+ and (c.relpages > MIN_RELPAGES or (select pg_get_serial_sequence(quote_ident(nspname) || '.' || quote_ident(relname), attname)) is not null)
40
43
and t.typname in ('int2', 'int4')
41
44
and nspname <> 'pg_toast'
45
+ group by 1, 2, 3, 4, 5, 6
46
+ having count(*) = 1 -- skip PKs with 2+ columns
42
47
loop
48
+ raise debug 'table: %', rec.table_name;
49
+
43
50
if rec.seq is null then
44
- execute format('select max(%I) from %I.%I;', rec.attname, rec.schema_name, rec.table_name) into val ;
51
+ sql := format('select max(%I) from %I.%I;', rec.attname, rec.schema_name, rec.table_name);
45
52
else
46
- execute format('SELECT last_value FROM %s;', rec.seq) into val ;
53
+ sql := format('select last_value from %s;', rec.seq);
47
54
end if;
55
+
56
+ raise debug 'sql: %', sql;
57
+ execute sql into val;
58
+
48
59
if rec.typname = 'int4' then
49
60
ratio := (val::numeric / 2^31)::numeric;
50
61
elsif rec.typname = 'int2' then
51
62
ratio := (val::numeric / 2^15)::numeric;
52
63
else
53
64
assert false, 'unreachable point';
54
65
end if;
66
+
55
67
if ratio > 0.1 then -- report only if > 10% of capacity is reached
56
68
i := i + 1;
57
- out := out || '{"' || rec.table_name || '":' || json_build_object(
69
+
70
+ out1 := json_build_object(
58
71
'table',
59
72
coalesce(nullif(quote_ident(rec.schema_name), 'public') || '.', '') || quote_ident(rec.table_name),
60
73
'pk',
65
78
val,
66
79
'capacity_used_percent',
67
80
round(100 * ratio, 2)
68
- ) || '}';
81
+ );
82
+
83
+ raise debug 'cur: %', out1;
84
+
85
+ if out <> '' then out := out || ', '; end if;
86
+
87
+ out := out || '"' || rec.table_name || '":' || out1 || '';
69
88
end if;
70
89
end loop;
90
+
91
+ out := '{' || out || '}';
92
+
71
93
raise info '%', out;
72
94
end;
73
95
\$ $ language plpgsql;
0 commit comments