delta-0002-31march-pg_restore-skip-error-for-CRETE-ROLE-username.noci
application/octet-stream
Filename: delta-0002-31march-pg_restore-skip-error-for-CRETE-ROLE-username.noci
Type: application/octet-stream
Part: 1
Message:
Re: Non-text mode for pg_dumpall
From d75856b200e5ab707f36119b79b258b5e3baa7c7 Mon Sep 17 00:00:00 2001
From: Mahendra Singh Thalor <mahi6run@gmail.com>
Date: Mon, 31 Mar 2025 21:39:26 +0530
Subject: [PATCH] pg_restore: skip error for CRETE ROLE username
---
---
---
src/bin/pg_dump/pg_restore.c | 25 +++++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index d8037da653f..4800527a2f3 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -542,8 +542,7 @@ main(int argc, char **argv)
* Open global.dat file and execute/append all the global sql
* commands.
*/
- n_errors = process_global_sql_commands(conn, inputFileSpec,
- opts->filename);
+ n_errors = process_global_sql_commands(conn, inputFileSpec, opts->filename);
if (conn)
PQfinish(conn);
@@ -1284,8 +1283,13 @@ process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *o
char global_file_path[MAXPGPATH];
PGresult *result;
StringInfoData sqlstatement;
+ StringInfoData rolesqlstatement;
FILE *pfile;
int n_errors = 0;
+ bool check_role_cmd = true;
+
+ /* Should have valid connection. */
+ Assert(conn);
snprintf(global_file_path, MAXPGPATH, "%s/global.dat", dumpdirpath);
@@ -1308,9 +1312,26 @@ process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *o
/* Init sqlstatement to append commands. */
initStringInfo(&sqlstatement);
+ /* Preapre "CREATE ROLE username" command. */
+ initStringInfo(&rolesqlstatement);
+ appendStringInfoString(&rolesqlstatement, "\n\n--\n-- Roles\n--\n\nCREATE ROLE ");
+ appendStringInfoString(&rolesqlstatement, PQuser(conn));
+ appendStringInfoString(&rolesqlstatement, ";");
+
/* Process file till EOF and execute sql statements. */
while (read_one_statement(&sqlstatement, pfile) != EOF)
{
+
+ /*
+ * If this command is for "CREATE ROLE username", then skip this as
+ * current user is already created.
+ */
+ if (check_role_cmd && (strcmp(sqlstatement.data, rolesqlstatement.data) == 0))
+ {
+ check_role_cmd = false;
+ continue;
+ }
+
pg_log_info("executing query: %s", sqlstatement.data);
result = PQexec(conn, sqlstatement.data);
--
2.39.3