Search code examples
sqlpostgresqlexecutiontemp-tables

Joining return tables from sql functions which have same name temp tables


I need to join return tables from multiple SQL functions. In those sql functions, they use a bunch of temp tables with the same name (such as tmp_result, tmp_datetime, etc.). Within each sql functions, there are statements to drop the temp tables if exists before creating the temp tables. When I join the return tables from those sql functions, do the joining part be evaluated sequentially? Use the generic query below as an example, do I need to worry that tmp_result table created by my_function_1 be dropped by drop table statement in my_function_2 pre-maturely? I did some test and the results looked fine. But I'd like to know if there are potential issues. Thanks.

select a.*, b.*
from my_function_1(1,2,3) a
    join my_function_2(4,5,6) b on a.id=b.id
order by 1;

Solution

  • By some testing, I think the sql functions are evaluated/called in sequence. Only after the previous one is done, the next one will be called. in my test, I used clock_timestamp() at the beginning and end of each sql function to monitor the invoking time and sequence. I made one sql function to take long time to finish and move it around in the join sequence. All my tests showed that the sql functions were evaluated in sequence. So even with same temp table names in different sql functions, it should be fine.