11import types
22from enum import Enum
3- from typing import Any , Callable , Optional , TypeVar
3+ from typing import Any , Callable , List , Optional , TypeVar
44
55from typing_extensions import Self
66
@@ -104,6 +104,14 @@ class IsolationLevel(Enum):
104104 RepeatableRead = 3
105105 Serializable = 4
106106
107+ class ConnLoadBalanceHosts (Enum ):
108+ """Load balancing configuration."""
109+
110+ # Make connection attempts to hosts in the order provided.
111+ Disable = (1 ,)
112+ # Make connection attempts to hosts in a random order.
113+ Random = (2 ,)
114+
107115class ReadVariant (Enum ):
108116 """Class for Read Variant for transaction."""
109117
@@ -869,8 +877,23 @@ class ConnectionPool:
869877 username : Optional [str ] = None ,
870878 password : Optional [str ] = None ,
871879 host : Optional [str ] = None ,
880+ hosts : Optional [List [str ]] = None ,
872881 port : Optional [int ] = None ,
882+ ports : Optional [List [int ]] = None ,
873883 db_name : Optional [str ] = None ,
884+ options : Optional [str ] = None ,
885+ application_name : Optional [str ] = None ,
886+ connect_timeout_sec : Optional [int ] = None ,
887+ connect_timeout_nanosec : Optional [int ] = None ,
888+ tcp_user_timeout_sec : Optional [int ] = None ,
889+ tcp_user_timeout_nanosec : Optional [int ] = None ,
890+ keepalives : Optional [bool ] = None ,
891+ keepalives_idle_sec : Optional [int ] = None ,
892+ keepalives_idle_nanosec : Optional [int ] = None ,
893+ keepalives_interval_sec : Optional [int ] = None ,
894+ keepalives_interval_nanosec : Optional [int ] = None ,
895+ keepalives_retries : Optional [int ] = None ,
896+ load_balance_hosts : Optional [ConnLoadBalanceHosts ] = None ,
874897 max_db_pool_size : int = 2 ,
875898 conn_recycling_method : Optional [ConnRecyclingMethod ] = None ,
876899 ) -> None :
@@ -879,22 +902,67 @@ class ConnectionPool:
879902 It connects to the database and create pool.
880903
881904 You cannot set the minimum size for the connection
882- pool, by default it is 1.
905+ pool, by it is 0.
906+ `ConnectionPool` doesn't create connections on startup.
907+ It makes new connection on demand.
883908
884- This connection pool can:
885- - Startup itself with `startup` method
886- - Execute queries and return `QueryResult` class as a result
887- - Create new instance of `Transaction`
909+ If you specify `dsn` parameter then `username`, `password`,
910+ `host`, `hosts`, `port`, `ports`, `db_name` and `target_session_attrs`
911+ parameters will be ignored.
888912
889913 ### Parameters:
890- - `dsn`: full dsn connection string.
914+ - `dsn`: Full dsn connection string.
891915 `postgres://postgres:postgres@localhost:5432/postgres?target_session_attrs=read-write`
892- - `username`: username of the user in postgres
893- - `password`: password of the user in postgres
894- - `host`: host of postgres
895- - `port`: port of postgres
896- - `db_name`: name of the database in postgres
897- - `max_db_pool_size`: maximum size of the connection pool
916+ - `username`: Username of the user in the PostgreSQL
917+ - `password`: Password of the user in the PostgreSQL
918+ - `host`: Host of the PostgreSQL
919+ - `hosts`: Hosts of the PostgreSQL
920+ - `port`: Port of the PostgreSQL
921+ - `ports`: Ports of the PostgreSQL
922+ - `db_name`: Name of the database in PostgreSQL
923+ - `target_session_attrs`: Specifies requirements of the session.
924+ - `options`: Command line options used to configure the server
925+ - `application_name`: Sets the application_name parameter on the server.
926+ - `connect_timeout_sec`: The time limit in seconds applied to each socket-level
927+ connection attempt.
928+ Note that hostnames can resolve to multiple IP addresses,
929+ and this limit is applied to each address. Defaults to no timeout.
930+ - `connect_timeout_nanosec`: nanosec for connection timeout,
931+ can be used only with connect_timeout_sec.
932+ - `tcp_user_timeout_sec`: The time limit that
933+ transmitted data may remain unacknowledged
934+ before a connection is forcibly closed.
935+ This is ignored for Unix domain socket connections.
936+ It is only supported on systems where TCP_USER_TIMEOUT
937+ is available and will default to the system default if omitted
938+ or set to 0; on other systems, it has no effect.
939+ - `tcp_user_timeout_nanosec`: nanosec for cp_user_timeout,
940+ can be used only with tcp_user_timeout_sec.
941+ - `keepalives`: Controls the use of TCP keepalive.
942+ This option is ignored when connecting with Unix sockets.
943+ Defaults to on.
944+ - `keepalives_idle_sec`: The number of seconds of inactivity after
945+ which a keepalive message is sent to the server.
946+ This option is ignored when connecting with Unix sockets.
947+ Defaults to 2 hours.
948+ - `keepalives_idle_nanosec`: Nanosec for keepalives_idle_sec.
949+ - `keepalives_interval_sec`: The time interval between TCP keepalive probes.
950+ This option is ignored when connecting with Unix sockets.
951+ - `keepalives_interval_nanosec`: Nanosec for keepalives_interval_sec.
952+ - `keepalives_retries`: The maximum number of TCP keepalive probes
953+ that will be sent before dropping a connection.
954+ This option is ignored when connecting with Unix sockets.
955+ - `load_balance_hosts`: Controls the order in which the client tries to connect
956+ to the available hosts and addresses.
957+ Once a connection attempt is successful no other
958+ hosts and addresses will be tried.
959+ This parameter is typically used in combination with multiple host names
960+ or a DNS record that returns multiple IPs.
961+ If set to disable, hosts and addresses will be tried in the order provided.
962+ If set to random, hosts will be tried in a random order, and the IP addresses
963+ resolved from a hostname will also be tried in a random order.
964+ Defaults to disable.
965+ - `max_db_pool_size`: maximum size of the connection pool.
898966 - `conn_recycling_method`: how a connection is recycled.
899967 """
900968 async def execute (
@@ -945,21 +1013,91 @@ def connect(
9451013 username : Optional [str ] = None ,
9461014 password : Optional [str ] = None ,
9471015 host : Optional [str ] = None ,
1016+ hosts : Optional [List [str ]] = None ,
9481017 port : Optional [int ] = None ,
1018+ ports : Optional [List [int ]] = None ,
9491019 db_name : Optional [str ] = None ,
1020+ options : Optional [str ] = None ,
1021+ application_name : Optional [str ] = None ,
1022+ connect_timeout_sec : Optional [int ] = None ,
1023+ connect_timeout_nanosec : Optional [int ] = None ,
1024+ tcp_user_timeout_sec : Optional [int ] = None ,
1025+ tcp_user_timeout_nanosec : Optional [int ] = None ,
1026+ keepalives : Optional [bool ] = None ,
1027+ keepalives_idle_sec : Optional [int ] = None ,
1028+ keepalives_idle_nanosec : Optional [int ] = None ,
1029+ keepalives_interval_sec : Optional [int ] = None ,
1030+ keepalives_interval_nanosec : Optional [int ] = None ,
1031+ keepalives_retries : Optional [int ] = None ,
1032+ load_balance_hosts : Optional [ConnLoadBalanceHosts ] = None ,
9501033 max_db_pool_size : int = 2 ,
9511034 conn_recycling_method : Optional [ConnRecyclingMethod ] = None ,
9521035) -> ConnectionPool :
953- """Create new connection pool.
1036+ """Create new PostgreSQL connection pool.
1037+
1038+ It connects to the database and create pool.
1039+
1040+ You cannot set the minimum size for the connection
1041+ pool, by it is 0.
1042+ `ConnectionPool` doesn't create connections on startup.
1043+ It makes new connection on demand.
1044+
1045+ If you specify `dsn` parameter then `username`, `password`,
1046+ `host`, `hosts`, `port`, `ports`, `db_name` and `target_session_attrs`
1047+ parameters will be ignored.
9541048
9551049 ### Parameters:
956- - `dsn`: full dsn connection string.
1050+ - `dsn`: Full dsn connection string.
9571051 `postgres://postgres:postgres@localhost:5432/postgres?target_session_attrs=read-write`
958- - `username`: username of the user in postgres
959- - `password`: password of the user in postgres
960- - `host`: host of postgres
961- - `port`: port of postgres
962- - `db_name`: name of the database in postgres
963- - `max_db_pool_size`: maximum size of the connection pool
1052+ - `username`: Username of the user in the PostgreSQL
1053+ - `password`: Password of the user in the PostgreSQL
1054+ - `host`: Host of the PostgreSQL
1055+ - `hosts`: Hosts of the PostgreSQL
1056+ - `port`: Port of the PostgreSQL
1057+ - `ports`: Ports of the PostgreSQL
1058+ - `db_name`: Name of the database in PostgreSQL
1059+ - `target_session_attrs`: Specifies requirements of the session.
1060+ - `options`: Command line options used to configure the server
1061+ - `application_name`: Sets the application_name parameter on the server.
1062+ - `connect_timeout_sec`: The time limit in seconds applied to each socket-level
1063+ connection attempt.
1064+ Note that hostnames can resolve to multiple IP addresses,
1065+ and this limit is applied to each address. Defaults to no timeout.
1066+ - `connect_timeout_nanosec`: nanosec for connection timeout,
1067+ can be used only with connect_timeout_sec.
1068+ - `tcp_user_timeout_sec`: The time limit that
1069+ transmitted data may remain unacknowledged
1070+ before a connection is forcibly closed.
1071+ This is ignored for Unix domain socket connections.
1072+ It is only supported on systems where TCP_USER_TIMEOUT
1073+ is available and will default to the system default if omitted
1074+ or set to 0; on other systems, it has no effect.
1075+ - `tcp_user_timeout_nanosec`: nanosec for cp_user_timeout,
1076+ can be used only with tcp_user_timeout_sec.
1077+ - `keepalives`: Controls the use of TCP keepalive.
1078+ This option is ignored when connecting with Unix sockets.
1079+ Defaults to on.
1080+ - `keepalives_idle_sec`: The number of seconds of inactivity after
1081+ which a keepalive message is sent to the server.
1082+ This option is ignored when connecting with Unix sockets.
1083+ Defaults to 2 hours.
1084+ - `keepalives_idle_nanosec`: Nanosec for keepalives_idle_sec.
1085+ - `keepalives_interval_sec`: The time interval between TCP keepalive probes.
1086+ This option is ignored when connecting with Unix sockets.
1087+ - `keepalives_interval_nanosec`: Nanosec for keepalives_interval_sec.
1088+ - `keepalives_retries`: The maximum number of TCP keepalive probes
1089+ that will be sent before dropping a connection.
1090+ This option is ignored when connecting with Unix sockets.
1091+ - `load_balance_hosts`: Controls the order in which the client tries to connect
1092+ to the available hosts and addresses.
1093+ Once a connection attempt is successful no other
1094+ hosts and addresses will be tried.
1095+ This parameter is typically used in combination with multiple host names
1096+ or a DNS record that returns multiple IPs.
1097+ If set to disable, hosts and addresses will be tried in the order provided.
1098+ If set to random, hosts will be tried in a random order, and the IP addresses
1099+ resolved from a hostname will also be tried in a random order.
1100+ Defaults to disable.
1101+ - `max_db_pool_size`: maximum size of the connection pool.
9641102 - `conn_recycling_method`: how a connection is recycled.
9651103 """
0 commit comments