mirror of
https://github.com/postgres/postgres.git
synced 2025-12-22 17:42:17 +03:00
Add new GUC reserved_connections.
This provides a way to reserve connection slots for non-superusers. The slots reserved via the new GUC are available only to users who have the new predefined role pg_use_reserved_connections. superuser_reserved_connections remains as a final reserve in case reserved_connections has been exhausted. Patch by Nathan Bossart. Reviewed by Tushar Ahuja and by me. Discussion: http://postgr.es/m/20230119194601.GA4105788@nathanxps13
This commit is contained in:
@@ -719,6 +719,7 @@ InitPostgres(const char *in_dbname, Oid dboid,
|
||||
bool am_superuser;
|
||||
char *fullpath;
|
||||
char dbname[NAMEDATALEN];
|
||||
int nfree = 0;
|
||||
|
||||
elog(DEBUG3, "InitPostgres");
|
||||
|
||||
@@ -922,16 +923,30 @@ InitPostgres(const char *in_dbname, Oid dboid,
|
||||
}
|
||||
|
||||
/*
|
||||
* The last few connection slots are reserved for superusers. Replication
|
||||
* connections are drawn from slots reserved with max_wal_senders and not
|
||||
* limited by max_connections or superuser_reserved_connections.
|
||||
* The last few connection slots are reserved for superusers and roles with
|
||||
* privileges of pg_use_reserved_connections. Replication connections are
|
||||
* drawn from slots reserved with max_wal_senders and are not limited by
|
||||
* max_connections, superuser_reserved_connections, or
|
||||
* reserved_connections.
|
||||
*
|
||||
* Note: At this point, the new backend has already claimed a proc struct,
|
||||
* so we must check whether the number of free slots is strictly less than
|
||||
* the reserved connection limits.
|
||||
*/
|
||||
if (!am_superuser && !am_walsender &&
|
||||
SuperuserReservedConnections > 0 &&
|
||||
!HaveNFreeProcs(SuperuserReservedConnections))
|
||||
ereport(FATAL,
|
||||
(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
|
||||
errmsg("remaining connection slots are reserved for superusers")));
|
||||
(SuperuserReservedConnections + ReservedConnections) > 0 &&
|
||||
!HaveNFreeProcs(SuperuserReservedConnections + ReservedConnections, &nfree))
|
||||
{
|
||||
if (nfree < SuperuserReservedConnections)
|
||||
ereport(FATAL,
|
||||
(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
|
||||
errmsg("remaining connection slots are reserved for superusers")));
|
||||
|
||||
if (!has_privs_of_role(GetUserId(), ROLE_PG_USE_RESERVED_CONNECTIONS))
|
||||
ereport(FATAL,
|
||||
(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
|
||||
errmsg("remaining connection slots are reserved for roles with privileges of pg_use_reserved_connections")));
|
||||
}
|
||||
|
||||
/* Check replication permissions needed for walsender processes. */
|
||||
if (am_walsender)
|
||||
|
||||
@@ -2168,6 +2168,17 @@ struct config_int ConfigureNamesInt[] =
|
||||
NULL, NULL, NULL
|
||||
},
|
||||
|
||||
{
|
||||
{"reserved_connections", PGC_POSTMASTER, CONN_AUTH_SETTINGS,
|
||||
gettext_noop("Sets the number of connection slots reserved for roles "
|
||||
"with privileges of pg_use_reserved_connections."),
|
||||
NULL
|
||||
},
|
||||
&ReservedConnections,
|
||||
0, 0, MAX_BACKENDS,
|
||||
NULL, NULL, NULL
|
||||
},
|
||||
|
||||
{
|
||||
{"min_dynamic_shared_memory", PGC_POSTMASTER, RESOURCES_MEM,
|
||||
gettext_noop("Amount of dynamic shared memory reserved at startup."),
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
# (change requires restart)
|
||||
#port = 5432 # (change requires restart)
|
||||
#max_connections = 100 # (change requires restart)
|
||||
#reserved_connections = 0 # (change requires restart)
|
||||
#superuser_reserved_connections = 3 # (change requires restart)
|
||||
#unix_socket_directories = '/tmp' # comma-separated list of directories
|
||||
# (change requires restart)
|
||||
|
||||
Reference in New Issue
Block a user