5

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
    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. – Colin 't Hart Mar 9 '17 at 13:10
6

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.

| improve this answer | |
2

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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