pg.ddx.io pgsql-committers@postgresql.org mailing list archive
help / color / mirror / Atom feedpgsql: postgres_fdw: reestablish new connection if cached one is detect
13+ messages / 4 participants
[nested] [flat]
Commits from this thread:
2020-10-16 7fc1a81e49 postgres_fdw: Restructure connection retry logic.
2020-10-10 85d08b8b72 Band-aid new postgres_fdw test case to remove error text dependency.
* pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-06 01:52 Fujii Masao <fujii@postgresql.org>
0 siblings, 1 reply; 13+ messages in thread
From: Fujii Masao @ 2020-10-06 01:52 UTC (permalink / raw)
To: pgsql-committers@lists.postgresql.org
postgres_fdw: reestablish new connection if cached one is detected as broken.
In postgres_fdw, once remote connections are established, they are cached
and re-used for subsequent queries and transactions. There can be some
cases where those cached connections are unavaiable, for example,
by the restart of remote server. In these cases, previously an error was
reported and the query accessing to remote server failed if new remote
transaction failed to start because the cached connection was broken.
This commit improves postgres_fdw so that new connection is remade
if broken connection is detected when starting new remote transaction.
This is useful to avoid unnecessary failure of queries when connection is
broken but can be reestablished.
Author: Bharath Rupireddy, tweaked a bit by Fujii Masao
Reviewed-by: Ashutosh Bapat, Tatsuhito Kasahara, Fujii Masao
Discussion: https://postgr.es/m/CALj2ACUAi23vf1WiHNar_LksM9EDOWXcbHCo-fD4Mbr1d=78YQ@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/32a9c0bdf493cf5fc029ab44a22384d547290eff
Modified Files
--------------
contrib/postgres_fdw/connection.c | 54 ++++++++++++++++++++------
contrib/postgres_fdw/expected/postgres_fdw.out | 48 +++++++++++++++++++++++
contrib/postgres_fdw/sql/postgres_fdw.sql | 41 +++++++++++++++++++
3 files changed, 131 insertions(+), 12 deletions(-)
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-07 02:13 Michael Paquier <michael@paquier.xyz>
parent: Fujii Masao <fujii@postgresql.org>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2020-10-07 02:13 UTC (permalink / raw)
To: Fujii Masao <fujii@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org
Hi Fujii-san,
On Tue, Oct 06, 2020 at 01:52:55AM +0000, Fujii Masao wrote:
> postgres_fdw: reestablish new connection if cached one is detected as broken.
>
> In postgres_fdw, once remote connections are established, they are cached
> and re-used for subsequent queries and transactions. There can be some
> cases where those cached connections are unavaiable, for example,
> by the restart of remote server. In these cases, previously an error was
> reported and the query accessing to remote server failed if new remote
> transaction failed to start because the cached connection was broken.
>
> This commit improves postgres_fdw so that new connection is remade
> if broken connection is detected when starting new remote transaction.
> This is useful to avoid unnecessary failure of queries when connection is
> broken but can be reestablished.
lorikeet is telling that the test introduced by this commit is
unstable:
https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=lorikeet&dt=2020-10-06%2008%3A28%3A36
Some details:
BEGIN;
SELECT 1 FROM ft1 LIMIT 1;
- ?column?
-----------
- 1
-(1 row)
-
+ERROR: could not receive data from server: Software caused connection abort
+CONTEXT: remote SQL command: START TRANSACTION ISOLATION LEVEL REPEATABLE READ
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-07 03:54 Fujii Masao <masao.fujii@oss.nttdata.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Fujii Masao @ 2020-10-07 03:54 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org
On 2020/10/07 11:13, Michael Paquier wrote:
> Hi Fujii-san,
>
> On Tue, Oct 06, 2020 at 01:52:55AM +0000, Fujii Masao wrote:
>> postgres_fdw: reestablish new connection if cached one is detected as broken.
>>
>> In postgres_fdw, once remote connections are established, they are cached
>> and re-used for subsequent queries and transactions. There can be some
>> cases where those cached connections are unavaiable, for example,
>> by the restart of remote server. In these cases, previously an error was
>> reported and the query accessing to remote server failed if new remote
>> transaction failed to start because the cached connection was broken.
>>
>> This commit improves postgres_fdw so that new connection is remade
>> if broken connection is detected when starting new remote transaction.
>> This is useful to avoid unnecessary failure of queries when connection is
>> broken but can be reestablished.
>
> lorikeet is telling that the test introduced by this commit is
> unstable:
> https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=lorikeet&dt=2020-10-06%2008%3A28%3A36
Thanks for letting me know this!
>
> Some details:
> BEGIN;
> SELECT 1 FROM ft1 LIMIT 1;
> - ?column?
> -----------
> - 1
> -(1 row)
> -
> +ERROR: could not receive data from server: Software caused connection abort
> +CONTEXT: remote SQL command: START TRANSACTION ISOLATION LEVEL REPEATABLE READ
This error means that new connection was successfully reestablished
after the cached connection was terminated, and then the above connection
error occurred when issuing "START TRANSACTION" command on that
new connection. There seems no suspicious relevant log messages in the
logfile. So I'm not sure why this error happened, yet.
Per the previous discusson at [1], lorikeet sometimes seems to cause
connection-relation failure in the regression test. So the cause of error
that we faced today also may be lorikeet itself.
[1]
https://www.postgresql.org/message-id/CA+hUKGL3Son9iAeqgjPbXCpU_6hhZhw9X24uNO14mOC4bG0cCA@mail.gmail...
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-07 13:25 Fujii Masao <masao.fujii@oss.nttdata.com>
parent: Fujii Masao <masao.fujii@oss.nttdata.com>
0 siblings, 1 reply; 13+ messages in thread
From: Fujii Masao @ 2020-10-07 13:25 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org
On 2020/10/07 12:54, Fujii Masao wrote:
>
>
> On 2020/10/07 11:13, Michael Paquier wrote:
>> Hi Fujii-san,
>>
>> On Tue, Oct 06, 2020 at 01:52:55AM +0000, Fujii Masao wrote:
>>> postgres_fdw: reestablish new connection if cached one is detected as broken.
>>>
>>> In postgres_fdw, once remote connections are established, they are cached
>>> and re-used for subsequent queries and transactions. There can be some
>>> cases where those cached connections are unavaiable, for example,
>>> by the restart of remote server. In these cases, previously an error was
>>> reported and the query accessing to remote server failed if new remote
>>> transaction failed to start because the cached connection was broken.
>>>
>>> This commit improves postgres_fdw so that new connection is remade
>>> if broken connection is detected when starting new remote transaction.
>>> This is useful to avoid unnecessary failure of queries when connection is
>>> broken but can be reestablished.
>>
>> lorikeet is telling that the test introduced by this commit is
>> unstable:
>> https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=lorikeet&dt=2020-10-06%2008%3A28%3A36
>
> Thanks for letting me know this!
>
>>
>> Some details:
>> BEGIN;
>> SELECT 1 FROM ft1 LIMIT 1;
>> - ?column?
>> -----------
>> - 1
>> -(1 row)
>> -
>> +ERROR: could not receive data from server: Software caused connection abort
>> +CONTEXT: remote SQL command: START TRANSACTION ISOLATION LEVEL REPEATABLE READ
>
> This error means that new connection was successfully reestablished
> after the cached connection was terminated, and then the above connection
> error occurred when issuing "START TRANSACTION" command on that
> new connection. There seems no suspicious relevant log messages in the
> logfile. So I'm not sure why this error happened, yet.
>
> Per the previous discusson at [1], lorikeet sometimes seems to cause
> connection-relation failure in the regression test. So the cause of error
> that we faced today also may be lorikeet itself.
Since it's not good to keep the buildfarm member red, I will revert
the commit unless I come up with something even after further
investigation.
My current just guess is that PQstatus(conn) doesn't indicate
CONNECTION_BAD when the above error occurs, and which
prevents new connection from being reestablished because of
the following check.
+ if (PQstatus(entry->conn) != CONNECTION_BAD ||
+ entry->xact_depth > 0 ||
+ retry_conn)
+ PG_RE_THROW();
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-07 15:48 Fujii Masao <masao.fujii@oss.nttdata.com>
parent: Fujii Masao <masao.fujii@oss.nttdata.com>
0 siblings, 1 reply; 13+ messages in thread
From: Fujii Masao @ 2020-10-07 15:48 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org
On 2020/10/07 22:25, Fujii Masao wrote:
>
>
> On 2020/10/07 12:54, Fujii Masao wrote:
>>
>>
>> On 2020/10/07 11:13, Michael Paquier wrote:
>>> Hi Fujii-san,
>>>
>>> On Tue, Oct 06, 2020 at 01:52:55AM +0000, Fujii Masao wrote:
>>>> postgres_fdw: reestablish new connection if cached one is detected as broken.
>>>>
>>>> In postgres_fdw, once remote connections are established, they are cached
>>>> and re-used for subsequent queries and transactions. There can be some
>>>> cases where those cached connections are unavaiable, for example,
>>>> by the restart of remote server. In these cases, previously an error was
>>>> reported and the query accessing to remote server failed if new remote
>>>> transaction failed to start because the cached connection was broken.
>>>>
>>>> This commit improves postgres_fdw so that new connection is remade
>>>> if broken connection is detected when starting new remote transaction.
>>>> This is useful to avoid unnecessary failure of queries when connection is
>>>> broken but can be reestablished.
>>>
>>> lorikeet is telling that the test introduced by this commit is
>>> unstable:
>>> https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=lorikeet&dt=2020-10-06%2008%3A28%3A36
>>
>> Thanks for letting me know this!
>>
>>>
>>> Some details:
>>> BEGIN;
>>> SELECT 1 FROM ft1 LIMIT 1;
>>> - ?column?
>>> -----------
>>> - 1
>>> -(1 row)
>>> -
>>> +ERROR: could not receive data from server: Software caused connection abort
>>> +CONTEXT: remote SQL command: START TRANSACTION ISOLATION LEVEL REPEATABLE READ
>>
>> This error means that new connection was successfully reestablished
>> after the cached connection was terminated, and then the above connection
>> error occurred when issuing "START TRANSACTION" command on that
>> new connection. There seems no suspicious relevant log messages in the
>> logfile. So I'm not sure why this error happened, yet.
>>
>> Per the previous discusson at [1], lorikeet sometimes seems to cause
>> connection-relation failure in the regression test. So the cause of error
>> that we faced today also may be lorikeet itself.
>
> Since it's not good to keep the buildfarm member red, I will revert
> the commit unless I come up with something even after further
> investigation.
>
> My current just guess is that PQstatus(conn) doesn't indicate
> CONNECTION_BAD when the above error occurs, and which
> prevents new connection from being reestablished because of
> the following check.
>
> + if (PQstatus(entry->conn) != CONNECTION_BAD ||
> + entry->xact_depth > 0 ||
> + retry_conn)
> + PG_RE_THROW();
The error message in discussion is reported when recv() fails and
errno=ECONNABORTED. As far as I read the code, pqReadData() marks
the connection as CONNECTION_BAD when errno=ECONNRESET,
but not when errno=ECONNABORTED. So since PQstatus(entry->conn)
doesn't indicate CONNECTION_BAD in ECONNABORTED case,
the above check is passed through, an error is re-thrown and
new connection is not reestablished.
Therefore, the easy fix is to make libpq mark the connection as
CONNECTION_BAD even in ECONNABORTED, like we do in ECONNRESET.
But is it safe to do that? Thought?
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-08 03:43 Fujii Masao <masao.fujii@oss.nttdata.com>
parent: Fujii Masao <masao.fujii@oss.nttdata.com>
0 siblings, 1 reply; 13+ messages in thread
From: Fujii Masao @ 2020-10-08 03:43 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org
On 2020/10/08 0:48, Fujii Masao wrote:
>
>
> On 2020/10/07 22:25, Fujii Masao wrote:
>>
>>
>> On 2020/10/07 12:54, Fujii Masao wrote:
>>>
>>>
>>> On 2020/10/07 11:13, Michael Paquier wrote:
>>>> Hi Fujii-san,
>>>>
>>>> On Tue, Oct 06, 2020 at 01:52:55AM +0000, Fujii Masao wrote:
>>>>> postgres_fdw: reestablish new connection if cached one is detected as broken.
>>>>>
>>>>> In postgres_fdw, once remote connections are established, they are cached
>>>>> and re-used for subsequent queries and transactions. There can be some
>>>>> cases where those cached connections are unavaiable, for example,
>>>>> by the restart of remote server. In these cases, previously an error was
>>>>> reported and the query accessing to remote server failed if new remote
>>>>> transaction failed to start because the cached connection was broken.
>>>>>
>>>>> This commit improves postgres_fdw so that new connection is remade
>>>>> if broken connection is detected when starting new remote transaction.
>>>>> This is useful to avoid unnecessary failure of queries when connection is
>>>>> broken but can be reestablished.
>>>>
>>>> lorikeet is telling that the test introduced by this commit is
>>>> unstable:
>>>> https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=lorikeet&dt=2020-10-06%2008%3A28%3A36
>>>
>>> Thanks for letting me know this!
>>>
>>>>
>>>> Some details:
>>>> BEGIN;
>>>> SELECT 1 FROM ft1 LIMIT 1;
>>>> - ?column?
>>>> -----------
>>>> - 1
>>>> -(1 row)
>>>> -
>>>> +ERROR: could not receive data from server: Software caused connection abort
>>>> +CONTEXT: remote SQL command: START TRANSACTION ISOLATION LEVEL REPEATABLE READ
>>>
>>> This error means that new connection was successfully reestablished
>>> after the cached connection was terminated, and then the above connection
>>> error occurred when issuing "START TRANSACTION" command on that
>>> new connection. There seems no suspicious relevant log messages in the
>>> logfile. So I'm not sure why this error happened, yet.
>>>
>>> Per the previous discusson at [1], lorikeet sometimes seems to cause
>>> connection-relation failure in the regression test. So the cause of error
>>> that we faced today also may be lorikeet itself.
>>
>> Since it's not good to keep the buildfarm member red, I will revert
>> the commit unless I come up with something even after further
>> investigation.
>>
>> My current just guess is that PQstatus(conn) doesn't indicate
>> CONNECTION_BAD when the above error occurs, and which
>> prevents new connection from being reestablished because of
>> the following check.
>>
>> + if (PQstatus(entry->conn) != CONNECTION_BAD ||
>> + entry->xact_depth > 0 ||
>> + retry_conn)
>> + PG_RE_THROW();
>
> The error message in discussion is reported when recv() fails and
> errno=ECONNABORTED. As far as I read the code, pqReadData() marks
> the connection as CONNECTION_BAD when errno=ECONNRESET,
> but not when errno=ECONNABORTED. So since PQstatus(entry->conn)
> doesn't indicate CONNECTION_BAD in ECONNABORTED case,
> the above check is passed through, an error is re-thrown and
> new connection is not reestablished.
>
> Therefore, the easy fix is to make libpq mark the connection as
> CONNECTION_BAD even in ECONNABORTED, like we do in ECONNRESET.
Patch attached. This patch also changes errcode_for_socket_access()
so that it uses ERRCODE_CONNECTION_FAILURE rather than
ERRCODE_INTERNAL_ERROR as sqlerrorcode in ECONNABORTED case
like ECONNRESET. Is this sane?
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
Attachments:
econnaborted_as_conn_error.patch (text/plain, 1.6K) download
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index d0b368530e..6ca814224f 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -714,6 +714,9 @@ errcode_for_socket_access(void)
case EPIPE:
#ifdef ECONNRESET
case ECONNRESET:
+#endif
+#ifdef ECONNABORTED
+ case ECONNABORTED:
#endif
edata->sqlerrcode = ERRCODE_CONNECTION_FAILURE;
break;
diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c
index ff840b7730..657310f22c 100644
--- a/src/interfaces/libpq/fe-misc.c
+++ b/src/interfaces/libpq/fe-misc.c
@@ -683,6 +683,11 @@ retry3:
#ifdef ECONNRESET
if (SOCK_ERRNO == ECONNRESET)
goto definitelyFailed;
+#endif
+ /* We might get ECONNABORTED here if using TCP and backend died */
+#ifdef ECONNABORTED
+ if (SOCK_ERRNO == ECONNABORTED)
+ goto definitelyFailed;
#endif
/* pqsecure_read set the error message for us */
return -1;
@@ -773,6 +778,11 @@ retry4:
#ifdef ECONNRESET
if (SOCK_ERRNO == ECONNRESET)
goto definitelyFailed;
+#endif
+ /* We might get ECONNABORTED here if using TCP and backend died */
+#ifdef ECONNABORTED
+ if (SOCK_ERRNO == ECONNABORTED)
+ goto definitelyFailed;
#endif
/* pqsecure_read set the error message for us */
return -1;
diff --git a/src/interfaces/libpq/win32.h b/src/interfaces/libpq/win32.h
index c42d7abfe3..40cff28f3a 100644
--- a/src/interfaces/libpq/win32.h
+++ b/src/interfaces/libpq/win32.h
@@ -22,6 +22,9 @@
#ifndef ECONNRESET
#define ECONNRESET WSAECONNRESET
#endif
+#ifndef ECONNABORTED
+#define ECONNABORTED WSAECONNABORTED
+#endif
#ifndef EINPROGRESS
#define EINPROGRESS WSAEINPROGRESS
#endif
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-08 13:47 Tom Lane <tgl@sss.pgh.pa.us>
parent: Fujii Masao <masao.fujii@oss.nttdata.com>
0 siblings, 2 replies; 13+ messages in thread
From: Tom Lane @ 2020-10-08 13:47 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@oss.nttdata.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; pgsql-committers@lists.postgresql.org
Fujii Masao <masao.fujii@oss.nttdata.com> writes:
>> Therefore, the easy fix is to make libpq mark the connection as
>> CONNECTION_BAD even in ECONNABORTED, like we do in ECONNRESET.
> Patch attached. This patch also changes errcode_for_socket_access()
> so that it uses ERRCODE_CONNECTION_FAILURE rather than
> ERRCODE_INTERNAL_ERROR as sqlerrorcode in ECONNABORTED case
> like ECONNRESET. Is this sane?
Some digging around on the net suggests that Windows will only return
WSAECONNRESET for the case where it gets a TCP RST packet from the remote
host. Other cases of TCP connection loss (e.g. timeout) are reported
as WSAECONNABORTED.
I think that our code generally only expects ECONNRESET to be reported
for a lost connection. Based on this info, we clearly need to handle
ECONNABORTED as being equivalent to that. I don't see any reason why
we'd care to distinguish the two cases.
So it seems like (a) we'd better make sure that ECONNABORTED is
recognized anyplace that we're testing for ECONNRESET, and (b) that
seems like a back-patchable bug fix, independently of the immediate
issue.
On the other hand, it seems a bit odd that we've not recognized
this issue long since. Maybe the preference for ECONNABORTED only
exists on a few Windows versions?
I've not checked your patch in detail.
regards, tom lane
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-08 20:00 Tom Lane <tgl@sss.pgh.pa.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 13+ messages in thread
From: Tom Lane @ 2020-10-08 20:00 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@oss.nttdata.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; pgsql-committers@lists.postgresql.org
I wrote:
> I've not checked your patch in detail.
Bread crumb for the archives: I started a -hackers thread about this:
https://www.postgresql.org/message-id/flat/2621622.1602184554%40sss.pgh.pa.us
regards, tom lane
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-11 00:16 Tom Lane <tgl@sss.pgh.pa.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 13+ messages in thread
From: Tom Lane @ 2020-10-11 00:16 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@oss.nttdata.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; pgsql-committers@lists.postgresql.org
Fujii Masao <masao.fujii@oss.nttdata.com> writes:
>>> Therefore, the easy fix is to make libpq mark the connection as
>>> CONNECTION_BAD even in ECONNABORTED, like we do in ECONNRESET.
So in the wake of commit fe27009cb, this is what lorikeet is doing:
@@ -9028,9 +9028,7 @@
CALL terminate_backend_and_wait('fdw_retry_check');
SAVEPOINT s;
SELECT 1 FROM ft1 LIMIT 1; -- should fail
-ERROR: server closed the connection unexpectedly
- This probably means the server terminated abnormally
- before or while processing the request.
+ERROR: could not receive data from server: Software caused connection abort
CONTEXT: remote SQL command: SAVEPOINT s2
COMMIT;
-- Clean up
which is better --- the connection recovery is working --- but
obviously still not a "pass".
The only way to make this test pass as-is is to hack libpq so that
it deems ECONNABORTED to be a server crash, which frankly I do not
think is a good change. I don't see any principled reason to think
that it means that rather than a network connectivity failure.
What I've done instead as a stopgap is to adjust the test case to be
insensitive to the exact error message text. Maybe there's a better
way, but I can't think of anything besides having two (or more?)
expected-output files. That would be quite unpalatable as things
stand, though perhaps we could make it tolerable by splitting this
test off into a second test script.
Meanwhile, now that I've looked at commit 32a9c0bdf, I'm not very
happy with it:
* The control flow seems rather forced. I think it was designed
on the assumption that reindenting the existing code is forbidden.
Why not use an actual loop, instead of a goto? I also think that
it's far from obvious that the loop isn't an infinite loop; it
took me quite a while to glom onto the fact that the test inside the
PG_CATCH avoids that by checking retry_conn. Maybe a comment would
be enough to improve that, but I feel the control logic could stand
a rethink.
* The CATCH case is completely failing to clean up after itself.
At the minimum, there has to be a FlushErrorState() there.
I don't think it'd be terribly hard to drive this code to an
error-stack-overflow PANIC.
* As is so often true of proposed patches in which PG_CATCH is
thought to be an adequate way to catch an error, this is just
unbelievably fragile. It will break, and badly so, if it catches
an error that is anything other than what it is expecting ...
and it's not even particularly trying to verify that the error is
what it's expecting. It might be okay, or at least a little bit
harder to break, if it verified that the error's SQLSTATE is
ERRCODE_CONNECTION_FAILURE.
regards, tom lane
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-13 18:19 Fujii Masao <masao.fujii@oss.nttdata.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 13+ messages in thread
From: Fujii Masao @ 2020-10-13 18:19 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; pgsql-committers@lists.postgresql.org
On 2020/10/11 9:16, Tom Lane wrote:
> Fujii Masao <masao.fujii@oss.nttdata.com> writes:
>>>> Therefore, the easy fix is to make libpq mark the connection as
>>>> CONNECTION_BAD even in ECONNABORTED, like we do in ECONNRESET.
>
> So in the wake of commit fe27009cb,
Many thanks for the commit fe27009cb !!
> this is what lorikeet is doing:
>
> @@ -9028,9 +9028,7 @@
> CALL terminate_backend_and_wait('fdw_retry_check');
> SAVEPOINT s;
> SELECT 1 FROM ft1 LIMIT 1; -- should fail
> -ERROR: server closed the connection unexpectedly
> - This probably means the server terminated abnormally
> - before or while processing the request.
> +ERROR: could not receive data from server: Software caused connection abort
> CONTEXT: remote SQL command: SAVEPOINT s2
> COMMIT;
> -- Clean up
>
> which is better --- the connection recovery is working --- but
> obviously still not a "pass".
>
> The only way to make this test pass as-is is to hack libpq so that
> it deems ECONNABORTED to be a server crash, which frankly I do not
> think is a good change. I don't see any principled reason to think
> that it means that rather than a network connectivity failure.
>
> What I've done instead as a stopgap is to adjust the test case to be
> insensitive to the exact error message text.
For now I've not come up with better idea than current this fix.
> Maybe there's a better
> way, but I can't think of anything besides having two (or more?)
> expected-output files. That would be quite unpalatable as things
> stand, though perhaps we could make it tolerable by splitting this
> test off into a second test script.
>
> Meanwhile, now that I've looked at commit 32a9c0bdf, I'm not very
> happy with it:
>
> * The control flow seems rather forced. I think it was designed
> on the assumption that reindenting the existing code is forbidden.
> Why not use an actual loop, instead of a goto? I also think that
> it's far from obvious that the loop isn't an infinite loop; it
> took me quite a while to glom onto the fact that the test inside the
> PG_CATCH avoids that by checking retry_conn. Maybe a comment would
> be enough to improve that, but I feel the control logic could stand
> a rethink.
Isn't it simpler and easier-to-read to just reestablish new connection again
in the retry case instead of using a loop because we retry only once?
Patch attached.
>
> * The CATCH case is completely failing to clean up after itself.
> At the minimum, there has to be a FlushErrorState() there.
> I don't think it'd be terribly hard to drive this code to an
> error-stack-overflow PANIC.
You're right. Sorry I forgot to take into consideration this :(
I fixed this issue in the attached patch.
>
> * As is so often true of proposed patches in which PG_CATCH is
> thought to be an adequate way to catch an error, this is just
> unbelievably fragile. It will break, and badly so, if it catches
> an error that is anything other than what it is expecting ...
> and it's not even particularly trying to verify that the error is
> what it's expecting. It might be okay, or at least a little bit
> harder to break, if it verified that the error's SQLSTATE is
> ERRCODE_CONNECTION_FAILURE.
"PQstatus()==CONNECTION_BAD" was checked for that purpose. But that's not enough?
Anyway, in the patch, I changed the code so that sqlstate==ERRCODE_CONNECTION_FAILURE
is checked.
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
Attachments:
rethink_get_connection.patch (text/plain, 6.1K) download
diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c
index 76994f3820..8bbad61c3b 100644
--- a/contrib/postgres_fdw/connection.c
+++ b/contrib/postgres_fdw/connection.c
@@ -74,6 +74,7 @@ static unsigned int prep_stmt_number = 0;
static bool xact_got_connection = false;
/* prototypes of private functions */
+static void make_new_connection(ConnCacheEntry *entry, UserMapping *user);
static PGconn *connect_pg_server(ForeignServer *server, UserMapping *user);
static void disconnect_pg_server(ConnCacheEntry *entry);
static void check_conn_params(const char **keywords, const char **values, UserMapping *user);
@@ -108,9 +109,10 @@ PGconn *
GetConnection(UserMapping *user, bool will_prep_stmt)
{
bool found;
- volatile bool retry_conn = false;
+ bool retry = false;
ConnCacheEntry *entry;
ConnCacheKey key;
+ MemoryContext ccxt = CurrentMemoryContext;
/* First time through, initialize connection cache hashtable */
if (ConnectionHash == NULL)
@@ -160,23 +162,14 @@ GetConnection(UserMapping *user, bool will_prep_stmt)
/* Reject further use of connections which failed abort cleanup. */
pgfdw_reject_incomplete_xact_state_change(entry);
-retry:
-
/*
* If the connection needs to be remade due to invalidation, disconnect as
- * soon as we're out of all transactions. Also, if previous attempt to
- * start new remote transaction failed on the cached connection,
- * disconnect it to retry a new connection.
+ * soon as we're out of all transactions.
*/
- if ((entry->conn != NULL && entry->invalidated &&
- entry->xact_depth == 0) || retry_conn)
+ if (entry->conn != NULL && entry->invalidated && entry->xact_depth == 0)
{
- if (retry_conn)
- elog(DEBUG3, "closing connection %p to reestablish a new one",
- entry->conn);
- else
- elog(DEBUG3, "closing connection %p for option changes to take effect",
- entry->conn);
+ elog(DEBUG3, "closing connection %p for option changes to take effect",
+ entry->conn);
disconnect_pg_server(entry);
}
@@ -186,58 +179,67 @@ retry:
* will remain in a valid empty state, ie conn == NULL.)
*/
if (entry->conn == NULL)
- {
- ForeignServer *server = GetForeignServer(user->serverid);
-
- /* Reset all transient state fields, to be sure all are clean */
- entry->xact_depth = 0;
- entry->have_prep_stmt = false;
- entry->have_error = false;
- entry->changing_xact_state = false;
- entry->invalidated = false;
- entry->server_hashvalue =
- GetSysCacheHashValue1(FOREIGNSERVEROID,
- ObjectIdGetDatum(server->serverid));
- entry->mapping_hashvalue =
- GetSysCacheHashValue1(USERMAPPINGOID,
- ObjectIdGetDatum(user->umid));
-
- /* Now try to make the connection */
- entry->conn = connect_pg_server(server, user);
-
- elog(DEBUG3, "new postgres_fdw connection %p for server \"%s\" (user mapping oid %u, userid %u)",
- entry->conn, server->servername, user->umid, user->userid);
- }
+ make_new_connection(entry, user);
/*
* We check the health of the cached connection here when starting a new
- * remote transaction. If a broken connection is detected in the first
- * attempt, we try to reestablish a new connection. If broken connection
- * is detected again here, we give up getting a connection.
+ * remote transaction. If a broken connection is detected, we try to
+ * reestablish a new connection later.
*/
PG_TRY();
{
/* Start a new transaction or subtransaction if needed. */
begin_remote_xact(entry);
- retry_conn = false;
}
PG_CATCH();
{
- if (PQstatus(entry->conn) != CONNECTION_BAD ||
- entry->xact_depth > 0 ||
- retry_conn)
+ MemoryContext ecxt = MemoryContextSwitchTo(ccxt);
+ ErrorData *errdata = CopyErrorData();
+
+ /*
+ * The error code ERRCODE_CONNECTION_FAILURE indicates a broken
+ * connection. If it's detected when starting a new remote transaction
+ * (not subtransaction), new connection will be reestablished later.
+ */
+ if (errdata->sqlerrcode != ERRCODE_CONNECTION_FAILURE ||
+ entry->xact_depth > 0)
+ {
+ MemoryContextSwitchTo(ecxt);
PG_RE_THROW();
- retry_conn = true;
+ }
+
+ /* Clean up the error state */
+ FlushErrorState();
+ FreeErrorData(errdata);
+ errdata = NULL;
+
+ retry = true;
}
PG_END_TRY();
- if (retry_conn)
+ /*
+ * If a broken connection is detected, disconnect it, reestablish a new
+ * connection and retry a new remote transaction. If connection failure
+ * is reported again, we give up getting a connection.
+ */
+ if (retry)
{
+ Assert(PQstatus(entry->conn) == CONNECTION_BAD);
+ Assert(entry->xact_depth == 0);
+
ereport(DEBUG3,
(errmsg_internal("could not start remote transaction on connection %p",
entry->conn)),
errdetail_internal("%s", pchomp(PQerrorMessage(entry->conn))));
- goto retry;
+
+ elog(DEBUG3, "closing connection %p to reestablish a new one",
+ entry->conn);
+ disconnect_pg_server(entry);
+
+ if (entry->conn == NULL)
+ make_new_connection(entry, user);
+
+ begin_remote_xact(entry);
}
/* Remember if caller will prepare statements */
@@ -246,6 +248,37 @@ retry:
return entry->conn;
}
+/*
+ * Reset all transient state fields in the cached connection entry and
+ * establish new connection to the remote server.
+ */
+static void
+make_new_connection(ConnCacheEntry *entry, UserMapping *user)
+{
+ ForeignServer *server = GetForeignServer(user->serverid);
+
+ Assert(entry->conn == NULL);
+
+ /* Reset all transient state fields, to be sure all are clean */
+ entry->xact_depth = 0;
+ entry->have_prep_stmt = false;
+ entry->have_error = false;
+ entry->changing_xact_state = false;
+ entry->invalidated = false;
+ entry->server_hashvalue =
+ GetSysCacheHashValue1(FOREIGNSERVEROID,
+ ObjectIdGetDatum(server->serverid));
+ entry->mapping_hashvalue =
+ GetSysCacheHashValue1(USERMAPPINGOID,
+ ObjectIdGetDatum(user->umid));
+
+ /* Now try to make the connection */
+ entry->conn = connect_pg_server(server, user);
+
+ elog(DEBUG3, "new postgres_fdw connection %p for server \"%s\" (user mapping oid %u, userid %u)",
+ entry->conn, server->servername, user->umid, user->userid);
+}
+
/*
* Connect to remote server using specified server and user mapping properties.
*/
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-13 18:34 Tom Lane <tgl@sss.pgh.pa.us>
parent: Fujii Masao <masao.fujii@oss.nttdata.com>
0 siblings, 1 reply; 13+ messages in thread
From: Tom Lane @ 2020-10-13 18:34 UTC (permalink / raw)
To: Fujii Masao <masao.fujii@oss.nttdata.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; pgsql-committers@lists.postgresql.org
Fujii Masao <masao.fujii@oss.nttdata.com> writes:
> On 2020/10/11 9:16, Tom Lane wrote:
>> Meanwhile, now that I've looked at commit 32a9c0bdf, I'm not very
>> happy with it:
>>
>> * The control flow seems rather forced. I think it was designed
>> on the assumption that reindenting the existing code is forbidden.
> Isn't it simpler and easier-to-read to just reestablish new connection again
> in the retry case instead of using a loop because we retry only once?
> Patch attached.
Yeah, that seems better.
>> * As is so often true of proposed patches in which PG_CATCH is
>> thought to be an adequate way to catch an error, this is just
>> unbelievably fragile. It will break, and badly so, if it catches
>> an error that is anything other than what it is expecting ...
>> and it's not even particularly trying to verify that the error is
>> what it's expecting. It might be okay, or at least a little bit
>> harder to break, if it verified that the error's SQLSTATE is
>> ERRCODE_CONNECTION_FAILURE.
> "PQstatus()==CONNECTION_BAD" was checked for that purpose. But that's not enough?
> Anyway, in the patch, I changed the code so that sqlstate==ERRCODE_CONNECTION_FAILURE
> is checked.
The reason I'm concerned about this is that we could potentially throw an
elog(ERROR) somewhere between return from libpq and the expected ereport
call in pgfdw_report_error. It wouldn't be wise to ignore such a
condition (it might be out-of-memory or some such).
Personally I'd code the check with explicit tests for *both*
PQstatus()==CONNECTION_BAD and sqlstate==ERRCODE_CONNECTION_FAILURE,
rather than just asserting that the latter must imply the former.
By my reading, pgfdw_report_error will report ERRCODE_CONNECTION_FAILURE
for any libpq-originated error condition, but not all of those will
set CONNECTION_BAD.
Other than that, this seems reasonable by eyeball (I didn't do any
testing).
regards, tom lane
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-15 07:21 Fujii Masao <masao.fujii@oss.nttdata.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 13+ messages in thread
From: Fujii Masao @ 2020-10-15 07:21 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; pgsql-committers@lists.postgresql.org
On 2020/10/14 3:34, Tom Lane wrote:
> Fujii Masao <masao.fujii@oss.nttdata.com> writes:
>> On 2020/10/11 9:16, Tom Lane wrote:
>>> Meanwhile, now that I've looked at commit 32a9c0bdf, I'm not very
>>> happy with it:
>>>
>>> * The control flow seems rather forced. I think it was designed
>>> on the assumption that reindenting the existing code is forbidden.
>
>> Isn't it simpler and easier-to-read to just reestablish new connection again
>> in the retry case instead of using a loop because we retry only once?
>> Patch attached.
>
> Yeah, that seems better.
>
>>> * As is so often true of proposed patches in which PG_CATCH is
>>> thought to be an adequate way to catch an error, this is just
>>> unbelievably fragile. It will break, and badly so, if it catches
>>> an error that is anything other than what it is expecting ...
>>> and it's not even particularly trying to verify that the error is
>>> what it's expecting. It might be okay, or at least a little bit
>>> harder to break, if it verified that the error's SQLSTATE is
>>> ERRCODE_CONNECTION_FAILURE.
>
>> "PQstatus()==CONNECTION_BAD" was checked for that purpose. But that's not enough?
>> Anyway, in the patch, I changed the code so that sqlstate==ERRCODE_CONNECTION_FAILURE
>> is checked.
>
> The reason I'm concerned about this is that we could potentially throw an
> elog(ERROR) somewhere between return from libpq and the expected ereport
> call in pgfdw_report_error. It wouldn't be wise to ignore such a
> condition (it might be out-of-memory or some such).
Understood.
> Personally I'd code the check with explicit tests for *both*
> PQstatus()==CONNECTION_BAD and sqlstate==ERRCODE_CONNECTION_FAILURE,
> rather than just asserting that the latter must imply the former.
> By my reading, pgfdw_report_error will report ERRCODE_CONNECTION_FAILURE
> for any libpq-originated error condition, but not all of those will
> set CONNECTION_BAD.
Yes, this makes sense. I updated the patch so that both sqlstate and
PQstatus() are checked. Patch attached.
> Other than that, this seems reasonable by eyeball (I didn't do any
> testing).
Thanks! Barring any objection, I will commit it.
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
Attachments:
rethink_get_connection_v2.patch (text/plain, 6.7K) download
diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c
index 76994f3820..2f411cf2f7 100644
--- a/contrib/postgres_fdw/connection.c
+++ b/contrib/postgres_fdw/connection.c
@@ -74,6 +74,7 @@ static unsigned int prep_stmt_number = 0;
static bool xact_got_connection = false;
/* prototypes of private functions */
+static void make_new_connection(ConnCacheEntry *entry, UserMapping *user);
static PGconn *connect_pg_server(ForeignServer *server, UserMapping *user);
static void disconnect_pg_server(ConnCacheEntry *entry);
static void check_conn_params(const char **keywords, const char **values, UserMapping *user);
@@ -108,9 +109,10 @@ PGconn *
GetConnection(UserMapping *user, bool will_prep_stmt)
{
bool found;
- volatile bool retry_conn = false;
+ bool retry = false;
ConnCacheEntry *entry;
ConnCacheKey key;
+ MemoryContext ccxt = CurrentMemoryContext;
/* First time through, initialize connection cache hashtable */
if (ConnectionHash == NULL)
@@ -160,23 +162,14 @@ GetConnection(UserMapping *user, bool will_prep_stmt)
/* Reject further use of connections which failed abort cleanup. */
pgfdw_reject_incomplete_xact_state_change(entry);
-retry:
-
/*
* If the connection needs to be remade due to invalidation, disconnect as
- * soon as we're out of all transactions. Also, if previous attempt to
- * start new remote transaction failed on the cached connection,
- * disconnect it to retry a new connection.
+ * soon as we're out of all transactions.
*/
- if ((entry->conn != NULL && entry->invalidated &&
- entry->xact_depth == 0) || retry_conn)
+ if (entry->conn != NULL && entry->invalidated && entry->xact_depth == 0)
{
- if (retry_conn)
- elog(DEBUG3, "closing connection %p to reestablish a new one",
- entry->conn);
- else
- elog(DEBUG3, "closing connection %p for option changes to take effect",
- entry->conn);
+ elog(DEBUG3, "closing connection %p for option changes to take effect",
+ entry->conn);
disconnect_pg_server(entry);
}
@@ -186,58 +179,78 @@ retry:
* will remain in a valid empty state, ie conn == NULL.)
*/
if (entry->conn == NULL)
- {
- ForeignServer *server = GetForeignServer(user->serverid);
-
- /* Reset all transient state fields, to be sure all are clean */
- entry->xact_depth = 0;
- entry->have_prep_stmt = false;
- entry->have_error = false;
- entry->changing_xact_state = false;
- entry->invalidated = false;
- entry->server_hashvalue =
- GetSysCacheHashValue1(FOREIGNSERVEROID,
- ObjectIdGetDatum(server->serverid));
- entry->mapping_hashvalue =
- GetSysCacheHashValue1(USERMAPPINGOID,
- ObjectIdGetDatum(user->umid));
-
- /* Now try to make the connection */
- entry->conn = connect_pg_server(server, user);
-
- elog(DEBUG3, "new postgres_fdw connection %p for server \"%s\" (user mapping oid %u, userid %u)",
- entry->conn, server->servername, user->umid, user->userid);
- }
+ make_new_connection(entry, user);
/*
* We check the health of the cached connection here when starting a new
- * remote transaction. If a broken connection is detected in the first
- * attempt, we try to reestablish a new connection. If broken connection
- * is detected again here, we give up getting a connection.
+ * remote transaction. If a broken connection is detected, we try to
+ * reestablish a new connection later.
*/
PG_TRY();
{
/* Start a new transaction or subtransaction if needed. */
begin_remote_xact(entry);
- retry_conn = false;
}
PG_CATCH();
{
- if (PQstatus(entry->conn) != CONNECTION_BAD ||
- entry->xact_depth > 0 ||
- retry_conn)
+ MemoryContext ecxt = MemoryContextSwitchTo(ccxt);
+ ErrorData *errdata = CopyErrorData();
+
+ /*
+ * If connection failure is reported when starting a new remote
+ * transaction (not subtransaction), new connection will be
+ * reestablished later.
+ *
+ * After a broken connection is detected in libpq, any error other
+ * than connection failure (e.g., out-of-memory) can be thrown
+ * somewhere between return from libpq and the expected ereport() call
+ * in pgfdw_report_error(). In this case, since PQstatus() indicates
+ * CONNECTION_BAD, checking only PQstatus() causes the false detection
+ * of connection failure. To avoid this, we also verify that the
+ * error's sqlstate is ERRCODE_CONNECTION_FAILURE. Note that also
+ * checking only the sqlstate can cause another false detection
+ * because pgfdw_report_error() may report ERRCODE_CONNECTION_FAILURE
+ * for any libpq-originated error condition.
+ */
+ if (errdata->sqlerrcode != ERRCODE_CONNECTION_FAILURE ||
+ PQstatus(entry->conn) != CONNECTION_BAD ||
+ entry->xact_depth > 0)
+ {
+ MemoryContextSwitchTo(ecxt);
PG_RE_THROW();
- retry_conn = true;
+ }
+
+ /* Clean up the error state */
+ FlushErrorState();
+ FreeErrorData(errdata);
+ errdata = NULL;
+
+ retry = true;
}
PG_END_TRY();
- if (retry_conn)
+ /*
+ * If a broken connection is detected, disconnect it, reestablish a new
+ * connection and retry a new remote transaction. If connection failure is
+ * reported again, we give up getting a connection.
+ */
+ if (retry)
{
+ Assert(entry->xact_depth == 0);
+
ereport(DEBUG3,
(errmsg_internal("could not start remote transaction on connection %p",
entry->conn)),
errdetail_internal("%s", pchomp(PQerrorMessage(entry->conn))));
- goto retry;
+
+ elog(DEBUG3, "closing connection %p to reestablish a new one",
+ entry->conn);
+ disconnect_pg_server(entry);
+
+ if (entry->conn == NULL)
+ make_new_connection(entry, user);
+
+ begin_remote_xact(entry);
}
/* Remember if caller will prepare statements */
@@ -246,6 +259,37 @@ retry:
return entry->conn;
}
+/*
+ * Reset all transient state fields in the cached connection entry and
+ * establish new connection to the remote server.
+ */
+static void
+make_new_connection(ConnCacheEntry *entry, UserMapping *user)
+{
+ ForeignServer *server = GetForeignServer(user->serverid);
+
+ Assert(entry->conn == NULL);
+
+ /* Reset all transient state fields, to be sure all are clean */
+ entry->xact_depth = 0;
+ entry->have_prep_stmt = false;
+ entry->have_error = false;
+ entry->changing_xact_state = false;
+ entry->invalidated = false;
+ entry->server_hashvalue =
+ GetSysCacheHashValue1(FOREIGNSERVEROID,
+ ObjectIdGetDatum(server->serverid));
+ entry->mapping_hashvalue =
+ GetSysCacheHashValue1(USERMAPPINGOID,
+ ObjectIdGetDatum(user->umid));
+
+ /* Now try to make the connection */
+ entry->conn = connect_pg_server(server, user);
+
+ elog(DEBUG3, "new postgres_fdw connection %p for server \"%s\" (user mapping oid %u, userid %u)",
+ entry->conn, server->servername, user->umid, user->userid);
+}
+
/*
* Connect to remote server using specified server and user mapping properties.
*/
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: pgsql: postgres_fdw: reestablish new connection if cached one is detect
@ 2020-10-16 05:01 Fujii Masao <masao.fujii@oss.nttdata.com>
parent: Fujii Masao <masao.fujii@oss.nttdata.com>
0 siblings, 0 replies; 13+ messages in thread
From: Fujii Masao @ 2020-10-16 05:01 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael@paquier.xyz>; Fujii Masao <fujii@postgresql.org>; pgsql-committers@lists.postgresql.org
On 2020/10/15 16:21, Fujii Masao wrote:
>
>
> On 2020/10/14 3:34, Tom Lane wrote:
>> Fujii Masao <masao.fujii@oss.nttdata.com> writes:
>>> On 2020/10/11 9:16, Tom Lane wrote:
>>>> Meanwhile, now that I've looked at commit 32a9c0bdf, I'm not very
>>>> happy with it:
>>>>
>>>> * The control flow seems rather forced. I think it was designed
>>>> on the assumption that reindenting the existing code is forbidden.
>>
>>> Isn't it simpler and easier-to-read to just reestablish new connection again
>>> in the retry case instead of using a loop because we retry only once?
>>> Patch attached.
>>
>> Yeah, that seems better.
>>
>>>> * As is so often true of proposed patches in which PG_CATCH is
>>>> thought to be an adequate way to catch an error, this is just
>>>> unbelievably fragile. It will break, and badly so, if it catches
>>>> an error that is anything other than what it is expecting ...
>>>> and it's not even particularly trying to verify that the error is
>>>> what it's expecting. It might be okay, or at least a little bit
>>>> harder to break, if it verified that the error's SQLSTATE is
>>>> ERRCODE_CONNECTION_FAILURE.
>>
>>> "PQstatus()==CONNECTION_BAD" was checked for that purpose. But that's not enough?
>>> Anyway, in the patch, I changed the code so that sqlstate==ERRCODE_CONNECTION_FAILURE
>>> is checked.
>>
>> The reason I'm concerned about this is that we could potentially throw an
>> elog(ERROR) somewhere between return from libpq and the expected ereport
>> call in pgfdw_report_error. It wouldn't be wise to ignore such a
>> condition (it might be out-of-memory or some such).
>
> Understood.
>
>
>> Personally I'd code the check with explicit tests for *both*
>> PQstatus()==CONNECTION_BAD and sqlstate==ERRCODE_CONNECTION_FAILURE,
>> rather than just asserting that the latter must imply the former.
>> By my reading, pgfdw_report_error will report ERRCODE_CONNECTION_FAILURE
>> for any libpq-originated error condition, but not all of those will
>> set CONNECTION_BAD.
>
> Yes, this makes sense. I updated the patch so that both sqlstate and
> PQstatus() are checked. Patch attached.
>
>
>> Other than that, this seems reasonable by eyeball (I didn't do any
>> testing).
>
> Thanks! Barring any objection, I will commit it.
Pushed. Thanks!
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
^ permalink raw reply [nested|flat] 13+ messages in thread
end of thread, other threads:[~2020-10-16 05:01 UTC | newest]
Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-10-06 01:52 pgsql: postgres_fdw: reestablish new connection if cached one is detect Fujii Masao <fujii@postgresql.org>
2020-10-07 02:13 ` Michael Paquier <michael@paquier.xyz>
2020-10-07 03:54 ` Fujii Masao <masao.fujii@oss.nttdata.com>
2020-10-07 13:25 ` Fujii Masao <masao.fujii@oss.nttdata.com>
2020-10-07 15:48 ` Fujii Masao <masao.fujii@oss.nttdata.com>
2020-10-08 03:43 ` Fujii Masao <masao.fujii@oss.nttdata.com>
2020-10-08 13:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-10-08 20:00 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-10-11 00:16 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-10-13 18:19 ` Fujii Masao <masao.fujii@oss.nttdata.com>
2020-10-13 18:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-10-15 07:21 ` Fujii Masao <masao.fujii@oss.nttdata.com>
2020-10-16 05:01 ` Fujii Masao <masao.fujii@oss.nttdata.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox