Skip to content

Instantly share code, notes, and snippets.

@Checksum
Last active January 14, 2020 06:15
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 Checksum/00c140cbfb2a16080032061e515d831c to your computer and use it in GitHub Desktop.
Save Checksum/00c140cbfb2a16080032061e515d831c to your computer and use it in GitHub Desktop.
PostgreSQL handy queries
-- Get size of database
SELECT pg_size_pretty(pg_database_size('database-name'));
-- Set the current session as read only
SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;
-- Locks
-- https://www.postgresql.org/docs/10/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
SELECT pg_try_advisory_lock(123123213123) as lockObtained;
SELECT pg_try_advisory_unlock(123123213123);
-- Anonymous functions to verify migrations
do language plpgsql $$
begin
if (select count(*) from table_name) > 0 then
raise exception 'Invalid count in table_name';
end if;
end;
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment