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 1tvhE5-007BXv-1T for pgsql-general@arkaria.postgresql.org; Fri, 21 Mar 2025 18:36:13 +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 1tvhE2-00DPwe-VF for pgsql-general@arkaria.postgresql.org; Fri, 21 Mar 2025 18:36:10 +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 1tvhE2-00DPrN-JY for pgsql-general@lists.postgresql.org; Fri, 21 Mar 2025 18:36:10 +0000 Received: from mout.gmx.net ([212.227.15.18]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tvhDw-000MaF-2f for pgsql-general@lists.postgresql.org; Fri, 21 Mar 2025 18:36:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1742582162; x=1743186962; i=jimis@gmx.net; bh=UO/UXjY9LN1sfPLqvafvGMnlmpDvfQLJR0MYYYkiJ24=; h=X-UI-Sender-Class:Date:From:To:Subject:In-Reply-To:Message-ID: References:MIME-Version:Content-Type:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=HZWMV2xpS1Djo6CIt/knGqfvrUt+z+/u1dOIjNDLT+IVlvSydcywu5TmXGM0p0xd FTIpFVnu14TGQPoChlwDkwnwo2KCDrdQgyH2xY9tThzeHSAf+qxWL59b1KpHix5oY 8sjTBcsIM3A4VlnXKrOAztDAnbX+3xxoyw0U607OBwURXUeS4ISeuPTK7eaHS12Ro dzClB0jDKR2CY+8bOTlE9/qc8sOFeFAoxBG69Cbgzh/M94uc/bdAX//wxpeMpjDwg b8EyYRWGrWFfiqzpd3n1mblyVnZJ+Sw3BCg/UNUSl3uaTxqTGhDdI1vmFeNlZX4Cw RVgIKGfEQXT2nxRG+w== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [10.9.70.81] ([185.55.106.54]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MzhnH-1t08aR1oKv-00th7x for ; Fri, 21 Mar 2025 19:36:02 +0100 Date: Fri, 21 Mar 2025 19:36:00 +0100 (CET) From: Dimitrios Apostolou To: pgsql-general@lists.postgresql.org Subject: Re: Experience and feedback on pg_restore --data-only In-Reply-To: <53760c70-4a87-a453-9e02-57abc9cb2e54@gmx.net> Message-ID: <756f3204-5cb9-5c7e-642c-bc5ec78db59d@gmx.net> References: <53760c70-4a87-a453-9e02-57abc9cb2e54@gmx.net> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed X-Provags-ID: V03:K1:WG2Sjg06vwO8Y03KOy5LQp2OtWkyrM19dKFQ4Nf74guDGRTwKZS 7qJIo23bOjWJJCQGtZIcBEibc3+lKabqK0wuFH9A9U7nChtLNg8e0uh9oOJ7MvT4PTZWsyv eSloUL6DMnywDUZ5c0/ZFc0vVWLSytXODeiR5huJ8KlQF2iYv+vah/Fr3I2lHm3w/r34zyp rX51seDtanfluA7+ZfSYw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:Iqx+PaOR8kg=;MCnW3iFKm3EMD2Kdfdmg/Txb1AH Vvf/CVRAjTRph3GoGFsDGoxKasFIeb9PTA6oL/gtL3yZIX/tv1dhflK0eEHgX/toBOS0LXtXj F3CdKfkzrbTQXkQx1ADv9fVlJY3BOKLD4pP2z1uM4rdwqCceTALlIKojIxLrY/GzFSp0ctseJ TbiebPA478T62X1nPOW+zAYiAxUt/HkyX/97jOfG14O8u2SO+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2MEaSyybKMD7FV/YkPDtYBgTo5Od1q8DHBX39Rcsr1K3JRHfbO w6QGVOH7Q/J4Dmwn61wVYkTYjmpRJAT0vbdj2C87H+3MymdGWFPqfRSysGDaUwWUjMHOVUFmw 1GxgYBKu3hkWfCSbZrWRqtbACwKUhg1y0RKISuTGiStV7V3pHgeE2bg5OeITOZwO86B2eT0xX Ss6kjjQ== Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 20 Mar 2025, 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 i= n > those cases it makes often sense to get rid of the churn and create a cl= ean > 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. Another important reason I use --data-only: tablespaces. The new host has different storage configuration and tablespaces and the schema has been updated to reflect those. Therefore I must create the database using the updated schema definitions on the new host. > > 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. Thing= s > would work if pg_restore did "SET ROLE" instead of "SET SESSION > AUTHORIZATION" to switch user. Is this a straightforward change or the= re > are issues I don't see? > > * 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 i= f > pg_restore would optionally truncate before COPY. I believe it would > require superuser privilege for it, that could achieve using the > --superuser=3Dusername option used today for disabling the triggers. > > Performance issues: (important as my db size is >5TB) > > * WAL writes: I didn't manage to avoid writing to the WAL, despite havin= g > setting wal_level=3Dminimal. 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 thi= s > 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 spee= d > up the process quite a bit. > > > Any feedback for improving my process? Should I put these ideas somewher= e as > ideas for improvement on pg_restore? > > Thank you in advance, > Dimitris > >