pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
update returning order by syntax error question
5+ messages / 4 participants
[nested] [flat]

* update returning order by syntax error question
@ 2019-09-12 03:17  raf <raf@raf.org>
  0 siblings, 2 replies; 5+ messages in thread

From: raf @ 2019-09-12 03:17 UTC (permalink / raw)
  To: pgsql-general

Hi,

postgresql-9.6.15

I just tried something like:

  select * from
  (update tblname t set ... where ... returning ...)
  order by ...;

assuming it would work but it didn't.
That's OK. I found on stackoverflow
that a CTE can be used to do it:

  with u as
  (update tblname t set ... where ... returning ...)
  select * from u order by ...;

What surprises me is the syntax error:

  ERROR:  syntax error at or near "t"
  LINE 2:  tblname t
                   ^
If the syntax was invalid because an update returning
statement can't appear in a from clause, I'd expect the
error to be at the token "update".

It's almost as if the parser sees "update" as a possible
table name (rather than a reserved word) and "tblname"
as the alias for that table and it's expecting a comma
or left/right/full etc. when it seess the "t".

Anyone care to explain why the error is what it is?
It's no big deal. I'm just curious.

cheers,
raf






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: update returning order by syntax error question
@ 2019-09-12 05:33  Luca Ferrari <fluca1978@gmail.com>
  parent: raf <raf@raf.org>
  1 sibling, 0 replies; 5+ messages in thread

From: Luca Ferrari @ 2019-09-12 05:33 UTC (permalink / raw)
  To: raf <raf@raf.org>; +Cc: pgsql-general

On Thu, Sep 12, 2019 at 5:45 AM raf <raf@raf.org> wrote:
>   ERROR:  syntax error at or near "t"
>   LINE 2:  tblname t

This works on 9.6.9 and 11.4:

luca=> with u as ( update t_all set id = 5 where id <= 5 returning *)
select * from u;
 id | ref_id
----+--------
  5 |      1
(1 row)

luca=> select version();
                                             version
--------------------------------------------------------------------------------------------------
 PostgreSQL 9.6.9 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu
7.2.0-8ubuntu3.2) 7.2.0, 64-bit
(1 row)


However, I know for sure that UPDATE has some restrictions on the
table aliasing (at least, they are not used as for a SELECT), so the
problem could be in the real query you are executing.
It works with or without the order by.

Luca





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: update returning order by syntax error question
@ 2019-09-12 13:44  Tom Lane <tgl@sss.pgh.pa.us>
  parent: raf <raf@raf.org>
  1 sibling, 1 reply; 5+ messages in thread

From: Tom Lane @ 2019-09-12 13:44 UTC (permalink / raw)
  To: raf <raf@raf.org>; +Cc: pgsql-general

raf <raf@raf.org> writes:
> It's almost as if the parser sees "update" as a possible
> table name (rather than a reserved word) and "tblname"
> as the alias for that table and it's expecting a comma
> or left/right/full etc. when it seess the "t".

No, it's *exactly* as if that.  UPDATE is an unreserved
keyword so it's fully legitimate as a table name.
If you made the syntax be what the grammar is expecting:

regression=# select * from (update t cross join update t2);   
ERROR:  relation "update" does not exist
LINE 1: select * from (update t cross join update t2);
                       ^

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: update returning order by syntax error question
@ 2019-09-12 14:31  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Adrian Klaver @ 2019-09-12 14:31 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; raf <raf@raf.org>; +Cc: pgsql-general

On 9/12/19 6:44 AM, Tom Lane wrote:
> raf <raf@raf.org> writes:
>> It's almost as if the parser sees "update" as a possible
>> table name (rather than a reserved word) and "tblname"
>> as the alias for that table and it's expecting a comma
>> or left/right/full etc. when it seess the "t".
> 
> No, it's *exactly* as if that.  UPDATE is an unreserved
> keyword so it's fully legitimate as a table name.
> If you made the syntax be what the grammar is expecting:
> 
> regression=# select * from (update t cross join update t2);
> ERROR:  relation "update" does not exist
> LINE 1: select * from (update t cross join update t2);


I am not following.
PostgreSQL 11.5

The OP had:

with u as
   (update tblname t set ... where ... returning ...)
   select * from u order by ...;


I tested with:

WITH u AS (
     UPDATE up_test t SET col_2 = col_2 + 1 WHERE id < 3
     RETURNING *
)
SELECT * FROM u;
BEGIN
  id | col1 | col_2
----+------+-------
   1 | t    |     2
   2 | NULL |     3
(2 rows)

I'm missing something.

>                         ^
> 
> 			regards, tom lane
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: update returning order by syntax error question
@ 2019-09-12 14:38  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2019-09-12 14:38 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: raf <raf@raf.org>; pgsql-general

Adrian Klaver <adrian.klaver@aklaver.com> writes:
> On 9/12/19 6:44 AM, Tom Lane wrote:
>> No, it's *exactly* as if that.  UPDATE is an unreserved
>> keyword so it's fully legitimate as a table name.

> I am not following.

Sure, the WITH thing works too.  The point is that given
"SELECT ... FROM (UPDATE ...)", there is a workable parse
path where UPDATE is treated as a table name.  So if you
try to put an UPDATE command there, the syntax error
isn't thrown till a couple tokens later, where the
table-name syntax no longer matches.  The OP's question
was about why the error was thrown where it was, not about
how to do this correctly.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2019-09-12 14:38 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-09-12 03:17 update returning order by syntax error question raf <raf@raf.org>
2019-09-12 05:33 ` Luca Ferrari <fluca1978@gmail.com>
2019-09-12 13:44 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-09-12 14:31   ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-09-12 14:38     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox