From 2e5a5a5f0da0a020951c3dd7b5b55aae0ff78820 Mon Sep 17 00:00:00 2001 From: Masahiko Sawada Date: Wed, 23 Sep 2020 16:17:11 +0900 Subject: [PATCH v26 09/11] postgres_fdw supports automatically resolution. Co-authored-by: Masahiko Sawada, Ashutosh Bapat --- contrib/postgres_fdw/connection.c | 17 ++++++++++++++++- contrib/postgres_fdw/postgres_fdw.c | 2 ++ contrib/postgres_fdw/postgres_fdw.h | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index fa8aa6d5df..f7951afe73 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -58,6 +58,7 @@ typedef struct ConnCacheEntry bool have_error; /* have any subxacts aborted in this xact? */ bool changing_xact_state; /* xact state change in process */ bool invalidated; /* true if reconnect is pending */ + bool modified; /* true if data on the foreign server is modified */ uint32 server_hashvalue; /* hash value of foreign server OID */ uint32 mapping_hashvalue; /* hash value of user mapping OID */ } ConnCacheEntry; @@ -202,6 +203,20 @@ GetConnectionCacheEntry(Oid umid) return entry; } +void +MarkConnectionModified(UserMapping *user) +{ + ConnCacheEntry *entry; + + entry = GetConnectionCacheEntry(user->umid); + + if (entry && !entry->modified) + { + FdwXactRegisterXact(user->serverid, user->userid, true); + entry->modified = true; + } +} + /* * Connect to remote server using specified server and user mapping properties. @@ -493,7 +508,7 @@ begin_remote_xact(ConnCacheEntry *entry, UserMapping *user) entry->conn); /* Register the foreign server to the transaction */ - FdwXactRegisterXact(user->serverid, user->userid); + FdwXactRegisterXact(user->serverid, user->userid, false); if (IsolationIsSerializable()) sql = "START TRANSACTION ISOLATION LEVEL SERIALIZABLE"; diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index caccad9846..b072cb42f3 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -2377,6 +2377,7 @@ postgresBeginDirectModify(ForeignScanState *node, int eflags) * establish new connection if necessary. */ dmstate->conn = GetConnection(user, false); + MarkConnectionModified(user); /* Update the foreign-join-related fields. */ if (fsplan->scan.scanrelid == 0) @@ -3571,6 +3572,7 @@ create_foreign_modify(EState *estate, /* Open connection; report that we'll create a prepared statement. */ fmstate->conn = GetConnection(user, true); + MarkConnectionModified(user); fmstate->p_name = NULL; /* prepared statement not made yet */ /* Set up remote query information. */ diff --git a/contrib/postgres_fdw/postgres_fdw.h b/contrib/postgres_fdw/postgres_fdw.h index 659222b97a..12cd55258f 100644 --- a/contrib/postgres_fdw/postgres_fdw.h +++ b/contrib/postgres_fdw/postgres_fdw.h @@ -132,6 +132,7 @@ extern void reset_transmission_modes(int nestlevel); /* in connection.c */ extern PGconn *GetConnection(UserMapping *user, bool will_prep_stmt); extern void ReleaseConnection(PGconn *conn); +extern void MarkConnectionModified(UserMapping *user); extern unsigned int GetCursorNumber(PGconn *conn); extern unsigned int GetPrepStmtNumber(PGconn *conn); extern PGresult *pgfdw_get_result(PGconn *conn, const char *query); -- 2.23.0