Hi All,

I realize using foreign data wrappers with transaction pooling may not be
strictly supported, but for the most part they work. I am however
occasionally noticing errors stemming from the prepared statement names
created by the fdw modify code colliding between sessions/DBs.

Would the development team be open to a patch which somehow makes this less
likely? Something like the attached patch works, but probably isn't ideal?
Perhaps there is a better unique identifier I can use here. I am very new
to the postgres codebase.


Best
*Marco Montagna*
From c63630d5ebf3dd10fdf4de2faafb741acf71e6e0 Mon Sep 17 00:00:00 2001
From: Marco Montagna <ma...@mightysignal.com>
Date: Thu, 28 Jan 2021 23:55:38 -0800
Subject: [PATCH] Reduce likelihood of fdw prepared statement collisions.

---
 contrib/postgres_fdw/postgres_fdw.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 2f2d4d1..c826d6e 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -3725,9 +3725,14 @@ prepare_foreign_modify(PgFdwModifyState *fmstate)
 	char	   *p_name;
 	PGresult   *res;
 
-	/* Construct name we'll use for the prepared statement. */
-	snprintf(prep_name, sizeof(prep_name), "pgsql_fdw_prep_%u",
-			 GetPrepStmtNumber(fmstate->conn));
+	/* Construct name we'll use for the prepared statement.
+	 *
+	 * Prepend a random number to reduce the likelihood of
+	 * prepared statement collisions when using transaction pooling.
+	 */
+	snprintf(prep_name, sizeof(prep_name), "pgsql_fdw_prep_%u_%u",
+			 GetPrepStmtNumber(fmstate->conn),
+			 rand());
 	p_name = pstrdup(prep_name);
 
 	/*
-- 
2.7.4

Reply via email to