在python脚本里执行

    sql_gp1 = "VACUUM dwd_access_record_inout_temp"
sql_gp2 = "delete from dwd_access_record_inout_temp t where t.indate > (select now()::timestamp-interval '36 hour')"
conn = gputil.connect(logger,target_host,target_user,target_password,target_db)
#gputil是自己导入pgdb写的模块
cur = conn.cursor()
cur.execute(sql_gp1)
cur.execute(sql_gp2)
conn.commit()
报错
    psycopg2.InternalError: VACUUM cannot run inside a transaction block
    
查阅资料
   
 报错的原因是:Psycopg2 会开启一个 新的 transaction 在每次调用 execute()时,而VACUUM需要在transaction之外执行,所以我们需要打开一个 autocommit connection 去执行 vacuum。
    
    
修改后代码:

 conn1 = gputil.connect(logger,target_host,target_user,target_password,target_db)
conn2 = psycopg2.connect(database=target_db, user=target_user, password=target_password,host=target_host)
# conn2.set_isolation_level(0) 如果psycopg是2.4.2版本前的,只能这样写
conn2.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) cur1 = conn1.cursor()
cur2 = conn2.cursor() cur2.execute(sql_gp1) cur1.execute(sql_gp2)
conn1.commit() os.system("python /application/datax2/bin/datax.py /root/test_hgm/access_record_process/dwd_inout_temp.json")
conn1.commit()

  可能遇到的问题

    如果如下这样写,或者 或者自己用其他包写的conn2报下面的错了

conn2=gputil.connect(logger,target_host,target_user,target_password,target_db)

 
    用psycopg2包(pgdb包也是调用的psycopg2)调用connect()
conn2 = psycopg2.connect(database=target_db, user=target_user,password = target_password,host=target_host)

  

 
 

postgrel执行VACUUM报VACUUM cannot run inside a transaction block的更多相关文章

  1. 执行quartz报错java.lang.NoClassDefFoundError: javax/transaction/UserTransaction

    使用maven ,可以在 http://mvnrepository.com 中去查找 pom 配置如何写 <!-- https://mvnrepository.com/artifact/org. ...

  2. idea执行mapreduce报错 Could not locate Hadoop executable: C:\hadoop-3.1.1\bin\winutils.exe

    window执行mapreduce报错 Exception in thread "main" java.lang.RuntimeException: java.io.FileNot ...

  3. Postgresql之VACUUM和VACUUM FULL对比

    VACUUM命令存在两种形式,VACUUM和VACUUM FULL,它们之间的区别见如下表格: 无VACUUM VACUUM VACUUM FULL 删除大量数据之后 只是将删除数据的状态置为已删除, ...

  4. 解决apscheduler报错:Run time of job …… next run at: ……)” was missed by

    在Django中使用apscheduler django_apscheduler 实现定时任务, 来完成数据拉取. 一段时间后发现数据量对不上,遂查日志 发现报错如下: Run time of job ...

  5. vacuum和vacuum full的处理过程

    对于数据库系统的并发控制,KingbaseES采用MVCC(多版本并发控制)进行处理. 这种机制有一个缺点,就是随着时间的推移,数据文件中积累的dead tuples会越来越多. 怎么去清理这些dea ...

  6. 执行mysqld_safe报错:mysqld does not exist or is not executable

    执行mysqld_safe报错: [root@edu data]# /usr/local/mysql5.7/bin/mysqld_safe --user=mysql160427 12:41:28 my ...

  7. 数据库执行sql报错Got a packet bigger than 'max_allowed_packet' bytes及重启mysql

    准备在mysql上使用数据库A,但mysql5经过重装后,上面的数据库已丢失,只得通过之前备份的A.sql重新生成数据库A. 1.执行sql报错 在执行A.sql的过程中,出现如下错误:Got a p ...

  8. selenium执行js报错

    selenium执行js报错 Traceback (most recent call last):    dr.execute_script(js)  File "C:\Python27\l ...

  9. mysql执行update报错1175解决方法

    mysql执行update报错 update library set status=true where 1=1 Error Code: 1175. You are using safe update ...

随机推荐

  1. HDU - 1036

    题意描述很垃圾,后来看别人代码才知道怎么回事:对(题目所给d/总时间:所有时间加起来)四舍五入并取整,然后对结果/60得到用了几分钟:对结果%60得到用了几秒. presentation error一 ...

  2. hibernate核心类及常用方法

    Configuration configure = new Configuration().configure(); SessionFactory factory = configure.buildS ...

  3. P2757 [国家集训队]等差子序列

    P2757 [国家集训队]等差子序列 题目传送门 推荐一篇好题解 此题要求我们在一个序列中找出一个等差子序列. 显然,我们只需要考虑子序列长度len=3的情况,因为在长度为4的子序列中必定有一个长度为 ...

  4. Mysql 多实例 +表损坏

    什么是实例? 进程+多个线程+预分配的内存结构 MySQL多实例: 多个进程+多个线程+多个预分配内存结构 多个配置文件: 1)多个端口 2)多个数据目录 3)多个socket文件 ./mysql_i ...

  5. 手把手详解持续集成之GitLab CI/CD

    一.环境准备 首先需要有一台 GitLab 服务器,然后需要有个项目:这里示例项目以 Spring Boot 项目为例,然后最好有一台专门用来 Build 的机器,实际生产中如果 Build 任务不频 ...

  6. 服务器端 less的安装

    一. 安装 npm apt-get install npm 二. 安装less 在服务器端安装 LESS 的最简单方式就是通过 npm(node 的包管理器), 像这样: $ npm install ...

  7. 编译问题解决:LINK : fatal error LNK1104: 无法打开文件“*.dll”

    一.引言 编译项目的时候,总会遇到些奇怪的问题,比如说以下这种: LINK : fatal error LNK1104: 无法打开文件“..\bin\ICPRegistration.dll” 我在编译 ...

  8. 在GNU/Linux下制作Windows 10安装U盘

    今年春节回家期间,我需要将家里的一台安装了Debian Stretch的ZaReason笔记本电脑更换为Windows 10系统,好让爸妈从老台式机上的XP系统升级到新的平台上来.回家前,小仙女已在微 ...

  9. 下拉框、下拉控件之Select2。自动补全的使用

    参考链接: 参考一:https://blog.csdn.net/weixin_36146275/article/details/79336158 参考二:https://www.cnblogs.com ...

  10. Angular动画——路由动画及高阶动画函数

    一.路由动画 路由动画需要在host元数据中指定触发器.动画注意不要过多,否则适得其反. 内容优先,引导用户去注意到某个内容.动画只是辅助手段. 定义一个进场动画,一个离场动画. 因为进场动画和离场动 ...