From 097e3b4aa65c7d82d6caad354b4eb9a3c37afe78 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 2 Jun 2022 14:53:54 -0500 Subject: [PATCH] Add Show The Hidden Queries Behind Backslash Commands as a Postgres TIL --- README.md | 3 +- ...idden-queries-behind-backslash-commands.md | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 postgres/show-the-hidden-queries-behind-backslash-commands.md diff --git a/README.md b/README.md index d2c02a0..b84ec32 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1213 TILs and counting..._ +_1214 TILs and counting..._ --- @@ -652,6 +652,7 @@ _1213 TILs and counting..._ - [Sets With The Values Command](postgres/sets-with-the-values-command.md) - [Shorthand Absolute Value Operator](postgres/shorthand-absolute-value-operator.md) - [Show All Versions Of An Operator](postgres/show-all-versions-of-an-operator.md) +- [Show The Hidden Queries Behind Backslash Commands](postgres/show-the-hidden-queries-behind-backslash-commands.md) - [Sleeping](postgres/sleeping.md) - [Special Math Operators](postgres/special-math-operators.md) - [Storing Emails With citext](postgres/storing-emails-with-citext.md) diff --git a/postgres/show-the-hidden-queries-behind-backslash-commands.md b/postgres/show-the-hidden-queries-behind-backslash-commands.md new file mode 100644 index 0000000..16bf385 --- /dev/null +++ b/postgres/show-the-hidden-queries-behind-backslash-commands.md @@ -0,0 +1,36 @@ +# Show The Hidden Queries Behind Backslash Commands + +The `ECHO_HIDDEN` variable in PostgreSQL's `psql` determines whether the +queries behind backslash commands are displayed. It defaults to `false`. So, +generally, when you run something like `\d` or `\l+`, you'll just see the +result and not the query that helped produce it. + +If you're curious what's behind any of these backslash commands, then set +`ECHO_HIDDEN` to `true` to get a look. + +```sql +> \set ECHO_HIDDEN true +> \d +********* QUERY ********** +SELECT n.nspname as "Schema", + c.relname as "Name", + CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type", + pg_catalog.pg_get_userbyid(c.relowner) as "Owner" +FROM pg_catalog.pg_class c + LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace +WHERE c.relkind IN ('r','p','v','m','S','f','') + AND n.nspname <> 'pg_catalog' + AND n.nspname <> 'information_schema' + AND n.nspname !~ '^pg_toast' + AND pg_catalog.pg_table_is_visible(c.oid) +ORDER BY 1,2; +************************** + + List of relations + Schema | Name | Type | Owner +--------+--------------+----------+------------ + public | users | table | jbranchaud + public | users_id_seq | sequence | jbranchaud +``` + +That query is what `psql` uses to list the relations for your current database.