From d70ca01ed896c7b87dc5b73de382f924828f8bac Mon Sep 17 00:00:00 2001 From: James Coleman Date: Mon, 9 Mar 2020 21:45:15 -0400 Subject: [PATCH v4] Improve standby connection denied error message. Currently when a standby is finished starting up but hot_standby is configured to off, the error message when a client connects is "the database system is starting up", which is needless confusing (and not really all that accurate either). Instead send a helpful error message so that the user immediately knows that their server is configured to deny these connections. --- src/backend/postmaster/postmaster.c | 16 ++++++++++++---- src/include/libpq/libpq-be.h | 7 ++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index edab95a19e..ac2877bdc9 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -2289,8 +2289,14 @@ retry1: case CAC_STARTUP: ereport(FATAL, (errcode(ERRCODE_CANNOT_CONNECT_NOW), - errmsg("the database system is starting up"))); + errmsg("consistent state has not yet been reached"))); break; + case CAC_STANDBY: + ereport(FATAL, + (errcode(ERRCODE_CANNOT_CONNECT_NOW), + errmsg("hot standby mode is disabled"))); + break; + case CAC_SHUTDOWN: ereport(FATAL, (errcode(ERRCODE_CANNOT_CONNECT_NOW), @@ -2432,10 +2438,12 @@ canAcceptConnections(int backend_type) { if (Shutdown > NoShutdown) return CAC_SHUTDOWN; /* shutdown is pending */ - else if (!FatalError && - (pmState == PM_STARTUP || - pmState == PM_RECOVERY)) + else if (!FatalError && pmState == PM_STARTUP) return CAC_STARTUP; /* normal startup */ + else if (!FatalError && pmState == PM_RECOVERY && EnableHotStandby) + return CAC_STARTUP; /* hot standby is starting up */ + else if (!FatalError && pmState == PM_RECOVERY) + return CAC_STANDBY; /* connection disallowed on non-hot standby */ else return CAC_RECOVERY; /* else must be crash recovery */ } diff --git a/src/include/libpq/libpq-be.h b/src/include/libpq/libpq-be.h index 30fb4e613d..908eeb5469 100644 --- a/src/include/libpq/libpq-be.h +++ b/src/include/libpq/libpq-be.h @@ -70,7 +70,12 @@ typedef struct typedef enum CAC_state { - CAC_OK, CAC_STARTUP, CAC_SHUTDOWN, CAC_RECOVERY, CAC_TOOMANY, + CAC_OK, + CAC_STARTUP, + CAC_SHUTDOWN, + CAC_RECOVERY, + CAC_STANDBY, + CAC_TOOMANY, CAC_SUPERUSER } CAC_state; -- 2.20.1