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 1tiOYp-00FTlh-JQ for pgsql-general@arkaria.postgresql.org; Thu, 13 Feb 2025 02:02:39 +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 1tiOYn-000Oc4-HG for pgsql-general@arkaria.postgresql.org; Thu, 13 Feb 2025 02:02:38 +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 1tiOYm-000OWv-U0 for pgsql-general@lists.postgresql.org; Thu, 13 Feb 2025 02:02:37 +0000 Received: from mail-io1-xd43.google.com ([2607:f8b0:4864:20::d43]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tiOYl-000V38-2h for pgsql-general@lists.postgresql.org; Thu, 13 Feb 2025 02:02:36 +0000 Received: by mail-io1-xd43.google.com with SMTP id ca18e2360f4ac-8553108e7a2so26521739f.2 for ; Wed, 12 Feb 2025 18:02:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739412155; x=1740016955; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=lKj+ZyOenoKV3wrXsRO/jH+Di5qjdfAgrAzQVfpoDLE=; b=NGodEipXeeuWchJDmujVrAMW96SGbv+Qu1tbLNymgj8TM0haH2R0RECLz52HKQemeq PJC6ELGqSNk4sPfD/sk/nr9AT/KFGM5zgEsQmS4sFOeBgYL8qJQZz6aGKYOZ1Vq3VCzC Ugh/gpHYlfROnIohycA64DYtOrU43dVLT/WVxMFv7Pc6PRDO8SFUjWd6ZTeP3ZuJqp0k ShixDgStvLti3BG29Bl9w1dioYzOVV/alZ5RrkDjR93fvmdtOPHnlz69DakO7Zhh+xHx pzFv4zIoq3lmmXdMDWKS08xjXj8OrPTv+CVTr6yK7IVQ9h5aM+6JLZ/cix6BrP8agxze vlhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739412155; x=1740016955; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lKj+ZyOenoKV3wrXsRO/jH+Di5qjdfAgrAzQVfpoDLE=; b=RfuXAghz/HZrPVa0zwUsINNVcwixWYFzfHRN3CAbGxhv1fr54zOaG/gWw9aS/vp31o 3hMONMApuTarB5hxZTi5XvBh/2/VFOTjO0VIvXZyT5IIWAxSgpWlN9DVk+FVHuyvOHlm CsH2SA0I9n2REoABDK93aEa3+bFB9Nb+6VNeba+Qxmd1wjQjQi/JBQ8j/DEDSASeIxHu gfnvtIbpY9coTUtSqzASnhS5oTJZDgIxt52LgNHfcLMowayUOupmGthCu3mFQ5XTPQVS ULuwSu0E/NDkk/613mNFMteO+KA3oFyDejXUSBZ2YsvkIRoXySqfIYlv58gtTXZ8FPJy xfWg== X-Gm-Message-State: AOJu0YzadQxKyy5/GxynBsVpFY2Jiqtd4j+7vCXjgdUHlsbRTXnT1sCT kg3y79NgFyr7vpsuopwVR/0tckZQs9K4Al+o9XwobdCoophHqrfr6RQmhE3LgE/UVjALVxwEZrP czkjqJTOPfuRMzNlOYM+6tVnuO2g= X-Gm-Gg: ASbGnctFXVIsj4K153xuma4wVFBnT55+Mh9+LVWtCP58Q1gBUad/wtaNDdF7sE46I55 EfpGi38zLmSJ6HjjrlaJjV2faoyG3yEp6PXSCHqgfOObiHch7oUdJUFPwzfWj6yBUjzR19zxzPQ == X-Google-Smtp-Source: AGHT+IH0ddMLAot/QSjczJRGm4IZHgWXCp9H5GIrI3VF8t283balB18LAPF2dp3WLncbmQoVolVmeYJk4THKHquU4Cw= X-Received: by 2002:a05:6602:6b09:b0:84f:2915:daef with SMTP id ca18e2360f4ac-85555dd1d7emr491990639f.12.1739412153087; Wed, 12 Feb 2025 18:02:33 -0800 (PST) MIME-Version: 1.0 References: <8ac6a1dd-db82-4ea7-8186-0c354d79049f@aklaver.com> <9fa75099-1ee9-43de-ac6e-945841b184eb@aklaver.com> <22406cdf-83f5-4b98-b07e-7827afe97f93@aklaver.com> In-Reply-To: <22406cdf-83f5-4b98-b07e-7827afe97f93@aklaver.com> From: Marcelo Fernandes Date: Thu, 13 Feb 2025 15:02:22 +1300 X-Gm-Features: AWEUYZkQ9wHlRf9klMwL18rzcE2oguDdyIRyhu0nCLbKRp22QROmu4xvSEeki3s Message-ID: Subject: Re: Best Approach for Swapping a Table with its Copy To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Feb 13, 2025 at 1:33=E2=80=AFPM Adrian Klaver wrote: > Not seeing it: > > https://reorg.github.io/pg_repack/ > > "Details > Full Table Repacks > > [...] > > pg_repack will only hold an ACCESS EXCLUSIVE lock for a short period > during initial setup (steps 1 and 2 above) and during the final > swap-and-drop phase (steps 6 and 7). For the rest of its time, pg_repack > only needs to hold an ACCESS SHARE lock on the original table, meaning > INSERTs, UPDATEs, and DELETEs may proceed as usual." > > During the ACCESS EXCLUSIVE stages you will not have access. The operations under ACCESS EXCLUSIVE are short-lived. In my benchmarks, th= e "repack_swap" function finishes within the order of magnitude of millisecon= ds. The operations seem to be catalogue-only. I'd love someone to confirm this though, because I am not a specialist in C and Postgres extensions. Here is the code if you want to have a look: https://github.com/reorg/pg_repack/blob/306b0d4f7f86e807498ac00baec89ecd334= 11398/lib/repack.c#L843 What I am after is the same, but I seek a deeper understanding of what it d= oes, and why it does it. For example, it swaps relfilenode. Why? > Not only that with pg_repack you are not changing the table definition, > whereas in your case you are introducing a new constraint and associated > index. Correct, but I am not using pg_repack. I have cloned the table using my own tool. I'm citing pg_repack because it does perform a table swap (a bloated = table is swapped by a new non-bloated table). Given that I know pg_repack works well in large databases, it has to follow that the approach they have to swapping the tables is robust. > Do you have a dev setup where you can test alternatives with a test > sample of data? Do you mean alternatives to table-cloning-and-swapping? Regards, - Marcelo