pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Odd Update Behaviour
2+ messages / 2 participants
[nested] [flat]

* Odd Update Behaviour
@ 2019-06-20 21:13 Harry Ambrose <harry.ambrose@gmail.com>
  2019-06-20 21:16 ` Re: Odd Update Behaviour David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Harry Ambrose @ 2019-06-20 21:13 UTC (permalink / raw)
  To: pgsql-general

Hi,

I noticed some weird update behaviour today in one of our development
environments. In my opinion this appears to be a bug, but before reporting
it I thought I should seek the opinions of others in the community. Maybe
this is known and/or seen before?

The behaviour is visible when a subquery is used in an update. More
specifically when a subquery is rejected by the parser when executed on
it's own. This causes the where condition to be ignored entirely and thus
the whole table updated rather than the parser throwing an error.

Example below that can be use to replicate:
update_bug=# create table dummy_data (rowid bigserial primary key, type
int);
CREATE TABLE

update_bug=# insert into dummy_data (type) select 1 from
generate_series(1,10000);
INSERT 0 10000

update_bug=# insert into dummy_data (type) select 2 from
generate_series(1,10000);
INSERT 0 10000

update_bug=# select type, count(*) from dummy_data group by 1 order by 1;
 type | count
------+-------
    1 | 10000
    2 | 10000
(2 rows)

update_bug=# select rowid from (select rowid as idnumber from dummy_data
where type = 1) q;
ERROR:  column "rowid" does not exist
LINE 1: select rowid from (select rowid as idnumber from dummy_data ...

update_bug=# update dummy_data set type = 3 where rowid in (select rowid
from (select rowid as idnumber from dummy_data where type = 1) q);
UPDATE 20000

update_bug=# select type, count(*) from dummy_data group by 1 order by 1;
 type | count
------+-------
    3 | 20000
(1 row)

I have managed to replicate this behaviour on both 9.5.16 & 10.6 and will
continue to test further.

Any thoughts/opinions are obviously welcomed.

Best wishes,
Harry

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

* Re: Odd Update Behaviour
  2019-06-20 21:13 Odd Update Behaviour Harry Ambrose <harry.ambrose@gmail.com>
@ 2019-06-20 21:16 ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: David G. Johnston @ 2019-06-20 21:16 UTC (permalink / raw)
  To: Harry Ambrose <harry.ambrose@gmail.com>; +Cc: pgsql-general

On Thu, Jun 20, 2019 at 2:13 PM Harry Ambrose <harry.ambrose@gmail.com>
wrote:

> Hi,
>
> I noticed some weird update behaviour today in one of our development
> environments. In my opinion this appears to be a bug, but before reporting
> it I thought I should seek the opinions of others in the community. Maybe
> this is known and/or seen before?
>

Yep, seen before, even has an FAQ entry:

https://wiki.postgresql.org/wiki/FAQ#Why_doesn.27t_PostgreSQL_report_a_column_not_found_error_when_u...


> The behaviour is visible when a subquery is used in an update. More
> specifically when a subquery is rejected by the parser when executed on
> it's own. This causes the where condition to be ignored entirely and thus
> the whole table updated rather than the parser throwing an error.
>

David J.

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


end of thread, other threads:[~2019-06-20 21:16 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-06-20 21:13 Odd Update Behaviour Harry Ambrose <harry.ambrose@gmail.com>
2019-06-20 21:16 ` David G. Johnston <david.g.johnston@gmail.com>

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