Question

I was doing a query on pgAdmin and stumbled upon this weird behavior.
I was connected to a server running PostgreSQL 9.1.9.
I have a table called messages with the following definition:

ghareh@godot:~$ psql
psql (9.1.9)
Type "help" for help.

ghareh=# \d messages
                            Table "public.messages"
    Column     |            Type             |            Modifiers            
---------------+-----------------------------+---------------------------------
 messageid     | character varying(200)      | not null
 senderaliasid | integer                     | not null
 referenceid   | character varying(200)      | default NULL::character varying
 recipaliasid  | integer                     | 
 datetime      | timestamp(2) with time zone | not null
 subject       | character varying(512)      | not null
 body          | text                        | not null
 listid        | integer                     | 
Indexes:
    "messages_pkey" PRIMARY KEY, btree (messageid)
    "messages_datetime_idx" btree (datetime)
    "recipaliasid_idx" btree (recipaliasid)
    "referenceid_idx" btree (referenceid)
    "senderaliasid_idx" btree (senderaliasid)
Foreign-key constraints:
    "messages_listid_fkey" FOREIGN KEY (listid) REFERENCES lists(listid)
    "messages_recip_fkey" FOREIGN KEY (recipaliasid, listid) REFERENCES aliases(aliasid, listid)
    "messages_sender_fkey" FOREIGN KEY (senderaliasid, listid) REFERENCES aliases(aliasid, listid)
Referenced by:
    TABLE "messages_attachments" CONSTRAINT "pkfkmid" FOREIGN KEY (messageid) REFERENCES messages(messageid)

My question involves the columns, body and subject.

I had a query that generated a set of results. Then, to refine my query, I added the term: where body like '%JSON%' i.e., the subset of results where body contains the string 'JSON'.
I got some results containing the word, and some that did not! But if I searched for an arbitrary string the results would be ok. I checked and found out that the query is not just searching the body column, but also the subject column as well which is crazy.

Here is my initial query:

select * from messages where messageid = '44BC310F.1090305@torrez.us'

which returns 1 row:

messageid: "44BC310F.1090305@torrez.us";
senderaliasid: 13777;
referenceid: "7edfeeef0607171746r7d708067g15c77c3aa0ef9158@mail.gmail.com";
recipaliasid: ;
datetime: "2006-07-17 20:53:35-07";
listid: 251;
subject: "Re: svn commit: r422930 - /incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java";
body: "busted! thanks for the thorough review.


-Elias

Garrett Rooney wrote:
> On 7/17/06, eliast@apache.org <eliast@apache.org> wrote:
>> Author: eliast
>> Date: Mon Jul 17 17:44:10 2006
>> New Revision: 422930
>>
>> URL: http://svn.apache.org/viewvc?rev=422930 (...)"

If I search:

select * from messages
where messageid = '44BC310F.1090305@torrez.us'
and body like '%JSON%'

I should not get any results because there is none in the body. But I still get the same row returned - it would seem because 'JSON' is in the subject?

I even tried this:

select * from messages
where messageid = '44BC310F.1090305@torrez.us'
and body like '%incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/json/JSONWriter.java%'

and I still got the same row back. I am severely confused.

I attempted to reproduce the results on sqlfiddle.com, but I was not successful. There, I get what is expected of an sql select query:
http://sqlfiddle.com/#!1/ec74c/4

Was it helpful?

Solution

You cannot reproduce the same effect on SQL Fiddle.

I recreated your table in Postgres 9.1.13 (always upgrade to the latest point release!) and ran the queries in pgAdmin (current version 1.18.1). I cannot reproduce the problem.

pgAdmin?

I don't see how pgAdmin could play a role in this - unless you have been selecting only a part of your query, unaware of this effect:
pgAdmin shortcuts to execute scripts

Or you might be fooled by the "Max. characters per column" setting, which truncates long values in the display, hiding the match in the truncated part, like @IMSoP suggested in his comment. Check File -> Options ...

pgAdmin option

If that's not it and unless we are dealing with typos or circumstances not in your question, this would indicate something is broken in your database.

Corruption?

In simple cases with just a corrupted index, a REINDEX TABLE might do the trick:

REINDEX TABLE messages;

However, on a closer look, I don't see an index that could possibly be the culprit here.

Corrupted system catalog? Read this first:
http://wiki.postgresql.org/wiki/Corruption

Then read the Notes section for REINDEX and run from the shell:

$ export PGOPTIONS="-P"
$ psql broken_db
...
broken_db=> REINDEX DATABASE broken_db;
broken_db=> \q

Corruption often indicates a problem with your hardware. A failing disk or something. Follow up on that ...

Related question:
Repair Corrupt database postgresql

OTHER TIPS

In this case it was pgAdmin's fault. As @IMSoP mentioned, it appears that pgAdmin truncated the results. I got confused since I recently installed a new version of pgAdmin and this behavior is new to this version (at least new by default) as I clearly remember running the same queries 1 year ago and getting full text results.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top