DDL:
CREATE FUNCTION public.abs(interval) RETURNS interval
LANGUAGE sql IMMUTABLE
AS $_$ select case when ($1<interval '0') then -$1 else $1 end; $_$;
CREATE FUNCTION public.vacuum_dead_size(i_now timestamp with time zone, OUT
schemaname text, OUT relname text, OUT total_bytes numeric, OUT
dead_tup_size nume
ric) RETURNS SETOF record
LANGUAGE sql
AS $_$
WITH closest_metric_stat_user_tables AS (
SELECT now FROM stat_user_tables ORDER BY abs(now-$1) LIMIT 1
), closest_metric_table_sizes AS (
SELECT now FROM table_sizes ORDER BY abs(now - $1) LIMIT 1
)
SELECT sut.schemaname, sut.relname, ts.total_bytes, 1::numeric
FROM stat_user_tables sut
LEFT JOIN table_sizes ts ON ts.table_name = sut.relname AND
ts.table_schema = sut.schemaname
WHERE ts.now = (SELECT now FROM closest_metric_table_sizes) AND sut.now
= (SELECT now FROM closest_metric_stat_user_tables)
ORDER BY 1;
$_$;
CREATE TABLE public.stat_user_tables (
now timestamp with time zone,
relid oid,
schemaname name,
relname name,
seq_scan bigint,
seq_tup_read bigint,
idx_scan bigint,
idx_tup_fetch bigint,
n_tup_ins bigint,
n_tup_upd bigint,
n_tup_del bigint,
n_tup_hot_upd bigint,
n_live_tup bigint,
n_dead_tup bigint,
n_mod_since_analyze bigint,
last_vacuum timestamp with time zone,
last_autovacuum timestamp with time zone,
last_analyze timestamp with time zone,
last_autoanalyze timestamp with time zone,
vacuum_count bigint,
autovacuum_count bigint,
analyze_count bigint,
autoanalyze_count bigint
);
CREATE TABLE public.table_sizes (
now timestamp with time zone,
oid oid,
table_schema name,
table_name name,
row_estimate real,
total_bytes bigint,
index_bytes bigint,
toast_bytes bigint,
table_bytes bigint,
total text,
index text,
toast text,
"table" text
);
--
Sent from: https://www.postgresql-archive.org/PostgreSQL-bugs-f2117394.html