1

Postgres 版本:11

程序代码:

CREATE OR REPLACE PROCEDURE public.prc_insert_proc(table_name character varying)
 LANGUAGE plpgsql
AS $procedure$ declare StartTime timestamptz;

EndTime timestamptz;

Delta double precision;
err_context text;
tab_partition RECORD;
v_tab_partition varchar;
begin

for tab_partition in (SELECT
    child.relname       AS child
FROM pg_inherits
    JOIN pg_class parent            ON pg_inherits.inhparent = parent.oid
    JOIN pg_class child             ON pg_inherits.inhrelid   = child.oid
    JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
    JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace
WHERE parent.relname=table_name) loop
v_tab_partition = tab_partition.child;
begin
execute 'insert
    into
        test_tab (id,
    report_date,
        effective_asof_date,
        effective_until_date,
        active_flag) select id,
      to_date(report_date,''YYMMDD'') report_date,
            now(),
            ''31-Dec-2200'',
            ''Y''
        from '||
            tab_partition.child ||'  on
            conflict (id) do update set report_date=excluded.report_date;';
commit;
end;

end loop;



exception
    when others then
        GET STACKED DIAGNOSTICS err_context = PG_EXCEPTION_CONTEXT;
        RAISE INFO 'Error Name:%',SQLERRM;
        RAISE INFO 'Error State:%', SQLSTATE;
        RAISE INFO 'Error Context:%', err_context;

end;

$procedure$;

我通过创建 sqlalchemy 引擎然后调用此过程来调用上述过程。

def load_test(session, table, log):  # pragma: no cover
    try:
        sql = "call prc_insert_proc('" + \
            table + "');"
        session.execute(text(sql).execution_options(autocommit=False))
        session.commit()
    except ErrorBase as e:
        log.error(f"{str(e)}")
        raise

它抛出以下错误。会话是使用 sessionmaker 和 create_engine 创建的。

engine = create_engine(config.connection_string, echo=False)
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()

无效的交易终止

我对 sqlalchemy 和 PostgreSQL 都很陌生。表的每个分区都有大约一百万条记录,我想在每次插入后提交。其他选项是在 python 脚本中获取分区列表并在过程中添加分区名称参数。循环遍历python中的过程并在每次调用后提交。但是,当我在 PostgreSQL 11 中读到这一点时,我们可以在过程中提交,而不是我想用的。

4

0 回答 0