def connect(dburl, utility=False, verbose=False, encoding=None, allowSystemTableMods=None, logConn=True): if utility: options = '-c gp_session_role=utility' else: options = '' # MPP-13779, et al if allowSystemTableMods in ['dml']: options += ' -c allow_system_table_mods=' + allowSystemTableMods elif allowSystemTableMods is not None: raise Exception('allowSystemTableMods invalid: %s' % allowSystemTableMods) # bypass pgdb.connect() and instead call pgdb._connect_ # to avoid silly issues with : in ipv6 address names and the url string # dbbase = dburl.pgdb dbhost = dburl.pghost dbport = int(dburl.pgport) dbopt = options dbtty = "1" dbuser = dburl.pguser dbpasswd = dburl.pgpass timeout = dburl.timeout cnx = None # All quotation and escaping here are to handle database name containing # special characters like ' and \ and white spaces. # Need to escape backslashes and single quote in db name # Also single quoted the connection string for dbname dbbase = dbbase.replace('\\', '\\\\') dbbase = dbbase.replace('\'', '\\\'') # MPP-14121, use specified connection timeout # Single quote the connection string for dbbase name if timeout is not None: cstr = "dbname='%s' connect_timeout=%s" % (dbbase, timeout) retries = dburl.retries else: cstr = "dbname='%s'" % dbbase retries = 1 # This flag helps to avoid logging the connection string in some special # situations as requested if (logConn == True): (logger.info if timeout is not None else logger.debug)("Connecting to %s" % cstr) for i in range(retries): try: cnx = pgdb._connect_(cstr, dbhost, dbport, dbopt, dbtty, dbuser, dbpasswd) break except pgdb.InternalError, e: if 'timeout expired' in str(e): logger.warning('Timeout expired connecting to %s, attempt %d/%d' % (dbbase, i+1, retries)) continue raise
def connect(dburl, utility=False, verbose=False, encoding=None, allowSystemTableMods=None, upgrade=False, logConn=True): if utility: options = '-c gp_session_role=utility' else: options = '' # MPP-13779, et al if allowSystemTableMods in ['dml']: options += ' -c allow_system_table_mods=' + allowSystemTableMods elif allowSystemTableMods is not None: raise Exception('allowSystemTableMods invalid: %s' % allowSystemTableMods) # gpmigrator needs gpstart to make master connection in maintenance mode if upgrade: options += ' -c gp_maintenance_conn=true' # bypass pgdb.connect() and instead call pgdb._connect_ # to avoid silly issues with : in ipv6 address names and the url string # dbbase = dburl.pgdb dbhost = dburl.pghost dbport = int(dburl.pgport) dbopt = options dbtty = "1" dbuser = dburl.pguser dbpasswd = dburl.pgpass timeout = dburl.timeout cnx = None # MPP-14121, use specified connection timeout # if timeout is not None: cstr = "dbname=%s connect_timeout=%s" % (dbbase, timeout) retries = dburl.retries else: cstr = "dbname=%s" % dbbase retries = 1 # This flag helps to avoid logging the connection string in some special # situations as requested if (logConn == True): (logger.info if timeout is not None else logger.debug)("Connecting to %s" % cstr) for i in range(retries): try: cnx = pgdb._connect_(cstr, dbhost, dbport, dbopt, dbtty, dbuser, dbpasswd) break except pgdb.InternalError, e: if 'timeout expired' in str(e): logger.warning('Timeout expired connecting to %s, attempt %d/%d' % (dbbase, i+1, retries)) continue raise
def connect(dburl, utility=False, verbose=False, encoding=None, allowSystemTableMods=False, logConn=True, unsetSearchPath=True): if utility: options = '-c gp_session_role=utility' else: options = '' # MPP-13779, et al if allowSystemTableMods: options += ' -c allow_system_table_mods=true' # bypass pgdb.connect() and instead call pgdb._connect_ # to avoid silly issues with : in ipv6 address names and the url string # dbbase = dburl.pgdb dbhost = dburl.pghost dbport = int(dburl.pgport) dbopt = options dbtty = "1" dbuser = dburl.pguser dbpasswd = dburl.pgpass timeout = dburl.timeout cnx = None # All quotation and escaping here are to handle database name containing # special characters like ' and \ and white spaces. # Need to escape backslashes and single quote in db name # Also single quoted the connection string for dbname dbbase = dbbase.replace('\\', '\\\\') dbbase = dbbase.replace('\'', '\\\'') # MPP-14121, use specified connection timeout # Single quote the connection string for dbbase name if timeout is not None: cstr = "dbname='%s' connect_timeout=%s" % (dbbase, timeout) retries = dburl.retries else: cstr = "dbname='%s'" % dbbase retries = 1 options = [] #by default, libpq will print WARNINGS to stdout if not verbose: options.append("-c CLIENT_MIN_MESSAGES=ERROR") # set client encoding if needed if encoding: options.append("-c CLIENT_ENCODING=%s" % encoding) cstr += " options='%s'" % " ".join(options) # This flag helps to avoid logging the connection string in some special # situations as requested if (logConn == True): (logger.info if timeout is not None else logger.debug)("Connecting to %s" % cstr) for i in range(retries): try: cnx = pgdb._connect_(cstr, dbhost, dbport, dbopt, dbtty, dbuser, dbpasswd) break except pgdb.InternalError, e: if 'timeout expired' in str(e): logger.warning('Timeout expired connecting to %s, attempt %d/%d' % (dbbase, i+1, retries)) continue raise