pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mark Zellers <markz@adaptiveinsights.com>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Migrating an application with Oracle temporary tables
Date: Wed, 1 May 2019 21:26:10 +0000
Message-ID: <BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0@BYAPR06MB4536.namprd06.prod.outlook.com> (raw)

One of the issues I have run into in migrating from Oracle to PostgreSQL is the difference in temporary table behavior.  I've come up with what I think is a cunning plan, but I wonder if anyone with more PostgreSQL understanding can shoot holes in it.

Oracle defines their temporary tables statically, such that the first use in a session instantiates a private instance of the table.  That implies that you need to jump through flaming hoops in order to be able to use temporary tables in stored procedures and functions.

My idea was to create a "prototype" table (which will never have any rows in it) and, when I need to use the temporary table, create the temporary table such that it hides the prototype.  I have a simple example that seems to work, although before I invest more effort in using it for real work, I'd like to get a second opinion as to whether this approach is going to fail in a more complex scenario.


drop function if exists bar;
drop table if exists foo;

create table FOO(X INTEGER not null constraint PTCC_FOO check (X is NULL));
commit;

create function bar() returns INTEGER
as
$$
DECLARE
   result INTEGER;
begin
    select SUM(X) into result from FOO ;
    return result;
end;
$$ language plpgsql;

commit;

create temporary table FOO(X INTEGER) on commit drop;
alter table FOO add constraint PK_FOO primary KEY(X);
insert into FOO(X) VALUES(1);
insert into FOO(X) VALUES(2);
insert into FOO(X) VALUES(3);

select 'Expect to see 6' as Expectation, BAR();
commit;


create temporary table FOO(X INTEGER) on commit drop;
alter table FOO add constraint PK_FOO primary key (X);
insert into FOO(X) VALUES(1);
insert into FOO(X) VALUES(2);
insert into FOO(X) VALUES(3);
insert into FOO(X) VALUES(4);
select 'Expect to see 10' as Expectation, BAR();
rollback;

create temporary table FOO(X INTEGER) on commit drop;
insert into FOO(X) VALUES(1);
insert into FOO(X) VALUES(2);
select 'Expect to see 3' as Expectation, BAR();
commit;


What I am not clear on is what the rules are as to when a function/procedure is effectively recompiled.  Is there a danger that. assuming the temporary table is created for a session that one session might see another session's data due to the procedure having effectively compiled the temporary table into its definition?


While this approach does have the disadvantage of requiring the application to define the temporary table before using it (which could be as simple as using `CREATE TABLE AS SELECT * FROM prototype_table`), it seems simpler and potentially more performant than the approach I found here: https://www.codeproject.com/Articles/1176045/Oracle-style-global-temporary-tables-for-PostgreSQ

It is also in direct opposition to this post I found: https://www.cybertec-postgresql.com/en/using-temporary-tables-the-way-they-should-not-be-used/

So far, I have not found a case where, as long as I don't read or write to the permanent table, I get the wrong results from the above approach.  It allows me to minimize the impact on my application (basically, it means that at the start of any transaction that might need a certain temporary table, I need to manually create it.  The number of places I would need to do that is relatively finite, so I'm willing to take that hit, in exchange for not having to use dynamic SQL to refer to my temporary tables.

view thread (7+ messages)  latest in thread

Message-ID: <BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0@BYAPR06MB4536.namprd06.prod.outlook.com>
Permalink:  ../BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0@BYAPR06MB4536.namprd06.prod.outlook.com/
Also on:    postgresql.org/message-id/BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0@BYAPR06MB4536.namprd06.prod.outlook.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: markz@adaptiveinsights.com
  Subject: Re: Migrating an application with Oracle temporary tables
  In-Reply-To: <BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0@BYAPR06MB4536.namprd06.prod.outlook.com>
  References: <BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0@BYAPR06MB4536.namprd06.prod.outlook.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox