pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14250: Error in subquery fails silently and parent query continues to execute
8+ messages / 4 participants
[nested] [flat]

* BUG #14250: Error in subquery fails silently and parent query continues to execute
@ 2016-07-14 13:52  jason@signalvine.com
  0 siblings, 1 reply; 8+ messages in thread

From: jason@signalvine.com @ 2016-07-14 13:52 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14250
Logged by:          jason turim
Email address:      jason@signalvine.com
PostgreSQL version: 9.5.3
Operating system:   Ubuntu 14.04 / Mac 10.11.5 (15F34)
Description:        

create table parent (id serial primary key, name text);
create table child (id serial primary key, parent_id int, name text);

insert into parent (name) values ('a');
insert into parent (name) values ('b');
insert into parent (name) values ('c');

insert into child (name, parent_id) values ('x', 1);
insert into child (name, parent_id) values ('y', 1);
insert into child (name, parent_id) values ('z', 1);

insert into child (name, parent_id) values ('r', 2);
insert into child (name, parent_id) values ('s', 2);
insert into child (name, parent_id) values ('t', 2);

insert into child (name, parent_id) values ('m', 3);
insert into child (name, parent_id) values ('n', 3);
insert into child (name, parent_id) values ('o', 3);

-- the sub-query contains an error, there is no parent_id in the parent
table
-- we'd expect the following query to fail, however, all the records in the
child table are deleted
delete from child where parent_id in (select parent_id from parent where id
= 1);



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


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

* Re: BUG #14250: Error in subquery fails silently and parent query continues to execute
@ 2016-07-14 14:15  David G. Johnston <david.g.johnston@gmail.com>
  parent: jason@signalvine.com
  0 siblings, 1 reply; 8+ messages in thread

From: David G. Johnston @ 2016-07-14 14:15 UTC (permalink / raw)
  To: jason@signalvine.com; +Cc: pgsql-bugs

On Thu, Jul 14, 2016 at 9:52 AM, <jason@signalvine.com> wrote:

> The following bug has been logged on the website:
>
> Bug reference:      14250
> Logged by:          jason turim
> Email address:      jason@signalvine.com
> PostgreSQL version: 9.5.3
> Operating system:   Ubuntu 14.04 / Mac 10.11.5 (15F34)
> Description:
>
> create table parent (id serial primary key, name text);
> create table child (id serial primary key, parent_id int, name text);
> ​[...]
>
> -- the sub-query contains an error, there is no parent_id in the parent
> table
> -- we'd expect the following query to fail, however, all the records in the
> child table are deleted
> delete from child where parent_id in (select parent_id from parent where id
> = 1);
>

​A common complaint but unfortunately something that simply has to be
learned.

The reference to parent_id in the subquery comes from the child - which in
this case makes the where clause (child.parent_id IN (child.parent_id))
​which will always evaluate to true.

This is termed (though not in our docs) a "correlated subquery" and can be
very useful when used correctly.  Its presence is also why it is
recommended to table-qualify columns when dealing with subqueries.

WHERE child.parent_id IN (SELECT parent.parent_id FROM parent WHERE
parent.id = 1)

The above will provoke the error you wish to see.

​​David J.

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

* Re: BUG #14250: Error in subquery fails silently and parent query continues to execute
@ 2016-07-14 22:40  Jason Turim <jason@signalvine.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Jason Turim @ 2016-07-14 22:40 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-bugs

I see, thanks.  Have you all considered making it an error to execute
correlated queries without table qualifying the column names?

On Jul 14, 2016 11:15 PM, "David G. Johnston" <david.g.johnston@gmail.com>
wrote:

> On Thu, Jul 14, 2016 at 9:52 AM, <jason@signalvine.com> wrote:
>
>> The following bug has been logged on the website:
>>
>> Bug reference:      14250
>> Logged by:          jason turim
>> Email address:      jason@signalvine.com
>> PostgreSQL version: 9.5.3
>> Operating system:   Ubuntu 14.04 / Mac 10.11.5 (15F34)
>> Description:
>>
>> create table parent (id serial primary key, name text);
>> create table child (id serial primary key, parent_id int, name text);
>> ​[...]
>>
>> -- the sub-query contains an error, there is no parent_id in the parent
>> table
>> -- we'd expect the following query to fail, however, all the records in
>> the
>> child table are deleted
>> delete from child where parent_id in (select parent_id from parent where
>> id
>> = 1);
>>
>
> ​A common complaint but unfortunately something that simply has to be
> learned.
>
> The reference to parent_id in the subquery comes from the child - which in
> this case makes the where clause (child.parent_id IN (child.parent_id))
> ​which will always evaluate to true.
>
> This is termed (though not in our docs) a "correlated subquery" and can be
> very useful when used correctly.  Its presence is also why it is
> recommended to table-qualify columns when dealing with subqueries.
>
> WHERE child.parent_id IN (SELECT parent.parent_id FROM parent WHERE
> parent.id = 1)
>
> The above will provoke the error you wish to see.
>
> ​​David J.
>
>

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

* Re: BUG #14250: Error in subquery fails silently and parent query continues to execute
@ 2016-07-14 22:49  David G. Johnston <david.g.johnston@gmail.com>
  parent: Jason Turim <jason@signalvine.com>
  0 siblings, 1 reply; 8+ messages in thread

From: David G. Johnston @ 2016-07-14 22:49 UTC (permalink / raw)
  To: Jason Turim <jason@signalvine.com>; +Cc: pgsql-bugs

On Thu, Jul 14, 2016 at 6:40 PM, Jason Turim <jason@signalvine.com> wrote:

> I see, thanks.  Have you all considered making it an error to execute
> correlated queries without table qualifying the column names?
>
​Will never happen.  I'm not even sure it would be desirable in a
greenfield situation let alone in an established product.

One automated way to handle this would be for a schema-aware static
analyzer to be run on the query in question and point out the discrepancy.
If we ever got something like that into core people could choose this
behavior as part of its configuration.  There is quite a bit of room
outside of core for tools of this nature to be of value.

David J.
​

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

* Re: BUG #14250: Error in subquery fails silently and parent query continues to execute
@ 2016-07-14 22:57  Alvaro Herrera <alvherre@2ndquadrant.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Alvaro Herrera @ 2016-07-14 22:57 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Jason Turim <jason@signalvine.com>; pgsql-bugs

David G. Johnston wrote:
> On Thu, Jul 14, 2016 at 6:40 PM, Jason Turim <jason@signalvine.com> wrote:
> 
> > I see, thanks.  Have you all considered making it an error to execute
> > correlated queries without table qualifying the column names?
> >
> ​Will never happen.  I'm not even sure it would be desirable in a
> greenfield situation let alone in an established product.

The problem is that this is defined by the SQL standard, so we're not at
liberty to change it.  The opinion of several people is that it would be
safer to require the qualification.  If this were a green field I'm sure
we'd do it differently.

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: BUG #14250: Error in subquery fails silently and parent query continues to execute
@ 2016-07-14 23:35  Jason Turim <jason@signalvine.com>
  parent: Alvaro Herrera <alvherre@2ndquadrant.com>
  1 sibling, 0 replies; 8+ messages in thread

From: Jason Turim @ 2016-07-14 23:35 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: pgsql-bugs@postgresql.org, "David G. Johnston" <david.g.johnston@gmail.com>

Thanks again, it's very clear now

On Jul 15, 2016 7:57 AM, "Alvaro Herrera" <alvherre@2ndquadrant.com> wrote:

> David G. Johnston wrote:
> > On Thu, Jul 14, 2016 at 6:40 PM, Jason Turim <jason@signalvine.com>
> wrote:
> >
> > > I see, thanks.  Have you all considered making it an error to execute
> > > correlated queries without table qualifying the column names?
> > >
> > ​Will never happen.  I'm not even sure it would be desirable in a
> > greenfield situation let alone in an established product.
>
> The problem is that this is defined by the SQL standard, so we're not at
> liberty to change it.  The opinion of several people is that it would be
> safer to require the qualification.  If this were a green field I'm sure
> we'd do it differently.
>
> --
> Álvaro Herrera                http://www.2ndQuadrant.com/
> PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
>

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

* Re: BUG #14250: Error in subquery fails silently and parent query continues to execute
@ 2016-07-14 23:42  David G. Johnston <david.g.johnston@gmail.com>
  parent: Alvaro Herrera <alvherre@2ndquadrant.com>
  1 sibling, 1 reply; 8+ messages in thread

From: David G. Johnston @ 2016-07-14 23:42 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Jason Turim <jason@signalvine.com>; pgsql-bugs

On Thu, Jul 14, 2016 at 6:57 PM, Alvaro Herrera <alvherre@2ndquadrant.com>
wrote:

> David G. Johnston wrote:
> > On Thu, Jul 14, 2016 at 6:40 PM, Jason Turim <jason@signalvine.com>
> wrote:
> >
> > > I see, thanks.  Have you all considered making it an error to execute
> > > correlated queries without table qualifying the column names?
> > >
> > ​Will never happen.  I'm not even sure it would be desirable in a
> > greenfield situation let alone in an established product.
>
> The problem is that this is defined by the SQL standard, so we're not at
> liberty to change it.


​​We as a product are at liberty to make the change.​


> The opinion of several people is that it would be
> safer to require the qualification.  If this were a green field I'm sure
> we'd do it differently.
>
> ​
​The fact that it is standard is my point.  A brand new database product
today would likely choose to adhere to the standard and/or prevailing
convention on this topic instead of going it alone and requiring the
qualification.

David J.

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

* Re: BUG #14250: Error in subquery fails silently and parent query continues to execute
@ 2016-07-15 00:55  Alvaro Herrera <alvherre@2ndquadrant.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Alvaro Herrera @ 2016-07-15 00:55 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Jason Turim <jason@signalvine.com>; pgsql-bugs

David G. Johnston wrote:
> On Thu, Jul 14, 2016 at 6:57 PM, Alvaro Herrera <alvherre@2ndquadrant.com>
> wrote:
> 
> > David G. Johnston wrote:
> > > On Thu, Jul 14, 2016 at 6:40 PM, Jason Turim <jason@signalvine.com>
> > wrote:
> > >
> > > > I see, thanks.  Have you all considered making it an error to execute
> > > > correlated queries without table qualifying the column names?
> > > >
> > > ​Will never happen.  I'm not even sure it would be desirable in a
> > > greenfield situation let alone in an established product.
> >
> > The problem is that this is defined by the SQL standard, so we're not at
> > liberty to change it.
> 
> ​​We as a product are at liberty to make the change.​

Sure, if we were open to the option of going against the standard.

> > The opinion of several people is that it would be
> > safer to require the qualification.  If this were a green field I'm sure
> > we'd do it differently.
> >
> ​The fact that it is standard is my point.  A brand new database product
> today would likely choose to adhere to the standard and/or prevailing
> convention on this topic instead of going it alone and requiring the
> qualification.

I meant "if the standard was being written today they would probably
choose to do differently, seeing how the initial choice is so prone to
causing trouble."

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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


end of thread, other threads:[~2016-07-15 00:55 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-07-14 13:52 BUG #14250: Error in subquery fails silently and parent query continues to execute jason@signalvine.com
2016-07-14 14:15 ` David G. Johnston <david.g.johnston@gmail.com>
2016-07-14 22:40   ` Jason Turim <jason@signalvine.com>
2016-07-14 22:49     ` David G. Johnston <david.g.johnston@gmail.com>
2016-07-14 22:57       ` Alvaro Herrera <alvherre@2ndquadrant.com>
2016-07-14 23:35         ` Jason Turim <jason@signalvine.com>
2016-07-14 23:42         ` David G. Johnston <david.g.johnston@gmail.com>
2016-07-15 00:55           ` Alvaro Herrera <alvherre@2ndquadrant.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