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 1tiKHC-00EXCj-HD for pgsql-general@arkaria.postgresql.org; Wed, 12 Feb 2025 21:28:10 +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 1tiKHA-00Erbm-9q for pgsql-general@arkaria.postgresql.org; Wed, 12 Feb 2025 21:28:08 +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 1tiKH9-00Erbc-VY for pgsql-general@lists.postgresql.org; Wed, 12 Feb 2025 21:28:08 +0000 Received: from mail-io1-xd44.google.com ([2607:f8b0:4864:20::d44]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tiKH9-000Sxv-05 for pgsql-general@lists.postgresql.org; Wed, 12 Feb 2025 21:28:07 +0000 Received: by mail-io1-xd44.google.com with SMTP id ca18e2360f4ac-854a68f5a9cso13682039f.0 for ; Wed, 12 Feb 2025 13:28:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739395686; x=1740000486; 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=HQV0qVY71FWmgp4EtLT1k4hrFnJcr05vnZkThO5HINw=; b=BdGjPJ5PR96j6iuwsi2Cd0xjj7jAC+llFOPsHYS2a4UuUIUCodtfZFoQ5lU5hPV1F8 145wxKO7t27Wops9ln2cSYpgIGigsy14HE2LkWVgRVXsGhbe3nui5bEOTzdnzlEOxkVb BFhp4y5LIHsnLjIAif2ECPalEPeeyJ9S286eaNAyKc/g8Y24Oa4ELVoVFbw3+/SGV48Q N1r0bXqJxqqU5DKDjCoa/TsK91+CK02T2se9qzYoF9rwncWqRSZZiahFl58UWQak/bgr iP1nYhU67uzlmJHGOA4q6CavGVKWfldQ8R4oHh+AExtVQINvBhz4vTK0i47En/HNpSq+ E19g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739395686; x=1740000486; 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=HQV0qVY71FWmgp4EtLT1k4hrFnJcr05vnZkThO5HINw=; b=cA5477iXQJtob6dEdvZX3LWpN0b6Ud0WNBQih41I2J+AJNQzBBvKc4sAirET2tvg4f X/XEnHU4MCDtJxgp/k07hmLi2a+0OvS2zoR+kF/OIDVIFteLAzAK/G4Vg0DdmnS+ZrRq zXc2V81py6bdTSe2OzGwoEIBZB/D4JOLSvrl0epYlI9lP9RhLLhREt4JKIVjpFEWDfiM FdtNzLdNzhkghIeLrYevXxb0w7ElMps+bW5ecxLzhz6ylSz7wg1GYqSCzz0IPh5L7+fF cvMDdtFbRxbGmGTpxWhHHvePEsVHVVMszSy1qD3Z+tQDNirY3MfrEktE6BBtEg5jzcNn G1eg== X-Gm-Message-State: AOJu0YxTfJBOqzyZsPijbUVhoWFHXskFNRmpVE1YJNRgpqpjIL+ibFxP sSRppmZN9IZQ9sGDnAXndYOhUZKELk+X3cdQ/cUP11X2FcXZR1TE1hfaLPxfFUzBZobBMT/uAII fY0VlDDASVOnljQ8Y2mvd2aXgetY= X-Gm-Gg: ASbGncvTQB4Ci35LqZDSWGeYY8YXL1EV8dJCc6MYtg+eF8eFwJ07b8RHD8qrHKd+Wp6 MN3O4c5u+1P2DzE+ERHQ4kfFj/BI+ZR9InZp+bmNONxNMmB6gGw3/QlyPscIimBUgwjawUcltzA == X-Google-Smtp-Source: AGHT+IGXtUDjiwU+k+O1b8QLy83YB+PEH74IpmPKVR8SvzstQ4Lpdi1O1MthjAyRNKStpLUrXwXP8+epEZbqtX4qvoo= X-Received: by 2002:a92:c26b:0:b0:3d0:101e:45e7 with SMTP id e9e14a558f8ab-3d17bff67f3mr51067155ab.19.1739395686363; Wed, 12 Feb 2025 13:28:06 -0800 (PST) MIME-Version: 1.0 References: <8ac6a1dd-db82-4ea7-8186-0c354d79049f@aklaver.com> In-Reply-To: <8ac6a1dd-db82-4ea7-8186-0c354d79049f@aklaver.com> From: Marcelo Fernandes Date: Thu, 13 Feb 2025 10:27:55 +1300 X-Gm-Features: AWEUYZn3y9wbqmD7UVOTKbTmS9ohc8R0gEu376FvdW8y_hT2Ayzirrk1_hkgkhI 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 10:02=E2=80=AFAM Adrian Klaver wrote: > This needs more information: > 1) Postgres version. That would be for Postgres 15. > 2) The table definition. This is a simplified version of the original table: CREATE TABLE bookings ( id SERIAL PRIMARY KEY, resource_id INT NOT NULL, start_date DATE NOT NULL, end_date DATE NOT NULL ); > 3) The exclusion constraint definition. The copy table would have an exclusion constraint such as: ALTER TABLE bookings ADD CONSTRAINT no_date_overlap_for_resource_id EXCLUDE USING gist ( resource_id WITH =3D, daterange(start_date, end_date, '[]') WITH && ); > 4) Definition of what 'fairly large' is. This table is over 400GB > 5) How is the application interfacing with the database? This is a web application that interfaces with the database using psycopg. Also pulling in your question in the other reply: > Why can't you just add the exclusion constraint to the original table? With unique constraints, one can use a unique index to create the constrain= t concurrently. With check constraints, one can create the constraint as invalid and then validate it while only requiring a share update exclusive lock. But with exclusion constraints, neither of those techniques are available. = In that sense, there is no way to create this type of constraint in a large ta= ble without copying the original table, adding the constraint, and performing a table swap. This is done to avoid having to hold an exclusive lock for a long amount of time, thus creating application outages. Hope that clarifies the situation a bit better - Marcelo