Re: subquery column error causes data destroying equality

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Cy <frompostgres(at)cy1(dot)allowed(dot)org>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: subquery column error causes data destroying equality
Date: 2020-07-25 14:35:30
Message-ID: 672650.1595687730@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Cy <frompostgres(at)cy1(dot)allowed(dot)org> writes:
> When updating my table, my query was this:

> update comic.panels set medium = x'2a958'::int where page = (
> select page from comic.pages where comic = x'1db'::int and which = 0);

> Unfortunately, I forgot that the column in the "pages" table was named "id" not "page".
> This would have been the correct query:

> update comic.panels set medium = x'2a958'::int where page = (
> select id from comic.pages where comic = x'1db'::int and which = 0);

Yup. This is an absolutely standard gotcha. Unfortunately, that's
"standard" as in "SQL standard": the spec defines that reference to
"page" as being a valid outer reference.

> So... please have errors in subqueries cause the surrounding query to fail.

This is not an error. Removing the ability to have outer references
in subqueries would be contrary to spec and would greatly weaken this
language feature.

The usual recommendation to avoid such mistakes is to table-qualify
every column reference in a subquery.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2020-07-25 14:42:59 Re: BUG #16554: Consistent sequence gaps occuring next day
Previous Message David G. Johnston 2020-07-25 14:08:17 Re: BUG #16554: Consistent sequence gaps occuring next day