Skip to content

Instantly share code, notes, and snippets.

@adunstan
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adunstan/9770065 to your computer and use it in GitHub Desktop.
Save adunstan/9770065 to your computer and use it in GitHub Desktop.
Vacuum a PostgreSQL schema's tables via dblink
-- requires dblink extension the be installed
-- vacuum a schema via dblink
create or replace function vacuum_schema(schemaname text)
returns void
language plpgsql
as
$func$
declare
rec record;
begin
-- fix connstr here to suit
perform dblink_connect('dbname='|| current_database());
for rec in
select relname
from pg_class c
join pg_namespace n
on c.relnamespace = n.oid
and n.nspname = schemaname
where c.relkind in ('r','m')
loop
perform dblink('VACUUM ' || quote_ident(schemaname) || '.'
|| quote_ident(rec.relname));
end loop;
perform dblink_disconnect();
end;
$func$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment