PostgreSQL创建临时表可能会阻止数据插入? [英] postgresql create temp table could block data insertion?

查看:219
本文介绍了PostgreSQL创建临时表可能会阻止数据插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在后端使用PostgreSQL 8.4。在后端,我做了一个postgres函数来获取一些数据。该功能不会将任何数据写入数据库,因此只需从其他表中读取数据即可。该函数将在内部创建一个临时表,然后返回一组记录。

I am using postgresql 8.4 in backend. In backend I made a postgres function to get some data. The function won't write any data to DB so just read data from other tables. The function internally will create a temp table then return a set of records.

当我监视服务器时,我发现此函数正在阻止其他连接进行数据插入。

When I monitoring the server I found out this function is blocking other connections doing data insertion.

所以只是想知道创建临时表会阻止其他连接的数据插入吗?

So just wondering creating temp table could block data insertion from other connections?

其他问题。我有一个函数A,此函数内部有一个对B的函数调用,函数B将返回一组记录(该记录有两列,分别表示名称,电子邮件)。然后,我需要在函数A中使用一个变量来保存从函数B返回的数据。当从B返回的数据时,我将使用第一列进行操作,然后将第二列用于其他工作人员。
因此,此刻我使用临时表来保存从函数B返回的setof记录。还有另一种方法来保存返回的记录吗?

Further question. I have a function A, inside this function there is a function call to B, function B will return a set of records (the record has two columns say "name","email"). Then I need a variable in function A to hold the data returned from function B. When data returned from B I will use the first column to do something then use the second col to other staff. So at the moment I use temp table to hold the returned setof records from function B. Is there another way to hold returned records?

找到了这个不确定的对象是否有用:
https://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/ pgSQL_functions

Found this not sure helpful or not: https://wiki.postgresql.org/wiki/Return_more_than_one_row_of_data_from_PL/pgSQL_functions

推荐答案

您应该避免创建此类功能,例如在多会话环境中,甚至只是使用来自同一用户会话的多行请求,服务器将尝试为每个用户会话+每个查询行创建临时表。
由于多个请求需要创建相同的资源(您的临时表)-当然,服务器只能同时处理一个请求的一行。

You should def avoid creating such kind of functions, as in multi-session environment, or even just using multi-row requests from the same user session, server will try to create the temp table per each user session + per each queried row. As multiple requests need to create the same resource (your temp table) - sure, server can handle just one row of one request at the same time.

查看有关 postgresql显式锁定

考虑使用视图而不是创建临时表。

Consider using view instead of creating temp table.

P.S。您关于查询不写任何数据,只是读取的陈述是不正确的。查询实际上在执行过程中执行DDL和DML操作。

P.S. Your statement that query does not write any data, just reads, is not true. Query actually does DDL and DML operations during its execution.

这篇关于PostgreSQL创建临时表可能会阻止数据插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆