Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hM6Hc-0006aq-0Z for pgsql-general@arkaria.postgresql.org; Thu, 02 May 2019 07:37:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hM6HZ-0004Ap-K5 for pgsql-general@arkaria.postgresql.org; Thu, 02 May 2019 07:37:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hLwk8-0002x4-Nc for pgsql-general@lists.postgresql.org; Wed, 01 May 2019 21:26:21 +0000 Received: from mx0b-001ee601.pphosted.com ([148.163.159.169] helo=mx0a-001ee601.pphosted.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hLwk3-0000Di-HB for pgsql-general@lists.postgresql.org; Wed, 01 May 2019 21:26:18 +0000 Received: from pps.filterd (m0089290.ppops.net [127.0.0.1]) by mx0b-001ee601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x41LOf5R010391 for ; Wed, 1 May 2019 14:26:13 -0700 Received: from nam04-co1-obe.outbound.protection.outlook.com (mail-co1nam04lp2058.outbound.protection.outlook.com [104.47.45.58]) by mx0b-001ee601.pphosted.com with ESMTP id 2s7jeu8gx8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 01 May 2019 14:26:12 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adaptiveinsights.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=heiXTRv7sMSIn1MBcRovERt9XP8t+A9nX+MDEFNWYYY=; b=eV9FtcyU+1g1fGM7UqDjQ9vLq4T6WhJha3rYB1j31wm9+zdhbW9AC7BfsVYY6zUcESNr8OwJfGdmx0RwEgjQJtlPq0ZxiB/emenC6+h6As8rfaFYWrAWtdWdmYg0xJrArW2G7nGygrBu00hjX+QOFATCR1QzmI30NSOt82jiT3c= Received: from BYAPR06MB4536.namprd06.prod.outlook.com (52.135.233.206) by BYAPR06MB4888.namprd06.prod.outlook.com (20.177.184.17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1835.15; Wed, 1 May 2019 21:26:10 +0000 Received: from BYAPR06MB4536.namprd06.prod.outlook.com ([fe80::7dd7:3afd:a7d0:fc0a]) by BYAPR06MB4536.namprd06.prod.outlook.com ([fe80::7dd7:3afd:a7d0:fc0a%3]) with mapi id 15.20.1856.008; Wed, 1 May 2019 21:26:10 +0000 From: Mark Zellers To: "pgsql-general@lists.postgresql.org" Subject: Migrating an application with Oracle temporary tables Thread-Topic: Migrating an application with Oracle temporary tables Thread-Index: AdUAY6CWQTHSxAVCT0C8WF/kHdTcNw== Date: Wed, 1 May 2019 21:26:10 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [12.244.182.158] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: bb2db2a6-9614-4bab-c7d1-08d6ce7ba16d x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600141)(711020)(4605104)(4618075)(2017052603328)(7193020);SRVR:BYAPR06MB4888; x-ms-traffictypediagnostic: BYAPR06MB4888: x-ms-exchange-purlcount: 4 x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:10000; x-forefront-prvs: 00246AB517 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39860400002)(396003)(366004)(376002)(346002)(136003)(199004)(189003)(54094003)(102836004)(53936002)(55016002)(81156014)(66556008)(256004)(5640700003)(6306002)(8936002)(8676002)(2501003)(54896002)(9686003)(2351001)(81166006)(64756008)(236005)(66476007)(52536014)(5660300002)(76116006)(14454004)(6916009)(99286004)(316002)(14444005)(73956011)(86362001)(66946007)(606006)(478600001)(966005)(2906002)(6506007)(66446008)(6436002)(68736007)(71190400001)(33656002)(71200400001)(74316002)(7736002)(25786009)(7696005)(66066001)(790700001)(6116002)(486006)(186003)(476003)(3846002)(26005);DIR:OUT;SFP:1102;SCL:1;SRVR:BYAPR06MB4888;H:BYAPR06MB4536.namprd06.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: adaptiveinsights.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: ldTiARdW13k4PL1oe9o6J0Vw47U5oyLLnqRUJsbIDGLwfPUN4TpIToU9VDR5AyhFWfiQlIxMCC5jMTto4GmX4lReaOnzWECRFsBADb5H559qrIXNvKAYgHmqUZ5IYyhVAuqqyXZwPo/l9cvaLha22X/jWdZYQ3ZlRy3RkISlh6PpZ2o2QEdz0XQS1MyfFj688QsHTysHJq6FkZ8J7c54HfFRCXfXcklxISb7kgRu2POYUD+h+RdkimwclZxKJijHhvwuOg2N6heFwc2dCjh+c2JFRJ5Osispd19H1m+2DlOSssGr9fZ+cnKpa7XBJVCIhqc8mM8R5TCmF9uVM7hDoPV1XsIx1nZrjlJIeD7CMdvhf17+zDdiY7LqNF09zbCss4k5yLfVoMCe1umz7iqar0UIzhXGRrt+/TqAyVoMFqc= Content-Type: multipart/alternative; boundary="_000_BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0BYAPR06MB4536namp_" MIME-Version: 1.0 X-OriginatorOrg: adaptiveinsights.com X-MS-Exchange-CrossTenant-Network-Message-Id: bb2db2a6-9614-4bab-c7d1-08d6ce7ba16d X-MS-Exchange-CrossTenant-originalarrivaltime: 01 May 2019 21:26:10.2628 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 32e232f6-4e92-46d3-b401-fe8baa3788e9 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR06MB4888 external_sender: yes X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-05-01_09:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905010132 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --_000_BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0BYAPR06MB4536namp_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable 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 thin= k is a cunning plan, but I wonder if anyone with more PostgreSQL understand= ing can shoot holes in it. Oracle defines their temporary tables statically, such that the first use i= n a session instantiates a private instance of the table. That implies tha= t you need to jump through flaming hoops in order to be able to use tempora= ry tables in stored procedures and functions. My idea was to create a "prototype" table (which will never have any rows i= n it) and, when I need to use the temporary table, create the temporary tab= le 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 i= n 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/procedur= e is effectively recompiled. Is there a danger that. assuming the temporar= y table is created for a session that one session might see another session= 's data due to the procedure having effectively compiled the temporary tabl= e 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 a= nd potentially more performant than the approach I found here: https://www.= codeproject.com/Articles/1176045/Oracle-style-global-temporary-tables-for-P= ostgreSQ 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 a= llows 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 tha= t is relatively finite, so I'm willing to take that hit, in exchange for no= t having to use dynamic SQL to refer to my temporary tables. --_000_BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0BYAPR06MB4536namp_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

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 wit= h more PostgreSQL understanding can shoot holes in it.

 

Oracle defines their temporary tables statically, su= ch that the first use in a session instantiates a private instance of the t= able.  That implies that you need to jump through flaming hoops in ord= er 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 temporar= y 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 s= econd opinion as to whether this approach is going to fail in a more comple= x scenario.

 

 

drop function i= f exists bar;

drop table if e= xists foo;

 

create table FO= O(X INTEGER not null constraint PTCC_FOO check (X is NULL));

commit;

 

create function= bar() returns INTEGER

as

$$

DECLARE

   re= sult INTEGER;

begin 

  &nb= sp; select SUM(X) into result from FOO ;

  &nb= sp; return result;

end;=

$$ language plp= gsql;

 

commit;

 

create temporar= y 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 temporar= y 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;<= /o:p>

 

create temporar= y 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 w= hen a function/procedure is effectively recompiled.  Is there a danger= that. assuming the temporary table is created for a session that one sessi= on 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 re= quiring the application to define the temporary table before using it (whic= h could be as simple as using `CREATE TABLE AS SELECT * FROM prototype_tabl= e`), 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 wri= te 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.&n= bsp; 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 dynam= ic SQL to refer to my temporary tables.


 

 

--_000_BYAPR06MB45364F6A71EB19D4F4BEEA9FD53B0BYAPR06MB4536namp_--