Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 74BBC633044 for ; Fri, 5 Mar 2010 16:45:09 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 94190-03 for ; Fri, 5 Mar 2010 20:44:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mout.perfora.net (mout.perfora.net [74.208.4.194]) by mail.postgresql.org (Postfix) with ESMTP id A3013633904 for ; Fri, 5 Mar 2010 16:44:58 -0400 (AST) Received: from [192.168.223.100] (cpe-74-75-231-235.maine.res.rr.com [74.75.231.235]) by mrelay.perfora.net (node=mrus1) with ESMTP (Nemesis) id 0MDzRn-1Nvb1Y0OzP-00HSr6; Fri, 05 Mar 2010 15:44:53 -0500 Subject: Re: Querying the same column and table across schemas From: "John A. Sullivan III" To: "Daniel J. Summers" Cc: pgsql-admin@postgresql.org In-Reply-To: <4B916284.3040401@djs-consulting.com> References: <1267818283.15754.42.camel@Family.pacifera.com> <4B916284.3040401@djs-consulting.com> Content-Type: text/plain Date: Fri, 05 Mar 2010 15:44:49 -0500 Message-Id: <1267821889.15754.49.camel@Family.pacifera.com> Mime-Version: 1.0 X-Mailer: Evolution 2.22.3.1 Content-Transfer-Encoding: 7bit X-Provags-ID: V01U2FsdGVkX1+sW9znE92otXLZCTXW/mu3QEbklEK/EzAkNaa 4tG0TyICyjDfFkND40/hSvpR72yWrSEAnlyLg4FMe6K8tLQD53 QteQ8H4Dt/IEu5Jdwnnmh/UdqtIP4yyMLTkMMkGtPU= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.74 tagged_above=-10 required=5 tests=BAYES_20=-0.74 X-Spam-Level: X-Archive-Number: 201003/46 X-Sequence-Number: 33845 On Fri, 2010-03-05 at 19:59 +0000, Daniel J. Summers wrote: > On 03/05/2010 07:44 PM, John A. Sullivan III wrote: > > I'm trying to avoid making a thousand call like > > > > select user1.session_id from user1.sessions; > > > > when I could do it in a single query especially since the database is > > remote and secured with SSL. > > > CREATE VIEW all_sessions AS > SELECT user1.session_id, 1 as user_number > FROM user1.sessions > UNION > SELECT user2.session_id, 2 AS user_number > FROM user2.sessions > UNION > ...more schemas... > > Then, "SELECT * FROM all_sessions" would show you each session ID (and, > with the user_number field, what user - you could use a string literal > there too). Of course, the user creating and running this would need > SELECT privileges on each schema's "sessions" table. > > > Daniel > That sounds quite reasonable. I'm guessing that a view is superior to creating a new schema with tables derived from selects from all the schemas because it would be less overhead and dynamic, i.e., I only create the view once and it always has the most current data. Is that correct? As we add new schemas, is there an easy way to update the view? That was not obvious to me looking at the documentation for ALTER VIEW and CREATE OR REPLACE VIEW seems to be sensitive to ensuring the new view is identical to the old except for appends. It would be nice if we could simply append UNION SELECT * from user3.sessions to the view. Thanks very, very much - John