def determine_schema_privileges(role, dbcontext):
    """
    Identify and return two lists:
        1) a list of schemas this role can write to
        2) a list of schemas this role can read from

    If a role can write it to a schema is assumed it can read as well, so that schema will only
    show up in the write list. As a result of this, we determine schemas with write access first
    so we can exclude write schemas from the read schemas list.

    Note that in Postgres if you own a schema you will always have read (USAGE) access to that
    schema but you _may not_ have write (CREATE) access to it, though you do by default. Because
    this is confusing and non-intuitive, we will include schemas in the write section even if they
    are owned by this role (and because they are in the write section then they will also be granted
    read privileges as well).
    """
    # Make a copy of personal_schemas (by making a new set from it) as we will be mutating it
    personal_schemas = set(dbcontext.get_all_personal_schemas())

    # Get all schemas this role has write and read access to
    write_schemas_and_owners = dbcontext.get_role_current_nondefaults(role, 'schemas', 'write')
    read_schemas_and_owners = dbcontext.get_role_current_nondefaults(role,  'schemas', 'read')

    write_schemas = {s for s, _ in write_schemas_and_owners}
    read_schemas = {s for s, _ in read_schemas_and_owners}

    # Get all schemas owned by this role
    all_owned_schemas = dbcontext.get_all_schemas_and_owners()
    role_owned_schemas = {s for s, owner in all_owned_schemas.items() if owner == role}

    # Add all schemas owned by this role to the write and read schemas
    write_schemas.update(role_owned_schemas)
    read_schemas.update(role_owned_schemas)

    # Remove this role's personal schema if it exists
    write_schemas.difference_update({role})
    read_schemas.difference_update({role})
    personal_schemas.difference_update({role})

    # If all personal schemas are in write_schemas then replace them with 'personal_schemas'
    if personal_schemas and personal_schemas.difference(write_schemas) == set():
        write_schemas.difference_update(personal_schemas)
        write_schemas.add('personal_schemas')

    if personal_schemas and personal_schemas.difference(read_schemas) == set():
        read_schemas.difference_update(personal_schemas)
        read_schemas.add('personal_schemas')

    # Remove all schemas this role has write access to
    read_only_schemas = read_schemas.difference(write_schemas)

    schemas_privs = {}
    if write_schemas:
        schemas_privs['write'] = sorted(write_schemas)
    if read_only_schemas:
        schemas_privs['read'] = sorted(read_only_schemas)

    return schemas_privs