Re: Transaction locks on first insert into partitioned table partition

From: David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>
To: Martin Lund Askøe <martinlundaskoe(at)gmail(dot)com>
Cc: pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Re: Transaction locks on first insert into partitioned table partition
Date: 2019-06-07 01:11:07
Message-ID: CAKJS1f9GS1HPp97E61FaQjqJ8kL8x8yuo_9qRM9sUoCukOuxEA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Fri, 7 Jun 2019 at 03:31, Martin Lund Askøe
<martinlundaskoe(at)gmail(dot)com> wrote:
>
> I'm experiencing something I cannot explain with regards to partitioned tables.
> I create a partitioned table and create a partition on it in one transaction.
> I then insert a record into the partition, somehow taking a lock on the master table.
> Then I rollback. I then insert a record again, but this time I do not take a lock on the master table.
>
> Why is this happening?

Attaching the partition to the partitioned table causes a relcache
invalidation (basically an internal cache that each session maintains
to quickly access relation metadata). When you open a relation for the
first time after its cache entry was invalidated, the data must be
reloaded. This requires locking the relation to ensure nobody drops it
out from under us. For this particular case, we must look up the
parent partitioned table's details in order to determine if the tuple
you're inserting is suitable for that partition. The code in question
is in generate_partition_qual(). Since the partition bound is then
stored in the partition's relcache entry, and not the partitioned
table then we've no need to open the parent again on subsequent
inserts that go directly to the partition. If you attached another
partition or did something else like add a column, then this would
invalidate the entry again and you'd see the lock for the duration of
the transaction.

--
David Rowley http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Martin Lund Askøe 2019-06-07 07:05:25 Re: Transaction locks on first insert into partitioned table partition
Previous Message Martin Lund Askøe 2019-06-06 15:30:31 Transaction locks on first insert into partitioned table partition