Postgres-使用SELECT和ON COMMIT DROP创建临时表

use*_*sam 3 postgresql temp-tables

在postgres(9.4)中,我试图通过select创建一个临时表并将“提交时提交”应用于同一表。我正在使用以下语法。

CREATE TEMPORARY TABLE t5 ON COMMIT DROP AS select * from test4
Run Code Online (Sandbox Code Playgroud)

它给消息

Query returned successfully: 5 rows affected, 62 ms execution time.
Run Code Online (Sandbox Code Playgroud)

但是当我查询同一张表时,

select * from t5
Run Code Online (Sandbox Code Playgroud)

它引发错误,

ERROR:  relation "t5" does not exist
LINE 1: select * from t5
                      ^
********** Error **********

ERROR: relation "t5" does not exist
SQL state: 42P01
Character: 15
Run Code Online (Sandbox Code Playgroud)

请理解后,让我知道问题出在哪里。

谢谢

Eel*_*lke 7

您需要处于事务中,否则每个查询都将在其自己的事务中执行。

BEGIN; -- start transaction

CREATE TEMPORARY TABLE t5 ON COMMIT DROP AS select * from test4;

select * from t5;

COMMIT; -- drops the temp table
Run Code Online (Sandbox Code Playgroud)