pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dean Rasheed <dean.a.rasheed@gmail.com>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Renat <renat.nasyrov@itv.ru>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #6079: Wrong select result
Date: Tue, 28 Jun 2011 22:45:45 +0100
Message-ID: <BANLkTikwdP9utpjNTb9p7SmBJf57KPFMtA@mail.gmail.com> (raw)
In-Reply-To: <BANLkTik8Ew3Hqf9Hpqwn88XDj=q0AM7DUA@mail.gmail.com>
References: <201106281441.p5SEfU5x051446@wwwmaster.postgresql.org>
	<BANLkTik8Ew3Hqf9Hpqwn88XDj=q0AM7DUA@mail.gmail.com>

> 2011/6/28 Renat <renat.nasyrov@itv.ru>:
>>
>> create table foo (
>> id bigint not null,
>> date_to timestamp without time zone,
>> CONSTRAINT foo_pkey PRIMARY KEY (id)
>> );
>>
>> CREATE INDEX foo_date_to_index
>>  ON foo
>>  USING btree
>>  (date_to)
>>
>> insert into foo (id, date_to) values (1, now());
>> insert into foo (id, date_to) values (2, NULL);
>>
>> select * from foo where date_to is null and date_to > '2011-01-01'
>>
>> Expected: 0 rows
>>
>> But: it return 1 row with id=2
>>

I get the same error on HEAD too. An even simpler test case is this:

create table foo(a int);
create index foo_a_idx on foo(a);
insert into foo values (10),(NULL);
select 1 from foo where a is null and a > 10000;

 ?column?
----------
        1
(1 row)

The problem seems to be in _bt_preprocess_keys(), which discards the
"a > 10000" predicate in favour of the "a is null" predicate on the
grounds that "null > 10000" in a nulls-last index.

It looks like a previous revision had the right check, based on the
logic that x IS NULL is incompatible with any other predicate.

Regards,
Dean

Attachments:

nbtutils.patch (application/octet-stream, 650B) download diff --git a/src/backend/access/nbtree/nbtutils.c b/src/backend/access/nbtree/nbtutils.c new file mode 100644 index 2e896a2..09b8c8e *** a/src/backend/access/nbtree/nbtutils.c --- b/src/backend/access/nbtree/nbtutils.c *************** _bt_preprocess_keys(IndexScanDesc scan) *** 339,344 **** --- 339,351 ---- if (!chk || j == (BTEqualStrategyNumber - 1)) continue; + /* IS NULL together with any other predicate must fail */ + if (eq->sk_flags & SK_SEARCHNULL) + { + so->qual_ok = false; + return; + } + if (_bt_compare_scankey_args(scan, chk, eq, chk, &test_result)) {

view thread (4+ messages)  latest in thread

Message-ID: <BANLkTikwdP9utpjNTb9p7SmBJf57KPFMtA@mail.gmail.com>
Permalink:  ../BANLkTikwdP9utpjNTb9p7SmBJf57KPFMtA@mail.gmail.com/
Also on:    postgresql.org/message-id/BANLkTikwdP9utpjNTb9p7SmBJf57KPFMtA@mail.gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: dean.a.rasheed@gmail.com, pavel.stehule@gmail.com, renat.nasyrov@itv.ru
  Subject: Re: BUG #6079: Wrong select result
  In-Reply-To: <BANLkTikwdP9utpjNTb9p7SmBJf57KPFMtA@mail.gmail.com>
  References: <201106281441.p5SEfU5x051446@wwwmaster.postgresql.org> <BANLkTik8Ew3Hqf9Hpqwn88XDj=q0AM7DUA@mail.gmail.com> <BANLkTikwdP9utpjNTb9p7SmBJf57KPFMtA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox