Question

When I run a script in PostgreSQL I usually do the following from psql:

my_database> \i my_script.sql

Where in my_script.sql I may have code like the following:

select a.run_uid, s.object_uid into temp_table from dt.table_run_group as a 
inner join dt.table_segment as s on a.group_uid = s.object_uid; 

In this particular case, I am only interested in creating temp_table with the results of the query.

Are these results in disk on the server? In memory? Is the table stored permanently?

Was it helpful?

Solution

  • Temporary tables are stored in RAM until the available memory is used up, at which time they spill onto disk. The relevant setting here is temp_buffers.

  • Either way, they live for the duration of a session and are dropped at the end automatically.

  • You can also drop them at the end of a transaction automatically (ON COMMIT DROP) or manually any time.

  • Temporary table are only visible to the the same user in the same session. Others cannot access it - and also not conflict with it.

  • Always use CREATE TABLE tbl AS .... The alternative form SELECT ... INTO tbl is discouraged since it conflicts with the INTO clause in plpgsql.

Your query could look like:

CREATE TEMP TABLE tbl AS
SELECT a.run_uid, s.object_uid
FROM   dt.table_run_group a 
JOIN   dt.table_segment   s ON a.group_uid = s.object_uid;

OTHER TIPS

SELECT INTO table ... is the same as CREATE TABLE table AS ..., which creates a normal, permanent table.

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