pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #6079: Wrong select result
4+ messages / 4 participants
[nested] [flat]

* BUG #6079: Wrong select result
@ 2011-06-28 14:41 Renat <renat.nasyrov@itv.ru>
  2011-06-28 17:16 ` Re: BUG #6079: Wrong select result Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Renat @ 2011-06-28 14:41 UTC (permalink / raw)
  To: pgsql-bugs


The following bug has been logged online:

Bug reference:      6079
Logged by:          Renat
Email address:      renat.nasyrov@itv.ru
PostgreSQL version: 9.0.4
Operating system:   Windows
Description:        Wrong select result
Details: 

INPUT:

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

If we will replace foo_date_to_index to:

CREATE INDEX foo_date_to_index
  ON foo
  USING btree
  (date_to)
  WHERE date_to is NOT NULL

Then:

SELECT * FROM foo where date_to is null and date_to > '2011-01-01'

Return: 0 rows

Please explain for me what happens?



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #6079: Wrong select result
  2011-06-28 14:41 BUG #6079: Wrong select result Renat <renat.nasyrov@itv.ru>
@ 2011-06-28 17:16 ` Pavel Stehule <pavel.stehule@gmail.com>
  2011-06-28 21:45   ` Re: BUG #6079: Wrong select result Dean Rasheed <dean.a.rasheed@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Pavel Stehule @ 2011-06-28 17:16 UTC (permalink / raw)
  To: Renat <renat.nasyrov@itv.ru>; +Cc: pgsql-bugs

Hello

It working as expected on 9.0.4 Linux. Please, can you recheck your
application? Try to use a console - psql,

postgres=# select * from foo where date_to is null and date_to > '2011-01-01';
 id | date_to
----+---------
(0 rows)

Regards

Pavel Stehule

2011/6/28 Renat <renat.nasyrov@itv.ru>:
>
> The following bug has been logged online:
>
> Bug reference:      6079
> Logged by:          Renat
> Email address:      renat.nasyrov@itv.ru
> PostgreSQL version: 9.0.4
> Operating system:   Windows
> Description:        Wrong select result
> Details:
>
> INPUT:
>
> 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
>
> If we will replace foo_date_to_index to:
>
> CREATE INDEX foo_date_to_index
>  ON foo
>  USING btree
>  (date_to)
>  WHERE date_to is NOT NULL
>
> Then:
>
> SELECT * FROM foo where date_to is null and date_to > '2011-01-01'
>
> Return: 0 rows
>
> Please explain for me what happens?
>
> --
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs
>



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #6079: Wrong select result
  2011-06-28 14:41 BUG #6079: Wrong select result Renat <renat.nasyrov@itv.ru>
  2011-06-28 17:16 ` Re: BUG #6079: Wrong select result Pavel Stehule <pavel.stehule@gmail.com>
@ 2011-06-28 21:45   ` Dean Rasheed <dean.a.rasheed@gmail.com>
  2011-06-29 23:45     ` Re: BUG #6079: Wrong select result Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Dean Rasheed @ 2011-06-28 21:45 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: Renat <renat.nasyrov@itv.ru>; pgsql-bugs

> 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)) {
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: BUG #6079: Wrong select result
  2011-06-28 14:41 BUG #6079: Wrong select result Renat <renat.nasyrov@itv.ru>
  2011-06-28 17:16 ` Re: BUG #6079: Wrong select result Pavel Stehule <pavel.stehule@gmail.com>
  2011-06-28 21:45   ` Re: BUG #6079: Wrong select result Dean Rasheed <dean.a.rasheed@gmail.com>
@ 2011-06-29 23:45     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2011-06-29 23:45 UTC (permalink / raw)
  To: Dean Rasheed <dean.a.rasheed@gmail.com>; +Cc: Pavel Stehule <pavel.stehule@gmail.com>; Renat <renat.nasyrov@itv.ru>; pgsql-bugs

Dean Rasheed <dean.a.rasheed@gmail.com> writes:
>> 2011/6/28 Renat <renat.nasyrov@itv.ru>:
>>> 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

> 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.

Yeah, seems to be my fault :-(.  Will fix.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2011-06-29 23:45 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-06-28 14:41 BUG #6079: Wrong select result Renat <renat.nasyrov@itv.ru>
2011-06-28 17:16 ` Pavel Stehule <pavel.stehule@gmail.com>
2011-06-28 21:45   ` Dean Rasheed <dean.a.rasheed@gmail.com>
2011-06-29 23:45     ` Tom Lane <tgl@sss.pgh.pa.us>

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