# Buggy code
def _fetch_option(cfg, ret_config, virtualname, attr_name):
    """
    Fetch a given option value from the config.

    @see :func:`get_returner_options`
    """
    # c_cfg is a dictionary returned from config.option for
    # any options configured for this returner.
    if isinstance(cfg, dict):
        c_cfg = cfg
    else:
        c_cfg = cfg("{0}".format(virtualname), {})

    default_cfg_key = "{0}.{1}".format(virtualname, attr_name)
    if not ret_config:
        # Using the default configuration key
        if isinstance(cfg, dict):
            return c_cfg.get(attr_name, cfg.get(default_cfg_key))
        else:
            return c_cfg.get(attr_name, cfg(default_cfg_key))

    # Using ret_config to override the default configuration key
    ret_cfg = cfg("{0}.{1}".format(ret_config, virtualname), {})

    override_default_cfg_key = "{0}.{1}.{2}".format(
        ret_config,
        virtualname,
        attr_name,
    )
    override_cfg_default = cfg(override_default_cfg_key)

    # Look for the configuration item in the override location
    ret_override_cfg = ret_cfg.get(attr_name, override_cfg_default)
    if ret_override_cfg:
        return ret_override_cfg

    # if not configuration item found, fall back to the default location.
    return c_cfg.get(attr_name, cfg(default_cfg_key))


# Traceback (SaltMasterError)
[17:16:50.317] [ERROR   ] postgres returner could not connect to database: FATAL:  password authentication failed for user "root"
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/client/ssh/__init__.py", line 619, in run
self.returners['{0}.save_load'.format(self.opts['master_job_cache'])](jid, job_load)
File "/usr/lib/python2.7/site-packages/salt/returners/postgres.py", line 267, in save_load
with _get_serv(commit=True) as cur:
File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/lib/python2.7/site-packages/salt/returners/postgres.py", line 204, in _get_serv
raise salt.exceptions.SaltMasterError('postgres returner could not connect to database: {exc}'.format(exc=exc))
SaltMasterError: postgres returner could not connect to database: FATAL:  password authentication failed for user "root"

# Fixed code
def _fetch_option(cfg, ret_config, virtualname, attr_name):
    """
    Fetch a given option value from the config.

    @see :func:`get_returner_options`
    """
    # c_cfg is a dictionary returned from config.option for
    # any options configured for this returner.
    if isinstance(cfg, dict):
        c_cfg = cfg
    else:
        c_cfg = cfg("{0}".format(virtualname), {})

    default_cfg_key = "{0}.{1}".format(virtualname, attr_name)
    if not ret_config:
        # Using the default configuration key
        if isinstance(cfg, dict):
            return c_cfg.get(default_cfg_key, cfg.get(attr_name))
        else:
            return c_cfg.get(attr_name, cfg(default_cfg_key))

    # Using ret_config to override the default configuration key
    ret_cfg = cfg("{0}.{1}".format(ret_config, virtualname), {})

    override_default_cfg_key = "{0}.{1}.{2}".format(
        ret_config,
        virtualname,
        attr_name,
    )
    override_cfg_default = cfg(override_default_cfg_key)

    # Look for the configuration item in the override location
    ret_override_cfg = ret_cfg.get(attr_name, override_cfg_default)
    if ret_override_cfg:
        return ret_override_cfg

    # if not configuration item found, fall back to the default location.
    return c_cfg.get(attr_name, cfg(default_cfg_key))