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 1twkMg-00HFkb-OO for pgsql-general@arkaria.postgresql.org; Mon, 24 Mar 2025 16:09:26 +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 1twkMf-005xqd-G0 for pgsql-general@arkaria.postgresql.org; Mon, 24 Mar 2025 16:09:25 +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 1twkMf-005xpd-55 for pgsql-general@lists.postgresql.org; Mon, 24 Mar 2025 16:09:25 +0000 Received: from fhigh-b4-smtp.messagingengine.com ([202.12.124.155]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1twkMc-000u4r-1w for pgsql-general@lists.postgresql.org; Mon, 24 Mar 2025 16:09:24 +0000 Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfhigh.stl.internal (Postfix) with ESMTP id CB0C2254015A; Mon, 24 Mar 2025 12:09:20 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Mon, 24 Mar 2025 12:09:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1742832560; x=1742918960; bh=4tJC1ECjJiryqsrJWGHAKePuGJn8bEEMz69vTfdZk1E=; b= XIRkaUcsfm+fyfNyAu0I8kMb/mwvwYSW6UmWwq63vsk5iEUFpiNg9XQbD0GtyAHA zdZnEFhZMX1B948+Rp56SDyhMiCGx9J30IH4uefuHz63BCzmShANsZmmIZm24c9D wmnhDtVlja+r+wj9BxuVL4fIEDu+gp8H/WNUKXlv6w1597c3v5utW7XxpFh+psNE 9A27vyFMxizEEGsfAJSSMl3MnIacI3ENpnKnLv65toDFqvis6XCbE7qVNyQx3B8I nftPvxJeo2X7N9c2Wt0lcRxqqLDoNSPtC7KPfrj2rm3GreZBXg0hGWiIZFkHG1Mo Cr6UC55CVEBcsvZH4qsBTQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1742832560; x= 1742918960; bh=4tJC1ECjJiryqsrJWGHAKePuGJn8bEEMz69vTfdZk1E=; b=Z 6g2wr9m6Wsu8rr2VkcUhlMG0BDyjKsyNAbZ5AMUIa1YVSlQOVeZPwQVcNAgEiumE y9b0Ww/gYD3fh4DVTDIYiC191wmSW6nehKbtEafG+QTlJMDczrhKJJcZzOYXaeXc 2YW7pMTJniAxokhA6J1RRXb9Y6oyE9SBCOS1+vhY2yNEeNkIfHyUc2sV3XqKnKsb I0xrnCCdBwx+i5R0k4c6xrH9+km88aTr1clec10+riGrgSOgSIQhE7Z9kdlXxOyR FYPEzPgqCH+M2TTvNkQ440LED2kX5awcPLoJiIKRghuC5xTmKvdTBqH2FUflkPP+ T4HXaxz3Uu+/ZtXdnVjUw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduiedtvdduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepkfffgggfuffvvehfhfgjtgfgsehtkeertddt vdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepgfdufeekhfevfeel veeiueevhedvuddukeduvddvlefhueeuieejtdeuvdevvdeunecuffhomhgrihhnpehpoh hsthhgrhgvshhqlhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhep mhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpd hnsggprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehjihhm ihhssehgmhigrdhnvghtpdhrtghpthhtoheplhgruhhrvghniidrrghlsggvsegthigsvg hrthgvtgdrrghtpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlheslhhishhtshdr phhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 24 Mar 2025 12:09:19 -0400 (EDT) Message-ID: Date: Mon, 24 Mar 2025 09:09:19 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Experience and feedback on pg_restore --data-only To: Dimitrios Apostolou Cc: Laurenz Albe , pgsql-general@lists.postgresql.org References: <53760c70-4a87-a453-9e02-57abc9cb2e54@gmx.net> <455d28421ae33c73b73a6f527d2f72816ca5dd29.camel@cybertec.at> <5f1ebeda-f080-cb31-75c0-ce2211ea348f@gmx.net> <832c1cdd-c0fe-464b-b4b9-f9d0482b9b78@aklaver.com> <7e990eae-e55c-0d04-1be8-f49bb3251073@gmx.net> Content-Language: en-US From: Adrian Klaver In-Reply-To: <7e990eae-e55c-0d04-1be8-f49bb3251073@gmx.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/24/25 08:51, Dimitrios Apostolou wrote: > On Mon, 24 Mar 2025, Adrian Klaver wrote: > >> On 3/24/25 07:24, Dimitrios Apostolou wrote: >>>  On Sun, 23 Mar 2025, Laurenz Albe wrote: >>> >>>>  On Thu, 2025-03-20 at 23:48 +0100, Dimitrios Apostolou wrote: >>>>>  Performance issues: (important as my db size is >5TB) >>>>> >>>>>  * WAL writes: I didn't manage to avoid writing to the WAL, despite >>>>>  having >>>>>     setting wal_level=minimal. I even wrote my own function to >>>>> ALTER all >>>>>     tables to UNLOGGED, but failed with "could not change table T to >>>>>     unlogged because it references logged table".  I'm out of ideas on >>>>>  this >>>>>     one. >>>> >>>>  You'd have to create an load the table in the same transaction, >>>> that is, >>>>  you'd have to run pg_restore with --single-transaction. >>> >>>  That would restore the schema from the dump, while I want to create the >>>  schema from the SQL code in version control. >> >> >> I am not following, from your original post: >> >> " >> ... create a >> clean database by running the SQL schema definition from version >> control, and >> then copy the data for only the tables created. >> >> For this case, I choose to run pg_restore --data-only, and run it as >> the user >> who owns the database (dbowner), not as a superuser, in order to avoid >> changes being introduced under the radar. >> " >> >> You are running the process in two steps, where the first does not >> involve >> pg_restore. Not sure why doing the pg_restore --data-only portion in >> single >> transaction is not possible? > > Laurenz informed me that I could avoid writing to the WAL if I "create and > load the table in a single transaction". From here: https://www.postgresql.org/message-id/455d28421ae33c73b73a6f527d2f72816ca5dd29.camel%40cybertec.at What he said was: "You'd have to create an load the table in the same transaction, that is, you'd have to run pg_restore with --single-transaction." Where I assume he meant '... create and load ...'. That is not the same as what you are doing below. > I haven't tried, but here is what I would do to try --single-transaction: > > Transaction 1: manually issuing all of CREATE TABLE etc. > > Transaction 2: pg_restore --single-transaction --data-only > > The COPY command in transaction 2 would still need to write to WAL, since > it's separate from the CREATE TABLE. > > Am I wrong somewhere? > >> COPY is all or none(version 17+ caveat(see >> https://www.postgresql.org/docs/current/sql-copy.html  ON_ERROR)), so >> if the >> data dump fails in --single-transaction everything rolls back. > > So if I restore all tables, then an error about a "table not found" would > not roll back already copied tables, since it's not part of a COPY? If you are following what you show above then the tables and other objects would be created manually from the version control outside of pg_restore and on successful completion and commit of that transaction they would persist until such time as you change them. The second step pg_restore --single-transaction --data-only is where you could 99% of the way through and have a failure that rolls back all the data entered in the tables. > > > Thank you for the feedback, > Dimitris > -- Adrian Klaver adrian.klaver@aklaver.com