Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary table postgresql function

I can't find a clear explanation of the syntax to create (and use) tables just for the inside calculations of a function. Could anyone give me a syntax exemple please ?

From what I've found, I have tried this (with and without @ before temp_table) :

CREATE FUNCTION test.myfunction()
RETURNS SETOF test.out_table
AS $$

DECLARE @temp_table TABLE
( 
        id int,
        value text
 )
BEGIN
 INSERT INTO @temp_table 
        SELECT id, value
        FROM test.another_table;

 INSERT INTO test.out_table
        SELECT id, value
        FROM @temp_table;
RETURN END
$$ LANGUAGE SQL;

I get :

ERROR: syntax error at or near "DECLARE" LINE 5: DECLARE @temp_table TABLE

-

I also tried the CREATE TABLE approach suggested here, this way :

CREATE FUNCTION test.myfunction()
RETURNS SETOF test.out_table
AS $$

    CREATE TABLE temp_table AS
        SELECT id, value
        FROM test.another_table;

    INSERT INTO test.out_table
        SELECT id, value
        FROM temp_table;

$$ LANGUAGE SQL;

And I get this :

ERROR: relation "temp_table " does not exist LINE 11: FROM temp_table

(Obviously, I'm aware the temp_table is not necessary for what I'm doing in the code above, but that's not the point :) => I want to understand the syntax to get it to work)

like image 606
François M. Avatar asked Mar 18 '16 17:03

François M.


People also ask

What is the use of temporary table in PostgreSQL?

A temporary table, as the name implies, is a short-lived table that exists for the duration of a database session. PostgreSQL automatically drops the temporary tables at the end of a session or a transaction.

Can we create temporary table in function?

We can create a local temporary table by using # before the table name. They are available only for the current user session. They get discarded automatically once the session has ended.

Can we create temporary table in PostgreSQL?

Syntax to create PostgreSQL Temporary tables In the syntax, Specify the TEMP or TEMPORARY keyword after the CREATE keyword. Specify the list of columns with datatype after the name of the temp table.

Where are temporary tables stored in Postgres?

Temporary tables get put into a schema called "pg_temp_NNN", where "NNN" indicates which server backend you're connected to. This is implicitly added to your search path in the session that creates them.


1 Answers

The appropriate syntax for creating a temp table is

create temp table...

but you have to be sure to drop the temp table before existing out of the function. Also, I'd suggest this syntax instead:

CREATE TEMP TABLE IF NOT EXISTS temp_table AS
    SELECT id, value
    FROM test.another_table;

Thus your function will be like this:

CREATE FUNCTION test.myfunction()
RETURNS SETOF test.out_table
AS $$

    CREATE TEMP TABLE IF NOT EXISTS temp_table AS
        SELECT id, value
        FROM test.another_table;

    INSERT INTO test.out_table
        SELECT id, value
        FROM temp_table;

DROP TABLE temp_table;

$$ LANGUAGE SQL;

But if I can be so kind, I'd like to rewrite this function so it is more correct:

CREATE FUNCTION test.myfunction()
RETURNS TABLE (id int, value varchar) -- change your datatype as needed
AS $$
BEGIN;
CREATE TEMP TABLE IF NOT EXISTS temp_table AS
    SELECT id, value
    FROM test.another_table;

INSERT INTO test.out_table
    SELECT id, value
    FROM temp_table;

DROP TABLE temp_table;

RETURN QUERY 
SELECT id, value
from temp_table;

END;
$$ LANGUAGE plpgsql;

Untested; let me know if this fails.

like image 129
dizzystar Avatar answered Oct 22 '22 23:10

dizzystar