delta-0001-pg_restore-skip-error-if-db-already-created.patch
application/octet-stream
Filename: delta-0001-pg_restore-skip-error-if-db-already-created.patch
Type: application/octet-stream
Part: 1
Message:
Re: Non-text mode for pg_dumpall
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch 0001
Subject: pg_restore: if database is already created, then set createdb as 0
| File | + | − |
|---|---|---|
| src/bin/pg_dump/pg_restore.c | 38 | 0 |
From eb1bd481d4216bb27db227410cc48edc4bf2634e Mon Sep 17 00:00:00 2001
From: Mahendra Singh Thalor <mahi6run@gmail.com>
Date: Mon, 31 Mar 2025 14:01:41 +0530
Subject: [PATCH] pg_restore: if database is already created, then set createdb
as 0
If database is already created, then set createdb as 0 so that user
will not get errors.
Also reset some flags for each database.
as dumpData, dumpSchema, dumpStatistics
---
src/bin/pg_dump/pg_restore.c | 38 ++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index ce70b7e12b2..3d8be43241d 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -1107,6 +1107,14 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
int num_total_db;
int n_errors_total;
int count = 0;
+ char *connected_db = NULL;
+ bool dumpData = opts->dumpData;
+ bool dumpSchema = opts->dumpSchema;
+ bool dumpStatistics = opts->dumpSchema;
+
+ /* Save db name. */
+ if (opts->cparams.dbname)
+ connected_db = pg_strdup(opts->cparams.dbname);
num_total_db = get_dbname_oid_list_from_mfile(dumpdirpath, &dbname_oid_list);
@@ -1209,9 +1217,39 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath,
pg_log_info("restoring database \"%s\"", db_cell->str);
+ /* If database is already created, then don't set createDB flag. */
+ if (opts->cparams.dbname)
+ {
+ conn = ConnectDatabase(db_cell->str, NULL, opts->cparams.pghost,
+ opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
+ false, progname, NULL, NULL, NULL, NULL);
+
+ if (conn)
+ {
+ opts->createDB = 0;
+ PQfinish(conn);
+
+ /* Use already created database for connection. */
+ if (opts->cparams.dbname)
+ opts->cparams.dbname = pg_strdup(db_cell->str);
+ }
+ }
+
/* Restore single database. */
n_errors = restoreOneDatabase(subdirpath, opts, numWorkers, true, count);
+ /* Set opts->createDB flag. */
+ if (opts->createDB == 0)
+ {
+ opts->createDB = 1;
+ opts->cparams.dbname = pg_strdup(connected_db);
+ }
+
+ /* Reset flags for next database. */
+ opts->dumpData = dumpData;
+ opts->dumpSchema = dumpSchema;
+ opts->dumpStatistics = dumpStatistics;
+
/* Print a summary of ignored errors during single database restore. */
if (n_errors)
{
--
2.39.3