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 1uCQIU-00Evbd-IJ for pgsql-general@arkaria.postgresql.org; Tue, 06 May 2025 21:57:55 +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 1uCQIT-00A5cj-0Q for pgsql-general@arkaria.postgresql.org; Tue, 06 May 2025 21:57:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uCQIS-00A5b9-KK for pgsql-general@lists.postgresql.org; Tue, 06 May 2025 21:57:52 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uCQIP-000W7w-1X for pgsql-general@lists.postgresql.org; Tue, 06 May 2025 21:57:51 +0000 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-3a07a7b517dso4061856f8f.3 for ; Tue, 06 May 2025 14:57:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1746568668; x=1747173468; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=z0dPj7enMDJidataaQjrmJ5udBxJqFePyR0ksWgh6JU=; b=TVDsBqVkucMi1kcNNfoi9cVfiqF74fAkDkrYBXUiIIr8/L0Rx1Gpsj9p2lcnXQ/sQF 5CDSHOje1Dkf3CK52Y8jz7C6dWBPY9XzDya9F/dMsEgjfnRgggK2J48gwY7bMKpclCwk awWMyWE/3lxGZ5q56z9FdAsNKK2xsq7kCbyFRjO1cmnWCnrkqcGHrU+Vam4AGk3/LU8g MxDc/umZgCyx6H047iF+AA0Oyn8Xkfh0B7w+ERwa7IhQzFSdQ9FzV6K077T0Nwryul5M oAjdHzTfE7vsEC+hpD/7IsP/tBaHYKm4YOfIRoh4waEiF93t6T4J0s23BUESCy/agHF+ Tylg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746568668; x=1747173468; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=z0dPj7enMDJidataaQjrmJ5udBxJqFePyR0ksWgh6JU=; b=B/lhaWQdDyEZ6VI4bKGcXtuE9h1EjPcMd4Gbe+AVzPmGAMkUgDlrC9R+MCALhLb/Mi oJshYfhLNPMZwoH2CImEQzdbNYy5g41JlnXKrwvqFPPQNCMPXfC6YPFqOtjFJIcKPj9W 7kEl1mvjPL0hiiLQkhS455bPqH//k+IvmP7S0198INl/RCni01JMoQ06L2PvEOhD5zGT obqaVb3wOKcibS4oFkmC2dm0z54Vmbgej7Cv0xa/Aak1xAkpkjjkLetoZsW0FVzGNarb wwNUSKPxA9RnMxLHRCQHsua/7VcLG68wRwRyaQa7FKelUFXdJ1rWyxet2unccuw5r2xT UiPg== X-Forwarded-Encrypted: i=1; AJvYcCWwJfUrYFnHYPpFz0i3Hpb6LuLPSX7x3OjkG/u0p2+IVzZYk+xQywtpSCN3/wGATvji/0x8VwIY9So88AzC@lists.postgresql.org X-Gm-Message-State: AOJu0YzPGYDu8b1xzxXCWFsr3wm/PhXsim29Ip5tNaHDxyxWPe5s+rXQ cxLMD5roQXQjeRgy31Bc0TTb/kzSwcuU3pNYKYv9WiXec5lQVmYLmF4btyzlLgg= X-Gm-Gg: ASbGncuJrEfdcaLD3gHkCp8WBX9jSucAkwmFKlnIdmXDYj2SqBnqj2h3c1yZMr7MTzA Svx0czqa4pwEUTeJCWlH/9sAtj2pR8De289PkgMQXkdcoxwjFTSwd/DnJso4P+RAeBX0gMtxkjv gyfI4diJN2Mht3AncuTYNoxzlSYFqRJPmt3sAKpiDSaeyiGVCJUnIqEjxrzKTYU45KQPx7MqVMa fQ+OmsBapmcSksmtZjybGhERkcgOZ6gLFKQWHg+y2Q3Mup39vzAICRBNQkBuYmt1gYKDRWVUBge BF7te/XRSf6qEnEOu6ZCCAUUfXPe1X4656uDGbYSCAoLDZ9gBFrgXXnUBZqC7w== X-Google-Smtp-Source: AGHT+IFKxiBQvwpiAVp893JSkd5RQ+TJJjhn2UzLmV9VDaKktQ6JvuKj3ZtjraiLsoyNbzYeT3VseQ== X-Received: by 2002:adf:ef09:0:b0:3a0:b4f1:8bc3 with SMTP id ffacd0b85a97d-3a0b4f18d0amr522367f8f.53.1746568668370; Tue, 06 May 2025 14:57:48 -0700 (PDT) Received: from localhost.localdomain ([2001:871:255:52d3:3ac8:61ab:b634:342]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a099b171b2sm14671637f8f.87.2025.05.06.14.57.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 May 2025 14:57:48 -0700 (PDT) Message-ID: <2d0e4eac41147fed7e09a05e8f9318e119dd995a.camel@cybertec.at> Subject: Re: Soundness of strategy for detecting locks acquired by DDL statements From: Laurenz Albe To: Agis Anastasopoulos , pgsql-general@lists.postgresql.org Date: Tue, 06 May 2025 23:57:47 +0200 In-Reply-To: References: 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 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 (with= =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_locks= ,=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 compute th= e diff=20 > between this and the "before" view > =C2=A0=C2=A0 4. from the first tx, you rollback the transaction >=20 > By diffing the after/before pg_locks view, my assumption is that you=20 > know what locks will be acquired by the DDL statements (but not for how= =20 > long). The query I'm thinking is: >=20 > =C2=A0=C2=A0=C2=A0=C2=A0 SELECT locktype, database, relation, objid, mode= FROM=20 > pg_catalog.pg_locks WHERE pid =3D $1 AND locktype IN ('relation',=20 > 'object') AND granted"; >=20 > The type of statements that would be fed as input would be `ALTER|CREATE= =20 > TABLE`, `CREATE|DROP INDEX` and perhaps DML statements (`UPDATE`,=20 > `INSERT`, `DELETE`). >=20 > Do you think this is a robust way to detect the locks that were=20 > acquired? Are there any caveats/drawbacks/flaws in this strategy? I think that that is a good strategy, as long as you run all DDL statements in a single transaction. Yours, Laurenz Albe