Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8141979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:40:59+00:00 2026-06-06T12:40:59+00:00

I have a table called tester which I need to overlay with a temporary

  • 0

I have a table called tester which I need to overlay with a temporary table in order to do some testing. So far I have managed that using:

CREATE TEMPORARY TABLE tester AS
  SELECT * FROM tester WHERE name='bananas';

This temporary table will disappear at the end of the session which is fine by me, however it is the external application that is responsible for the connection and it has a tendency to cache connections for reuse (thus there is no guarantee that the temporary table has been dropped). This means that if I try to execute the above query again it may fail because the temporary table still exists.

Explicitly dropping the temporary table is not really an option. I’d rather live with the failed query than risk dropping the underlying “real” table by mistake.

What I would like to do is only create the temporary table if it doesn’t already exist.
I did find this query which people suggested should be used when checking to see if a table exists:

SELECT * FROM pg_catalog.pg_class WHERE relkind = 'r' AND relname = 'tester';

result when temporary table exists:

 relname | relnamespace | reltype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | reltoastrelid | reltoastidxid | relhasindex | relisshared | relkind | relnatts | relchecks | reltriggers | relukeys | relfkeys | relrefs | relhasoids | relhaspkey | relhasrules | relhassubclass | relfrozenxid | relacl | reloptions
---------+--------------+---------+----------+-------+-------------+---------------+----------+-----------+---------------+---------------+-------------+-------------+---------+----------+-----------+-------------+----------+----------+---------+------------+------------+-------------+----------------+--------------+--------+------------
 tester  |         2200 | 1533065 |  1531747 |     0 |     1533063 |             0 |        0 |         0 |       1533067 |             0 | t           | f           | r       |        3 |         0 |           0 |        0 |        0 |       0 | f          | t          | f           | f              |     17654031 |        |
 tester  |      1533088 | 1533065 |  1531747 |     0 |     1533160 |             0 |        0 |         0 |       1533163 |             0 | f           | f           | r       |        3 |         0 |           0 |        0 |        0 |       0 | f          | f          | f           | f              |     17654066 |        |

As you can see, there are two records. One for the original table, and one for the temp table. The second record is the temporary table – I know this because it disappears when working in a different session – but other than the lack of an index or a primary key that’s the only way I can identify it as being temporary. This is only a test table – some of the real tables I’m working with may lack indexes and/or primary keys.

Question (tl;dr edition): How can I test if a temporary table exists if there is already a “real” table with the same name? (or at least identify that a table is temporary?)

disclaimer: I added the Java tag because that’s what I’m using to execute queries (the SQL is only a small part of the application). Even though Java is not directly relevant to the question, I kinda need than answer that I can use from a Java context.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:41:00+00:00Added an answer on June 6, 2026 at 12:41 pm

    User Defined Function to check whether temp table exists.

    CREATE or REPLACE FUNCTION public.iftableexists( varchar)
    RETURNS pg_catalog.bool AS
    $BODY$
    DECLARE
    
     BEGIN
    
         /* check the table exist in database and is visible*/
     perform n.nspname ,c.relname
    FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid
    = c.relnamespace
    where n.nspname like 'pg_temp_%' AND pg_catalog.pg_table_is_visible(c.oid)
    AND Upper(relname) = Upper($1);
    
         IF FOUND THEN
            RETURN TRUE;
         ELSE
            RETURN FALSE;
         END IF;
    
     END;
    $BODY$
    LANGUAGE 'plpgsql' VOLATILE
    

    See here for more details

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have table called type which contains data family,individual i want in some pages
I have table in my SQL Server database called OrderDetail which stores the order
i have table called as Support, which have a field named Name and contains
I have a table called scheduler_sched which has several columns, including a column called
I have table called page which represents every single page in my website. page_id
Hi I have table called Sold Products which stores buying information for product &
In my db I a have table called users. In that table I have
I have table called FinalForgotten which only contains one field called aname. The field
Have a table called person. which has personid and contactid. if a person is
I have a table called ticket in which I want to swap two rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.