Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UrKJs-0003oW-VT for pgsql-bugs@arkaria.postgresql.org; Tue, 25 Jun 2013 03:53:29 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1UrKJs-0004Dc-7p for pgsql-bugs@arkaria.postgresql.org; Tue, 25 Jun 2013 03:53:28 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UrKJq-0004DT-4s for pgsql-bugs@postgresql.org; Tue, 25 Jun 2013 03:53:26 +0000 Received: from szxga02-in.huawei.com ([119.145.14.65]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UrKJl-0002fS-Py for pgsql-bugs@postgresql.org; Tue, 25 Jun 2013 03:53:25 +0000 Received: from 172.24.2.119 (EHLO szxeml206-edg.china.huawei.com) ([172.24.2.119]) by szxrg02-dlp.huawei.com (MOS 4.3.4-GA FastPath queued) with ESMTP id BDT88663; Tue, 25 Jun 2013 11:53:16 +0800 (CST) Received: from SZXEML416-HUB.china.huawei.com (10.82.67.155) by szxeml206-edg.china.huawei.com (172.24.2.59) with Microsoft SMTP Server (TLS) id 14.1.323.7; Tue, 25 Jun 2013 11:52:56 +0800 Received: from blrprnc09ns (10.18.96.98) by szxeml416-hub.china.huawei.com (10.82.67.155) with Microsoft SMTP Server id 14.1.323.7; Tue, 25 Jun 2013 11:53:09 +0800 From: Amit Kapila To: =?UTF-8?B?J9CR0L7RgNC40YEg0KDQvtC80LDRiNC+0LIn?= CC: "'Tom Lane'" , "'pgsql-bugs'" References: <24774.1371756513@sss.pgh.pa.us> <51c7f44c.c6c5420a.2e0b.ffffa3b5SMTPIN_ADDED_BROKEN@mx.google.com> <51c83f06.65cc440a.1686.ffffac12SMTPIN_ADDED_BROKEN@mx.google.com> In-Reply-To: Subject: Re: BUG #8242: No way to debug "subquery must return only one column" error Date: Tue, 25 Jun 2013 09:23:08 +0530 Message-ID: <006601ce7157$82137610$863a6230$@kapila@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Ac5w76d/+MNar/XKTRy8316MrTu5DAAZx6Eg Content-Language: en-us X-Originating-IP: [10.18.96.98] X-CFilter-Loop: Reflected X-Pg-Spam-Score: -1.3 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On Monday, June 24, 2013 8:59 PM =D0=91=D0=BE=D1=80=D0=B8=D1=81 =D0=A0=D0= =BE=D0=BC=D0=B0=D1=88=D0=BE=D0=B2 wrote: > Amit, where should I post to force developing this feature ? You can post this to pgsql-hackers, but I think it would be more better if = you can check if any other database support that feature. I feel you need a more strong case for any developer to work on it and comm= unity to agree on it. This is just my personal opinion, so please feel free= to work the way you think is best. 2013/6/24 Amit Kapila On Monday, June 24, 2013 1:23 PM =D0=91=D0=BE=D1=80=D0=B8=D1=81 =D0=A0=D0= =BE=D0=BC=D0=B0=D1=88=D0=BE=D0=B2 wrote: >> Why do you want to know the exact row due to which this happens, and wha= t you want to do with it? > Suppose I have a query that should select something and this query has so= me subquery that is (possibly by error) supposed to return only one row, i.= e. > I mean that it fetches smth unique. But for some row it appears not to= be unique. > I will get that error. But I don't know about my error, I still suppose t= his to be unique. > In this case - how can I debug this ? I don't know which row was corrupti= ng uniqueness. > Moreover, let's assume I have more than one subquery. In this case this i= s even more complicated to debug error, because I need to check each subque= ry > for each row. It is not straightforward, but you can know by trying some logic like be= low: Declare the cursor with corresponding subquery For i In 1..10 Loop -- this loop is corresponding to outer query values While(Fetch new row) { If fetch returns row more than once then print it. } For the part which subquery is giving problem, you might need to break t= he query into smaller parts and check. At the moment I am not able to think of any other better way. >> I don't think there is any way, you can know exactly for which this erro= r occurred. > Why? Query executor knows what it executes and which row is now. Why cann= ot it log this info ? What I mean was that AFAIK currently there is no way to know that, if we= enhance the way you are suggesting, then it can possible. PostgreSQL does something similar for duplicate key, it prints the value= for which duplication happens. postgres=3D# insert into tbl values(4,2); ERROR: duplicate key value violates unique constraint "tbl_c1_idx" DETAIL: Key (c1)=3D(4) already exists. With Regards, Amit Kapila. 2013/6/24 Amit Kapila On Friday, June 21, 2013 1:24 PM =D0=91=D0=BE=D1=80=D0=B8=D1=81 =D0=A0=D0= =BE=D0=BC=D0=B0=D1=88=D0=BE=D0=B2 wrote: > I just realized that I wanted to ask about another error. > more than one row returned by a subquery used as an expression > not about > subquery must return only one column > 2013/6/21 =D0=91=D0=BE=D1=80=D0=B8=D1=81 =D0=A0=D0=BE=D0=BC=D0=B0=D1=88= =D0=BE=D0=B2 > Tom, suppose you haven't understood what the problem I'm facing with. > Let me explain deeper. > Try to execute the following 2 queries. > select (select generate_series(1,2)); > select (select generate_series(1,1)); > They differ only in data, both of them are well-written, so there is not = the problem in parsing. > But first query gives (even in psql) > ERROR: more than one row returned by a subquery used as an expression > Certainly - instead of generate_series I could write any usual query that= fetches some data from database. > And if this query returns one row - everything is correct. > And certainly, instead of selecting from dual (that is how it is called i= n Oracle) - I could construct more complex external query such that subquery > could return "more than one row" for just in some exact row (not in each = row) of external record set. > Example > select id, (select friend.id from user friend where friend.id =3D user.id= ) user from user > This query fetches all users with their friends assuming that every user = has only one friend. > But if some of them will have 2 friends - this query will fail with > ERROR: more than one row returned by a subquery used as an expression > And I will have no chance to guess - which user exactly this happened for. I don't think there is any way, you can know exactly for which this error= occurred. The main reason is that this error occurs when an expression subquery ret= urns more than one row when it is not expected. In some cases it is okay even if subquery expression returns more than on= e row, for example: postgres=3D# select 1 In (select generate_series(1,2)); ?column? ---------- t (1 row) postgres=3D# select 4 In (select generate_series(1,2)); ?column? ---------- f (1 row) postgres=3D# select 1 =3D (select generate_series(1,2)); ERROR: more than one row returned by a subquery used as an expression postgres=3D# Why do you want to know the exact row due to which this happens, and what y= ou want to do with it? With Regards, Amit Kapila. --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs