From 74d0ca729b22d8f5537a7b9a443427583922156d Mon Sep 17 00:00:00 2001 From: Nikolay Samokhvalov Date: Fri, 21 Aug 2020 18:33:48 +0000 Subject: [PATCH 1/3] L003: do not analyze multi-columns PKs - Skip multi-column PKs: analyzing each column in them may cause a huge slowdown, - fix JSON syntax (previously, jq was not complaining, but syntax lacked some commas) --- resources/checks/L003_integer_in_pk.sh | 38 ++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/resources/checks/L003_integer_in_pk.sh b/resources/checks/L003_integer_in_pk.sh index d37ddf2..f8cb6e4 100644 --- a/resources/checks/L003_integer_in_pk.sh +++ b/resources/checks/L003_integer_in_pk.sh @@ -1,5 +1,5 @@ if [[ ! -z ${IS_LARGE_DB+x} ]] && [[ ${IS_LARGE_DB} == "1" ]]; then - MIN_RELPAGES=1000 + MIN_RELPAGES=100 else MIN_RELPAGES=0 fi @@ -10,11 +10,14 @@ f_stderr=$(mktemp) (${CHECK_HOST_CMD} "${_PSQL} -f - " < ${MIN_RELPAGES} or (select pg_get_serial_sequence(quote_ident(nspname) || '.' || quote_ident(relname), attname)) is not null) + and (c.relpages > or (select pg_get_serial_sequence(quote_ident(nspname) || '.' || quote_ident(relname), attname)) is not null) and t.typname in ('int2', 'int4') and nspname <> 'pg_toast' + group by 1, 2, 3, 4, 5, 6 + having count(*) = 1 -- skip PKs with 2+ columns loop + raise debug 'table: %', rec.table_name; + if rec.seq is null then - execute format('select max(%I) from %I.%I;', rec.attname, rec.schema_name, rec.table_name) into val; + sql := format('select max(%I) from %I.%I;', rec.attname, rec.schema_name, rec.table_name); else - execute format('SELECT last_value FROM %s;', rec.seq) into val; + sql := format('select last_value from %s;', rec.seq); end if; + + raise debug 'sql: %', sql; + execute sql into val; + if rec.typname = 'int4' then ratio := (val::numeric / 2^31)::numeric; elsif rec.typname = 'int2' then @@ -52,9 +63,11 @@ begin else assert false, 'unreachable point'; end if; + if ratio > 0.1 then -- report only if > 10% of capacity is reached i := i + 1; - out := out || '{"' || rec.table_name || '":' || json_build_object( + + out1 := json_build_object( 'table', coalesce(nullif(quote_ident(rec.schema_name), 'public') || '.', '') || quote_ident(rec.table_name), 'pk', @@ -65,9 +78,18 @@ begin val, 'capacity_used_percent', round(100 * ratio, 2) - ) || '}'; + ); + + raise debug 'cur: %', out1; + + if out <> '' then out := out || ', '; end if; + + out := out || '"' || rec.table_name || '":' || out1 || ''; end if; end loop; + + out := '{' || out || '}'; + raise info '%', out; end; \$$ language plpgsql; -- GitLab From ef67b99a4094f8bdd15c063899892219dee7b214 Mon Sep 17 00:00:00 2001 From: Nikolay Samokhvalov Date: Fri, 21 Aug 2020 18:37:07 +0000 Subject: [PATCH 2/3] Apply 1 suggestion(s) to 1 file(s) --- resources/checks/L003_integer_in_pk.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/checks/L003_integer_in_pk.sh b/resources/checks/L003_integer_in_pk.sh index f8cb6e4..7c237f9 100644 --- a/resources/checks/L003_integer_in_pk.sh +++ b/resources/checks/L003_integer_in_pk.sh @@ -10,7 +10,7 @@ f_stderr=$(mktemp) (${CHECK_HOST_CMD} "${_PSQL} -f - " < Date: Fri, 21 Aug 2020 18:38:00 +0000 Subject: [PATCH 3/3] Apply 1 suggestion(s) to 1 file(s) --- resources/checks/L003_integer_in_pk.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/checks/L003_integer_in_pk.sh b/resources/checks/L003_integer_in_pk.sh index 7c237f9..692ce9d 100644 --- a/resources/checks/L003_integer_in_pk.sh +++ b/resources/checks/L003_integer_in_pk.sh @@ -39,7 +39,7 @@ begin join pg_type t on t.oid = atttypid where i.indisprimary - and (c.relpages > or (select pg_get_serial_sequence(quote_ident(nspname) || '.' || quote_ident(relname), attname)) is not null) + and (c.relpages > MIN_RELPAGES or (select pg_get_serial_sequence(quote_ident(nspname) || '.' || quote_ident(relname), attname)) is not null) and t.typname in ('int2', 'int4') and nspname <> 'pg_toast' group by 1, 2, 3, 4, 5, 6 -- GitLab