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,24 @@ class IsolationLevel(Enum):
104104 RepeatableRead = 3
105105 Serializable = 4
106106
107+ class LoadBalanceHosts (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+
115+ class TargetSessionAttrs (Enum ):
116+ """Properties required of a session."""
117+
118+ # No special properties are required.
119+ Any = 1
120+ # The session must allow writes.
121+ ReadWrite = 2
122+ # The session allow only reads.
123+ ReadOnly = 3
124+
107125class ReadVariant (Enum ):
108126 """Class for Read Variant for transaction."""
109127
@@ -869,8 +887,24 @@ class ConnectionPool:
869887 username : Optional [str ] = None ,
870888 password : Optional [str ] = None ,
871889 host : Optional [str ] = None ,
890+ hosts : Optional [List [str ]] = None ,
872891 port : Optional [int ] = None ,
892+ ports : Optional [List [int ]] = None ,
873893 db_name : Optional [str ] = None ,
894+ target_session_attrs : Optional [TargetSessionAttrs ] = None ,
895+ options : Optional [str ] = None ,
896+ application_name : Optional [str ] = None ,
897+ connect_timeout_sec : Optional [int ] = None ,
898+ connect_timeout_nanosec : Optional [int ] = None ,
899+ tcp_user_timeout_sec : Optional [int ] = None ,
900+ tcp_user_timeout_nanosec : Optional [int ] = None ,
901+ keepalives : Optional [bool ] = None ,
902+ keepalives_idle_sec : Optional [int ] = None ,
903+ keepalives_idle_nanosec : Optional [int ] = None ,
904+ keepalives_interval_sec : Optional [int ] = None ,
905+ keepalives_interval_nanosec : Optional [int ] = None ,
906+ keepalives_retries : Optional [int ] = None ,
907+ load_balance_hosts : Optional [LoadBalanceHosts ] = None ,
874908 max_db_pool_size : int = 2 ,
875909 conn_recycling_method : Optional [ConnRecyclingMethod ] = None ,
876910 ) -> None :
@@ -879,22 +913,67 @@ class ConnectionPool:
879913 It connects to the database and create pool.
880914
881915 You cannot set the minimum size for the connection
882- pool, by default it is 1.
916+ pool, by it is 0.
917+ `ConnectionPool` doesn't create connections on startup.
918+ It makes new connection on demand.
883919
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`
920+ If you specify `dsn` parameter then `username`, `password`,
921+ `host`, `hosts`, `port`, `ports`, `db_name` and `target_session_attrs`
922+ parameters will be ignored.
888923
889924 ### Parameters:
890- - `dsn`: full dsn connection string.
925+ - `dsn`: Full dsn connection string.
891926 `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
927+ - `username`: Username of the user in the PostgreSQL
928+ - `password`: Password of the user in the PostgreSQL
929+ - `host`: Host of the PostgreSQL
930+ - `hosts`: Hosts of the PostgreSQL
931+ - `port`: Port of the PostgreSQL
932+ - `ports`: Ports of the PostgreSQL
933+ - `db_name`: Name of the database in PostgreSQL
934+ - `target_session_attrs`: Specifies requirements of the session.
935+ - `options`: Command line options used to configure the server
936+ - `application_name`: Sets the application_name parameter on the server.
937+ - `connect_timeout_sec`: The time limit in seconds applied to each socket-level
938+ connection attempt.
939+ Note that hostnames can resolve to multiple IP addresses,
940+ and this limit is applied to each address. Defaults to no timeout.
941+ - `connect_timeout_nanosec`: nanosec for connection timeout,
942+ can be used only with connect_timeout_sec.
943+ - `tcp_user_timeout_sec`: The time limit that
944+ transmitted data may remain unacknowledged
945+ before a connection is forcibly closed.
946+ This is ignored for Unix domain socket connections.
947+ It is only supported on systems where TCP_USER_TIMEOUT
948+ is available and will default to the system default if omitted
949+ or set to 0; on other systems, it has no effect.
950+ - `tcp_user_timeout_nanosec`: nanosec for cp_user_timeout,
951+ can be used only with tcp_user_timeout_sec.
952+ - `keepalives`: Controls the use of TCP keepalive.
953+ This option is ignored when connecting with Unix sockets.
954+ Defaults to on.
955+ - `keepalives_idle_sec`: The number of seconds of inactivity after
956+ which a keepalive message is sent to the server.
957+ This option is ignored when connecting with Unix sockets.
958+ Defaults to 2 hours.
959+ - `keepalives_idle_nanosec`: Nanosec for keepalives_idle_sec.
960+ - `keepalives_interval_sec`: The time interval between TCP keepalive probes.
961+ This option is ignored when connecting with Unix sockets.
962+ - `keepalives_interval_nanosec`: Nanosec for keepalives_interval_sec.
963+ - `keepalives_retries`: The maximum number of TCP keepalive probes
964+ that will be sent before dropping a connection.
965+ This option is ignored when connecting with Unix sockets.
966+ - `load_balance_hosts`: Controls the order in which the client tries to connect
967+ to the available hosts and addresses.
968+ Once a connection attempt is successful no other
969+ hosts and addresses will be tried.
970+ This parameter is typically used in combination with multiple host names
971+ or a DNS record that returns multiple IPs.
972+ If set to disable, hosts and addresses will be tried in the order provided.
973+ If set to random, hosts will be tried in a random order, and the IP addresses
974+ resolved from a hostname will also be tried in a random order.
975+ Defaults to disable.
976+ - `max_db_pool_size`: maximum size of the connection pool.
898977 - `conn_recycling_method`: how a connection is recycled.
899978 """
900979 async def execute (
@@ -945,21 +1024,92 @@ def connect(
9451024 username : Optional [str ] = None ,
9461025 password : Optional [str ] = None ,
9471026 host : Optional [str ] = None ,
1027+ hosts : Optional [List [str ]] = None ,
9481028 port : Optional [int ] = None ,
1029+ ports : Optional [List [int ]] = None ,
9491030 db_name : Optional [str ] = None ,
1031+ target_session_attrs : Optional [TargetSessionAttrs ] = None ,
1032+ options : Optional [str ] = None ,
1033+ application_name : Optional [str ] = None ,
1034+ connect_timeout_sec : Optional [int ] = None ,
1035+ connect_timeout_nanosec : Optional [int ] = None ,
1036+ tcp_user_timeout_sec : Optional [int ] = None ,
1037+ tcp_user_timeout_nanosec : Optional [int ] = None ,
1038+ keepalives : Optional [bool ] = None ,
1039+ keepalives_idle_sec : Optional [int ] = None ,
1040+ keepalives_idle_nanosec : Optional [int ] = None ,
1041+ keepalives_interval_sec : Optional [int ] = None ,
1042+ keepalives_interval_nanosec : Optional [int ] = None ,
1043+ keepalives_retries : Optional [int ] = None ,
1044+ load_balance_hosts : Optional [LoadBalanceHosts ] = None ,
9501045 max_db_pool_size : int = 2 ,
9511046 conn_recycling_method : Optional [ConnRecyclingMethod ] = None ,
9521047) -> ConnectionPool :
953- """Create new connection pool.
1048+ """Create new PostgreSQL connection pool.
1049+
1050+ It connects to the database and create pool.
1051+
1052+ You cannot set the minimum size for the connection
1053+ pool, by it is 0.
1054+ `ConnectionPool` doesn't create connections on startup.
1055+ It makes new connection on demand.
1056+
1057+ If you specify `dsn` parameter then `username`, `password`,
1058+ `host`, `hosts`, `port`, `ports`, `db_name` and `target_session_attrs`
1059+ parameters will be ignored.
9541060
9551061 ### Parameters:
956- - `dsn`: full dsn connection string.
1062+ - `dsn`: Full dsn connection string.
9571063 `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
1064+ - `username`: Username of the user in the PostgreSQL
1065+ - `password`: Password of the user in the PostgreSQL
1066+ - `host`: Host of the PostgreSQL
1067+ - `hosts`: Hosts of the PostgreSQL
1068+ - `port`: Port of the PostgreSQL
1069+ - `ports`: Ports of the PostgreSQL
1070+ - `db_name`: Name of the database in PostgreSQL
1071+ - `target_session_attrs`: Specifies requirements of the session.
1072+ - `options`: Command line options used to configure the server
1073+ - `application_name`: Sets the application_name parameter on the server.
1074+ - `connect_timeout_sec`: The time limit in seconds applied to each socket-level
1075+ connection attempt.
1076+ Note that hostnames can resolve to multiple IP addresses,
1077+ and this limit is applied to each address. Defaults to no timeout.
1078+ - `connect_timeout_nanosec`: nanosec for connection timeout,
1079+ can be used only with connect_timeout_sec.
1080+ - `tcp_user_timeout_sec`: The time limit that
1081+ transmitted data may remain unacknowledged
1082+ before a connection is forcibly closed.
1083+ This is ignored for Unix domain socket connections.
1084+ It is only supported on systems where TCP_USER_TIMEOUT
1085+ is available and will default to the system default if omitted
1086+ or set to 0; on other systems, it has no effect.
1087+ - `tcp_user_timeout_nanosec`: nanosec for cp_user_timeout,
1088+ can be used only with tcp_user_timeout_sec.
1089+ - `keepalives`: Controls the use of TCP keepalive.
1090+ This option is ignored when connecting with Unix sockets.
1091+ Defaults to on.
1092+ - `keepalives_idle_sec`: The number of seconds of inactivity after
1093+ which a keepalive message is sent to the server.
1094+ This option is ignored when connecting with Unix sockets.
1095+ Defaults to 2 hours.
1096+ - `keepalives_idle_nanosec`: Nanosec for keepalives_idle_sec.
1097+ - `keepalives_interval_sec`: The time interval between TCP keepalive probes.
1098+ This option is ignored when connecting with Unix sockets.
1099+ - `keepalives_interval_nanosec`: Nanosec for keepalives_interval_sec.
1100+ - `keepalives_retries`: The maximum number of TCP keepalive probes
1101+ that will be sent before dropping a connection.
1102+ This option is ignored when connecting with Unix sockets.
1103+ - `load_balance_hosts`: Controls the order in which the client tries to connect
1104+ to the available hosts and addresses.
1105+ Once a connection attempt is successful no other
1106+ hosts and addresses will be tried.
1107+ This parameter is typically used in combination with multiple host names
1108+ or a DNS record that returns multiple IPs.
1109+ If set to disable, hosts and addresses will be tried in the order provided.
1110+ If set to random, hosts will be tried in a random order, and the IP addresses
1111+ resolved from a hostname will also be tried in a random order.
1112+ Defaults to disable.
1113+ - `max_db_pool_size`: maximum size of the connection pool.
9641114 - `conn_recycling_method`: how a connection is recycled.
9651115 """
0 commit comments