Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uCXJx-00GSqG-J3 for pgsql-general@arkaria.postgresql.org; Wed, 07 May 2025 05:27:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uCXJv-00BlCU-Gn for pgsql-general@arkaria.postgresql.org; Wed, 07 May 2025 05:27:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uCXJv-00BlCM-2f for pgsql-general@lists.postgresql.org; Wed, 07 May 2025 05:27:51 +0000 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uCXJs-000XMD-0n for pgsql-general@lists.postgresql.org; Wed, 07 May 2025 05:27:49 +0000 Received: by mail-wr1-x430.google.com with SMTP id ffacd0b85a97d-3a0b291093fso308290f8f.0 for ; Tue, 06 May 2025 22:27:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1746595667; x=1747200467; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=9zAACTtwz3k41Z2FWSfNUdQd3K0qFL51/M9K40t4Pt8=; b=PIJ13hqPhho/pA7dPMU6BeJF8bSH5ZZwYJLmlYqYe/xmHAYHCIWfF4JMEkVZ/Pi46N KVHjeFQ76EgpuaeD3RRZ3puUmYrCY9DWYvLmNJF4Oyaj18GZbzE0bKVzttzo2emoZa2e K9QhFzB6FLdvjFIK2K5aLaMTBR2d+X0iaprJVxhuMQ2znRpYRcwyEdx1ybtCnE9uN+4O +VIYy4+7nbvkhyOkNVSsKP/A6veeDDBYkOCA16MKHEOd+tic/4FXzhUkhQYsD5es6ur6 zQ8nd8Bkw5aKXma0jVvppQES1wuH9Uw/W9a5DS7v7LQv123dfs8XhCZgB/Z4e5aBnRnv 4frA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746595667; x=1747200467; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=9zAACTtwz3k41Z2FWSfNUdQd3K0qFL51/M9K40t4Pt8=; b=LqwpqC1RU5+fPTb3+GLDAWPLOxCS53YoyfjpAZmlwUBTVmUGUTi5AEDrUizlDZvbqJ o8r8jp0rlwU3rTWd2Qh6DJTPrxdXByBwZUe7POWoftwARq137fHNY0qIP8fpIU7jzRdn LCMHhL8WfdpkhrBY30rr0llpntXM/W4qpG8h95pU23DYvJGOYit43tphJIeCVaoy+GY0 TcXOqT+0lbnOjbqLCH57q1Pgm2Ef1AppB/nqm0PkLO282unYgRAOi7AQy5g8vt6Bfc3q yDCsxYBZ44e2zGV4MhtqxcmihvxvEiLvAOaNmbewLTuhgxxv6dxqQ3QHbw1ThbPkjjLm fYow== X-Gm-Message-State: AOJu0YzmzveUVKLDSBUOaATg1wiLtW11gyEV21didTO7xvXWZnsJItLG QlnCOpW1tU1b9y9s/MtKzeWwyI+4AlTvXWHZCy6m0vb9QAe1pKyfhus6dJ5856tMtVnGefDxekS ZbtLpFA== X-Gm-Gg: ASbGnctGVocTI4Bsl0R+ZqZnAcdaJSCIQNOgYsLxbroxmdnZei0+XvBfxsXCXDDCKXe 2wY0F8gnQ/7CEGndqFGe7VomCRAofaUQTOqKfXqwzRLCZtSjrgNbG8MjXLgXiKRUMQNMcBFh3D/ YaXxZbrRF268p88oC2ulBk/a0msFD90Rem4gIbnozt7jneqWWkYnXT2gRKdlZDy1lrsgA5tppHW 2uZEGqhxCQU+LBjd1GHaf3yCfW+ufNlNkagMuM3DOio8lYKmilu5hb2CCauh0mGS/OP824EbJ9M zR/ZrY/1lhgQ53s1G4nbV/zizRBQ+Q03pWseW64G9h+Z1CaLwEM3x2h7Msm77A== X-Google-Smtp-Source: AGHT+IHMYV6YUddIHI9kxhNwNuijXQZsfXYP62F9y1zanyu+fqiowo2kYeuBmZvh07JFg9pL0XCCQg== X-Received: by 2002:a05:6000:2905:b0:3a0:b1f7:c1e9 with SMTP id ffacd0b85a97d-3a0b43aefeemr1824396f8f.1.1746595667315; Tue, 06 May 2025 22:27:47 -0700 (PDT) Received: from localhost.localdomain ([2001:871:255:52d3:3ac8:61ab:b634:342]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a0ad661602sm4173478f8f.61.2025.05.06.22.27.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 May 2025 22:27:46 -0700 (PDT) Message-ID: Subject: Re: Soundness of strategy for detecting locks acquired by DDL statements From: Laurenz Albe To: Agis Cc: pgsql-general@lists.postgresql.org Date: Wed, 07 May 2025 07:27:46 +0200 In-Reply-To: References: <2d0e4eac41147fed7e09a05e8f9318e119dd995a.camel@cybertec.at> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2025-05-07 at 06:08 +0300, Agis wrote: > On Wed, May 7, 2025, 00:57 Laurenz Albe wrote: > > On Tue, 2025-05-06 at 12:06 +0300, Agis Anastasopoulos wrote: > > > I'd like to "preflight" a given schema migration (i.e. one or=20 > > > more DDL statements) before applying it to the production database (e= .g.=20 > > > for use in a CI pipeline). I'm thinking of a strategy and would like = to=20 > > > know about its soundness. > > >=20 > > > The general idea is: > > >=20 > > > - you have a test database that's a clone of your production one (wit= h=20 > > > or without data but with the schema being identical) > > > - given the DDL statements, you open a transaction, grab its pid, and= =20 > > > for each statement: > > > =C2=A0=C2=A0 1. from a different "observer" connection, you read pg_l= ocks,=20 > > > filtering locks for that pid. This is the "before" locks > > > =C2=A0=C2=A0 2. from the first tx, you execute the statement > > > =C2=A0=C2=A0 3. from the observer, you grab again pg_locks and comput= e the diff=20 > > > between this and the "before" view > > > =C2=A0=C2=A0 4. from the first tx, you rollback the transaction > >=20 > > I think that that is a good strategy, as long as you run all DDL statem= ents > > in a single transaction. >=20 > Can you elaborate on that? >=20 > I was thinking that we should mirror the way the statements are going to = be > executed in production: if they're all going to be executed inside a sing= le > tx, then we should do the same. But if not, them we should follow course = and > execute them in separate txs. >=20 > Am I missing something? No; I was sloppy. What I wanted to emphasize is that you have to look at "pg_locks" *before* the transaction is committed, otherwise you won't see any locks. It doesn't have to be one single transaction. Yours, Laurenz Albe