通过 python 执行的 Redshift 查询没有给出任何记录

Redshift query executed through python gives no records

我已经编写了以下查询以通过 Python 执行。

query="""SELECT DISTINCT count(*) FROM PG_TABLE_DEF WHERE schemaname='master'  AND tablename LIKE '%%tmp%%'; """
#print(query)

conn=func_redshiftconnection()
res=conn.execute(sqlalchemy.text(query))
res=res.fetchall()
print(res)                # Prints (0,)

它在数据库中运行良好,但当我通过 Python 和使用 psycopg2 的 SqlAlchemy 执行它时,结果为 0。

我不明白问题出在哪里。感谢阅读

PG_TABLE_DEF 仅在 Redshift 中 returns 有关用户可见的表的信息,换句话说,它只会向您显示模式中的表在变量 search_path 中定义。如果 PG_TABLE_DEF 没有 return 预期结果,请验证 search_path 参数是否设置正确以包含相关架构。
来源 - PG_TABLE_DEF

试试这个 -

mydb=# set search_path="$user",master;

那么运行你的查询-

mydb=# select tablename from pg_table_def where schemaname = 'master';

如果我做出了错误的假设,请发表评论,我会重新调整我的回答。