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 1twKsr-00D1WQ-7b for pgsql-general@arkaria.postgresql.org; Sun, 23 Mar 2025 12:56:57 +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 1twKsp-00FsuI-VQ for pgsql-general@arkaria.postgresql.org; Sun, 23 Mar 2025 12:56:55 +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 1tvOgV-00AnqE-Ps for pgsql-general@lists.postgresql.org; Thu, 20 Mar 2025 22:48:19 +0000 Received: from mout.gmx.net ([212.227.15.15]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tvOgQ-000DgT-2j for pgsql-general@lists.postgresql.org; Thu, 20 Mar 2025 22:48:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1742510893; x=1743115693; i=jimis@gmx.net; bh=TN+FbodchFFK4FWx9pmW3DlRDWjQsR9M5OaB0criDrk=; h=X-UI-Sender-Class:Date:From:To:Subject:Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=CXBlT8CZMPRK6CuxefVW03D3J7R5Q6YARkZGojfbXBIBVTDvx/QrVX80NgJ7aRWR MsEvSO038cv76e2Ehvxvbi+SYz8cE2oywMInP9PpRHs3k5E/tUxOxSeEjLgeG0rIQ pVoFzMJuGcjBSdwZZDuhKNvRMAZgQsDeALX4wBZNkuKH9mdyCgs6ABg6FO3rgEcoy p8/nvGZyY7PjIcA+Hxycp/rHGv0lC7Z7vV4nBrwGHvt/Ib4cKWc5Qu9jD+0ep0WvF +gxKp4dULzJdZh8DzjtWrrb6wbQItGHGr6BcyBpwX91llCRl3PdwUpaAA0pEBY2HC K8vFKFbvlZXo6HAWdg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [10.9.70.81] ([185.55.106.54]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N4QwW-1tConr0trq-017HbP for ; Thu, 20 Mar 2025 23:48:13 +0100 Date: Thu, 20 Mar 2025 23:48:11 +0100 (CET) From: Dimitrios Apostolou To: pgsql-general@lists.postgresql.org Subject: Experience and feedback on pg_restore --data-only Message-ID: <53760c70-4a87-a453-9e02-57abc9cb2e54@gmx.net> MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII X-Provags-ID: V03:K1:6gZUuSm6POiHWClatJEO3Wl1+R+DlLH/RnMIx8LfYrSfoUvNIyb hKox84BtJzpo5pXb/8ci1xvFk5IAplDFaH7uueyy6TgOkOqG89+HnLDOnNrwGl5HY3BFV4a 8E1hKe24o3AQ7pFPpSuVp4gd6vPjsPy4ecP+lbYl2OryrSZ7X/X+iY+KDRl9Jr3w7hL8gZK HV8qNhTzKv48JDHJjqNHg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:oT5J4/+B2Bo=;WR58yGYWc+qW3FsaMo/Y5IpjXOv V96+WBi1o+sBAAZwAHPqcMNtSRyW4f67ci2Eru0uwixlwFfaFuT3JcRNT/6YBc2nvwBG1JxwU nXDrGcXvnZ5uYkPPvI8Ddh5bj0qgAlV8E+G2rjQ8V7plIX1AlPDegruconCMeNhg/YxP9S8Fw 5YfGiRhL65DJyBqj7pvXRbT2w0izJ+0oC6OINWjFl9P9eX2grMYSGAhPxlg06N18qQne6GWmu c9hHsuF80LHhwUHtTJ5BHZVxcNHhfIgBokj+HAVPdh0jL5q3SkaNuxPuxu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L84xX3dHqhgshMZgVHUfX8M4l6X4fr7L0v6VH0Qvccyb/xPe7r EuvD9+YgYomTVmoQvZByee8HOz+z8DIjIWwvbRsiZ4hSKXIyAUvPlg0YgSds5Zo3ytfzfsmx6 P51r+qQ== Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 ther= e 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 if 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 having 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 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