From: Tom Lane <tgl@sss.pgh.pa.us>
To: Steven Winfield <Steven.Winfield@cantabcapital.com>
Cc: Thomas Munro <thomas.munro@gmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Row locks, SKIP LOCKED, and transactions
Date: Wed, 18 Dec 2019 09:29:04 -0500
Message-ID: <30295.1576679344@sss.pgh.pa.us> (raw)
In-Reply-To: <E9FA92C2921F31408041863B74EE4C2001E7665213@CCPMAILDAG03.cantab.local>
References: <E9FA92C2921F31408041863B74EE4C2001E7662416@CCPMAILDAG03.cantab.local>
<CA+hUKGKY430dgePM6HAHNmJXe7xfmFiT10zhRFwTH6q0qa_PVQ@mail.gmail.com>
<E9FA92C2921F31408041863B74EE4C2001E7665213@CCPMAILDAG03.cantab.local>
Steven Winfield <Steven.Winfield@cantabcapital.com> writes:
>> There are various ways you could deal with this, but I'd probably go for a
>> simple scheme where you only have to consult a single row to know if you
>> can claim it. You could still put the results into a separate table, but
>> use job.state to find work, and set it to DONE when you insert the result.
>> It may also be possible to add no new columns but do a dummy update to the
>> job row to get the join qual rechecked, but I'm not sure if that'd work.
>> Another reason to add a state column to the job table is so that you can
>> put a conditional index on it so you can find jobs to be done very
>> quickly, if you're not planning to remove the ones that are done.
> Thanks. I rejected the idea of doing a dummy update to the locked row as I wanted to avoid too much extra WAL - the real table originally had quite a few more columns than the toy example, but it's much slimmer now so this could be a viable option.
Yeah ... the fundamental reason why this isn't working for you is that
the FOR UPDATE will only lock/check conflicts in the "job" table.
You could add a FOR UPDATE in the sub-select to lock the "result" table,
but that will still only lock rows it read, not rows it didn't read
because they weren't there yet :-(. Updating the state of the job row
to show that it's claimed is much the most reliable way to fix this.
(Or you could use serializable mode, but that feels like using a hammer
to swat a fly.)
regards, tom lane
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-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, Steven.Winfield@cantabcapital.com, thomas.munro@gmail.com
Subject: Re: Row locks, SKIP LOCKED, and transactions
In-Reply-To: <30295.1576679344@sss.pgh.pa.us>
References: <E9FA92C2921F31408041863B74EE4C2001E7662416@CCPMAILDAG03.cantab.local> <CA+hUKGKY430dgePM6HAHNmJXe7xfmFiT10zhRFwTH6q0qa_PVQ@mail.gmail.com> <E9FA92C2921F31408041863B74EE4C2001E7665213@CCPMAILDAG03.cantab.local> <30295.1576679344@sss.pgh.pa.us>
* 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