PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - Removing Temporary Table

In PostgreSQL, temporary tables are often used to hold intermediate or temporary data during a session. They exist only for the duration of the session and are automatically dropped at the end of it. However, if you want to remove them before the session ends, you can do so explicitly.

Here's how you can manage and remove temporary tables:

1. Creating a Temporary Table:

If you're not familiar, you can create a temporary table using the TEMP or TEMPORARY keyword:

CREATE TEMPORARY TABLE temp_table_name (
    column1 datatype,
    column2 datatype,
    ...
);

or simply:

CREATE TEMP TABLE temp_table_name (
    column1 datatype,
    column2 datatype,
    ...
);

2. Removing a Temporary Table:

To drop or remove a temporary table explicitly:

DROP TABLE temp_table_name;

If you're unsure whether the table exists and want to avoid an error in case it doesn't, you can use the IF EXISTS clause:

DROP TABLE IF EXISTS temp_table_name;

3. Some Points to Remember:

  • Visibility: Temporary tables are only visible to the session that created them. Other sessions, even if run by the same user, cannot see or access the temporary table.

  • Lifecycle: While a temporary table exists for the duration of a session, you can also create them with the ON COMMIT behavior to drop or delete rows when a transaction commits. For example, CREATE TEMP TABLE temp_table_name ( ... ) ON COMMIT DELETE ROWS; will retain the table structure but remove its rows at the end of every transaction.

  • Namespace: Temporary tables are created in a special schema, so a permanent and a temporary table can have the same name. In such cases, the temporary table will hide the permanent table for the duration of the session.

  • Indexes: Just like regular tables, you can create indexes on temporary tables. These indexes are also temporary and are bound to the same lifecycle as the temporary table.

In conclusion, while PostgreSQL manages the lifecycle of temporary tables quite well, you have the flexibility to drop them as needed using the DROP TABLE command. This can be useful in cases where you want to free up resources or avoid holding onto temporary data longer than necessary.

  1. Remove temporary table in PostgreSQL:

    • Drop a specific temporary table.
    DROP TEMPORARY TABLE your_temp_table;
    
  2. Removing temporary table if it exists in PostgreSQL:

    • Safely check for existence before dropping.
    DROP TEMPORARY TABLE IF EXISTS your_temp_table;
    
  3. Dropping all temporary tables in PostgreSQL:

    • Remove all temporary tables in the current session.
    DO $$ DECLARE table_name text; BEGIN
      FOR table_name IN (SELECT table_name FROM information_schema.tables WHERE table_type = 'TEMPORARY') LOOP
        EXECUTE 'DROP TABLE IF EXISTS ' || table_name;
      END LOOP;
    END $$;