5 ms·
I always thought that most \-commands in psql were just frontends for queries? For example: % psql db_name (1)=# \set ECHO_HIDDEN (1)=# \d ******
by arp242 3y ago
I always thought that most \-commands in psql were just frontends for queries? For example:
% psql db_name
(1)=# \set ECHO_HIDDEN
(1)=# \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' TH
EN 'sequence' WHEN 't' THEN 'TOAST table' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partit
ioned 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
LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam
WHERE c.relkind IN ('r','p','v','m','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname !~ '^pg_toast'
AND n.nspname <> 'information_schema'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
List of relations
Schema │ Name │ Type │ Owner
────────┼────────────────────────────────────────┼──────────┼────────
...
I re-implemented quite a bit of that in a PostgreSQL management tool I built, based just on the ECHO_HIDDEN queries. Never even had to look at any C code.
I'm not sure if I entirely follow what translating C to JS gives you, other than a bit of (fairly simple) parsing of \cmd and flags? I didn't look too carefully at the code, so maybe I'm missing something.
- meepmorp 3y agoYeah, I don't understand why they'd translate the C code instead of just writing catalog/info schema queries, which seems easier AND more educational.
- gmac 3y agoSome commands issue multiple queries and include logic for stitching the results into a nice table. There’s not a one-to-one mapping from commands to queries by any means.
- arp242 3y agoYeah, that's right; but the "glue code" I've had to write for this has been really minimal. Certainly less effort than translating C to JavaScript with regexps (that on its own is something of an accomplishment). I didn't port everything so maybe I missed some more complex bits. I mean, if this works well for you then that's grand; just wanted to point out to others that \-commands are typically very "non-magic" and you don't need to go the route of translating C code.
- gmac 3y agoCertainly there’s a decently long tail of rarely-used commands, obscure features of commands, and multiple code paths supporting multiple server versions.
- matharmin 3y agoIf you just need one or two of the commands, just using those queries directly is easy. If you want to support all of them, including handling all the different Postgres versions, it becomes quite a lot to implement.
- zX41ZdbW 3y ago[flagged]
- arp242 3y agoAlmost every single post you make mentions clickhouse. You've replied to a few of my posts now with this vaguely on-topic forceful injection of clickhouse. Of course I'm not saying you can never bring up clickhouse, but at this point it's just becoming spam. High-effort spam, maybe. But still spam.
- millerm 3y agoWow, you are right. Nice catch.
- zX41ZdbW 3y agoI'm the author of ClickHouse, and this topic is interesting to me.
- arp242 3y agoI am aware. But this thread is not about Clickhouse. And your comment contributed nothing to the general conversation either. The general pattern is really not appropriate.
- zX41ZdbW 3y agoThank you! Let's take it as a comment about comparing styles in different database CLIs. Some have \-commands, and some have .-commands, which I found hacky - because it is a micro language notation in addition to the actual language.
- arp242 3y agoWhen you keep "comparing" tons of stuff to the project you wrote then you're just spamming. Like I said, this is hardly the first time I've seen it, and that's just from random posts I've seen.