| From: | Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: [WIP] Adding a { LOCK ROWS | LOCK KEY INDEX } clause to FK definitions |
| Date: | 2026-12-03 15:23:47 |
| Message-ID: | CAEze2Wiq3JtaN2n7HiS3hSY=eMAg=QiHH4vh9KdTJNLgP4jyLQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Mon, 22 Jun 2026 at 14:50, Matthias van de Meent
<boekewurm+postgres(at)gmail(dot)com> wrote:
>
> Work in progress:
>
> The atttached patchset has one known issue that I'm not 100% sure
> about if it's really an issue:
This was an actual issue, and is resolved in the attached patch v2.
> Another work in progress is the exact syntax: I personally think that
> the proposed { LOCK ROWS | LOCK KEY INDEX } naming is a good
> descriptive syntax, but I'm open to improvements.
The patchset has evolved to lock the FK constraint object itself,
instead of the backing index, to avoid issues with concurrent REINDEX
CONCURRENTLY switching the backing index of the constraint without
transfering the locks we rely on here. I decided against locking the
referenced relation, so that we don't have to block concurrent
autovacuum, nor block other table operations that are considered safe
to run with the LOCK ROW system's locks.
-=-=-=-=-=-=-=-=-
Here's v2, which has the following changes:
0001: New, adds fast-path locking for most internal locktags.
Relations still use the usual paths, but other objects with
MyDatabaseOid in their field1 can now also get a fast-path lock as
long as they're using the DEFAULT_LOCKMETHOD, and their lockmode is
low enough.
The new data we need to cover the whole locktag (16B-8B = 8 bytes) is
stored out-of-line; and a free bit in fpLockBits is used to indicate
to the fast-path system that this is not a relation fast-path lock,
and thus to look at the extra data.
As part of this patch, the order of LOCKTAG_OBJECT tags is changed;
the object ID is now stored in field2, and the class in field3. This
increases the entropy in field2 for OBJECT's tags, reducing the number
of required accesses to the extra data.
In 0002 (previously v1-0001):
- the locked object is changed from the backing index to the root FK constraint.
- new row "lock" syntax is added (FOR NO LOCK) to allow FK checks to
run MVCC-crossing checks without actually locking the row. This
hasn't been documented nor tested yet in user-land, the behaviour is
implemented on an as-needed basis by FK validations.
- updates/deletes on the referenced (fact) table now take the lock
during a BEFORE STATEMENT trigger. Triggering the lock AFTER ROW
would risk a significant increase in deadlocks from lock upgrades and
validation waits.
The majority of the changes in 0002 are tests, to make sure the bases
are covered.
Open questions:
1. The syntax of LOCK KEY INDEX is inaccurate, now that it locks the
constraint rather than its backing index. Suggestions for a better
name are welcome.
2. Docs for (and/or splitting out) the exposed FOR NO LOCK syntax.
Would it want/need that?
Kind regards,
Matthias van de Meent
Databricks (https://www.databricks.com)
| Attachment | Content-Type | Size |
|---|---|---|
| v2-0002-FK-RI-Add-heavy-weight-locking-option-for-RI-chec.patch | application/octet-stream | 193.0 KB |
| v2-0001-Allow-non-relation-fast-path-locking.patch | application/octet-stream | 34.8 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Nitin Motiani | 2026-12-03 15:05:51 | Re: Speed up tuplestore_seek() by starting from the nearest known position |