Thread: Partitioned tables and locks

Partitioned tables and locks

From
James Sewell
Date:
Is it expected that a lock on a partitioned table will take out 2 locks per child regardless of the number of children which are excluded at plan time?

For example I can select count(*) from a table with 3500 partitions in transaction, and see 7000 AccessShareLocks show up till I finish the transaction.

Bonus question - sometimes if I query pg_locks fast enough I can see ~30K locks - how could this possibly be the case. This is on an otherwise idle system.

Cheers,
James


The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.

Re: Partitioned tables and locks

From
Tom Lane
Date:
James Sewell <james.sewell@jirotech.com> writes:
> Is it expected that a lock on a partitioned table will take out 2 locks per
> child regardless of the number of children which are excluded at plan time?

Depends on the details of your query, and on which PG version you're
using, but it's by no means surprising for each child table to get
locked.  (I'm not sure where *two* locks would come from, though.)

If you're working with massively partitioned tables, increasing
max_locks_per_transaction is a good idea.

            regards, tom lane



Re: Partitioned tables and locks

From
James Sewell
Date:


> Is it expected that a lock on a partitioned table will take out 2 locks per
> child regardless of the number of children which are excluded at plan time?

Depends on the details of your query, and on which PG version you're
using, but it's by no means surprising for each child table to get
locked.  (I'm not sure where *two* locks would come from, though.)

The queries were just select count(*) both with a where clause which excludes some partitions and without, I’m on 11.

The table has over 3K partitions 


If you're working with massively partitioned tables, increasing
max_locks_per_transaction is a good idea.

It’s 256 at the moment, with 600 connections - guess I need more.
--
James Sewell,
Chief Architect

Suite 112, Jones Bay Wharf, 26-32 Pirrama Road, Pyrmont NSW 2009
P (+61) 2 8099 9000  W www.jirotech.com  F (+61) 2 8099 9099


The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.

Re: Partitioned tables and locks

From
Michael Lewis
Date:
I don't recall the details, but I know v12 included significant enhancements to lock relations later in the process such that when targeting relatively few of the partitions, it can be a major performance boost.