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 1twNO4-00DYw5-Sk for pgsql-general@arkaria.postgresql.org; Sun, 23 Mar 2025 15:37: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 1twNO3-000Xib-LD for pgsql-general@arkaria.postgresql.org; Sun, 23 Mar 2025 15:37:19 +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 1twNO2-000XiS-Ov for pgsql-general@lists.postgresql.org; Sun, 23 Mar 2025 15:37:19 +0000 Received: from fhigh-a7-smtp.messagingengine.com ([103.168.172.158]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1twNNz-000h4b-07 for pgsql-general@lists.postgresql.org; Sun, 23 Mar 2025 15:37:18 +0000 Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfhigh.phl.internal (Postfix) with ESMTP id 4BB0F1140173; Sun, 23 Mar 2025 11:37:13 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Sun, 23 Mar 2025 11:37:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= 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=fm3; t=1742744233; x=1742830633; bh=qae0p7g/9oNEchom47F45Wr0qkF91pa9bYmCr3QldRU=; b= kza6DY0VfBIDBTYxe1BPbdxyZMX+6L/1d62Rolk0zfQ8Fdf7rjwbUWcXKZOFgKgs T02PY8spmx0qfVWwZHBHwPOy4HK7UGxenblpMdof1T96R3BqBqAaNfIckuRw+mY4 +GrmNwEmNiWnBO195/Gf821ewZ+qYw5XNYf+FOHcz1ksyidPyY2Ir7N1WFnfVeE5 qc65U9S5+35XWpIw4aGL8xp6nxxctm+INANN5cxXfRtbhmE1kumo8rMd/+rUMUnw BIc02okAso6/ddrGJPUyZFTTqVQJbongP8oamr7MRW8nUGkuRwPDpWCMdpXYoM1t Rei6yFLyvDAiuZCyezFSAA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=fm1; t=1742744233; x=1742830633; bh=q ae0p7g/9oNEchom47F45Wr0qkF91pa9bYmCr3QldRU=; b=aAK19YaMRINih9AWM 5x7VcIJBk2g9XEL+HPZPdD3mgPu+P8XyZwffTCxu/GFuAx9K4DPFkzAjzgx+71Zm 5r8vIfclI2vADN+Q6dxUKteO87p8BiRNTScfozi3CrqxK1L8kksYydeEX7QHD2rW 5yJnW2Ul0Np9P766gSzz6j05zUOcTcuDY7HA/N6F/Ya9JMiaBx3hzxLn8GKopv4v /kjScmg3HQSg8pV9IFGNNfHQM5iM46kLhsp7Uh3AtlhQy2Ome6MLlZWWE0UjCoO8 HBG1XvthkH51F5VTtcNJYFIdthjTfPtphgUwXlAHX87WK8qVoCsDiAJejfiSbYxA KoZvA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduheejvdehucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkff ggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghv vghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrf grthhtvghrnhepffelgeeifefgveduhedthfekuedtffejveegffegjeevtdehgfduieet feehjeehnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh eprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnhgspghrtghpthht ohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepjhhimhhishesghhmgidrnh gvthdprhgtphhtthhopehpghhsqhhlqdhgvghnvghrrghlsehlihhsthhsrdhpohhsthhg rhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 23 Mar 2025 11:37:12 -0400 (EDT) Message-ID: <9e8852ec-d8fa-4fb6-a2d3-cd188ce0744a@aklaver.com> Date: Sun, 23 Mar 2025 08:37:11 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Experience and feedback on pg_restore --data-only To: Dimitrios Apostolou , pgsql-general@lists.postgresql.org References: <53760c70-4a87-a453-9e02-57abc9cb2e54@gmx.net> Content-Language: en-US From: Adrian Klaver In-Reply-To: <53760c70-4a87-a453-9e02-57abc9cb2e54@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/20/25 15:48, Dimitrios Apostolou wrote: > Rationale: > > When restoring a backup in an emergency situation, it's fine to run > pg_restore as superuser and get an exact replica of the dumped db. > AFAICT pg_restore (without --data-only) is optimised for such case. > > But pg_dump/restore can be used as a generic data-copying utility, and in > those cases it makes often sense to get rid of the churn and 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. > > Things that made my life hard: > > * plenty of permission denials for both ALTER OWNER or SET SESSION >   AUTHORIZATION (depending on command line switches).  Both of these >   require superuser privilege, but in my case this is not really needed. >   Dbowner has CREATEROLE and is the one who creates all the roles (WITH >   SET TRUE), and their private schemata in the specific database.  Things >   would work if pg_restore did "SET ROLE" instead of "SET SESSION >   AUTHORIZATION" to switch user. Is this a straightforward change or there >   are issues I don't see? If this is --data-only what are the ALTER OWNER and SET SESSION AUTHORIZATION for? > > * After each failed attempt, I need to issue a TRUNCATE table1,table2,... >   before I try again.  I wrote my own function for that. It would help if >   pg_restore would optionally truncate before COPY.  I believe it would >   require superuser privilege for it, that could achieve using the >   --superuser=username option used today for disabling the triggers. That is what --clean is for, though it needs to have the objects(tables) be in the restore e.g. not just --data-only. > > 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. > > * Indices: Could pg_restore have a switch to DROP indices before each >   COPY, and re-CREATE them after, exactly as they were?  This would speed >   up the process quite a bit. > > > Any feedback for improving my process? Should I put these ideas somewhere > as ideas for improvement on pg_restore? > > Thank you in advance, > Dimitris > > > -- Adrian Klaver adrian.klaver@aklaver.com