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 1sIEU5-00E4Gj-Js for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Jun 2024 21:29:21 +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 1sIEU3-006NSm-8L for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Jun 2024 21:29:20 +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 1sIEU2-006NSe-QV for pgsql-hackers@lists.postgresql.org; Fri, 14 Jun 2024 21:29:19 +0000 Received: from mail-io1-xd34.google.com ([2607:f8b0:4864:20::d34]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sIEU0-001Kun-N5 for pgsql-hackers@postgresql.org; Fri, 14 Jun 2024 21:29:18 +0000 Received: by mail-io1-xd34.google.com with SMTP id ca18e2360f4ac-7eb75c0d0a7so86210439f.3 for ; Fri, 14 Jun 2024 14:29:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718400556; x=1719005356; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=a+p/fIZbD9LoVY9ECVw5WMWpTuKpwM3AQp+iwjn7xkw=; b=WCSRxAnuDuRYKVi9tM6rwZJU8CPoxuJxHS9LSQ95hspQdK71z61noE2xk8X2R0j++C kEFTLyVhCg3kRPtPOlSKsNWIte5XSgtExvrxOcxo8hdEAs6MD3bbPd9qxe/2MDSFEjGZ NDML74Pa3XTdL3Al4uaxCSoFfZkmKMLMntVYRoEo2IEmccF95KJiC/IZghXSp7LFrrFx 4udLl2wPZC/07bDSYLoqPhJkwhyEo/k0psu64J9yd4qIhBkkW6KORPpzPaEUfBoX7oeL zqNHTAU2gWDC7Ef2GM64lukFCAYN1IxWVXsNpLruf+xhQACHVXkgFNh2s6xNfFokMAND KAUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718400556; x=1719005356; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=a+p/fIZbD9LoVY9ECVw5WMWpTuKpwM3AQp+iwjn7xkw=; b=tkv7vAsRT55O1DrIORJESw7wbM3rTpJTB7Mdr1IcmXIZ4ce8rR8NJwq/AauhmXhvGL KyCwKOf6Y0t9TG/Q111B678GQvVYvO12Ie93+9Ky3xdtbNg0k+j7i9fdxDGeHsVQ7j7B VzYE4KiMWA+DG/HEQvAuYHJ7Ozp1UJ7qDEEndrmKxhmkruVqrh60o6a4Vaw8XXT2ATZP IBwNahVQtH8vlTwt6yUFOb7GexMmSpbRHr5FfaGcJWi4KpqJFkj100StTJ202todOiS6 2092FdIDd/wmcv+6Zs5h+hC5T3x4BW7Tf/Wbba4eb8guOsZ9NPHoSpTiePBw/SeYuAgU iGOQ== X-Forwarded-Encrypted: i=1; AJvYcCUZq7S5x8znyfkrmluh+OZooP2z4L+Se41ZtRr7XbQxIdwfux1GmBzdMKWuku+2bwPOksWKsCI2uCJOpF4sUd6Rq5M0ParvHFCmoeBr X-Gm-Message-State: AOJu0Yy8VveJ8iBqgi46Kzp1PiGPUavZQze4xcW88dUyI9rDiHys7lhA bQqUtPDylW5pH3zX6KEfBIfK7ga4X3M2/N8PK9VT0a+JRlYbJe60 X-Google-Smtp-Source: AGHT+IFeD46X9NG8EAY7W6BIIesDjRaO2kAWtWJC4mnH6vRF2RtfedfCUCZxsw+IJwT0nDHxkP7MnA== X-Received: by 2002:a05:6602:150f:b0:7eb:8015:3ee1 with SMTP id ca18e2360f4ac-7ebeb4986dbmr437281239f.1.1718400555878; Fri, 14 Jun 2024 14:29:15 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4b9569ef5e5sm1099318173.112.2024.06.14.14.29.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 14:29:15 -0700 (PDT) Date: Fri, 14 Jun 2024 16:29:13 -0500 From: Nathan Bossart To: Matthias van de Meent Cc: Dilip Kumar , Ranier Vilela , pgsql-hackers@postgresql.org Subject: Re: use CREATE DATABASE STRATEGY = FILE_COPY in pg_upgrade Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="4LlVI0i1qmcRRH9U" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --4LlVI0i1qmcRRH9U Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Jun 12, 2024 at 09:41:01AM -0500, Nathan Bossart wrote: > Actually, I think you are right that we need a manual checkpoint, except I > think we need it to be after prepare_new_globals(). set_frozenxids() > connects to each database (including template0) and updates a bunch of > pg_class rows, and we probably want those on disk before we start copying > the files to create all the user's databases. Here is an updated patch. -- nathan --4LlVI0i1qmcRRH9U Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-Use-CREATE-DATABASE-.-STRATEGY-FILE_COPY-in-pg_up.patch" From c3aeed33b351475896c2b4002cff125f1edeeb42 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Fri, 14 Jun 2024 16:25:04 -0500 Subject: [PATCH v3 1/1] Use CREATE DATABASE ... STRATEGY = FILE_COPY in pg_upgrade. While the strategy is considered very costly due to expensive checkpoints when the target system has configured large shared_buffers, the issues that those checkpoints protect against don't apply in a pg_upgrade system. By skipping the checkpoints during CREATE DATABASE ... STRATEGY=FILE_COPY and using that strategy for pg_upgrade, we can save a lot of time and IO that would otherwise be spent on reading pages from disk, generating WAL records, writing those WAL records to disk, and then writing the pages to disk - which has the potential of at least double the IO of FILE_COPY. Co-authored-by: Matthias van de Meent Reviewed-by: Ranier Vilela, Dilip Kumar Discussion: https://postgr.es/m/Zl9ta3FtgdjizkJ5%40nathan --- src/backend/commands/dbcommands.c | 18 +++++++++++++++--- src/bin/pg_dump/pg_dump.c | 9 ++++++++- src/bin/pg_upgrade/pg_upgrade.c | 12 ++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index be629ea92c..99d7a9ba3c 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -563,9 +563,14 @@ CreateDatabaseUsingFileCopy(Oid src_dboid, Oid dst_dboid, Oid src_tsid, * happened while we're copying files, a file might be deleted just when * we're about to copy it, causing the lstat() call in copydir() to fail * with ENOENT. + * + * In binary upgrade mode, we can skip this checkpoint because we are + * careful to ensure that template0 is fully written to disk prior to any + * CREATE DATABASE commands. */ - RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | - CHECKPOINT_WAIT | CHECKPOINT_FLUSH_ALL); + if (!IsBinaryUpgrade) + RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | + CHECKPOINT_WAIT | CHECKPOINT_FLUSH_ALL); /* * Iterate through all tablespaces of the template database, and copy each @@ -657,10 +662,17 @@ CreateDatabaseUsingFileCopy(Oid src_dboid, Oid dst_dboid, Oid src_tsid, * seem to be much we can do about that except document it as a * limitation. * + * In binary upgrade mode, we can skip this checkpoint because neither of + * these problems applies: we don't ever replay the WAL generated during + * pg_upgrade, and we don't concurrently modify template0 (not to mention + * that trying to take a backup during pg_upgrade is pointless). + * * See CreateDatabaseUsingWalLog() for a less cheesy CREATE DATABASE * strategy that avoids these problems. */ - RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | CHECKPOINT_WAIT); + if (!IsBinaryUpgrade) + RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_FORCE | + CHECKPOINT_WAIT); } /* diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index e324070828..1e51a63442 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -3123,10 +3123,17 @@ dumpDatabase(Archive *fout) * since those can't be altered later. Other DB properties are left to * the DATABASE PROPERTIES entry, so that they can be applied after * reconnecting to the target DB. + * + * For binary upgrade, we use the FILE_COPY strategy because testing has + * shown it to be faster. When the server is in binary upgrade mode, it + * will also skip the checkpoints normally triggered by CREATE DATABASE + * STRATEGY = FILE_COPY. */ if (dopt->binary_upgrade) { - appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u", + appendPQExpBuffer(creaQry, + "CREATE DATABASE %s WITH TEMPLATE = template0 " + "OID = %u STRATEGY = FILE_COPY", qdatname, dbCatId.oid); } else diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c index af370768b6..45f095a230 100644 --- a/src/bin/pg_upgrade/pg_upgrade.c +++ b/src/bin/pg_upgrade/pg_upgrade.c @@ -534,9 +534,21 @@ static void create_new_objects(void) { int dbnum; + PGconn *conn_new_template1; prep_status_progress("Restoring database schemas in the new cluster"); + /* + * Ensure that any changes to template0 are fully written out to disk + * prior to restoring the databases. This is necessary because we use the + * FILE_COPY strategy to create the databases (which testing has shown to + * be faster), and we skip the checkpoints that this strategy ordinarily + * incurs. + */ + conn_new_template1 = connectToServer(&new_cluster, "template1"); + PQclear(executeQueryOrDie(conn_new_template1, "CHECKPOINT")); + PQfinish(conn_new_template1); + /* * We cannot process the template1 database concurrently with others, * because when it's transiently dropped, connection attempts would fail. -- 2.39.3 (Apple Git-146) --4LlVI0i1qmcRRH9U--