Question

How to drop the temporary tables in procedures for the same session.

Because I am facing a problem regarding temporary tables in Postgres Sql. If two procedures:

A() having a temporary table say temp(id, name) B() having a temporary table say temp(id, name, address) then if the procedure A is called first and after that procedure B is called then the temp table remains with the structure i.e. temp(id, name) defined in the procedure A and vice versa and the column "address" as defined in procedure B is not found.

Was it helpful?

Solution 2

Temp tables are session scoped. Meaning the same session can see it across procedure calls and SQL calls. Basically the temp table is global to your session.

So your options are this assuming A calls B:

  1. Call procedure A and create temp table within A and do what you want with it. At the end of procedure A drop the temp table. Call B from A, you can now create the temporary table with the same name inside of B and make use of it. As a good habit explicitly drop temp table once your're done with it.

  2. Otherwise use two different names for the temp table.

http://www.postgresql.org/docs/9.2/static/sql-createtable.html "TEMPORARY or TEMP If specified, the table is created as a temporary table. Temporary tables are automatically dropped at the end of a session, or optionally at the end of the current transaction (see ON COMMIT below). Existing permanent tables with the same name are not visible to the current session while the temporary table exists, unless they are referenced with schema-qualified names. Any indexes created on a temporary table are automatically temporary as well."

OTHER TIPS

PostgreSQL doesn't support procedure level temporary tables, so you just cannot do it.

A usage of temporary tables in Pg is little bit different than in other db. In your case, you have to use different names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top