Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kNc8q-0000h6-4d for pgsql-general@arkaria.postgresql.org; Wed, 30 Sep 2020 13:27:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kNc8o-000683-7B for pgsql-general@arkaria.postgresql.org; Wed, 30 Sep 2020 13:27:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kNc8n-00067s-JT for pgsql-general@lists.postgresql.org; Wed, 30 Sep 2020 13:27:29 +0000 Received: from tpx240.joinsnc.com ([194.116.78.240] helo=joinsnc.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kNc8g-0002RL-4j for pgsql-general@lists.postgresql.org; Wed, 30 Sep 2020 13:27:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joinsnc.com; s=default; h=To:From:Subject:Message-ID:Date: User-Agent:MIME-Version:Content-Type:Content-Language; bh=qk64+8 DPkk95tvI5lEflSYmjHk5CuOz1G+LrszMQqEc=; b=OEU+kNm4olKDy7RxzdSDTX q6qTBrFMRnqzfS8szSIniZUmbwv/mLWhg05GqGVaxRRBayMSV3qAKmTFagxWiYER baxCYCtxPxcuq87tRtej/SQ5z8uybLamqQxSKYipoc3WiH0xqGEC53KNz4qlop/u jb/cJQbQRriMwYNBSDzh6DwKzuVw7R5p+NSANMy56BowvgthKPjEGNqsIrKytqyH 97umzD4ws5hDrkkmKEnyS2dftWLb1xMzEq9Q9voJVQxqTxd82H9PX81gQw/NWcZz acuYwEbE4GBL5JI1ZrQd3hbHbg+nxGnvoSvI76jGyMyqLnf//MDArKTUCPQs/TRg == DomainKey-Signature: a=rsa-sha1; q=dns; c=simple; s=default; d=joinsnc.com; b=Zug8ifCfhTLBSvT/rZfz9Q2HAFVwU6km+EooDUAyf7wCzFKko07cKmfcvHQL/U2oB6QTiWejWgPV8hqy691buc1ROBkoG9qgkl5dG0hjDTNU/8+WpumPOLgV1boFB+oGMqu86iedBm465LeXUBs+KAS91VUrpcbZRiH7CD1NvLeA5rEWrukmPLzQLaj7bFYAA9LfLRcL7D1ThGcKkxtgCLwJbm3qtElMX444qxIq9EpVhALihJt9MmL+J2ZuOAkvE+yWu2J6eGFGe8p+/lbvZnteCXCoDDy+mKn8fxnIv5PwmujayURxj7cw4K7NcLkg2bs1pX/0s4PK7qacqltZ7A==; Received: (qmail 14727 invoked by uid 89); 30 Sep 2020 15:27:18 +0200 Received: from unknown (HELO joinsnc.com) () by joinsnc.com with SMTP; 30 Sep 2020 15:27:18 +0200 Received: (qmail 8295 invoked by uid 1010); 30 Sep 2020 15:27:18 +0200 Received: from 109.75.113.218 by MAILASAV (envelope-from , uid 1008) with qmail-scanner-1.25-st-qms (clamdscan: 0.95.3/9955. spamassassin: 3.2.3. perlscan: 1.25-st-qms. Clear:RC:1(109.75.113.218):. Processed in 0.1047 secs); 30 Sep 2020 13:27:18 -0000 X-Antivirus-joinsnc.com-Mail-From: v.brusa@joinsnc.com via MAILASAV X-Antivirus-joinsnc.com: 1.25-st-qms (Clear:RC:1(109.75.113.218):. Processed in 0.1047 secs Process 8283) Received: from host-218-113-75-109.isiline.org (HELO ?10.0.10.26?) (o.diruzza@joinsnc.com@109.75.113.218) by mail1.joinsnc.com with SMTP; 30 Sep 2020 15:27:18 +0200 To: pgsql-general@lists.postgresql.org From: "v.brusa@joinsnc.com" Subject: temp table same name real table Message-ID: <30bcf5f9-239c-ced7-1518-1c4709c0e697@joinsnc.com> Date: Wed, 30 Sep 2020 15:27:17 +0200 User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------3DD23BE494EE07E33E2E4A82" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------3DD23BE494EE07E33E2E4A82 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi all, I have this strange behavior when I use temp table with same name of a permanent table in a function. Postgres version is: PostgreSQL 11.3 (Debian 11.3-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit I use a query panel from pgAdmin droptableifexists taba; createtable taba (cola text); insertinto taba(cola) values ('val_permanent'); createorreplacefunction test_temp_table(_is_temp boolean, scan refcursor) RETURNS refcursor AS $BODY$ BEGIN if _is_temp then createtemptable taba oncommitdropasselect * from public.taba withnodata; insertinto taba(cola) values ('val_temp'); else endif; open scan for select * from taba; return scan; END; $BODY$ LANGUAGE plpgsql VOLATILE; select test_temp_table(true,'scan'); fetchallfrom scan; -- return data in temp table taba that is 'val_temp' select test_temp_table(false,'scan'); fetchallfrom scan; -- return data in permanent table taba that is 'val_permanent' select test_temp_table(true,'scan'); fetchallfrom scan; -- return data in permanent table taba that is 'val_permanent' and dont see temporary table If I force a change in search_path the code works correctly. droptableifexists taba; createtable taba (cola text); insertinto taba(cola) values ('val_permanent'); createorreplacefunction test_temp_table(_is_temp boolean, scan refcursor) RETURNS refcursor AS $BODY$ BEGIN if _is_temp then set search_path=pg_temp,public; createtemptable taba oncommitdropasselect * from public.taba withnodata; insertinto taba(cola) values ('val_temp'); else set search_path=public; endif; open scan for select * from taba; return scan; END; $BODY$ LANGUAGE plpgsql VOLATILE; select test_temp_table(true,'scan'); fetchallfrom scan; -- return data in temp table taba that is 'val_temp' select test_temp_table(false,'scan'); fetchallfrom scan; -- return data in permanent table taba that is 'val_permanent' select test_temp_table(true,'scan'); fetchallfrom scan; -- return data in temp table taba that is 'val_temp' it would seem that whenfunctionreferences permanent tablefirsttimeinnext calling (select * from taba;) temptableisnotandisnottrue postgres documentation (Existing permanent tableswith the same name are not visible to the currentsessionwhile the temporarytableexists, unless they are referenced withschema-qualified names) any idea? thanks Vittorio Brusa Zappellini --------------3DD23BE494EE07E33E2E4A82 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


Hi all,
I have this strange behavior when I use temp table with same name of a permanent table in a function.
Postgres version is:
PostgreSQL 11.3 (Debian 11.3-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
I use a query panel from pgAdmin

drop
 table if exists taba;
create table taba (cola text);
insert into taba(cola) values ('val_permanent');

create or replace function test_temp_table(_is_temp boolean, scan refcursor) RETURNS refcursor AS
$BODY$
BEGIN
if _is_temp then
     
create temp table taba on commit drop as select * from public.taba with no data;
     
insert into taba(cola) values ('val_temp');
else

end if;

open scan 
for
select * from taba;

return scan;

END;
$BODY$
LANGUAGE plpgsql VOLATILE;


select test_temp_table(true,'scan'); fetch all from scan; -- return data in temp table taba that is 'val_temp'

select test_temp_table(false,'scan'); fetch all from scan; -- return data in permanent table taba that is 'val_permanent'

select test_temp_table(true,'scan'); fetch all from scan; -- return data in permanent table taba that is 'val_permanent' and dont see temporary table


If I force a change in search_path the code works correctly.


drop table if exists taba;
create table taba (cola text);
insert into taba(cola) values ('val_permanent');

create or replace function test_temp_table(_is_temp boolean, scan refcursor) RETURNS refcursor AS
$BODY$
BEGIN

if _is_temp then
     
set search_path=pg_temp,public;
     
create temp table taba on commit drop as select * from public.taba with no data;
     
insert into taba(cola) values ('val_temp');
else
     
set search_path=public;
end if;

open scan 
for
     
select * from taba;

return scan;

END;
$BODY$
LANGUAGE plpgsql VOLATILE;


select test_temp_table(true,'scan'); fetch all from scan; -- return data in temp table taba that is 'val_temp'

select test_temp_table(false,'scan'); fetch all from scan; -- return data in permanent table taba that is 'val_permanent'

select test_temp_table(true,'scan'); fetch all from scan; -- return data in temp table taba that is 'val_temp'


it would seem that 
when function references permanent table first time in next calling (select * from taba;) temp table is not and is not true postgres documentation (Existing permanent tables with the same name are not visible to the current session while the temporary table exists, unless they are referenced with schema-qualified names)

any idea?

thanks

Vittorio Brusa Zappellini






--------------3DD23BE494EE07E33E2E4A82--