Alvaro Herrera <alvherre@alvh.no-ip.org> writes:
> PG Doc comments form wrote:
>> create table t(x int);
>> create table u(x int);
>>
>> insert into t values (1), (2), (2), (3), (3);
>> insert into u values (1), (2);
>>
>> select * from t except all select * from u;
>> x
>> ---
>> 3
>> 3
>> 2
>> (3 rows)
> I find this pretty odd behavior. Is this not an outright bug?
It's exactly what the standard says to do: if there are M occurrences
of a row value in the LHS, and N occurrences in the RHS, emit
max(M-N, 0) copies of the row. To my mind that's a reasonable definition
of EXCEPT if you suppose that nominally-identical rows are matched and
discarded one by one, rather than with de-duplication occurring
beforehand.
> [*] I didn't try terribly hard, but couldn't actually find where the
> behavior is defined.
In SQL:2011, it's 7.13 <query expression> general rule 3) b) iii) 3) B),
on page 420 in the draft version I have.
regards, tom lane