Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mvHtx-0005vo-3H for pgsql-novice@arkaria.postgresql.org; Thu, 09 Dec 2021 11:47:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mvHtQ-0000tc-3Q for pgsql-novice@arkaria.postgresql.org; Thu, 09 Dec 2021 11:47:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mvHtP-0000tT-Nx for pgsql-novice@lists.postgresql.org; Thu, 09 Dec 2021 11:47:19 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mvHtM-0000aC-Rr for pgsql-novice@lists.postgresql.org; Thu, 09 Dec 2021 11:47:18 +0000 Received: by mail-wr1-x42f.google.com with SMTP id v11so9172334wrw.10 for ; Thu, 09 Dec 2021 03:47:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=sQPxnckrTjUXP22aCsL2Hl4MuA+ikb5eWlvUfrYerYQ=; b=W6scqnps3oYu2uI7KpIdXRxJcXk5pg73jIUVIxZLnpGJAkPc8L86rZg5ZXM6eG9C8Z Y7cS5YFcc2hk79mQefkx7V1LWT1VQ8/FFSuLb/SpcnQLpMcJ4EjmVTd7u6kQxIGYhGd1 yFFCDif0Q1HRN3LM6pYHS/JYw6+2nTvmqhOvM6vm6x+t5W2EpnHYEHmGaGzuAgudDYTX OtdPhPvl3OJU8UNaCdCaS/K7fIHf+idtjOJcByvXQZwJnZlcUQ5Z0THqnQrvA264EjDk x43LceT0rSbnzwki5pI+YBaIkp2e0k9IaBKoXiQ6Azat8Vjqq1XhCea53/ROmoSkl6lt 0xIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=sQPxnckrTjUXP22aCsL2Hl4MuA+ikb5eWlvUfrYerYQ=; b=kEQ6qUmR7z6cOQ6BTelSFfC1+JfPr4ir6sa6hABPsqHUDYvltMIrIEmRXVy7XGiKgD 7pnqLp8RbwnWUADYoKcpP51GuDUB9kPvynJYFRCLHu/C6Q7OXNBZVNTxFFVglAqyI1Gl 8+m/LX89bL5ep7h0FhrTc9TOrEOGkiuw/a6rz+6SaNZXLfoBowUYWSKT7yai85lg36CW nu7eaK8x0DS/8G9Ia3akXkI4NiGry0HV5RQReD+7eYr8TfEmpTX5HLD5koWrhrhairAe gX70HlDcmxpm+NrkWdQfzNQ+UsfCL8zEjdJqmaatujnSvuQXAZ+pomF/QD2D96IKX3CT xGEA== X-Gm-Message-State: AOAM5337DsTrmkwDXmtYA8aTAJTjs01GQWGzNHfqN7hkBJRxRDfr59+g Q57G858Waa2wUJOV7nvBm6H4bcdOtPQQ1g== X-Google-Smtp-Source: ABdhPJwwlJmOCqptmZb068EmbqvZYfW2DwTx1A8RooFQtxORZMjtncBW0WmpmZPG4KZiWe8SNM0mKQ== X-Received: by 2002:adf:fbc5:: with SMTP id d5mr6262525wrs.291.1639050434084; Thu, 09 Dec 2021 03:47:14 -0800 (PST) Received: from localhost.localdomain (dynamic-eaol7q3yyl1ah8qba-pd01.res.v6.highway.a1.net. [2001:871:5e:1e7c:9500:7fff:fdd8:a036]) by smtp.gmail.com with ESMTPSA id l15sm5324962wme.47.2021.12.09.03.47.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Dec 2021 03:47:13 -0800 (PST) Message-ID: <8b788512e88c21ce5e913963827de3e3710d245e.camel@cybertec.at> Subject: Re: Table-Level Locks and Transactions From: Laurenz Albe To: Simon Riggs , greg.rychlewski@gmail.com Cc: pgsql-novice@lists.postgresql.org Date: Thu, 09 Dec 2021 12:47:13 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-2.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2021-12-09 at 10:33 +0000, Simon Riggs wrote: > On Thu, 9 Dec 2021 at 01:07, Greg Rychlewski 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 the CREATE 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