On Sat, 29 Mar 2025 at 06:00, Evgeny Morozov
<postgresql4@realityexists.net> wrote:
>
> On 23/03/2025 2:35 pm, David Rowley wrote:
> >> alter table entity_2 add column new_column text;
> > Is this just an example command? You can't add a column to a
> > partition directly.
>
> Yes, it was just the simplest way I could think of to take an exclusive
> lock. But on this note: I guess it takes a lock before checking that the
> table is partition because it might otherwise get attached/detached as a
> partition between doing the check and taking a lock, but it would be
> nice if it took a shared lock to do all checks, then upgraded to an
> exclusive lock to actually do the work. Then the user would not have to
> wait for an exclusive lock only for the command to fail.
Unfortunately, that's probably just swapping one problem for another.
Once you have 2 sessions following such a pattern of locking, you're
prone to unnecessary deadlocking.
For example:
-- session1
begin;
lock table t in access share mode; -- gets lock
-- session2
begin;
lock table t in access share mode; -- gets lock
lock table t in access exclusive mode; -- waits
-- session1
lock table t in access exclusive mode; -- deadlock
If you don't bother with the access share lock, there's no deadlock.
David