Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Temporary Table in PostgreSQL

4.82/5 (4 votes)
29 Dec 2014CPOL1 min read 23.7K  
Temporary table is a special table that is bound to a transaction or to a session. It means that the data in the temporary table and the definition lasts till the end of the transaction or session.

How to Create a Temporary Table

The syntax to create a temporary table is as provided below:

  • Creating a temporary table transaction-specific, where all rows are deleted on COMMIT.
    SQL
    CREATE [ GLOBAL|LOCAL ] {TEMPORARY | TEMP } TABLE table_name
    ( column_name data_type,
                  ...
                           ...
                                     ... ) ON COMMIT DELETE ROWS;
    
  • Creating a temporary table transaction-specific, where table is dropped on COMMIT.
    SQL
    CREATE [ GLOBAL|LOCAL ] {TEMPORARY | TEMP } TABLE table_name
     ( column_name data_type, 
                   ... 
                            ... 
                                      ... ) ON COMMIT DROP;
  • Creating a temporary table session-specific.
    SQL
    CREATE [ GLOBAL|LOCAL ] {TEMPORARY | TEMP } TABLE table_name
     ( column_name data_type, 
                   ... 
                            ... 
                                      ... ) ON COMMIT PRESERVE ROWS;

Note:

  1. In order to create a temporary table, you can use alternately TEMPORARY and TEMP.
  2. GLOBAL and LOCAL keywords may be used optionally. They are depreciated and don't make any differences in PostgreSQL.

Some Examples of Usage

  • may be used to convey data among triggers for either session or a transaction
  • to store temporarily data for arithmetic

Differences between Temporary Tables in PostgreSQL and Oracle

 PostgreSQLOracleComment
Syntax
SQL
CREATE [ GLOBAL|LOCAL ] 
{TEMPORARY | TEMP} TABLE table_name (
column_name data_type,
…
	…	
		… )[ ON COMMIT 
		{DELETE ROWS|PRESERVE ROWS|DROP }];
SQL
CREATE GLOBAL TEMPORARY TABLE 
table_name (
column_name data_type,
…
      …
            … )[ON COMMIT 
{DELETE ROWS | PRESERVE ROWS}]
  • In Oracle, GLOBAL keyword must be specified.
  • Oracle doesn't support ON COMMIT DROP.
    
VisibilityBoth table definition and data are visible to the current sessionThe data in temporary table is private to each session. The definition of temporary table is visible to all sessions. 

 
   
Definition of temporary tableThe definition isn't stored permanently. Each session must create it.The definition is stored permanently.PostgreSQL doesn't have very important feature that Oracle has: The definiton of temporary table remains after end of session. It's not necessary to perform many DDL operations. These operations shouldn't be a part of the system and should be used only in case to rebuild it.

 
   
DefaultON COMMIT PRESERVE ROWS is default in PostgreSQLON COMMIT DELETE ROWS is default in Oracle 

Related reading about Oracle Global Temporary Table: Highlights of the Oracle database: Global Temporary Table

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)