17
17
from . import introspection
18
18
from . import prepared_stmt
19
19
from . import protocol
20
+ from . import serverversion
20
21
from . import transaction
21
22
22
23
@@ -29,7 +30,8 @@ class Connection:
29
30
__slots__ = ('_protocol' , '_transport' , '_loop' , '_types_stmt' ,
30
31
'_type_by_name_stmt' , '_top_xact' , '_uid' , '_aborted' ,
31
32
'_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' )
33
35
34
36
def __init__ (self , protocol , transport , loop , addr , opts , * ,
35
37
statement_cache_size , command_timeout ):
@@ -53,6 +55,15 @@ def __init__(self, protocol, transport, loop, addr, opts, *,
53
55
54
56
self ._listeners = {}
55
57
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
+
56
67
async def add_listener (self , channel , callback ):
57
68
"""Add a listener for Postgres notifications.
58
69
@@ -84,6 +95,21 @@ def get_server_pid(self):
84
95
"""Return the PID of the Postgres server the connection is bound to."""
85
96
return self ._protocol .get_server_pid ()
86
97
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
+
87
113
def get_settings (self ):
88
114
"""Return connection settings.
89
115
@@ -188,8 +214,7 @@ async def _get_statement(self, query, timeout):
188
214
ready = state ._init_types ()
189
215
if ready is not True :
190
216
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 )
193
218
194
219
types = await self ._types_stmt .fetch (list (ready ))
195
220
protocol .get_settings ().register_data_types (types )
0 commit comments