possible wierd boolean bug?
I have a strange but reproducible problem where a query does not seem to
return the same results.
esp=# select 1::int4, * from data1.po_line_file
esp-# where pol_po_no = '00000002' and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
[fields omitted]
(0 rows)
esp=# select 1::int4, * from data1.po_line_file
esp-# where --pol_po_no = '00000002' and
esp-# --(pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
[fields omitted, note commented redundant clauses]
(1 row)
The obviously silly Boolean clause here was sql that was generated by a
driver. I recoded the driver to make more intelligent sql and now the
problem is gone, at least from my application. Note that the same query
form but with different tables/fields works correctly 99.99999% of the
time, just not this time...I'm just curious as to how two seemingly
equivalent statements could produce different results...perhpaps I
missed something?
Merlin
That is bizarre. Does EXPLAIN show any difference?
---------------------------------------------------------------------------
Merlin Moncure wrote:
I have a strange but reproducible problem where a query does not seem to
return the same results.esp=# select 1::int4, * from data1.po_line_file
esp-# where pol_po_no = '00000002' and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
[fields omitted]
(0 rows)esp=# select 1::int4, * from data1.po_line_file
esp-# where --pol_po_no = '00000002' and
esp-# --(pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
[fields omitted, note commented redundant clauses]
(1 row)The obviously silly Boolean clause here was sql that was generated by a
driver. I recoded the driver to make more intelligent sql and now the
problem is gone, at least from my application. Note that the same query
form but with different tables/fields works correctly 99.99999% of the
time, just not this time...I'm just curious as to how two seemingly
equivalent statements could produce different results...perhpaps I
missed something?Merlin
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
--
Bruce Momjian | http://candle.pha.pa.us
pgman@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
Bruce wrote:
That is bizarre. Does EXPLAIN show any difference?
------------------------------------------------------------------------
--
esp=# explain analyze select 1::int4, * from data1.po_line_file
esp-# where pol_po_no = '00000002' and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
QUER
Y PLAN
------------------------------------------------------------------------
----------------------------
------------------------------------------------------------------------
----------------------------
--
Limit (cost=0.00..5.76 rows=1 width=313) (actual time=0.000..0.000
rows=0 loops=1)
-> Index Scan using po_line_file_pkey on po_line_file
(cost=0.00..5.76 rows=1 width=313) (actua
l time=0.000..0.000 rows=0 loops=1)
Index Cond: ((pol_po_no = '00000002'::bpchar) AND (pol_po_no =
'00000002'::bpchar) AND (pol
_po_no = '00000002'::bpchar) AND ((pol_po_rel_no)::smallint = 0) AND
((pol_po_rel_no)::smallint = 0)
)
Filter: ((pol_item_no)::text = '1570'::text)
Total runtime: 0.000 ms
(5 rows)
esp=# explain analyze select 1::int4, * from data1.po_line_file
esp-# where -- pol_po_no = '00000002' and
esp-# -- (pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
QUERY
PLAN
------------------------------------------------------------------------
----------------------------
------------------------------------
Limit (cost=0.00..5.75 rows=1 width=313) (actual time=0.000..0.000
rows=1 loops=1)
-> Index Scan using po_line_file_pkey on po_line_file
(cost=0.00..5.75 rows=1 width=313) (actua
l time=0.000..0.000 rows=1 loops=1)
Index Cond: ((pol_po_no = '00000002'::bpchar) AND
((pol_po_rel_no)::smallint = 0))
Filter: ((pol_item_no)::text = '1570'::text)
Total runtime: 0.000 ms
(5 rows)
Import Notes
Resolved by subject fallback
That is bizarre. Does EXPLAIN show any difference?
Uh oh.
esp=# reindex table data1.parts_order_line_file;
REINDEX
esp=# explain analyze select 1::int4, * from data1.po_line_file
esp-# where pol_po_no = '00000002' and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
QUER
Y PLAN
------------------------------------------------------------------------
----------------------------
------------------------------------------------------------------------
----------------------------
--
Limit (cost=0.00..5.76 rows=1 width=313) (actual time=0.000..0.000
rows=0 loops=1)
-> Index Scan using po_line_file_pkey on po_line_file
(cost=0.00..5.76 rows=1 width=313) (actua
l time=0.000..0.000 rows=0 loops=1)
Index Cond: ((pol_po_no = '00000002'::bpchar) AND (pol_po_no =
'00000002'::bpchar) AND (pol
_po_no = '00000002'::bpchar) AND ((pol_po_rel_no)::smallint = 0) AND
((pol_po_rel_no)::smallint = 0)
)
Filter: ((pol_item_no)::text = '1570'::text)
Total runtime: 0.000 ms
(5 rows)
Import Notes
Resolved by subject fallback
That is bizarre. Does EXPLAIN show any difference?
Uh oh.
esp=# reindex table data1.parts_order_line_file;
whoops, never mind that. In fact, I re-indexed the wrong table.
Reindexing makes no difference. Here, explain analyze and running query
in psql return different results. Observe:
esp=# select 1::int4, * from data1.po_line_file
esp-# where pol_po_no = '00000002' and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
int4 | id | lastmod | pol_po_no | pol_po_rel_no | pol_seq_no |
pol_parts_unit_no | pol_item_no | po
l_desc1 | pol_desc2 | pol_vendor_part_no | pol_qty_ordered |
pol_est_cost | pol_purchase_uom | pol_w
eight | pol_requested_date | pol_expected_date | pol_plant_loc_needed |
pol_workstation_needed | pol
_stock_loc_needed | pol_internal_contact | pol_update_unit_cost |
pol_gl_acct_no | pol_pur_to_inv_ra
tio | pol_print_rev_no | pol_revision_flag | pol_qty_received |
pol_parts_tracking_flag | pol_qty_re
jected
------+----+---------+-----------+---------------+------------+---------
----------+-------------+---
--------+-----------+--------------------+-----------------+------------
--+------------------+------
------+--------------------+-------------------+----------------------+-
-----------------------+----
------------------+----------------------+----------------------+-------
---------+------------------
----+------------------+-------------------+------------------+---------
----------------+-----------
-------
(0 rows)
esp=# explain analyze select 1::int4, * from data1.po_line_file
esp-# where pol_po_no = '00000002' and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0) and
esp-# (pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570')
esp-# limit 1 ;
QUER
Y PLAN
------------------------------------------------------------------------
----------------------------
------------------------------------------------------------------------
----------------------------
--
Limit (cost=0.00..5.79 rows=1 width=313) (actual time=0.000..0.000
rows=0 loops=1)
-> Index Scan using po_line_file_pkey on po_line_file
(cost=0.00..5.79 rows=1 width=313) (actua
l time=0.000..0.000 rows=0 loops=1)
Index Cond: ((pol_po_no = '00000002'::bpchar) AND (pol_po_no =
'00000002'::bpchar) AND (pol
_po_no = '00000002'::bpchar) AND ((pol_po_rel_no)::smallint = 0) AND
((pol_po_rel_no)::smallint = 0)
)
Filter: ((pol_item_no)::text = '1570'::text)
Total runtime: 0.000 ms
(5 rows)
Import Notes
Resolved by subject fallback
I confirmed the problem on a linux server running beta3...so this
problem is quite reproducible by running the attached scripts on a
freshly loaded database.
To reproduce the problem [adjust host,etc as necessary]:
1. type/cat test_boolean.sql | psql template1 (this will create a
database called 'test', connect to it, and load a few things.)
2. bzip -cd < poline.bzip | psql test (this will load a table into test
that was dumped via pg_dump)
3. try the following query:
select 1::int4, * from data1.po_line_file
where pol_po_no = '00000002' and
(pol_po_no = '00000002' and pol_po_rel_no = 0) and
(pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570');
it should return 0 rows.
Try it with explain/analyze which reports 4 rows.
Try it a third time as:
select 1::int4, * from data1.po_line_file
where -- pol_po_no = '00000002' and
-- (pol_po_no = '00000002' and pol_po_rel_no = 0) and
(pol_po_no = '00000002' and pol_po_rel_no = 0 and
pol_item_no = '1570');
which is logically equivalent to the first form (isn't it?) and this
returns 1 row (the correct answer).
Merlin
Attachments:
poline.bzipapplication/octet-stream; name=poline.bzipDownload
BZh91AY&SYA/s� p�z:By��?�������`^^o>�������a���y�wl�%p;M�aP���m��
b��voyF�RH���Q�5���,!$I�d��!C
OP���2�z�S4@�4!��HJy)�hd� F� �~�(F�� 4� ���M4CC#PG�a
���L5M����H������F� "H!4"jxJ ��
�����*��� (`, ��_ d IDE����9�_o���J��{�mmm�
������\��9v��S��F�t.5�u;`���rw'�vE�Yx����W��y+�(�|y$;#��y��P�h�O�X�L����3����[��w^,a��
��JT��U�\�V��dt[.� �.�Q�����Ib�J��%*�r���� @y�������p;������|#�f����H���k5H� ��eQ|� "W� <��9�� �"C#L���X� ��x� w�H��w�PBd Y��LV�H�HE�� "���yT���Dv|+i��H���R��&��Z�|�O�j�RsY�Jre��U���������)C3
�/T�����vN�[(2�3V�Tc��)�<D���#��5�(!����dX"]f���[q�q��'}l�z���+p����Kh�R��B�i�2����0��.d1
���$������~5���K����/S�z���lO9}b��#Y(�X���{&)������i5��n�MS��!�po.l��kL2���0pM� E�
Cj<L�]����2
�p����������w�����9������8�r�RG&�0����j�u���[dn����_iUf<�<�0r���1�Y�pl1O�I^���?^�g��P�;V��z=���7����VF[8�Z��6�� �>cC�`9� w�s;`}�}�;�q����T,uh�2KM��^��bV�R���3��O.u� � �� 3�� UT *��
� ������t~|�5o���[��$���J>���������N��6Zk-:��q�
�������fzZ8y���m&�5��h/�KeC���L8��0���5��t��5�U��'��H��s2�W���c���K��9����\���:�����Q�}�HCV�N<{^�������y������[Vq�����W ���F@$M��*�a����Tx����R��c%�)'�!P@ IY��A�pp���}��z����^}5�3�q�`��4���I�ijsmjXsFPy����:<�'.����z�� �f�H(=����<z�y�E
��KM��V��������QX`���J":����!(mN���H�|pMm�=c,|� �������� H~���%���'��P���FAT,���4����2T9#� �$�� Od�:gUAC �uB�r�I���h��}a�� ������4G���E.�����n�^�t���%�8UY,�x7����a,����Uu��^}��]$���{mp\&���}�}�w�* 4@��0rk2����V��dGM�(�d.�[;�����"-��W:�,K�(T �}/�w�X�q��� g�yh��V�TX���:D.Rq���}�����qg�Y}%��;�g�D��^�q5����t�I%�x^�P9��]k�>�>4�B~��}L�5�@D���g��
HBHB\,j��0&ABu' ��O�^D�wF]�T��c+�����N�(��z�����/���>��D$'e���: f w_dZ�J�_~����, $)BSS��# @ZDe�
�w�|�K[�dq�>�F������%d�l1�3-����b�Xa+&