Skip to content

Instantly share code, notes, and snippets.

@carlosipe
Created December 23, 2020 17:24
Show Gist options
  • Save carlosipe/0ec5e9035870b69c51f9f9a1c80c9d97 to your computer and use it in GitHub Desktop.
Save carlosipe/0ec5e9035870b69c51f9f9a1c80c9d97 to your computer and use it in GitHub Desktop.
Use -E in psql
get used to run always psql with the `E` option.
psql -E $DATABASE_URL
Whenever you run \ commands like \d table1 you see the queries psql is running.
It's really useful to learn about postgresql internals everyday.
eg:
\d schools
******** QUERY **********
SELECT pubname
FROM pg_catalog.pg_publication p
JOIN pg_catalog.pg_publication_rel pr ON p.oid = pr.prpubid
WHERE pr.prrelid = '80421'
UNION ALL
SELECT pubname
FROM pg_catalog.pg_publication p
WHERE p.puballtables AND pg_catalog.pg_relation_is_publishable('80421')
ORDER BY 1;
**************************
********* QUERY **********
SELECT t.tgname, pg_catalog.pg_get_triggerdef(t.oid, true), t.tgenabled, t.tgisinternal
FROM pg_catalog.pg_trigger t
WHERE t.tgrelid = '80421' AND (NOT t.tgisinternal OR (t.tgisinternal AND t.tgenabled = 'D')
OR EXISTS (SELECT 1 FROM pg_catalog.pg_depend WHERE objid = t.oid
AND refclassid = 'pg_catalog.pg_trigger'::pg_catalog.regclass))
ORDER BY 1;
**************************
********* QUERY **********
SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '80421' AND c.relkind != 'p' ORDER BY inhseqno;
**************************
********* QUERY **********
SELECT c.oid::pg_catalog.regclass, pg_catalog.pg_get_expr(c.relpartbound, c.oid), c.relkind FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '80421' ORDER BY pg_catalog.pg_get_expr(c.relpartbound, c.oid) = 'DEFAULT', c.oid::pg_catalog.regclass::pg_catalog.text;
**************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment