PostgreSQL临时表的范围是什么?

时间:2016-06-27 18:11:00

标签: plpgsql postgresql-9.3

我已经google了很多,而且我有相当不错的阅读理解,但我不明白这个脚本是否可以在我的postgres / postgis框中的多个线程中工作。这是代码:

Do
$do$
DECLARE
    x RECORD;
    b int;       
    begin
    create temp table geoms (id serial, geom geometry) on commit drop;

    for x in select id,geom from asdf loop   

        truncate table geoms;
        insert into geoms (geom) select someGeomfield from sometable where st_intersects(somegeomfield,x.geom);

        ----do something with the records in geoms here...and insert that data somewhere else

    end loop;
end;
$do$

因此,如果我在多个客户端(从Java调用)中运行它,那么geoms临时表的范围会导致问题吗?如果是这样,在PostGres中解决这个问题的任何想法都会有所帮助。

由于

2 个答案:

答案 0 :(得分:4)

你会遇到一个微妙的陷阱,这就是为什么我还没准备好宣布它"安全"是范围是每个会话,但人们经常忘记删除表(因此他们断开连接)。

我认为如果你在完成它之后不需要你的函数来显式删除临时表,你会好得多。这将防止因尝试在同一事务中运行该函数两次而产生的问题。 (在提交时你正在放弃)

答案 1 :(得分:3)

PostgreSQL(或Postgres)中的临时表(PostGres不存在)只是本地的,并且与创建它们的会话相关。因此,没有其他会话(客户端)可以从其他会话中查看临时表。两者(架构和数据)对其他人都是不可见的。您的代码是安全的。