def table_exists_sql(any_schema=False):
    'SQL to check for existence of a table. Note that for temp tables, any_schema should be set to True.'
    if (not any_schema):
        schema_filter_sql = sql.SQL('AND schemaname = current_schema()')
    else:
        schema_filter_sql = sql.SQL('')
    return sql.SQL('SELECT EXISTS (SELECT 1\n                                       FROM pg_tables\n                                      WHERE tablename = %s\n                                            {schema_filter_sql})').format(schema_filter_sql=schema_filter_sql)
SQL to check for existence of a table. Note that for temp tables, any_schema should be set to True.
def table_exists_sql(any_schema=False):
    
    if (not any_schema):
        schema_filter_sql = sql.SQL('AND schemaname = current_schema()')
    else:
        schema_filter_sql = sql.SQL()
    return sql.SQL('SELECT EXISTS (SELECT 1\n                                       FROM pg_tables\n                                      WHERE tablename = %s\n                                            {schema_filter_sql})').format(schema_filter_sql=schema_filter_sql)