Re: Table-Level Locks and Transactions - Mailing list pgsql-novice

From Laurenz Albe
Subject Re: Table-Level Locks and Transactions
Date
Msg-id 8b788512e88c21ce5e913963827de3e3710d245e.camel@cybertec.at
Whole thread Raw
In response to Re: Table-Level Locks and Transactions  (Simon Riggs <simon.riggs@enterprisedb.com>)
Responses Re: Table-Level Locks and Transactions
List pgsql-novice
On Thu, 2021-12-09 at 10:33 +0000, Simon Riggs wrote:
> On Thu, 9 Dec 2021 at 01:07, Greg Rychlewski <greg.rychlewski@gmail.com> wrote:
> > 
> > I'm curious to understand when table-level locks are released by one transaction to use in another.
> 
> At the end of transaction.
> 
> > For instance, say I have the following transaction, where index1 and index2 both belong to the same table.
> > 
> > BEGIN;
> > DROP INDEX index1;
> > CREATE INDEX index2;
> > COMMIT;
> > 
> > This transaction acquires an ACCESS EXCLUSIVE lock during the DROP INDEX step and then acquires a SHARE lock during
theCREATE INDEX step.
 
> > 
> > I understand that there will be no conflicts between the statements in this transaction,
> > but I'm wondering if an outside transaction will think that the table has an ACCESS EXCLUSIVE
> > lock until this entire transaction is completed.
> 
> No, just index1, but the SHARE lock will prevent writes.

Not sure what you mean by that, but the ACCESS EXCLUSIVE lock from DROP INDEX
will be held until the end of the transaction.


> Have you thought about using DROP INDEX CONCURRENTLY and CREATE INDEX
> CONCURRENTLY?

These cannot be used in a transaction, however.

> If in doubt, try it. Look at pg_locks.

+1

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com




pgsql-novice by date:

Previous
From: Simon Riggs
Date:
Subject: Re: Table-Level Locks and Transactions
Next
From: Simon Riggs
Date:
Subject: Re: Table-Level Locks and Transactions