4444
4545registered_nodes = []
4646last_assigned_port = int (random .random () * 16384 ) + 49152 ;
47+ pg_config_data = {}
4748
4849
4950class ClusterException (Exception ): pass
@@ -58,8 +59,6 @@ def __init__(self, name, port):
5859 self .base_dir = tempfile .mkdtemp ()
5960 os .makedirs (self .logs_dir )
6061 self .working = False
61- self .config = {}
62- self .load_pg_config ()
6362
6463 @property
6564 def data_dir (self ):
@@ -82,23 +81,13 @@ def connstr(self):
8281 return "port=%s" % self .port
8382 # return "port=%s host=%s" % (self.port, self.host)
8483
85- def load_pg_config (self ):
86- """ Loads pg_config output into dict """
87- pg_config = os .environ .get ("PG_CONFIG" ) \
88- if "PG_CONFIG" in os .environ else "pg_config"
89-
90- out = subprocess .check_output ([pg_config ])
91- for line in out .split ("\n " ):
92- if line :
93- key , value = line .split ("=" , 1 )
94- self .config [key .strip ()] = value .strip ()
95-
9684 def get_bin_path (self , filename ):
9785 """ Returns full path to an executable """
98- if not "BINDIR" in self .config :
86+ pg_config = get_config ()
87+ if not "BINDIR" in pg_config :
9988 return filename
10089 else :
101- return "%s/%s" % (self . config [ "BINDIR" ] , filename )
90+ return "%s/%s" % (pg_config . get ( "BINDIR" ) , filename )
10291
10392 def init (self , allows_streaming = False ):
10493 """ Performs initdb """
@@ -129,15 +118,20 @@ def init(self, allows_streaming=False):
129118 "listen_addresses = '%s'\n " % self .host )
130119
131120 if allows_streaming :
121+ # TODO: wal_level = hot_standby (9.5)
132122 conf .write (
133- "wal_level = replica\n "
134123 "max_wal_senders = 5\n "
135124 "wal_keep_segments = 20\n "
136125 "max_wal_size = 128MB\n "
137126 "shared_buffers = 1MB\n "
138127 "wal_log_hints = on\n "
139128 "hot_standby = on\n "
140129 "max_connections = 10\n " )
130+ if get_config ().get ("VERSION_NUM" ) < 906000 :
131+ conf .write ("wal_level = hot_standby\n " )
132+ else :
133+ conf .write ("wal_level = replica\n " )
134+
141135 self .set_replication_conf ()
142136
143137 def init_from_backup (self , root_node , backup_name , has_streaming = False , hba_permit_replication = True ):
@@ -336,6 +330,38 @@ def get_username():
336330 """ Returns current user name """
337331 return pwd .getpwuid (os .getuid ())[0 ]
338332
333+ def get_config ():
334+ global pg_config_data
335+
336+ if not pg_config_data :
337+ pg_config_cmd = os .environ .get ("PG_CONFIG" ) \
338+ if "PG_CONFIG" in os .environ else "pg_config"
339+
340+ out = subprocess .check_output ([pg_config_cmd ])
341+ for line in out .split ("\n " ):
342+ if line :
343+ key , value = unicode (line ).split ("=" , 1 )
344+ pg_config_data [key .strip ()] = value .strip ()
345+
346+ # Numeric version format
347+ version_parts = pg_config_data .get ("VERSION" ).split (" " )
348+ pg_config_data ["VERSION_NUM" ] = version_to_num (version_parts [- 1 ])
349+
350+ return pg_config_data
351+
352+ def version_to_num (version ):
353+ """Converts PostgreSQL version to number for easier comparison"""
354+ import re
355+
356+ if not version :
357+ return 0
358+ parts = version .split ("." )
359+ while len (parts ) < 3 : parts .append ("0" )
360+ num = 0
361+ for part in parts :
362+ num = num * 100 + int (re .sub ("[^\d]" , "" , part ))
363+ return num
364+
339365def get_new_node (name ):
340366 global registered_nodes
341367 global last_assigned_port
0 commit comments