
    :"ri                       d dl mZ d dlZd dlmZmZ d dlZd dlZd dlZd dl	Z	d dl
mZ d dlmZmZmZ d dlZddlmZ ddlmZ dd	lmZ dd
lmZ  ej,                  e      Z G d de      Z G d dej6                  ed      Z G d d      Z G d d      Z G d d      Zddddddddddej@                  ejB                  ddZ"y)    )annotationsN)	AwaitableCallable)TracebackType)AnyOptionalType   )compat
connection)
exceptions)protocolc                  V     e Zd Zdd	 	 	 	 	 	 	 	 	 d fdZe	 	 	 	 	 	 dd       Z xZS )PoolConnectionProxyMetaF)wrapc                  |rt        t        j                        D ]  }|j                  d      s||v rt	        t        j                  |      }t        j                  |      sIt        j                  |      }| j                  ||      }t        j                  ||      }|||<    d|vrt        j                  j                  |d<   t        	| 5  | |||      S )N___doc__)dirr   
Connection
startswithgetattrinspect
isfunctioniscoroutinefunction_wrap_connection_method	functoolsupdate_wrapperr   super__new__)
mclsnamebasesdctr   attrnamemethiscoroutinewrapper	__class__s
            T/var/www/fortnox.pascalinesoft.com/venv/lib/python3.12/site-packages/asyncpg/pool.pyr!   zPoolConnectionProxyMeta.__new__   s     
 5 56 (&&s+x3z44h?))$/%99$?66xM#227DA 'H( #!+!6!6!>!>IwtT5#66    c                B     d fd}|rt        j                  |       |S )Nc                    | j                   $t        j                  dj                              t	        | j                   j
                        } || j                   g|i |S )NzJcannot call Connection.{}(): connection has been released back to the pool)_conr   InterfaceErrorformatr   r*   )selfargskwargsr'   	meth_names       r+   call_con_methodzHPoolConnectionProxyMeta._wrap_connection_method.<locals>.call_con_method=   se    yy  //DDJF!E#$ $
 499..	:D		3D3F33r,   )r2   r   r3   r   r4   r   returnr   )r   markcoroutinefunction)r5   r(   r6   s   `  r+   r   z/PoolConnectionProxyMeta._wrap_connection_method9   s!    		4 ((9r,   )
r#   strr$   ztuple[Type[Any], ...]r%   zdict[str, Any]r   boolr7   r   )r5   r9   r(   r:   r7   zCallable[..., Any])__name__
__module____qualname__r!   staticmethodr   __classcell__)r*   s   @r+   r   r      si     77 %7 	7 7 
!76 %)	 r,   r   c                  <    e Zd ZdZ	 	 	 	 	 	 ddZddZd	dZd
dZy)PoolConnectionProxy)r/   _holderc                B    || _         || _        |j                  |        y N)r/   rB   
_set_proxy)r2   holdercons      r+   __init__zPoolConnectionProxy.__init__T   s     	tr,   c                .    t        | j                  |      S rD   )r   r/   )r2   attrs     r+   __getattr__zPoolConnectionProxy.__getattr__[   s    tyy$''r,   c                j    | j                   y | j                   d c}| _         |j                  d        |S rD   )r/   rE   )r2   rG   s     r+   _detachzPoolConnectionProxy._detach_   s1    99DTYt
r,   c                    | j                   0dj                  | j                  j                  t	        |             S dj                  | j                  j                  | j                   t	        |             S )Nz <{classname} [released] {id:#x}>)	classnameidz<{classname} {con!r} {id:#x}>)rO   rG   rP   )r/   r1   r*   r;   rP   r2   s    r+   __repr__zPoolConnectionProxy.__repr__g   sn    995<<..11bh = @ @ 399..11tyyRX : O Or,   N)rF   PoolConnectionHolderrG   zconnection.Connectionr7   None)rJ   r9   r7   r   )r7   zOptional[connection.Connection])r7   r9   )r;   r<   r=   	__slots__rH   rK   rM   rR    r,   r+   rA   rA   N   s6     $I*1F	(Or,   rA   T)	metaclassr   c                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZddZddZddZddZy)rS   )r/   _pool_loop_proxy_max_queries_setup_max_inactive_time_in_use_inactive_callback_timeout_generationc                   || _         d | _        d | _        || _        || _        || _        d | _        d | _        d | _        d | _	        y rD   )
rY   r/   r[   r\   r^   r]   r`   r_   ra   rb   )r2   poolmax_queriessetupmax_inactive_times        r+   rH   zPoolConnectionHolder.__init__x   sN     
59	59'"36:15)-*.r,   c                X    | j                   d uxr | j                   j                          S rD   )r/   	is_closedrQ   s    r+   is_connectedz!PoolConnectionHolder.is_connected   s&    yy$BTYY-@-@-B)BBr,   c                    | j                    S rD   r_   rQ   s    r+   is_idlezPoolConnectionHolder.is_idle   s    <<r,   c                  K   | j                   t        j                  d      | j                  j	                          d {   | _         | j                  j
                  | _        | j                          | j                          y 7 Ew)NzMPoolConnectionHolder.connect() called while another connection already exists)r/   r   InternalClientErrorrY   _get_new_connectionrb   _maybe_cancel_inactive_callback_setup_inactive_callbackrQ   s    r+   connectzPoolConnectionHolder.connect   sp     99 00,- - **88::	::11,,.%%' ;s   ?B	BAB	c                P  K   | j                   | j                   j                         r d | _         | j                          d {    n| j                  | j                  j                  k7  rh| j                  j
                  j                  | j                   j                  | j                               d | _         | j                          d {    | j                          t        | | j                         x| _        }| j                  	 | j                  |       d {    | j                  j
                  j!                         | _        |S 7 7 7 4# t        t        j                  f$ r1}	 | j                   j                          d {  7   |# |xY wd }~ww xY ww)Ntimeout)r/   ri   rs   rb   rY   rZ   create_taskclosera   rq   rA   r[   r]   	ExceptionasyncioCancelledErrorcreate_futurer_   )r2   proxyexs      r+   acquirezPoolConnectionHolder.acquire   sG    99		 3 3 5DI,,.  !7!77JJ((		68DI,,.  ,,.1$		BBe;;"kk%((( zz''557A ! ! )w556 

 ))//+++H"Hsy   AF&EBF&E=F&E "E#E ',F&F&E F#3FFFFFFF##F&c                z  K   | j                   t        j                  d      | j                  j	                         ry d | _        | j                  j                  j                  | j                  k\  r%| j                  j                  |       d {    y | j                  | j                  j                  k7  r%| j                  j                  |       d {    y 	 |}| j                  j                  j                         rpt        j                         }t        j                   | j                  j                  j#                         |       d {    ||t        j                         |z
  z  }| j                  j$                  ~t        j&                  |      4 d {    | j                  j%                          d {    | j                  j%                  | j                         d {    d d d       d {    n$| j                  j)                  |       d {    | j3                          | j5                          y 7 7 y7 7 7 7 f7 X# 1 d {  7  sw Y   CxY w7 J# t*        t,        j.                  f$ r(}	 | j                  j1                          |# |xY wd }~ww xY ww)NzAPoolConnectionHolder.release() called on a free connection holderru   )r_   r   ro   r/   ri   ra   	_protocolqueries_countr\   rx   rb   rY   _is_cancellingtime	monotonicr   wait_for_wait_for_cancellation_resetrv   resetry   rz   r{   	terminate_releaserr   )r2   rv   budgetstartedr~   s        r+   releasezPoolConnectionHolder.release   sB    <<00+, , 99  99,,0A0AA ))//'/222tzz555 ))//'/222	Fyy""113 ..*ooII''>>@   %dnn.88Fzz  ,!>>&1 7 7))**,,,**++DII6667 7 7 iioofo555 	 	%%'[ 3 37,67 7 7 7 67112 		 		##%b		s   BJ;IAJ;IJ;!A5I7 IAI7 &I'I7 *I I	,I 5I6I :I7 I$I7 *I5+I7 /"J;J;I7 I7 I I I7  I2&I)'I2.I7 7J8J-+J3-J00J33J88J;c                R   K   | j                   y | j                    d {    y 7 wrD   rl   rQ   s    r+   wait_until_releasedz(PoolConnectionHolder.wait_until_released  s      <<,,s   '%'c                n   K   | j                   #| j                   j                          d {    y y 7 wrD   )r/   rx   rQ   s    r+   rx   zPoolConnectionHolder.close  s0     99  ))//### ! $s   *535c                R    | j                   | j                   j                          y y rD   )r/   r   rQ   s    r+   r   zPoolConnectionHolder.terminate  s#    99  II! !r,   c                    | j                   t        j                  d      | j                  r@| j                  j
                  j                  | j                  | j                        | _         y y )Nz/pool connection inactivity timer already exists)r`   r   ro   r^   rY   rZ   
call_later_deactivate_inactive_connectionrQ   s    r+   rr   z-PoolConnectionHolder._setup_inactive_callback  se    "".00AC C ""&*jj&6&6&A&A'')M)M'OD# #r,   c                `    | j                   "| j                   j                          d | _         y y rD   )r`   cancelrQ   s    r+   rq   z4PoolConnectionHolder._maybe_cancel_inactive_callback  s-    "".##**,&*D# /r,   c                    | j                   t        j                  d      | j                  +| j                  j	                          | j                          y y )Nz/attempting to deactivate an acquired connection)r_   r   ro   r/   r   _release_on_closerQ   s    r+   r   z4PoolConnectionHolder._deactivate_inactive_connection"  sU    <<#00AC C 99  II! ""$ !r,   c                R    | j                          | j                          d | _        y rD   )rq   r   r/   rQ   s    r+   r   z&PoolConnectionHolder._release_on_close0  s    ,,.	r,   c                :   | j                   y| j                   j                         s| j                   j                  d       d| _         | j                  !| j                  j	                          d| _        | j
                  j                  j                  |        y)zRelease this connection holder.N)r_   done
set_resultr[   rM   rY   _queue
put_nowaitrQ   s    r+   r   zPoolConnectionHolder._release5  su    <<||  "LL##D) ;;"KK!DK 	

$$T*r,   N)
rd   z'Pool're   floatrf   z:Optional[Callable[[PoolConnectionProxy], Awaitable[None]]]rg   r   r7   rT   )r7   r:   )r7   rT   )r7   rA   )rv   Optional[float]r7   rT   )r;   r<   r=   rU   rH   rj   rm   rs   r   r   r   rx   r   rr   rq   r   r   r   rV   r,   r+   rS   rS   p   s     I// 	/
 J/ !/ 
/*C 	(#J=(~$"O+
%
+r,   rS   c                     e Zd ZdZdZddddddZd Zd Zd Zd	 Z	d
 Z
d Zd Zd.dZd Zdd	 	 	 	 	 d/dZdd	 	 	 d0dZddd	 d1dZddddZddddZddddZddddddddddddddZddddddddddd
dZddddddddddddddddddZdddddd Zddd!Zd" Zddd#Zd$ Zd% Zd& Zd' Zd( Z d) Z!d* Z"d+ Z#d, Z$d- Z%y)2Poola  A connection pool.

    Connection pool can be used to manage a set of connections to the database.
    Connections are first acquired from the pool, then used, and then released
    back to the pool.  Once a connection is released, it's reset to close all
    open cursors and other resources *except* prepared statements.

    Pools are created by calling :func:`~asyncpg.pool.create_pool`.
    )r   rZ   _minsize_maxsize_init_connectr   _connect_args_connect_kwargs_holders_initialized_initializing_closing_closed_connection_class_record_classrb   r]   r\   !_max_inactive_connection_lifetimeN)rs   rf   initr   c               0   t        |      dkD  rt        j                  dt        d       |	t	        j
                         }	|	| _        |dk  rt        d      |dk  rt        d      ||kD  rt        d      |dk  rt        d	      |dk  rt        d
      t        |
t        j                        st        dj                  |
            t        |t        j                        st        dj                  |            || _        || _        g | _        d| _        d| _        d | _        |
| _        || _        d| _        d| _        d| _        ||nt        j4                  | _        || _        || _        || _        || _        || _         || _!        || _"        y )Nr
   zPassing multiple positional arguments to asyncpg.Pool constructor is deprecated and will be removed in asyncpg 0.17.0.  The non-deprecated form is asyncpg.Pool(<dsn>, **kwargs)   )
stacklevelr   z,max_size is expected to be greater than zeroz3min_size is expected to be greater or equal to zeroz!min_size is greater than max_sizez/max_queries is expected to be greater than zerozKmax_inactive_connection_lifetime is expected to be greater or equal to zerozMconnection_class is expected to be a subclass of asyncpg.Connection, got {!r}zErecord_class is expected to be a subclass of asyncpg.Record, got {!r}F)#lenwarningswarnDeprecationWarningrz   get_event_looprZ   
ValueError
issubclassr   r   	TypeErrorr1   r   Recordr   r   r   r   r   r   r   r   r   r   rb   rs   r   r   r   r]   r   r   r\   r   )r2   min_sizemax_sizere    max_inactive_connection_lifetimers   rf   r   r   loopconnection_classrecord_classconnect_argsconnect_kwargss                 r+   rH   zPool.__init__\  s    |q MM0 #q2 <))+D
q=KLLa<EG G h@AA!NOO+a/#$ $ *J,A,AB//5v6F/GI I ,8++16,+?A A ! !"!1)#*#6J<N<N)-
', 	.r,   c                4  K   | j                   r| S | j                  rt        j                  d      | j                  rt        j                  d      d| _        	 | j                          d {    | d| _        d| _         S 7 # d| _        d| _         w xY ww)Nz)pool is being initialized in another taskpool is closedTF)r   r   r   r0   r   _initializerQ   s    r+   _async__init__zPool._async__init__  s     K++;= =<<++,<==!	%""$$$!&D $D	 % "'D $Ds0   ABB .B/B 4BB BBc                  K   t        j                  | j                        | _        t	        | j                        D ]e  }t        | | j                  | j                  | j                        }| j                  j                  |       | j                  j                  |       g | j                  r| j                  d   }|j                          d {    | j                  dkD  ryg }t        t        | j                  d d             D ]8  \  }}|| j                  dz
  k\  r n!|j                  |j                                : t        j                   |  d {    y y y 7 7 	w)N)maxsize)re   rg   rf   r
   )rz   	LifoQueuer   r   rangerS   r\   r   r]   r   appendr   r   rs   	enumeratereversedgather)r2   r   chfirst_chconnect_tasksis         r+   r   zPool._initialize  s5    ''>t}}% 	'A% --"&"H"Hkk	#B MM  $KK""2&	' == }}R(H""$$$}}q  "&xcr0B'CD 7EArDMMA--!((6	7 nnm444 !  % 5s%   CE&E"BE&E$E&$E&c                6    | j                   xs | j                  S )z_Return ``True`` if the pool is closing or is closed.

        .. versionadded:: 0.28.0
        )r   r   rQ   s    r+   
is_closingzPool.is_closing  s    
 ||,t}},r,   c                :    t        d | j                  D              S )zaReturn the current number of connections in this pool.

        .. versionadded:: 0.25.0
        c              3  <   K   | ]  }|j                           y wrD   )rj   .0hs     r+   	<genexpr>z Pool.get_size.<locals>.<genexpr>  s     ;1>>#;s   sumr   rQ   s    r+   get_sizezPool.get_size  s    
 ;T]];;;r,   c                    | j                   S )zaReturn the minimum number of connections in this pool.

        .. versionadded:: 0.25.0
        )r   rQ   s    r+   get_min_sizezPool.get_min_size      
 }}r,   c                    | j                   S )ziReturn the maximum allowed number of connections in this pool.

        .. versionadded:: 0.25.0
        )r   rQ   s    r+   get_max_sizezPool.get_max_size  r   r,   c                :    t        d | j                  D              S )zfReturn the current number of idle connections in this pool.

        .. versionadded:: 0.25.0
        c              3  `   K   | ]&  }|j                         xr |j                          ( y wrD   )rj   rm   r   s     r+   r   z%Pool.get_idle_size.<locals>.<genexpr>  s%     K1>>#3		3Ks   ,.r   rQ   s    r+   get_idle_sizezPool.get_idle_size  s    
 KT]]KKKr,   c                "    |g| _         || _        y)a  Set the new connection arguments for this pool.

        The new connection arguments will be used for all subsequent
        new connection attempts.  Existing connections will remain until
        they expire. Use :meth:`Pool.expire_connections()
        <asyncpg.pool.Pool.expire_connections>` to expedite the connection
        expiry.

        :param str dsn:
            Connection arguments specified using as a single string in
            the following format:
            ``postgres://user:pass@host:port/database?option=value``.

        :param \*\*connect_kwargs:
            Keyword arguments for the :func:`~asyncpg.connection.connect`
            function.

        .. versionadded:: 0.16.0
        N)r   r   )r2   dsnr   s      r+   set_connect_argszPool.set_connect_args  s    * "U-r,   c                  K    | j                   | j                  | j                  | j                  | j                  d| j
                   d {   }t        || j                        s| j                  }|j                   d|j                   }t        |      }|j                  dk(  r|j                  }n|j                   d|j                   }t        j                  d| d| d      | j                  	 | j                  |       d {    |S |S 7 7 
# t        t        j                  f$ r'}	 |j!                          d {  7   |# |xY wd }~ww xY ww)N)r   r   r   .builtinsz9expected pool connect callback to return an instance of 'z', got '')r   r   rZ   r   r   r   
isinstancer<   r;   typer   r0   r   ry   rz   r{   rx   )r2   rG   goodgood_nbadbad_nr~   s          r+   rp   zPool._get_new_connection  sW    !DMM!33++	

 ""
 
 #t556))D($--9Fs)C~~+>>*!CLL>:++8<wa1 
 ::!jjo%% 
s
K
, &w556 

 ))+%%H"Hsg   AEDB+E;D DD ED E6E	E
EEEEEEru   c                  K   | j                         4 d{   } |j                  |g|d|i d{   cddd      d{    S 7 37 7 	# 1 d{  7  sw Y   yxY ww)a  Execute an SQL command (or commands).

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.execute() <asyncpg.connection.Connection.execute>`.

        .. versionadded:: 0.10.0
        Nrv   )r   execute)r2   queryrv   r3   rG   s        r+   r   zPool.executeA  sj      <<> 	D 	DS$UCTC7CC	D 	D 	DC	D 	D 	D 	DsS   A&AA&AAAA&AA&AA&A#AA#A&c                  K   | j                         4 d{   }|j                  |||       d{   cddd      d{    S 7 17 7 	# 1 d{  7  sw Y   yxY ww)aC  Execute an SQL *command* for each sequence of arguments in *args*.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.executemany()
        <asyncpg.connection.Connection.executemany>`.

        .. versionadded:: 0.10.0
        Nru   )r   executemany)r2   commandr3   rv   rG   s        r+   r   zPool.executemanyR  sd       <<> 	I 	IS$HH	I 	I 	IH	I 	I 	I 	IsS   A$A	A$AAAA$AA$AA$A!AA!A$rv   r   c                  K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a-  Run a query and return the results as a list of :class:`Record`.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetch() <asyncpg.connection.Connection.fetch>`.

        .. versionadded:: 0.10.0
        Nr   )r   fetchr2   r   rv   r   r3   rG   s         r+   r   z
Pool.fetche  ss      <<> 	 	S"  )	 	 	 		 	 	 	S   A'AA'AAAA'AA'AA'A$AA$ A'r   columnrv   c                  K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a+  Run a query and return a value in the first row.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchval()
        <asyncpg.connection.Connection.fetchval>`.

        .. versionadded:: 0.10.0
        Nr  )r   fetchval)r2   r   r  rv   r3   rG   s         r+   r  zPool.fetchval|  sp      <<> 	> 	>S%>>%+W> >	> 	> 	>>	> 	> 	> 	>r  c                  K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a  Run a query and return the first row.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchrow() <asyncpg.connection.Connection.fetchrow>`.

        .. versionadded:: 0.10.0
        Nr   )r   fetchrowr  s         r+   r  zPool.fetchrow  ss      <<> 	 	S%  )	 	 	 		 	 	 	r  c                  K   | j                         4 d{   }|j                  ||||       d{   cddd      d{    S 7 27 7 	# 1 d{  7  sw Y   yxY ww)an  Run a query for each sequence of arguments in *args*
        and return the results as a list of :class:`Record`.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchmany()
        <asyncpg.connection.Connection.fetchmany>`.

        .. versionadded:: 0.30.0
        Nr   )r   	fetchmany)r2   r   r3   rv   r   rG   s         r+   r
  zPool.fetchmany  sg      <<> 	 	StW< '  	 	 		 	 	 	sS   A%A
A%AAAA%AA%AA%A"AA"A%)columnsschema_namerv   r1   oids	delimiternullheaderquoteescapeforce_quoteencodingc                  K   | j                         4 d{   }|j                  |||||||||	|
||||       d{   cddd      d{    S 7 <7 7 	# 1 d{  7  sw Y   yxY ww)a;  Copy table contents to a file or file-like object.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_from_table()
        <asyncpg.connection.Connection.copy_from_table>`.

        .. versionadded:: 0.24.0
        N)outputr  r  rv   r1   r  r  r  r  r  r  r  r  )r   copy_from_table)r2   
table_namer  r  r  rv   r1   r  r  r  r  r  r  r  r  rG   s                   r+   r  zPool.copy_from_table  s     6 <<> 	 	S,,'#'! -  	 	 		 	 	 	sT   A/AA/#AAAA/AA/AA/A, A#!A,(A/)
rv   r1   r  r  r  r  r  r  r  r  c                  K   | j                         4 d{   } |j                  |g|||||||||	|
||d d{   cddd      d{    S 7 =7 7 	# 1 d{  7  sw Y   yxY ww)aC  Copy the results of a query to a file or file-like object.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_from_query()
        <asyncpg.connection.Connection.copy_from_query>`.

        .. versionadded:: 0.24.0
        N)r  rv   r1   r  r  r  r  r  r  r  r  )r   copy_from_query)r2   r   r  rv   r1   r  r  r  r  r  r  r  r  r3   rG   s                  r+   r  zPool.copy_from_query  s     2 <<> 	 	S,,, #'! 	 	 		 	 	 	sU   A0AA0$AA AA0AA0AA0A-!A$"A-)A0)r  r  rv   r1   r  freezer  r  r  r  r  r  force_not_null
force_nullr  wherec                  K   | j                         4 d{   }|j                  |||||||||	|
||||||||       d{   cddd      d{    S 7 @7 7 	# 1 d{  7  sw Y   yxY ww)a&  Copy data to the specified table.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_to_table()
        <asyncpg.connection.Connection.copy_to_table>`.

        .. versionadded:: 0.24.0
        N)sourcer  r  rv   r1   r  r  r  r  r  r  r  r  r  r  r  r  )r   copy_to_table)r2   r  r   r  r  rv   r1   r  r  r  r  r  r  r  r  r  r  r  r  rG   s                       r+   r!  zPool.copy_to_table  s     > <<> 	 	S**'#'-%!% +  	 	 		 	 	 	sV   A3AA3'AAAA3AA3AA3A0$A'%A0,A3)r  r  rv   r  c          	        K   | j                         4 d{   }|j                  ||||||       d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)aU  Copy a list of records to the specified table using binary COPY.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_records_to_table()
        <asyncpg.connection.Connection.copy_records_to_table>`.

        .. versionadded:: 0.24.0
        N)recordsr  r  rv   r  )r   copy_records_to_table)r2   r  r#  r  r  rv   r  rG   s           r+   r$  zPool.copy_records_to_table7  sr     & <<> 	 	S22' 3  	 	 		 	 	 	r  c                   t        | |      S )aT  Acquire a database connection from the pool.

        :param float timeout: A timeout for acquiring a Connection.
        :return: An instance of :class:`~asyncpg.connection.Connection`.

        Can be used in an ``await`` expression or with an ``async with`` block.

        .. code-block:: python

            async with pool.acquire() as con:
                await con.execute(...)

        Or:

        .. code-block:: python

            con = await pool.acquire()
            try:
                await con.execute(...)
            finally:
                await pool.release(con)
        )PoolAcquireContext)r2   rv   s     r+   r   zPool.acquireT  s    . "$00r,   c                    K    fd} j                   rt        j                  d       j                           |        d {   S t	        j
                   |              d {   S 7 (7 w)Nc                   K   j                   j                          d {   } 	 | j                          d {   }| _        |S 7 &7 # t        t
        j                  f$ r j                   j                  |         w xY wwrD   )r   getr   ra   ry   rz   r{   r   )r   r}   r2   rv   s     r+   _acquire_implz$Pool._acquire.<locals>._acquire_impln  sr     {{((B	 jjl* & )*w556 &&r*s5   BABA A
A 
B
A 6BBzpool is closingru   )r   r   r0   _check_initr   r   )r2   rv   r*  s   `` r+   _acquirezPool._acquirem  sk     	 ==++,=>>?&((2 2 2 )2s$   AA5A1	#A5,A3-A53A5c                 K   t        |      t        us|j                  j                  | ur%t	        j
                  dj                  |            |j                  y| j                          |j                  j                          |j                  }||j                  }t        j                  |j                  |             d{   S 7 w)a  Release a database connection back to the pool.

        :param Connection connection:
            A :class:`~asyncpg.connection.Connection` object to release.
        :param float timeout:
            A timeout for releasing the connection.  If not specified, defaults
            to the timeout provided in the corresponding call to the
            :meth:`Pool.acquire() <asyncpg.pool.Pool.acquire>` method.

        .. versionchanged:: 0.14.0
            Added the *timeout* parameter.
        zWPool.release() received invalid connection: {connection!r} is not a member of this poolr   N)r   rA   rB   rY   r   r0   r1   r/   r+  _on_releasera   rz   shieldr   )r2   r   rv   r   s       r+   r   zPool.release  s      $77""((4++>>Df) ?E ?+, ,
 ??" 	##%?kkG
 ^^BJJw$78888s   CC	C
Cc                  K   | j                   ry| j                          d| _        d}	 | j                  j	                  d| j
                        }| j                  D cg c]  }|j                          }}t        j                  |  d{    | j                  D cg c]  }|j                          }}t        j                  |  d{    	 ||j                          d| _         d| _        yc c}w 7 lc c}w 7 2# t        t        j                  f$ r | j                           w xY w# ||j                          d| _         d| _        w xY ww)a  Attempt to gracefully close all connections in the pool.

        Wait until all pool connections are released, close them and
        shut down the pool.  If any error (including cancellation) occurs
        in ``close()`` the pool will terminate by calling
        :meth:`Pool.terminate() <pool.Pool.terminate>`.

        It is advisable to use :func:`python:asyncio.wait_for` to set
        a timeout.

        .. versionchanged:: 0.16.0
            ``close()`` now waits until all pool connections are released
            before closing them and the pool.  Errors raised in ``close()``
            will cause immediate pool termination.
        NT<   F)r   r+  r   rZ   r   _warn_on_long_closer   r   rz   r   rx   ry   r{   r   r   )r2   warning_callbackr   release_corosclose_coross        r+   rx   z
Pool.close  sB      <<	"#zz44D,, . 48==B-/&&(BM B..-000 &*]]4!
4K 4..+...  + '')DL!DM!B04.7112 	NN	
  + '')DL!DMsd   'E4D C45D C9D C;6D D D !E4D ;D +D--D0 0"EEc                .    t         j                  d       y )NzPool.close() is taking over 60 seconds to complete. Check if you have any unreleased connections left. Use asyncio.wait_for() to set a timeout for Pool.close().)loggerwarningrQ   s    r+   r2  zPool._warn_on_long_close  s     ' 	(r,   c                    | j                   ry| j                          | j                  D ]  }|j                           d| _         y)z&Terminate all connections in the pool.NT)r   r+  r   r   r2   r   s     r+   r   zPool.terminate  s;    <<-- 	BLLN	r,   c                6   K   | xj                   dz  c_         yw)zExpire all currently open connections.

        Cause all currently open connections to get replaced on the
        next :meth:`~asyncpg.pool.Pool.acquire()` call.

        .. versionadded:: 0.16.0
        r
   N)rb   rQ   s    r+   expire_connectionszPool.expire_connections  s      	As   c                    | j                   s6| j                  rt        j                  d      t        j                  d      | j                  rt        j                  d      y )Nzipool is being initialized, but not yet ready: likely there is a race between creating a pool and using itzpool is not initializedr   )r   r   r   r0   r   rQ   s    r+   r+  zPool._check_init  s^      !! //    ++,EFF<<++,<== r,   c                t    | j                   D ])  }|j                  |j                  j                          + y rD   )r   r/   _drop_local_statement_cacher:  s     r+   _drop_statement_cachezPool._drop_statement_cache  s/    -- 	6Bww"335	6r,   c                t    | j                   D ])  }|j                  |j                  j                          + y rD   )r   r/   _drop_local_type_cacher:  s     r+   _drop_type_cachezPool._drop_type_cache  s/    -- 	1Bww"..0	1r,   c                >    | j                         j                         S rD   )r   	__await__rQ   s    r+   rE  zPool.__await__  s    ""$..00r,   c                B   K   | j                          d {    | S 7 wrD   )r   rQ   s    r+   
__aenter__zPool.__aenter__  s"     !!### 	$s   c                @   K   | j                          d {    y 7 wrD   )rx   )r2   excs     r+   	__aexit__zPool.__aexit__  s     jjls   rD   )r   r9   rv   r   r7   r9   )r   r9   rv   r   )r7   list)&r;   r<   r=   r   rU   rH   r   r   r   r   r   r   r   r   rp   r   r   r   r  r  r
  r  r  r!  r$  r   r,  r   rx   r2  r   r<  r+  r@  rC  rE  rG  rJ  rV   r,   r+   r   r   I  s   I O-b% 5B-<L.0&X "&	DD !	D
 
D, "&II
 !I.  
. 34T > 48d " 7; * !+d (^ )3t : "& 1220 48 $9L+"Z(	>611r,   r   c                  B    e Zd ZdZddZd Z	 	 	 d	 	 	 	 	 	 	 d	dZd Zy)
r&  )rv   r   r   rd   c                <    || _         || _        d | _        d| _        y )NF)rd   rv   r   r   )r2   rd   rv   s      r+   rH   zPoolAcquireContext.__init__  s    		r,   c                   K   | j                   | j                  rt        j                  d      | j                  j                  | j                         d {   | _         | j                   S 7 w)Nz a connection is already acquired)r   r   r   r0   rd   r,  rv   rQ   s    r+   rG  zPoolAcquireContext.__aenter__  sU     ??&$))++,NOO $		 2 24<< @@ As   AA0A.A0Nc                   K   d| _         | j                  }d | _        | j                  j                  |       d {    y 7 wNT)r   r   rd   r   )r2   exc_typeexc_valexc_tbrG   s        r+   rJ  zPoolAcquireContext.__aexit__#  s7      	ooii$$$s   9AAAc                v    d| _         | j                  j                  | j                        j	                         S rP  )r   rd   r,  rv   rE  rQ   s    r+   rE  zPoolAcquireContext.__await__.  s+    	yy!!$,,/99;;r,   )rd   r   rv   r   r7   rT   )NNN)rQ  zOptional[Type[BaseException]]rR  zOptional[BaseException]rS  zOptional[TracebackType]r7   rT   )r;   r<   r=   rU   rH   rG  rJ  rE  rV   r,   r+   r&  r&    sL    9I 37+/*.		%/	% )	% (		%
 
	%<r,   r&  
   iP  g     r@)r   r   re   r   rs   rf   r   r   r   r   r   c               2    t        | f|
|||||	|||||d|S )a8  Create a connection pool.

    Can be used either with an ``async with`` block:

    .. code-block:: python

        async with asyncpg.create_pool(user='postgres',
                                       command_timeout=60) as pool:
            await pool.fetch('SELECT 1')

    Or to perform multiple operations on a single connection:

    .. code-block:: python

        async with asyncpg.create_pool(user='postgres',
                                       command_timeout=60) as pool:
            async with pool.acquire() as con:
                await con.execute('''
                   CREATE TABLE names (
                      id serial PRIMARY KEY,
                      name VARCHAR (255) NOT NULL)
                ''')
                await con.fetch('SELECT 1')

    Or directly with ``await`` (not recommended):

    .. code-block:: python

        pool = await asyncpg.create_pool(user='postgres', command_timeout=60)
        con = await pool.acquire()
        try:
            await con.fetch('SELECT 1')
        finally:
            await pool.release(con)

    .. warning::
        Prepared statements and cursors returned by
        :meth:`Connection.prepare() <asyncpg.connection.Connection.prepare>`
        and :meth:`Connection.cursor() <asyncpg.connection.Connection.cursor>`
        become invalid once the connection is released.  Likewise, all
        notification and log listeners are removed, and ``asyncpg`` will
        issue a warning if there are any listener callbacks registered on a
        connection that is being released to the pool.

    :param str dsn:
        Connection arguments specified using as a single string in
        the following format:
        ``postgres://user:pass@host:port/database?option=value``.

    :param \*\*connect_kwargs:
        Keyword arguments for the :func:`~asyncpg.connection.connect`
        function.

    :param Connection connection_class:
        The class to use for connections.  Must be a subclass of
        :class:`~asyncpg.connection.Connection`.

    :param type record_class:
        If specified, the class to use for records returned by queries on
        the connections in this pool.  Must be a subclass of
        :class:`~asyncpg.Record`.

    :param int min_size:
        Number of connection the pool will be initialized with.

    :param int max_size:
        Max number of connections in the pool.

    :param int max_queries:
        Number of queries after a connection is closed and replaced
        with a new connection.

    :param float max_inactive_connection_lifetime:
        Number of seconds after which inactive connections in the
        pool will be closed.  Pass ``0`` to disable this mechanism.

    :param coroutine connect:
        A coroutine that is called instead of
        :func:`~asyncpg.connection.connect` whenever the pool needs to make a
        new connection.  Must return an instance of type specified by
        *connection_class* or :class:`~asyncpg.connection.Connection` if
        *connection_class* was not specified.

    :param coroutine setup:
        A coroutine to prepare a connection right before it is returned
        from :meth:`Pool.acquire()`.  An example use
        case would be to automatically set up notifications listeners for
        all connections of a pool.

    :param coroutine init:
        A coroutine to initialize a connection when it is created.
        An example use case would be to setup type codecs with
        :meth:`Connection.set_builtin_type_codec() <\
        asyncpg.connection.Connection.set_builtin_type_codec>`
        or :meth:`Connection.set_type_codec() <\
        asyncpg.connection.Connection.set_type_codec>`.

    :param coroutine reset:
        A coroutine to reset a connection before it is returned to the pool by
        :meth:`Pool.release()`.  The function is supposed
        to reset any changes made to the database session so that the next
        acquirer gets the connection in a well-defined state.

        The default implementation calls :meth:`Connection.reset() <\
        asyncpg.connection.Connection.reset>`, which runs the following::

            SELECT pg_advisory_unlock_all();
            CLOSE ALL;
            UNLISTEN *;
            RESET ALL;

        The exact reset query is determined by detected server capabilities,
        and a custom *reset* implementation can obtain the default query
        by calling :meth:`Connection.get_reset_query() <\
        asyncpg.connection.Connection.get_reset_query>`.

    :param loop:
        An asyncio event loop instance.  If ``None``, the default
        event loop will be used.

    :return: An instance of :class:`~asyncpg.pool.Pool`.

    .. versionchanged:: 0.10.0
       An :exc:`~asyncpg.exceptions.InterfaceError` will be raised on any
       attempted operation on a released connection.

    .. versionchanged:: 0.13.0
       An :exc:`~asyncpg.exceptions.InterfaceError` will be raised on any
       attempted operation on a prepared statement or a cursor created
       on a connection that has been released to the pool.

    .. versionchanged:: 0.13.0
       An :exc:`~asyncpg.exceptions.InterfaceWarning` will be produced
       if there are any active listeners (added via
       :meth:`Connection.add_listener()
       <asyncpg.connection.Connection.add_listener>`
       or :meth:`Connection.add_log_listener()
       <asyncpg.connection.Connection.add_log_listener>`) present on the
       connection at the moment of its release to the pool.

    .. versionchanged:: 0.22.0
       Added the *record_class* parameter.

    .. versionchanged:: 0.30.0
       Added the *connect* and *reset* parameters.
    )r   r   r   r   re   r   rs   rf   r   r   r   )r   )r   r   r   re   r   rs   rf   r   r   r   r   r   r   s                r+   create_poolrW  3  sE    ~ )!)I  r,   rD   )#
__future__r   rz   collections.abcr   r   r   r   loggingr   typesr   typingr   r   r	   r    r   r   r   r   	getLoggerr;   r7  r   r   _ConnectionProxyrA   rS   r   r&  r   r   rW  rV   r,   r+   <module>r`     s    #  /      & &      
		8	$/d /dO*55$;#ODV+ V+rG GT< <@m!16!+!6!6%__mr,   