Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 1B10A61BC24 for ; Fri, 27 Jul 2012 23:32:03 -0300 (ADT) Received: from nm6.bullet.mail.ac4.yahoo.com ([98.139.52.203]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Suwox-0005hE-CS for pgsql-sql@postgresql.org; Sat, 28 Jul 2012 02:32:02 +0000 Received: from [98.139.52.190] by nm6.bullet.mail.ac4.yahoo.com with NNFMP; 28 Jul 2012 02:31:46 -0000 Received: from [98.139.52.165] by tm3.bullet.mail.ac4.yahoo.com with NNFMP; 28 Jul 2012 02:31:46 -0000 Received: from [127.0.0.1] by omp1048.mail.ac4.yahoo.com with NNFMP; 28 Jul 2012 02:31:46 -0000 X-Yahoo-Newman-Id: 75483.23886.bm@omp1048.mail.ac4.yahoo.com Received: (qmail 76206 invoked from network); 28 Jul 2012 02:31:46 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=jZZ4Ej6UTdukmauDj3T5IXhns1Wza6ePPwpDhouYCP3ZIP88zZZpylccAoZWnmRgdHJ5RqJAwrHlLMRE1CBUqFg71YQiUiLrf1PMqHE15V4YtuDbbhQgt3IZ1Qo5/CTbevL4sKKmsJQSxpabGJuRZJlfJABxR2Z53AVJxo+2yOE= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1343442706; bh=1DbNnIYIvDTZCmobxyRsbvMLrExvLolfFtwtTvcknqA=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=LzBe7w32bNFULNxQw8na8jGO/Hz6YNOjDXnV9lCD5/K5iYqHpCAYt2cFn6AmR+CzkFet9at6r5cOLjiz70c1bPcoOD5KM80xkLvwYG1fLvZftBb+YpWCZYAdbWdygdw061kFcc4zB63VDMWQaJmX/22e97sJPxczgtFTWI+x7gs= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: TNYoOQQVM1k_G1nL3bDeRP_RegA0P9kMk5VoasKAIwudW80 vQG0rOqeZmd6FjP5QVGKIR_u416Gt.bIg30GIIBekqat5ezeIVKtTRiINC3K sE.c14092cx4mqsQFl3gTvtIzgFjypOptMhnKptFtTmLZ4A_gRPJ.PcydDX5 T3KZa6BaMW6kuz0IRu8L4mIqy7thpAUsCPua2IATq3MQmBhaKl1x4KbnzBPI 1gwTyHCE7z1PvAvPpI3hpsltzGA1_is0KwO9j7tRkNXpsYwk4yi7V3LYpz1z YFv4zgf90yoPA7Y0_Uyez5m0vdCqoByaqa2dLN4BGiIkrGlUS4NFgJko3cuc qTMIbTbrwLIm3YkqJS05gAVArZH83YwZJaU9MLCgf6_PrQUBtc7XSVP9CAxB MJWhpbEj0P.DYAMnnEWl_UNp2TniP216NZFvD X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.104] (polobo@24.93.23.188 with xymcookie) by smtp102-mob.biz.mail.ac4.yahoo.com with SMTP; 27 Jul 2012 19:31:45 -0700 PDT References: <501346EC.2000305@gmx.net> In-Reply-To: <501346EC.2000305@gmx.net> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Message-Id: Cc: "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (9B206) From: David Johnston Subject: Re: join against a function-result fails Date: Fri, 27 Jul 2012 22:31:46 -0400 To: Andreas X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201207/34 X-Sequence-Number: 36771 On Jul 27, 2012, at 21:57, Andreas wrote > Hi, > I have a table with user ids and names. > Another table describes some rights of those users and still another one d= escribes who inherits rights from who. >=20 > A function all_rights ( user_id ) calculates all rights of a user recursiv= ely and gives back a table with all userright_ids this user directly has or i= nherits of other users as ( user_id, userright_id ). >=20 > Now I'd like to find all users who have the right 42. >=20 >=20 > select user_id, user_name > from users > join all_rights ( user_id ) using ( user_id ) > where userright_id =3D 42; >=20 > won't work because the parameter user_id for the function all_rights() is u= nknown when the function gets called. >=20 > Is there a way to do this? >=20 Suggest you write a recursive query that does what you want. If you really w= ant to do it this way you can: With cte as (Select user_id, user_name, all_rights(user_id) as rightstbl) Select * from cte where (rightstbl).userright_id =3D 42; This is going to be very inefficient since you enumerate every right for eve= ry user before applying the filter. With a recursive CTE you can start at t= he bottom of the trees and only evaluate the needed branches. David J.