【发布时间】:2018-04-03 00:28:32
【问题描述】:
我正在从Python 循环多次执行自写的postgresql 函数。我正在使用psycopg2 框架来执行此操作。
我写的函数结构如下:
CREATE OR REPLACE FUNCTION my_func()
RETURNS void AS
$$
BEGIN
-- create a temporary table that should be deleted after
-- the functions finishes
-- normally a CREATE TABLE ... would be here
CREATE TEMPORARY TABLE temp_t
(
seq integer,
...
) ON COMMIT DROP;
-- now the insert
INSERT INTO temp_t
SELECT
...
END
$$
LANGUAGE 'plpgsql';
这基本上就是python部分
import time
import psycopg2
conn = psycopg2.connect(host="localhost", user="user", password="...", dbname="some_db")
cur = conn.cursor()
for i in range(1, 11):
print i
print time.clock()
cur.callproc("my_func")
print time.clock()
cur.close()
conn.close()
我在运行python 脚本时遇到的错误是:
---> relation "temp_t" already exists
基本上我想测量执行该功能需要多长时间。这样做,循环将运行多次。将SELECT 的结果存储在临时表中应该替换通常会创建输出表的CREATE TABLE ... 部分
我从Python执行函数后,为什么postgres不删除函数?
【问题讨论】:
标签: python postgresql psycopg2