Re: delete statement returning too many results

From: Harmen <harmen(at)lijzij(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Ron <ronljohnsonjr(at)gmail(dot)com>, Arlo Louis O'Keeffe <gnomelver(at)k5d(dot)de>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: delete statement returning too many results
Date: 2022-11-29 15:46:53
Message-ID: 20221129154653.GC15079@arp.lijzij.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Nov 28, 2022 at 12:11:53PM -0500, Tom Lane wrote:

> So basically it's unsafe to run the sub-select more than once,
> but the query as written leaves it up to the planner whether
> to do that. I'd suggest rephrasing as
>
> WITH target_rows AS MATERIALIZED (
> SELECT id
> FROM queue
> ORDER BY id
> LIMIT 1
> FOR UPDATE
> SKIP LOCKED
> )
> DELETE FROM queue
> WHERE id IN (SELECT * FROM target_rows)
> RETURNING *;

Thanks for the explanation and suggested fix, Tom.

I'm not the original poster, but I do use similar constructions for simple
postgres queues. I've been trying for a while, but I don't understand where the
extra rows come from, or what's "silent" about SKIP LOCKED.

Because we get different results depending on the plan postgres picks, I can
see two options: either the query is broken, or postgres is broken. Assuming it's
the former, would there be a way to make it clearer that the "obvious" (to me)
way to use SKIP LOCKED is wrong?

Thanks!
Harmen

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2022-11-29 16:28:16 Re: plpgsql_check_function issue after upgrade
Previous Message shashidhar Reddy 2022-11-29 15:37:11 Re: plpgsql_check_function issue after upgrade