6

Is there anyway at all, ever to change the behavior of comments in psql. Take the query below. Execute it.

CREATE TABLE foo
AS
  SELECT x AS id,
    -- x AS id2,
    x AS id3
  FROM generate_series(1,50) AS x;

Run that in psql. Then run \e. Now at least, for me what I see in my editor is the line absent. This is driving me crazy. Is there a way around this.. The comment is just absent from the buffer that gets passed to the editor. Often, it's commented and not deleted because I want to uncomment it at a later point.

Comment gone

Evan Carroll
  • 65,432
  • 50
  • 254
  • 507

2 Answers2

7

Workaround with C-style comments

For whatever reason.. this seems to work when you use C-style comments instead of standard SQL comments,

CREATE TABLE foo
AS
  SELECT x AS id,
    /* x AS id2, */
    x AS id3
  FROM generate_series(1,50) AS x;

Now you can do \e and edit it.

Evan Carroll
  • 65,432
  • 50
  • 254
  • 507
5

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.

Daniel Vérité
  • 32,662
  • 3
  • 78
  • 84