Inspired by the old meme about the top shell commands, I thought I'd see what the most
common SQL commands I run are. When you are using psql,
(the command-line interface to Postgres),
it keeps a history of all the commands you enter inside the file ~/.psql_history.
It only keeps 500 lines by default, which I find way too low, so I adjust it by setting
the HISTSIZE variable. Here's the usual contents of my .psqlrc file:
\pset null '\\N'
\set HISTSIZE 5000
\timing
\set G '\\x\\g\\x'
The first line gives a better visual output to nulls.
The second tells psql to allow the .psql_history file to grow to 5000 lines, while the third line times all the commands you enter. The final
line allows you to run a single command in expanded mode like so:
greg=# SELECT * FROM pg_class :G
Interestingly, it looks like a variant of the above may make its way into
the next version of psql...
On to the counting meme, however. Here's the command I used:
awk '{print $1}' ~/.psql_history | sort | uniq -c |sort -rn | head -10
The output on one of the production boxes I watch over gives:
1356 select
386 \d
347 \q
186 vacuum
141 \c
126 update
84 \x
59 SELECT
46 begin;
41 \dt
Note that older versions of psql did not wrap multi-line commands into a single line inside the file, so you may see odd results when using < 8.2