1717from . import introspection
1818from . import prepared_stmt
1919from . import protocol
20+ from . import serverversion
2021from . import transaction
2122
2223
@@ -29,7 +30,8 @@ class Connection:
2930 __slots__ = ('_protocol' , '_transport' , '_loop' , '_types_stmt' ,
3031 '_type_by_name_stmt' , '_top_xact' , '_uid' , '_aborted' ,
3132 '_stmt_cache_max_size' , '_stmt_cache' , '_stmts_to_close' ,
32- '_addr' , '_opts' , '_command_timeout' , '_listeners' )
33+ '_addr' , '_opts' , '_command_timeout' , '_listeners' ,
34+ '_server_version' , '_intro_query' )
3335
3436 def __init__ (self , protocol , transport , loop , addr , opts , * ,
3537 statement_cache_size , command_timeout ):
@@ -53,6 +55,15 @@ def __init__(self, protocol, transport, loop, addr, opts, *,
5355
5456 self ._listeners = {}
5557
58+ ver_string = self ._protocol .get_settings ().server_version
59+ self ._server_version = \
60+ serverversion .split_server_version_string (ver_string )
61+
62+ if self ._server_version < (9 , 2 ):
63+ self ._intro_query = introspection .INTRO_LOOKUP_TYPES_91
64+ else :
65+ self ._intro_query = introspection .INTRO_LOOKUP_TYPES
66+
5667 async def add_listener (self , channel , callback ):
5768 """Add a listener for Postgres notifications.
5869
@@ -84,6 +95,21 @@ def get_server_pid(self):
8495 """Return the PID of the Postgres server the connection is bound to."""
8596 return self ._protocol .get_server_pid ()
8697
98+ def get_server_version (self ):
99+ """Return the version of the connected PostgreSQL server.
100+
101+ The returned value is a named tuple similar to that in
102+ ``sys.version_info``:
103+
104+ .. code-block:: pycon
105+
106+ >>> con.get_server_version()
107+ ServerVersion(major=9, minor=6, micro=1,
108+ releaselevel='final', serial=0)
109+
110+ """
111+ return self ._server_version
112+
87113 def get_settings (self ):
88114 """Return connection settings.
89115
@@ -188,8 +214,7 @@ async def _get_statement(self, query, timeout):
188214 ready = state ._init_types ()
189215 if ready is not True :
190216 if self ._types_stmt is None :
191- self ._types_stmt = await self .prepare (
192- introspection .INTRO_LOOKUP_TYPES )
217+ self ._types_stmt = await self .prepare (self ._intro_query )
193218
194219 types = await self ._types_stmt .fetch (list (ready ))
195220 protocol .get_settings ().register_data_types (types )
0 commit comments