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 1s6yhn-002p4s-I6 for pgsql-general@arkaria.postgresql.org; Tue, 14 May 2024 20:25:01 +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 1s6yhn-002cD6-KD for pgsql-general@arkaria.postgresql.org; Tue, 14 May 2024 20:24:59 +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 1s6yhm-002cCy-TL for pgsql-general@lists.postgresql.org; Tue, 14 May 2024 20:24:59 +0000 Received: from mail-dm6nam12on2057.outbound.protection.outlook.com ([40.107.243.57] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s6yhh-000FGj-Rh for pgsql-general@lists.postgresql.org; Tue, 14 May 2024 20:24:57 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c8rKdhXKB7iuXMaU7kvT8LsWRd62uOaM4DIuysSdLZc0LzV1bDBMXs+mIK5I/Tld15cD/e9tuc1GQqJfbBe2Sh+CeZz7+3xc4waFglVW3zF2fs56LVl0PPLxRj8gv0fMMmpvfOHZ1VMI6HSN5n72uga1G7VZQQEFvp7VPer/YoE9/MhxefjyHE+g1AWpyYIQOwGM6pl1PQlOt/xYFvkx8yXJFjWG5Nj5WOCIXQo5jD7kTX1q8SMumv9cAaJe4g8gTLLcY7nQCj3b3hKJt1tgJA5DhOSvhG6VZM8LYtjmR9wkxd7LeqozyIjmy8HyVUGqIjXuvXBCqa7MjdrBacfiAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9azOyv7glsHURehXHfEGk6WGt1wnNt0grxY2gk8I+Ck=; b=mY2hym0xM9YZ/DWRnWNUNe9jCu6uWrnxsqUiTkDRFMmTalzTtsPmnAPc56W6hXeNq2ZM/zVfA7e7YmBkERmxX46/2wzcsajb5TdsTZlRvq9bYZeWOFO34P2Ueu/C82jMvvP7QN8ElegJDwurvPDE1mab1M0TzKLKbJjedf4FqRBzLKeWS+jPESrNZV1tNWv+32EvwfwVfoJF8oMENZJ/hFY9mq3vkv5uUMLMJOz0FoXTvPNhKMaI5PsnfGX5H17i3ejfRpQvmSm8NHYZVnsIZV4yh4FZAaDOcKsv0vQb6qoipJsi/kF9DrRrFsxWxatnJkXN6G476+/DJ3mxtoJWbQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=thomsonreuters.com; dmarc=pass action=none header.from=thomsonreuters.com; dkim=pass header.d=thomsonreuters.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thomsonreuters.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9azOyv7glsHURehXHfEGk6WGt1wnNt0grxY2gk8I+Ck=; b=JMH44wOn5gdPonkwQt/6K+A6SD7/4SbU2vkRFL3raWkzNJ2LVnrbZI/a9y8IRH+EZoL+dgbiN32Mgx0UTQjr9hWbzE47OXa0r6ucFnKVBOnImxK1Hy6ZQO4d5Z6EkcR+Rgv1LYBsNwRO9mOx0ECp1/0JfJHhGZmO6awZeYzgTPM= Received: from BL0PR03MB4001.namprd03.prod.outlook.com (2603:10b6:208:2e::15) by CH2PR03MB5333.namprd03.prod.outlook.com (2603:10b6:610:a2::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7544.55; Tue, 14 May 2024 20:24:49 +0000 Received: from BL0PR03MB4001.namprd03.prod.outlook.com ([fe80::d7a1:21c4:c2bb:d568]) by BL0PR03MB4001.namprd03.prod.outlook.com ([fe80::d7a1:21c4:c2bb:d568%7]) with mapi id 15.20.7544.052; Tue, 14 May 2024 20:24:49 +0000 From: "Dirschel, Steve" To: "pgsql-general@lists.postgresql.org" Subject: Using ALTER TABLE DETACH PARTITION CONCURRENTLY inside a procedure Thread-Topic: Using ALTER TABLE DETACH PARTITION CONCURRENTLY inside a procedure Thread-Index: AdqmO1nnsI6aILnmRumBC+x3Yd/TFw== Date: Tue, 14 May 2024 20:24:49 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=thomsonreuters.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: BL0PR03MB4001:EE_|CH2PR03MB5333:EE_ x-ms-office365-filtering-correlation-id: 495d3af2-ea1b-4382-b1bc-08dc7453e781 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0;ARA:13230031|366007|1800799015|376005|38070700009; x-microsoft-antispam-message-info: =?us-ascii?Q?/d3bG5nuUYjhZUAHFHjUQA+r2DSI83TLeS2psD4vmzJwJU/FWssmVBKb2qqE?= =?us-ascii?Q?2NjnbEm2ZhbhlmOsNas7R8Y6wsmpfZrr2gtIGMz1sN1+v2gK9id7olI8Rbzc?= =?us-ascii?Q?5tfbHnFoSfQlpJq2nVrwp9DAaypdFLfHXinYmpPXDykLz5CWWUEpK05iLMsG?= =?us-ascii?Q?GaTjDt7Zxm7k401n62gj4PlmRWH00mNjYjvkLD+59e0EzQMadYo+bp8L7mCX?= =?us-ascii?Q?Br3aENjr7Vw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?Q?9FoCoEEWaJKwVeLWFKQpj2bTnOBvvznE9yv9Cfs7oJCE8dEtybvN5GoB3woy?= =?us-ascii?Q?GyTZlg43izfGQV9xCQ3Y9d9IyVI7dWe7OvBQFfEj/tlsglKQoKfV7yPZJjUs?= =?us-ascii?Q?USowsvQSCyHzSaJfymOgv+S32woyX46ndDAyJ7bwTdqKGvc0JJlYXStQuZis?= =?us-ascii?Q?b5wRctyJtYdJ0EMGcAgzJlO0kRc07G1m6K5cSggiBcrKTQ=3D=3D?= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR03MB4001.namprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(1800799015)(376005)(38070700009);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?uYQ05EwI9iKp3C2Agly/s6T2iKYswmdq0E1dabyOK1P8aDh14XU9nW4/W2il?= =?us-ascii?Q?e4HqCD2jFqCkicYWkisMFmvwVQJ2Qw5qNjwXlflqPQg//2mkYxkvWQTYATy1?= =?us-ascii?Q?FTzoMNokBhPzVfaQd8tFC8V12p5O5C/Km1oBZsAcuD4CYCS1azTeMouOpqe9?= =?us-ascii?Q?lrPvTD7HoncHNFjT7o3R6p0GwdAr8QsWJIx0+x3EShY0cGmfRzMOjCoqAWHO?= =?us-ascii?Q?+5mPZzAFcGwQuTQjImb4iNAupuA3h0UAePkoKp5RO2cfXEE/WzJMKB2X6TMq?= =?us-ascii?Q?Ve7YPdy0y001f5A4MN+rVCNnUuXDTtjvaE+yCIErTuQxGRglpO8wKpyz7IXW?= =?us-ascii?Q?RpQe0L+MdcQzmO5NqnqZzKWMzTrm3r7S9ni2H4wL7VIljvP82joua8tNu8wq?= =?us-ascii?Q?M9vwakqjdSEwMAlSCXXO+nX/KEWnm4WiOIdxst5/c/3xHb4pvT3q8IkxNQPs?= =?us-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Zkc1nDZWGJ9?= =?us-ascii?Q?jIVUKuVLJ+oAxhrhy4jpvSSrL4jFjsXWS4kKFlYcRTRZYS6QKofy0XgvoHPk?= =?us-ascii?Q?FRMY/Q0sV+IqZpmmkyw5LYyebeV8O/QIRvNTPwBj46CMX/Pvd4WDd0/rTion?= =?us-ascii?Q?Ld8soh1jBaNRmAYO1Mw+eUHB3hqNuvmBVTQ20qVob2adMR06fXeQgLv5Li9b?= =?us-ascii?Q?qGB2NgBoHHecUhOS0irouFHPfALnYQioR83fOlIAWbAst31zTQoIl5gB9+0e?= =?us-ascii?Q?bA=3D=3D?= Content-Type: multipart/alternative; boundary="_000_BL0PR03MB40015668F50A4DDA59390AE5FAE32BL0PR03MB4001namp_" MIME-Version: 1.0 X-OriginatorOrg: thomsonreuters.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BL0PR03MB4001.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 495d3af2-ea1b-4382-b1bc-08dc7453e781 X-MS-Exchange-CrossTenant-originalarrivaltime: 14 May 2024 20:24:49.4757 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 62ccb864-6a1a-4b5d-8e1c-397dec1a8258 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: N/JlxduasmV1GwDCwVzS5IAsvaJOfNOmgf9una+2fcDLZpbTVnm8l/vt3Cd+d3voJBeve9EDEVjy54TEqrLsZgFVNIkk+c+ruXbGy6367RvXj9XwjXDy58TRYmCE/VCY X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR03MB5333 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --_000_BL0PR03MB40015668F50A4DDA59390AE5FAE32BL0PR03MB4001namp_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable We have a custom procedure to add/drop partitions in Postgres. It has a ma= in FOR LOOP to find tables needing to be processed. Inside that FOR LOOP t= here is a BEGIN so if a single table gets an error we catch the error in an= exception. At the end of the END for the FOR LOOP it issues a commit. So= if there are 20 tables to process and the 15th table gets an error it will= capture that error, write a message to a log file, and continue processing= the remaining tables. We have ran into some locking issues when trying to DETACH a partition wher= e if there is a long running query against the partitioned table it will bl= ock the DETACH PARTITION command. Then what happens app sessions trying to= insert into the table get blocked by the session trying to detach the part= ition and the app gets into a bad state. In testing I found if I use the C= ONCURRENTLY clause with DETACH PARTITION the detach partition command can s= till get blocked by a long running query but that does not block app sessio= ns from inserting into the table. So this is great. But when I try and run the command inside the procedure it throws this erro= r: STATE: 25001 MESSAGE: ALTER TABLE ... DETACH CONCURRENTLY cannot run inside= a transaction block DETAIL: HINT: CONTEXT: SQL statement "alter table t2= .test1 detach partition t2.test1_gentime_20240511 concurrently" PL/pgSQL function part.partition_maintenance() line 323 at EXECUTE The documentation states: CONCURRENTLY cannot be run in a transaction block and is not allowed if the= partitioned table contains a default partition. Is there an option to call that CONCURRENTLY inside a procedure as I descri= be? Thanks in advance. This e-mail is for the sole use of the intended recipient and contains info= rmation that may be privileged and/or confidential. If you are not an inten= ded recipient, please notify the sender by return e-mail and delete this e-= mail and any attachments. Certain required legal entity disclosures can be = accessed on our website: https://www.thomsonreuters.com/en/resources/disclo= sures.html --_000_BL0PR03MB40015668F50A4DDA59390AE5FAE32BL0PR03MB4001namp_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

We have a custom procedure to add/drop partitions in= Postgres.  It has a main FOR LOOP to find tables needing to be proces= sed.  Inside that FOR LOOP there is a BEGIN so if a single table gets = an error we catch the error in an exception.  At the end of the END for the FOR LOOP it issues a commit.  So if the= re are 20 tables to process and the 15th table gets an error it = will capture that error, write a message to a log file, and continue proces= sing the remaining tables.

 

We have ran into some locking issues when trying to = DETACH a partition where if there is a long running query against the parti= tioned table it will block the DETACH PARTITION command.  Then what ha= ppens app sessions trying to insert into the table get blocked by the session trying to detach the partition and th= e app gets into a bad state.  In testing I found if I use the CONCURRE= NTLY clause with DETACH PARTITION the detach partition command can still ge= t blocked by a long running query but that does not block app sessions from inserting into the table.  So t= his is great.

 

But when I try and run the command inside the proced= ure it throws this error:

 

STATE: 25001 MESSAGE: ALTER TABLE ... DETACH CONCURR= ENTLY cannot run inside a transaction block DETAIL:  HINT:  CONTE= XT: SQL statement "alter table t2.test1 detach partition t2.test1_gent= ime_20240511 concurrently"

PL/pgSQL function part.partition_maintenance() line = 323 at EXECUTE

 

The documentation states:

 

C= ONCURRENTLY cannot be run in a transaction block and is no= t allowed if the partitioned table contains a default partition.

 

Is there an option to call that CONCURRENTLY inside = a procedure as I describe? 

 

Thanks in advance.

 

This e-mail is for the sole use of the intended recipient and contains info= rmation that may be privileged and/or confidential. If you are not an inten= ded recipient, please notify the sender by return e-mail and delete this e-= mail and any attachments. Certain required legal entity disclosures can be accessed on our website: https://= www.thomsonreuters.com/en/resources/disclosures.html --_000_BL0PR03MB40015668F50A4DDA59390AE5FAE32BL0PR03MB4001namp_--