X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DF4115346A for ; Mon, 18 Apr 2005 23:43:26 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 97454-05 for ; Tue, 19 Apr 2005 02:43:20 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id E018653417 for ; Mon, 18 Apr 2005 23:43:19 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j3J2hENv030225 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 18 Apr 2005 20:43:16 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j3J2hDbl075534; Mon, 18 Apr 2005 20:43:13 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j3J2hDA4075533; Mon, 18 Apr 2005 20:43:13 -0600 (MDT) (envelope-from mfuhr) Date: Mon, 18 Apr 2005 20:43:13 -0600 From: Michael Fuhr To: Kai Hessing Cc: pgsql-sql@postgresql.org Subject: Re: can a function return a virtual table? Message-ID: <20050419024313.GA75425@winnie.fuhr.org> References: <3c4m1vF6b6e6aU1@individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3c4m1vF6b6e6aU1@individual.net> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/271 X-Sequence-Number: 21164 On Wed, Apr 13, 2005 at 03:44:25PM +0200, Kai Hessing wrote: > > This is the question i'm telling myself. It is because we don't really > delete table entries, just setting a status field to '-1'. So a valid > select would look like: SELECT xyz, abc FROM (SELECT * FROM tablex WHERE > status > -1); I'll pick a nit and point out that the above isn't a valid query: test=> SELECT xyz, abc FROM (SELECT * FROM tablex WHERE status > -1); ERROR: subquery in FROM must have an alias HINT: For example, FROM (SELECT ...) [AS] foo. In this simple example you could omit the subquery altogether: SELECT xyz, abc FROM tablex WHERE status > -1; > It would be much nicer to have to write something like: SELECT xyz, abc > FROM active(tablex); where the function 'active(x)' returns a virtual > table with all entries from table x where status is > -1. But sadly I > have no idea how write such a function. Good old O'reilly can't help (or > i'm to dumb *g*). See the documentation for writing set-returning functions (SRFs). The following links should get you started (if you're using a version of PostgreSQL older than 8.0, then see the documentation for that version): http://www.postgresql.org/docs/8.0/interactive/queries-table-expressions.html#QUERIES-TABLEFUNCTIONS http://www.postgresql.org/docs/8.0/interactive/xfunc-sql.html#AEN29503 http://www.postgresql.org/docs/8.0/interactive/plpgsql-control-structures.html#AEN32823 Another possibility would be to use views. You'd need to create a view on each table. http://www.postgresql.org/docs/8.0/interactive/tutorial-views.html http://www.postgresql.org/docs/8.0/interactive/sql-createview.html Yet another possibility would be to move the inactive rows to a separate table. You could reconstruct the original table with a UNION of the active and inactive tables. http://www.postgresql.org/docs/8.0/interactive/queries-union.html -- Michael Fuhr http://www.fuhr.org/~mfuhr/