Is there a way to log queries which use sequential scans in PostgreSQL? Or just log everything and then later grep for seqscans?
I don't know a way to log sequential scans only. Just a small remark: seqscans are not evil, you will get them (especially for smaller tables) all the time.
On the other hand, you can definitely log everything, if you want, and there are multiple ways.
The easiest is probably
[ALTER DATABASE yourdb ] SET log_statement TO 'all';
This will log every query that passes the parser:
Note: Statements that contain simple syntax errors are not logged even by the
log_statement = allsetting, because the log message is emitted only after basic parsing has been done to determine the statement type. In the case of extended query protocol, this setting likewise does not log statements that fail before the Execute phase (i.e., during parse analysis or planning). Set
ERROR(or lower) to log such statements.
Another insteresting way of achieving it is setting
log_min_duration_statement to a (very) low value:
[ALTER DATABASE yourdb ] SET log_min_duration_statement TO 0; -- or somewhat higher
The difference between the two is that the latter will also register the actual parameters passed to functions.
- you need superuser rights for changing either of these settings
- you can use both at the same time. For details, please read the documentation linked above.
The answer appears to be to log everything (or at least long-running queries) with the auto_explain module (https://www.postgresql.org/docs/current/static/auto-explain.html) turned on, then parse the results.