Re: How to do A union (B - ( A intersect B )) or sort of :-)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: A B <gentosaker(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: How to do A union (B - ( A intersect B )) or sort of :-)
Date: 2010-11-04 20:43:41
Message-ID: 587.1288903421@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

A B <gentosaker(at)gmail(dot)com> writes:
> Sorry for the subject, but I knew no words to describe it better.
> I will try to explain

> First a more general question: is there any clever way to do two
> selects A and B and then return the result
> A union ( B \ (A intersect B)) ( \ is "set subtraction")

Ummm ... isn't that just a UNION? Anyway, SQL's features for this
sort of thing are described here:
http://www.postgresql.org/docs/9.0/static/queries-union.html

> Any ideas besides writing the explicit queries? I guess one has to
> lock the table to get the same result on both selects unless one can
> cache the result of A and B so you don't run it twice?

Both arms of a UNION or similar query would be reading the table with
the same snapshot, so there wouldn't be any need for a lock to get
consistent results. You may also need to spend some time reading
http://www.postgresql.org/docs/9.0/static/mvcc.html

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message ALT SHN 2010-11-05 15:17:53 Data Types
Previous Message A B 2010-11-04 19:53:57 How to do A union (B - ( A intersect B )) or sort of :-)