From 2d9ab2a9569b25f31203e6a3d07a36dacb09dfdf Mon Sep 17 00:00:00 2001 From: wuyuechuan Date: Fri, 12 Mar 2021 16:15:08 +0800 Subject: [PATCH] =?UTF-8?q?gs=5Frestore=E6=97=B6=EF=BC=8C=E6=8C=87?= =?UTF-8?q?=E5=AE=9Ause-set-session-authorization=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E6=89=A7=E8=A1=8CSET=5FSESSION=E5=89=8D=E5=85=88RESET=5FSESSIO?= =?UTF-8?q?N?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/pg_dump/pg_backup_archiver.cpp | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/bin/pg_dump/pg_backup_archiver.cpp b/src/bin/pg_dump/pg_backup_archiver.cpp index 8fdcf46fd..330b2ddfc 100755 --- a/src/bin/pg_dump/pg_backup_archiver.cpp +++ b/src/bin/pg_dump/pg_backup_archiver.cpp @@ -136,6 +136,7 @@ static void _printTocEntry(ArchiveHandle* AH, TocEntry* te, RestoreOptions* ropt static char* replace_line_endings(const char* str); static void _doSetFixedOutputState(ArchiveHandle* AH); static void _doSetSessionAuth(ArchiveHandle* AH, const char* user); +static void _doResetSessionAuth(ArchiveHandle* AH); static void _doSetWithOids(ArchiveHandle* AH, const bool withOids); static void _reconnectToDB(ArchiveHandle* AH, const char* dbname); static void _becomeUser(ArchiveHandle* AH, const char* user); @@ -2606,6 +2607,9 @@ static void _doSetFixedOutputState(ArchiveHandle* AH) */ static void _doSetSessionAuth(ArchiveHandle* AH, const char* user) { + if (RestoringToDB(AH)) { + _doResetSessionAuth(AH); + } PQExpBuffer cmd = createPQExpBuffer(); char* rolepassword = NULL; @@ -2642,6 +2646,29 @@ static void _doSetSessionAuth(ArchiveHandle* AH, const char* user) (void)destroyPQExpBuffer(cmd); } +/* + * Issue a RESET SESSION AUTHORIZATION command. Only used before _doSetSessionAuth when gs_restoring. + */ +static void _doResetSessionAuth(ArchiveHandle* AH) { + PQExpBuffer cmd = createPQExpBuffer(); + (void)appendPQExpBuffer(cmd, "RESET SESSION AUTHORIZATION;"); + if (RestoringToDB(AH)) { + PGresult* res = NULL; + + res = PQexec(AH->connection, cmd->data); + if ((res == NULL) || PQresultStatus(res) != PGRES_COMMAND_OK) + /* NOT warn_or_exit_horribly... use -O instead to skip this. */ + exit_horribly(modulename, "could not reset session: %s", PQerrorMessage(AH->connection)); + PQclear(res); + } else { + // not used + (void)ahprintf(AH, "%s\n\n", cmd->data); + } + + (void)destroyPQExpBuffer(cmd); +} + + /* * Issue a SET default_with_oids command. Caller is responsible * for updating state if appropriate.