postgresql create temp table could block data insertion?

白昼怎懂夜的黑 提交于 2020-01-07 04:53:04

问题


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?

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?

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


回答1:


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.

See more about postgresql explicit-locking

Consider using view instead of creating temp table.

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.



来源:https://stackoverflow.com/questions/44479118/postgresql-create-temp-table-could-block-data-insertion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!