psql: include intra-query "--" comments in what's sent to the server.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 1 Dec 2021 17:06:31 +0000 (12:06 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 1 Dec 2021 17:06:31 +0000 (12:06 -0500)
psql's lexer has historically deleted dash-dash (single-line) comments
from what's collected and sent to the server.  This is inconsistent
with what it does for slash-star comments, and people have complained
before that they wish such comments would be captured in the server log.
Undoing the decision completely seems like too big a behavioral change,
however.  In particular, comments on lines preceding the start of a
query are generally not thought of as being part of that query.

What we can do to improve the situation is to capture comments that
are clearly *within* a query, that is after the first non-whitespace,
non-comment token but before the query's ending semicolon or backslash
command.  This is a nearly trivial code change, and it affects only a
few regression test results.

(It is tempting to try to apply the same rule to slash-star comments.
But it's hard to see how to do that without getting strange history
behavior for comments that cross lines, especially if the user then
starts a new query on the same line as the star-slash.  In view of
the lack of complaints, let's leave that case alone.)

Discussion: https://postgr.es/m/CAJcOf-cAdMVr7azeYR7nWKsNp7qhORzc84rV6d7m7knG5Hrtsw@mail.gmail.com

contrib/pg_stat_statements/expected/pg_stat_statements.out
src/fe_utils/psqlscan.l
src/test/regress/expected/aggregates.out
src/test/regress/expected/generated.out
src/test/regress/expected/with.out

index b52d1877223659ea01210391fa4ebd76795d2ed6..e0abe34bb6aa04a5462c06c0de24c24cf02fd505 100644 (file)
@@ -101,7 +101,7 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
 ------------------------------------------------------------------------------+-------+------
  PREPARE pgss_test (int) AS SELECT $1, $2 LIMIT $3                            |     1 |    1
  SELECT $1                                                                   +|     4 |    4
-                                                                             +|       | 
+   -- multiline                                                              +|       | 
    AS "text"                                                                  |       | 
  SELECT $1 + $2                                                               |     2 |    2
  SELECT $1 + $2 + $3 AS "add"                                                 |     3 |    3
index b0fd4394ec6bb0cdbf46dc5f4523279c9c97aa12..db8a8dfaf258387fdfe0214e71a4c1dc732bb875 100644 (file)
@@ -378,12 +378,11 @@ other         .
                    /*
                     * Note that the whitespace rule includes both true
                     * whitespace and single-line ("--" style) comments.
-                    * 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.
+                    * We suppress whitespace until we have collected some
+                    * non-whitespace data.  (This interacts with some
+                    * decisions in MainLoop(); see there for details.)
                     */
-                   if (!(output_buf->len == 0 || yytext[0] == '-'))
+                   if (output_buf->len > 0)
                        ECHO;
                }
 
index 5949996ebc02a6116436b5f3190aa10271d84060..be5fa5727d5325205e4a840eab5258e9778a0a5c 100644 (file)
@@ -1905,14 +1905,14 @@ from generate_series(1,5) x,
      (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
 group by p order by p;
 ERROR:  sum is not an ordered-set aggregate, so it cannot have WITHIN GROUP
-LINE 1: select p, sum() within group (order by x::float8)  
+LINE 1: select p, sum() within group (order by x::float8)  -- error
                   ^
 select p, percentile_cont(p,p)  -- error
 from generate_series(1,5) x,
      (values (0::float8),(0.1),(0.25),(0.4),(0.5),(0.6),(0.75),(0.9),(1)) v(p)
 group by p order by p;
 ERROR:  WITHIN GROUP is required for ordered-set aggregate percentile_cont
-LINE 1: select p, percentile_cont(p,p)  
+LINE 1: select p, percentile_cont(p,p)  -- error
                   ^
 select percentile_cont(0.5) within group (order by b) from aggtest;
  percentile_cont  
index c2e5676196b1ab6bdf7e5a5fc1facce42f399fc7..cb9373227d454d261e7c3fdfdb355606bb8bc09e 100644 (file)
@@ -928,7 +928,7 @@ CREATE TRIGGER gtest2a BEFORE INSERT OR UPDATE ON gtest26
   WHEN (NEW.b < 0)  -- error
   EXECUTE PROCEDURE gtest_trigger_func();
 ERROR:  BEFORE trigger's WHEN condition cannot reference NEW generated columns
-LINE 3:   WHEN (NEW.b < 0)  
+LINE 3:   WHEN (NEW.b < 0)  -- error
                 ^
 DETAIL:  Column "b" is a generated column.
 CREATE TRIGGER gtest2b BEFORE INSERT OR UPDATE ON gtest26
@@ -936,7 +936,7 @@ CREATE TRIGGER gtest2b BEFORE INSERT OR UPDATE ON gtest26
   WHEN (NEW.* IS NOT NULL)  -- error
   EXECUTE PROCEDURE gtest_trigger_func();
 ERROR:  BEFORE trigger's WHEN condition cannot reference NEW generated columns
-LINE 3:   WHEN (NEW.* IS NOT NULL)  
+LINE 3:   WHEN (NEW.* IS NOT NULL)  -- error
                 ^
 DETAIL:  A whole-row reference is used and the table contains generated columns.
 CREATE TRIGGER gtest2 BEFORE INSERT ON gtest26
index a3a2e383e3c98f24941e6f73fa6abed5665bf29d..75e61460d994790da05685d49b496a2d78b65419 100644 (file)
@@ -2100,7 +2100,7 @@ WITH outermost(x) AS (
 )
 SELECT * FROM outermost ORDER BY 1;
 ERROR:  relation "outermost" does not exist
-LINE 4:          SELECT * FROM outermost  
+LINE 4:          SELECT * FROM outermost  -- fail
                                ^
 DETAIL:  There is a WITH item named "outermost", but it cannot be referenced from this part of the query.
 HINT:  Use WITH RECURSIVE, or re-order the WITH items to remove forward references.
@@ -2124,7 +2124,7 @@ WITH RECURSIVE outermost(x) AS (
 )
 SELECT * FROM outermost ORDER BY 1;
 ERROR:  recursive reference to query "outermost" must not appear within a subquery
-LINE 2:   WITH innermost as (SELECT 2 FROM outermost) 
+LINE 2:   WITH innermost as (SELECT 2 FROM outermost) -- fail
                                            ^
 --
 -- This test will fail with the old implementation of PARAM_EXEC parameter