pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Dangerous Naming Confusion
4+ messages / 3 participants
[nested] [flat]

* Dangerous Naming Confusion
@ 2021-03-29 22:00  Don Seiler <don@seiler.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Don Seiler @ 2021-03-29 22:00 UTC (permalink / raw)
  To: pgsql-general

Good evening,

Please see my gist at
https://gist.github.com/dtseiler/9ef0a5e2b1e0efc6a13d5661436d4056 for a
complete test case.

I tested this on PG 12.6 and 13.2 and observed the same on both.

We were expecting the queries that use dts_temp to only return 3 rows.
However the subquery starting at line 36 returns ALL 250,000 rows from
dts_orders. Note that the "order_id" field doesn't exist in the dts_temp
table, so I'm assuming PG is using the "order_id" field from the dts_orders
table. If I use explicit table references like in the query at line 48,
then I get the error I would expect that the "order_id" column doesn't
exist in dts_temp.

When I use the actual column name "a" for dts_temp, then I get the 3 rows
back as expected.

I'm wondering if this is expected behavior that PG uses the
dts_orders.order_id value in the subquery "select order_id from dts_temp"
when dts_temp doesn't have its own order_id column. I would have expected
an error that the column doesn't exist. Seems very counter-intuitive to
think PG would use a column from a different table.

This issue was discovered today when this logic was used in an UPDATE and
ended up locking all rows in a 5M row table and brought many apps to a
grinding halt. Thankfully it was caught and killed before it actually
updated anything.

Thanks,
Don.
-- 
Don Seiler
www.seiler.us

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

* Re: Dangerous Naming Confusion
@ 2021-03-29 22:19  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Don Seiler <don@seiler.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Adrian Klaver @ 2021-03-29 22:19 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 3/29/21 3:00 PM, Don Seiler wrote:
> Good evening,
> 
> Please see my gist at 
> https://gist.github.com/dtseiler/9ef0a5e2b1e0efc6a13d5661436d4056 
> <https://gist.github.com/dtseiler/9ef0a5e2b1e0efc6a13d5661436d4056; for 
> a complete test case.
> 
> I tested this on PG 12.6 and 13.2 and observed the same on both.
> 
> We were expecting the queries that use dts_temp to only return 3 rows. 
> However the subquery starting at line 36 returns ALL 250,000 rows from 
> dts_orders. Note that the "order_id" field doesn't exist in the dts_temp 
> table, so I'm assuming PG is using the "order_id" field from the 
> dts_orders table. If I use explicit table references like in the query 
> at line 48, then I get the error I would expect that the "order_id" 
> column doesn't exist in dts_temp.
> 
> When I use the actual column name "a" for dts_temp, then I get the 3 
> rows back as expected.
> 
> I'm wondering if this is expected behavior that PG uses the 
> dts_orders.order_id value in the subquery "select order_id from 
> dts_temp" when dts_temp doesn't have its own order_id column. I would 
> have expected an error that the column doesn't exist. Seems very 
> counter-intuitive to think PG would use a column from a different table.

See:

https://www.postgresql.org/message-id/Pine.LNX.4.56.0308011345320.881@krusty.credativ.de

> 
> This issue was discovered today when this logic was used in an UPDATE 
> and ended up locking all rows in a 5M row table and brought many apps to 
> a grinding halt. Thankfully it was caught and killed before it actually 
> updated anything.
> 
> Thanks,
> Don.
> -- 
> Don Seiler
> www.seiler.us <http://www.seiler.us;


-- 
Adrian Klaver
adrian.klaver@aklaver.com





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

* Re: Dangerous Naming Confusion
@ 2021-03-29 22:22  David G. Johnston <david.g.johnston@gmail.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 4+ messages in thread

From: David G. Johnston @ 2021-03-29 22:22 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Mon, Mar 29, 2021 at 3:20 PM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 3/29/21 3:00 PM, Don Seiler wrote:
> >
> > I'm wondering if this is expected behavior that PG uses the
> > dts_orders.order_id value in the subquery "select order_id from
> > dts_temp" when dts_temp doesn't have its own order_id column. I would
> > have expected an error that the column doesn't exist. Seems very
> > counter-intuitive to think PG would use a column from a different table.
>
> See:
>
>
> https://www.postgresql.org/message-id/Pine.LNX.4.56.0308011345320.881@krusty.credativ.de
>
>
There is also an FAQ entry:

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

David J.

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

* Re: Dangerous Naming Confusion
@ 2021-03-30 13:48  Don Seiler <don@seiler.us>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Don Seiler @ 2021-03-30 13:48 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Mon, Mar 29, 2021 at 5:22 PM David G. Johnston <
david.g.johnston@gmail.com> wrote:

> On Mon, Mar 29, 2021 at 3:20 PM Adrian Klaver <adrian.klaver@aklaver.com>
> wrote:
>
>> On 3/29/21 3:00 PM, Don Seiler wrote:
>> >
>> > I'm wondering if this is expected behavior that PG uses the
>> > dts_orders.order_id value in the subquery "select order_id from
>> > dts_temp" when dts_temp doesn't have its own order_id column. I would
>> > have expected an error that the column doesn't exist. Seems very
>> > counter-intuitive to think PG would use a column from a different table.
>>
>> See:
>>
>>
>> https://www.postgresql.org/message-id/Pine.LNX.4.56.0308011345320.881@krusty.credativ.de
>>
>>
> There is also an FAQ entry:
>
>
> https://wiki.postgresql.org/wiki/FAQ#Why_doesn.27t_PostgreSQL_report_a_column_not_found_error_when_u...
>
> David J.
>
>
This is good to know. I figured it might "working as expected". Still seems
annoying/counter-intuitive to me but at least we know to look for it.

Thanks,
Don.

-- 
Don Seiler
www.seiler.us

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


end of thread, other threads:[~2021-03-30 13:48 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-29 22:00 Dangerous Naming Confusion Don Seiler <don@seiler.us>
2021-03-29 22:19 ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-03-29 22:22   ` David G. Johnston <david.g.johnston@gmail.com>
2021-03-30 13:48     ` Don Seiler <don@seiler.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