假设我打开一个新的npgsqlconnection
,并创建一个新的临时表temp1
,然后打开另一个新的连接。据我了解,临时表仅适用于打开它的会话,并且两个打开的连接不应共享同一会话。在这里,连接字符串是相同的,我尝试关闭池,但这并没有改变任何内容。伪代码为:
var conn1 = new NpgsqlConnection(MyConnectionString)
var conn2 = new NpgsqlConnection(MyConnectionString)
conn1.Open()
conn2.Open()
conn1.Execute("CREATE TEMP TABLE temp1(idx int)")
如果我对两个连接都执行查询SELECT COUNT(*) FROM pg_tables WHERE tablename = 'temp1'
,此查询将返回1。为什么conn2
能够访问在conn1
上创建的临时表?无论如何,有什么预防方法?
为什么conn2能够访问在conn1上创建的临时表?
不能。
其他连接可以通过系统目录看到一个表,但是他们无法访问它。
-- Connection 1
test=# SELECT schemaname FROM pg_tables WHERE tablename = 'temp1';
schemaname
------------
pg_temp_3
(1 row)
-- Connection 2
test=# select * from pg_temp_3.temp1;
ERROR: cannot access temporary tables of other sessions