Skip to content

Commit 33a0afa

Browse files
author
James William Pye
committed
Make the startup_data queries more robust.
The connection shouldn't fail because the usual data is not available.
1 parent e535ffa commit 33a0afa

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

postgresql/driver/pq3.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,21 +2050,22 @@ def connect(self):
20502050
# manual binding
20512051
self.sys = pg_lib.Binding(self, pg_lib.sys)
20522052

2053-
if self.version_info[:2] <= (8,0):
2054-
meth = self.sys.startup_data_only_version
2053+
vi = self.version_info[:2]
2054+
if vi <= (8,1):
2055+
sd = self.sys.startup_data_only_version()
20552056
else:
2056-
meth = self.sys.startup_data
2057+
sd = self.sys.startup_data()
20572058
# connection info
20582059
self.version, self.backend_start, \
2059-
self.client_address, self.client_port = meth()
2060+
self.client_address, self.client_port = sd
20602061

20612062
# First word from the version string.
20622063
self.type = self.version.split()[0]
20632064

20642065
##
20652066
# Set standard_conforming_strings
20662067
scstr = self.settings.get('standard_conforming_strings')
2067-
if scstr is None or (self.version_info[0] == 8 and self.version_info[1] == 1):
2068+
if scstr is None or vi == (8,1):
20682069
# There used to be a warning emitted here.
20692070
# It was noisy, and had little added value
20702071
# over a nice WARNING at the top of the driver documentation.

postgresql/lib/libsys.sql

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,37 @@ FROM
115115
pg_catalog.generate_series(1, array_upper(($1::text[][]), 1)) g(i)
116116

117117
[startup_data:transient:first]
118+
-- 8.2 and greater
118119
SELECT
119-
pg_catalog.version()::text,
120+
pg_catalog.version()::text AS version,
120121
backend_start::text,
121122
client_addr::text,
122-
client_port
123-
FROM pg_catalog.pg_stat_activity WHERE procpid = pg_catalog.pg_backend_pid();
123+
client_port::int
124+
FROM pg_catalog.pg_stat_activity WHERE procpid = pg_catalog.pg_backend_pid()
125+
UNION ALL SELECT
126+
pg_catalog.version()::text AS version,
127+
NULL::text AS backend_start,
128+
NULL::text AS client_addr,
129+
NULL::int AS client_port
130+
LIMIT 1;
124131

125132
[startup_data_no_start:transient:first]
133+
-- 8.1 only, but is unused as often the backend's activity row is not
134+
-- immediately present.
126135
SELECT
127-
pg_catalog.version()::text,
136+
pg_catalog.version()::text AS version,
128137
NULL::text AS backend_start,
129138
client_addr::text,
130-
client_port
139+
client_port::int
131140
FROM pg_catalog.pg_stat_activity WHERE procpid = pg_catalog.pg_backend_pid();
132141

133142
[startup_data_only_version:transient:first]
143+
-- In 8.0, there's nothing there.
134144
SELECT
135-
pg_catalog.version()::text,
145+
pg_catalog.version()::text AS version,
136146
NULL::text AS backend_start,
137147
NULL::text AS client_addr,
138-
NULL::text AS client_port;
148+
NULL::int AS client_port;
139149

140150
[sizeof_db:transient:first]
141151
SELECT pg_catalog.pg_database_size(current_database())::bigint

0 commit comments

Comments
 (0)