Click here to Skip to main content
13,894,216 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

16.6K views
3 bookmarked
Posted 29 Dec 2014
Licenced CPOL

Temporary Table in PostgreSQL

, 29 Dec 2014
Rate this:
Please Sign up or sign in to vote.
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.
    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.
    CREATE [ GLOBAL|LOCAL ] {TEMPORARY | TEMP } TABLE table_name
     ( column_name data_type, 
                   ... 
                            ... 
                                      ... ) ON COMMIT DROP;
  • Creating a temporary table session-specific.
    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
CREATE [ GLOBAL|LOCAL ] 
{TEMPORARY | TEMP} TABLE table_name (
column_name data_type,
…
	…	
		… )[ ON COMMIT 
		{DELETE ROWS|PRESERVE ROWS|DROP }];
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)

Share

About the Author

Patrycja Dybka
Technical Writer Vertabelo
Poland Poland
No Biography provided

You may also be interested in...

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 29 Dec 2014
Article Copyright 2014 by Patrycja Dybka
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid