With PostgreSQL 15 or newer
Starting with psql
version 15, the comments introduced with --
are no longer suppressed, except for those at the start of the query.
According the release notes
Have psql send intra-query double-hyphen comments to the server (Tom
Lane, Greg Nancarrow).
Previously such comments were removed from the
query before being sent. Double-hyphen comments that are before query
text are not sent, and are not recorded as separate psql history
entries.
Before PostgreSQL 15
The psql
lexical analyzer removes the dash-dash-style comments from the user input as if they were non-significant whitespaces. Even without using \e
, the \p
command displaying the query buffer shows that they don't even make it into that buffer.
Is it a bug? The source code (.../psql/psqlscan.l
) contains that comment on processing the whitespace
token:
* We suppress whitespace at the start of the query
* buffer. We also suppress all single-line comments,
* which is pretty dubious but is the historical
* behavior.
It shows that the developers are aware of the behavior, even if the reason why it's like that originally is not specified, and its effect on \e
were probably not intended.
C-style comments don't have this problem. They're multi-line and nestable, so they're handled quite differently by the lexer. Comments inside functions are also no affected, since the function body as a whole is an opaque string for psql.