Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 22D98B5D95B for ; Mon, 26 Sep 2011 15:45:03 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 94110-02-3 for ; Mon, 26 Sep 2011 18:44:56 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sd-17937.dedibox.fr (sd-17937.dedibox.fr [88.191.100.74]) by mail.postgresql.org (Postfix) with ESMTP id B2B07B5DC61 for ; Mon, 26 Sep 2011 15:44:51 -0300 (ADT) Received: from [192.168.10.3] (AMontsouris-552-1-48-190.w92-140.abo.wanadoo.fr [92.140.87.190]) by sd-17937.dedibox.fr (Postfix) with ESMTPA id 67A8A3B8F5; Mon, 26 Sep 2011 20:44:50 +0200 (CEST) Subject: Re: PostgreSQL question From: Guillaume Lelarge To: Mridul Mathew Cc: pgsql-admin@postgresql.org Date: Mon, 26 Sep 2011 20:44:50 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.0.3 (3.0.3-1.fc15) Content-Transfer-Encoding: 7bit Message-ID: <1317062690.2085.1.camel@localhost.localdomain> Mime-Version: 1.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201109/188 X-Sequence-Number: 38425 On Mon, 2011-09-26 at 16:06 +0530, Mridul Mathew wrote: > Hello, > > I wanted to take a moment here and reach out to the community with a > question. How does postgreSQL handle schema switching in the database? I > have a test scenario. > > ################## > > vpn2=> show search_path; > search_path > ------------- > mridul > (1 row) > > CREATE OR REPLACE PACKAGE mridul.TEST_PKG > IS > PROCEDURE execute_for_all_schema(); > end TEST_PKG; > > CREATE OR REPLACE PACKAGE BODY mridul.TEST_PKG > IS > > PROCEDURE execute_for_all_schema() IS > > CURSOR c_schema_names IS > SELECT name > FROM mridul.schema_config > WHERE status = 'ACTIVE'; > > BEGIN > > FOR dat_schema_names IN c_schema_names LOOP > ----- EXECUTE IMMEDIATE 'ALTER SESSION SET CURRENT_SCHEMA = '|| > dat_schema_names.name; > EXECUTE IMMEDIATE 'SET SEARCH_PATH = '|| dat_schema_names.name; > EXECUTE IMMEDIATE 'insert into A values (1)'; > commit; > END LOOP; > EXCEPTION > WHEN OTHERS THEN > RAISE; > > END; > end TEST_PKG; > > vpn2=> select * from mridul.schema_config; > id | name | description | status | last_device_sync_date > ----+-------+-------------+--------+----------------------- > 1 | TEST | TESTING | ACTIVE | 22-SEP-11 18:28:11 > 6 | TEST1 | TESTING5 | ACTIVE | 22-SEP-11 11:46:09 > 2 | TEST2 | TESTING2 | ACTIVE | 23-SEP-11 11:54:35 > (3 rows) > > vpn2=> show search_path; > search_path > ------------- > mridul > (1 row) > > vpn2=> select count(*) from test.a; > count > ------- > 0 > (1 row) > > > vpn2=> select count(*) from test1.a; > count > ------- > 0 > (1 row) > > > vpn2=> select count(*) from test2.a; > count > ------- > 0 > (1 row) > > vpn2=> exec mridul.TEST_PKG.execute_for_all_schema; > > EDB-SPL Procedure successfully completed > vpn2=> select count(*) from test.a; > count > ------- > 1 > (1 row) > > > vpn2=> select count(*) from test1.a; > count > ------- > 1 > (1 row) > > > vpn2=> select count(*) from test2.a; > count > ------- > 1 > (1 row) > > If I convert the 'insert into A ...' part from a dynamic dml to a normal > insert, and then execute the procedure, there are 3 rows inserted into > test.a and none go into test1.a nor test2.a. However, if I echo the > search_path after executing the proc, it displays the last schema's name > (meaning, it has switched the schemas, but the insert has gone into only the > first schema). > This isn't a PostgreSQL question, this is an EnterpriseDB one. Packages are not part of the community PostgreSQL distribution. "EXECUTE IMMEDIATE" is also not part of the community PostgreSQL distribution. So I guess you need to ask them. -- Guillaume http://blog.guillaume.lelarge.info http://www.dalibo.com