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

1
  • 1
    Suggest you email the developers to confirm that this is intentional (feature) or an oversight (bug). My personal opinion is it's the latter. Principle of least surprise says that all content and formatting should be preserved. Commented Mar 9, 2017 at 13:10

2 Answers 2

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.