Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Created April 2, 2024 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CHERTS/f97306041c2ed3e993045849d6557ef3 to your computer and use it in GitHub Desktop.
Save CHERTS/f97306041c2ed3e993045849d6557ef3 to your computer and use it in GitHub Desktop.
PostgreSQL run analyze all table (using pl/pgsql script)
DO $$
DECLARE
tab RECORD;
schemaName VARCHAR := 'public';
BEGIN
for tab in (SELECT t.relname::varchar AS table_name
FROM pg_class t
JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE t.relkind = 'r' and n.nspname::varchar = schemaName
ORDER BY 1)
LOOP
RAISE NOTICE 'ANALYZE %.%', schemaName, tab.table_name;
EXECUTE format('ANALYZE %I.%I', schemaName, tab.table_name);
end loop;
end
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment