Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / SQL
Tip/Trick

Temporary Table in PostgreSQL

Rate me:
Please Sign up or sign in to vote.
4.82/5 (4 votes)
29 Dec 2014CPOL1 min read 23.3K   3   1
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)


Written By
Technical Writer Vertabelo
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseMy vote of 5! Pin
AlexeyYakovlev13-Mar-17 1:17
professionalAlexeyYakovlev13-Mar-17 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.