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 1tdV7K-007f2n-7s for pgsql-admin@arkaria.postgresql.org; Thu, 30 Jan 2025 14:02:02 +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 1tdV7I-00Brff-DY for pgsql-admin@arkaria.postgresql.org; Thu, 30 Jan 2025 14:02:00 +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 1tdV7I-00BrfX-0q for pgsql-admin@lists.postgresql.org; Thu, 30 Jan 2025 14:02:00 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tdV7E-002M1d-2h for pgsql-admin@lists.postgresql.org; Thu, 30 Jan 2025 14:01:59 +0000 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-436a39e4891so5897735e9.1 for ; Thu, 30 Jan 2025 06:01:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1738245716; x=1738850516; 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=6lDpz3FVAvu65/71Do/lO1Geq3y718oc/rEA0sEaQVc=; b=G9cY1jN3Y3O42MdX8IrRMcdzgnoS+AfXX8sauhCw9l3McucV6w/A3ykMHwP889bCpU FgC2FYugjCPbHuq6Q61YgpYF16tP8R2oh71ne+ZvK1xDt0gEgDU+BRcIIM0D3wU6p0gv N6PM/5QzZZ4f6/k8Nm5pDvk5o6jDMOxxgvVelD2xwedf2T4WFUm3NmQKIWXgwjySa6qy OwQSmO7P1YW7+N+MX24EQYab7g+v7A7JsdHYvEZwKbGupiS/AKksgM0dlA7BFuXsdQ4I B16xxeD64cOrGMZYfMCO0RcBLCqfeRBFWucOOTqzuwVlPNVVZ77XB/Kcqfu1H8nWxOC7 dF4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738245716; x=1738850516; 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=6lDpz3FVAvu65/71Do/lO1Geq3y718oc/rEA0sEaQVc=; b=JzyxkKaz3Gelb9bQnHMVrbjhy7Vv4KSwyyBp9wHinUAyCb17raRlXvqIwl/YwEmP28 quXf0upkwy8T+H7Je4wJGdNnLb+P429zkJMSYfrcSg66CEBtyvhTuw9MGzw+1rksO5Pt Dv2PCkSBcMDB5G0lhHnByhb9G6yc6pkXbOjRsRvH6S2hlyVuygDSxAxs6DtzxSOew2EM Vvb6uHfHAlu05g3kaOqvfXq0CeWOxcUGSlUEuTCISOCt7X5EIcOIrCACOBEh3JnjLUjt 5oSgVNwsI51dXQgRoyrxHVE0vTc7xIO//0YRFpmNTQljPymOpacuPZ341Wtc5l3r1W8n kaeg== X-Forwarded-Encrypted: i=1; AJvYcCXIluEi+Dcb1NgKf/KlhuaSKDBtGni0o2IdwRXbngaHeeYsDiqz0BGeA6bfhRqAVoWMNg1a4e7WIqNKuQ==@lists.postgresql.org X-Gm-Message-State: AOJu0YxaSTBuqV07c1qbIrXaPvkTDCb8LcG8gbNfZAOKjc+IxpbAzudT MKYVQj79zBHau6jqZ08uOpkGoMOHVhV59jfsDodEZv1asD9NdaltLaJdh3yi88I= X-Gm-Gg: ASbGncssyCJoSmvQfPHeA9gg0GD9RhC0ojjN4yXjhnPBiy0QPjIh7SuY7NywLY0PitF ortf+idXrNYCFRyn0BnQxieGk/WfIMMaYrNwGWhpnSrhemhFoBG7vGSnCaUQisUb6VcsTdvPy+K OmfcBVrPGQR/uALQN6MC2alHkrhRGPJzpCWhhQTcwPNTwPq9W5YH1PjH9fe/D9mm2F9bpepMaeA N/wtGs+5bJFXgJTH6bc2DyozJwrxnm45xICOjbTW294MiEbxLa+jJK6FPFiSguHFGwWDDWh2vfV Bny1WCAkO9L7vKAKDT73kEA1PFl/YIxZ5kA= X-Google-Smtp-Source: AGHT+IFxwk19bw3pFkAT637irW2gLlnsqJB7A5lTwnDvX2sf5jrcHxsoQnPEtRV8ZCfF4w2QtPVNJA== X-Received: by 2002:a05:600c:35c5:b0:437:c3a1:5fe7 with SMTP id 5b1f17b1804b1-438dc410b2emr59434685e9.20.1738245715930; Thu, 30 Jan 2025 06:01:55 -0800 (PST) Received: from localhost.localdomain ([2001:871:5e:20dc:99d3:5235:292c:80eb]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-438e23de2d6sm24024085e9.11.2025.01.30.06.01.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2025 06:01:55 -0800 (PST) Message-ID: Subject: Re: Partition management - best practices and avoid long access exclusive lock during partition creation From: Laurenz Albe To: srinivasan s , pgsql-admin@lists.postgresql.org Date: Thu, 30 Jan 2025 15:01:53 +0100 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 Thu, 2025-01-30 at 14:17 +0530, srinivasan s wrote: > I am looking forward to=C2=A0some suggestions to avoid exclusive lock dur= ing the partition > creation in=C2=A0postgresql 15. Currently we have a simple monthly range = partition setup > on a table. >=20 > I Scheduled a pg_cron job, which runs every month=C2=A0and checks if we h= ave a partition > available for next six months and creates necessary partitions. >=20 > The command used to create the partition is given below, unfortunately th= is is > causing a huge exclusive lock for a long time and blocking other sessions= causing a > resource crunch on the system. this is very busy system 24/7 very difficu= lt to find > a maintenance window >=20 > CREATE TABLE IF NOT EXISTS %I PARTITION OF %I FOR VALUES FROM (%L) TO (%L= )', partition_name, table_name, DATE(start_date), DATE(end_date) >=20 > I was going through the documentation=C2=A0PostgreSQL: Documentation: > 15: 5.11.=C2=A0Table Partitioning=C2=A0and there was note by creating a t= able, adding a > check constraint=C2=A0& attaching to the parent table minimizes the lock = during partition > maintenance. something like below ?=C2=A0 looking forward to the suggesti= ons from > partitioning=C2=A0experts. the change that I am making to help to avoid s= uch huge locks > or other suggestions ? >=20 > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0EXECUTE format('CREATE TA= BLE IF NOT EXISTS %I (LIKE %I INCLUDING DEFAULTS INCLUDING CONSTRAINTS)', > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0p= artition_name, table_name); > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0-- Add the CHECK constrai= nt with the dynamic name > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0EXECUTE format('ALTER TAB= LE %I ADD CONSTRAINT %I CHECK (created_at >=3D DATE %L AND created_at < DAT= E %L)', > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0p= artition_name, constraint_name, start_date, end_date); > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0-- Attach the partition > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0EXECUTE format('ALTER TAB= LE %I ATTACH PARTITION %I FOR VALUES FROM (%L) TO (%L)', Yes, that should only take a SHARE UPDATE EXCLUSIVE lock, which won't confl= ict with SELECT or data modifications. But I am surprised that the original statement is a problem. Sore, it take= s a higher lock, but only for a very short time. Perhaps you have long-running transa= ctions all the time. If yes, that's a problem you should work on. Yours, Laurenz Albe