def sanitize(url, config):
    """
    Obtains dump of an Postgres database by executing `pg_dump` command and
    sanitizes it's output.

    :param url: URL to the database which is going to be sanitized, parsed by
                Python's URL parser.
    :type url: six.moves.urllib.parse.ParseResult

    :param config: Optional sanitizer configuration to be used for sanitation
                   of the values stored in the database.
    :type config: database_sanitizer.config.Configuration|None
    """
    if url.scheme not in ("postgres", "postgresql", "postgis"):
        raise ValueError("Unsupported database type: '%s'" % (url.scheme,))

    process = subprocess.Popen(
        (
            "pg_dump",
            # Force output to be UTF-8 encoded.
            "--encoding=utf-8",
            # Quote all table and column names, just in case.
            "--quote-all-identifiers",
            # Luckily `pg_dump` supports DB URLs, so we can just pass it the
            # URL as argument to the command.
            "--dbname",
            url.geturl().replace('postgis://', 'postgresql://'),
        ),
        stdout=subprocess.PIPE,
    )

    sanitize_value_line = None
    current_table = None
    current_table_columns = None

    for line in io.TextIOWrapper(process.stdout, encoding="utf-8"):
        # Eat the trailing new line.
        line = line.rstrip("\n")

        # Are we currently in middle of `COPY` statement?
        if current_table:
            # Backslash following a dot marks end of an `COPY` statement.
            if line == "\\.":
                current_table = None
                current_table_columns = None
                yield "\\."
                continue

            if not sanitize_value_line:
                yield line
                continue

            yield sanitize_value_line(line)
            continue

        # Is the line beginning of `COPY` statement?
        copy_line_match = COPY_LINE_PATTERN.match(line)
        if not copy_line_match:
            yield line
            continue

        current_table = copy_line_match.group("table")
        current_table_columns = parse_column_names(copy_line_match.group("columns"))

        sanitize_value_line = get_value_line_sanitizer(
            config, current_table, current_table_columns)

        yield line
Obtains dump of an Postgres database by executing `pg_dump` command and
    sanitizes it's output.

    :param url: URL to the database which is going to be sanitized, parsed by
                Python's URL parser.
    :type url: six.moves.urllib.parse.ParseResult

    :param config: Optional sanitizer configuration to be used for sanitation
                   of the values stored in the database.
    :type config: database_sanitizer.config.Configuration|None