Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql PostgresOperator正在获取超时_Postgresql_Airflow - Fatal编程技术网

Postgresql PostgresOperator正在获取超时

Postgresql PostgresOperator正在获取超时,postgresql,airflow,Postgresql,Airflow,我在Postgres中创建了一个函数,其中包含以下语句: FUNCTION SET statement_timeout TO "3600s" SELECT * FROM schema.table_name END FUNCTION 在Airflow中,我使用PostgresOperator执行此功能,但我收到消息[2018-06-01 00:00:01066]{models.py:1595}ERROR-由于语句超时而取消语句 我看到它使用,并使用ps

我在Postgres中创建了一个函数,其中包含以下语句:

FUNCTION
        SET statement_timeout TO "3600s"
     SELECT * FROM schema.table_name
        END 
FUNCTION
在Airflow中,我使用
PostgresOperator
执行此功能,但我收到消息
[2018-06-01 00:00:01066]{models.py:1595}ERROR-由于语句超时而取消语句

我看到它使用,并使用
psycopg2
作为连接器

如我所见,我可以是cli应用程序的超时,而不是数据库的超时


我想知道怎么解决这个问题?我需要在Airflow中配置Psycopg,还是可以使用一些环境变量来设置超时以避免此问题?

您可以通过连接上的Airflow
extras
属性将连接参数传入psycopg2库。在编写本文时,postgres_钩子支持以下参数

['sslmode','sslcert','sslkey','sslrootcert','sslcrl','application\u name','keepalives\u idle']

为了将
语句\u timeout
参数传递给Postgreshake,您需要覆盖Postgreshake的
get\u conn
,以接受所需的参数

类方法重写

class NewPostgresHook(PostgresHook):
    def __init__(self, *args, **kwargs):
        super(NewPostgresHook, self).__init__(*args, **kwargs)

    def get_conn(self):
        conn = self.get_connection(self.postgres_conn_id)
        conn_args = dict(
            host=conn.host,
            user=conn.login,
            password=conn.password,
            dbname=self.schema or conn.schema,
            port=conn.port)
        # check for ssl parameters in conn.extra
        for arg_name, arg_val in conn.extra_dejson.items():
            if arg_name in ['sslmode', 'sslcert', 'sslkey',
                            'sslrootcert', 'sslcrl', 'application_name',
                            'keepalives_idle', 'statement_timeout']:
                conn_args[arg_name] = arg_val

        self.conn = psycopg2.connect(**conn_args)
        return self.conn
然后可以在connection
extras
字段中以JSON字符串的形式指定此参数

Ex.连接附加字段中的JSON字符串


{'statement\u timeout':'3600s'}

为了解决这个问题,我们将使用一个环境变量为Postgres全局设置
statement\u timeout
。谢谢你的回答。