Witam na mojej prywatnej stronie internetowej!
[If this is all Polish to you, click here: English]
Uwaga: z oczywistych powodów nie mogę zagwarantować swojej nieomylności, choć staram się o zgodność tego, co piszę, z Prawdą. Jest również oczywiste, że nie gwarantuję takiej zgodności w przypadku komentarzy. Umieszczenie linku do strony spoza niniejszego serwisu nie musi oznaczać, że podzielam poglądy autora tej strony, a jedynie, że uważam ją za wartościową z takich czy innych powodów.
Marcin ‘mbork’ Borkowski
It is well-known that select * is an antipattern (except maybe in an SQL REPL). What to do, however, if you have a select * in your codebase and you want to change it to a proper list of column names? Typing them out manually is fine if there are three of them, but what if there are three dozen? Of course, you may ask an LLM for a nice, comma-separated list of all the columns – but I will generate it three times while you are waiting for the LLM’s answer
! (Or, your company – or you personally – might have an anti-LLM policy because of privacy concerns.) Here’s how (of course, in psql). First you turn off the “aligned mode” (I assume you have it enabled, which is a sane default) with \a. Then you say \pset fieldsep ',' to use comma instead of the pipe as column separator. And then, just say select * from my_table limit 0 to only show the header. Optionally, say \a again and restore the column separator with \pset fieldsep '|' – but it’s faster to do all this in a separate, temporary psql session and just exit it.
That’s it for today, see you next week!