pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: tom.hantel@googlemail.com
Subject: BUG #15826: BUG: Where-Clause referring to unknown column in CTE is ignored in Update-statement
Date: Fri, 31 May 2019 10:55:01 +0000
Message-ID: <15826-7c98038f9e60224d@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      15826
Logged by:          Thomas Hantel
Email address:      tom.hantel@googlemail.com
PostgreSQL version: 11.3
Operating system:   Fedora Linux
Description:        

We are currently seeing the following unexpected effect:

In one of our services there is a statement that first sets up a CTE to
filter the data and then an UPDATE-statement that refers to that data. If
the WHERE-clause in the update refers to a column that is not selected in
the CTE, the whole clause is ignored so that all of the rows in the table
get updated.

The effect can be reproduced like this:

DROP TABLE IF EXISTS dummy;
CREATE TABLE IF NOT EXISTS dummy (id int PRIMARY KEY, value text);

INSERT INTO dummy VALUES
(1, 'text 1')
,(2, 'text 2')
,(3, 'text 3')
ON CONFLICT (id) DO UPDATE
SET value = EXCLUDEd.value;

WITH cte AS
(
SELECT value FROM dummy WHERE id = 1
)
UPDATE dummy
SET value = 'text 1 text 1'
WHERE id = (SELECT id FROM cte)
;

SELECT * FROM dummy;

`

In this example we use a temp table with two columns, "id" and "value". The
CTE selects just the column "value" from this table and filters the data by
using "WHERE id = 1". This is where in the real-world case our security
mechanism would make sure that only rows that may be updated are passed
on.

Now the update statement itself comes into play and is supposed to set the
value of the field "value" to "text 1 text 1" in just the one record we
expect to be contained in the CTE (the one with ID 1).

As it turns out, because the CTE just selects the field "value" and our
comparison operates on a column that is not selected there, we don't get an
error pertaining to the missing column ("unknown identifier" or something
along those lines) but instead the WHERE clause is ignored altogether and
all of the records get updated.

We would expect an error to be thrown in all cases where undefined columns
are referenced.



view thread (3+ messages)  latest in thread

Message-ID: <15826-7c98038f9e60224d@postgresql.org>
Permalink:  ../15826-7c98038f9e60224d@postgresql.org/
Also on:    postgresql.org/message-id/15826-7c98038f9e60224d@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, tom.hantel@googlemail.com
  Subject: Re: BUG #15826: BUG: Where-Clause referring to unknown column in CTE is ignored in Update-statement
  In-Reply-To: <15826-7c98038f9e60224d@postgresql.org>
  References: <15826-7c98038f9e60224d@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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