def METHOD_NAME(domain, doc_type, startdate, enddate):
    sql_ids = set()
    state = doc_type_to_state[doc_type]
    for db_alias in get_db_aliases_for_partitioned_query():
        queryset = XFormInstance.objects.using(db_alias) \
            .filter(domain=domain, state=state) \
            .exclude(xmlns=DEVICE_LOG_XMLNS)
        if startdate:
            queryset = queryset.filter(received_on__gte=startdate)
        if enddate:
            queryset = queryset.filter(received_on__lt=enddate)
        sql_ids.update(list(queryset.values_list('form_id', flat=True)))
    return sql_ids