Dont let those int8's drive you mad!!

Started by Jeff Amielover 21 years ago5 messagesgeneral
Jump to latest
#1Jeff Amiel
jamiel@istreamimaging.com

I was stuck for 3 hours today trying to figure out why postgres was doing a seq scan on a primary key/unique index column.

the statement was innocuous enough....

update transactions set state='O' where trans_id=14332

trans_id was the primary key and also had a unique btree index on it.
No matter what I did, seq_scan....
I vacuum/full/analyzed to no avail.
Then it hit me. trans_id is an int8.
simply changing the query to:

update transactions set state='O' where trans_id=int8(14332)

Sped it up from 4 milliseconds to .07 milliseconds (and obviously now did an index scan)!!!!
This HAS bitten me before.

Questions:
If postgres knows the field is an int8, why do I have to cast it in my query?
Any way I can avoid having to watch for this particular column (and 3 others in other tables) column in all my queries?

--
Jeff Amiel
Systems/Development Manager
iStream Imaging, an iTeam Company
jamiel@iStreamImaging.com
(262) 796-0925 x1011

#2Doug McNaught
doug@mcnaught.org
In reply to: Jeff Amiel (#1)
Re: Dont let those int8's drive you mad!!

Jeff Amiel <jamiel@istreamimaging.com> writes:

If postgres knows the field is an int8, why do I have to cast it in my query?

This is fixed in 8.0.

-Doug

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jeff Amiel (#1)
Re: Dont let those int8's drive you mad!!

Jeff Amiel <jamiel@istreamimaging.com> writes:

If postgres knows the field is an int8, why do I have to cast it in my query?

As of 8.0 you won't have to anymore.

You don't really want to know why it took us six years to find a
workable solution... suffice it to say that it was harder than you
might think, because of Postgres' extensible approach to datatypes.

regards, tom lane

#4Jeff Amiel
jamiel@istreamimaging.com
In reply to: Tom Lane (#3)
Re: Dont let those int8's drive you mad!!

Outstanding....
thanks all....

Tom Lane wrote:

Show quoted text

Jeff Amiel <jamiel@istreamimaging.com> writes:

If postgres knows the field is an int8, why do I have to cast it in my query?

As of 8.0 you won't have to anymore.

You don't really want to know why it took us six years to find a
workable solution... suffice it to say that it was harder than you
might think, because of Postgres' extensible approach to datatypes.

regards, tom lane

#5Joshua D. Drake
jd@commandprompt.com
In reply to: Jeff Amiel (#1)
Re: Dont let those int8's drive you mad!!

update transactions set state='O' where trans_id=int8(14332)

Sped it up from 4 milliseconds to .07 milliseconds (and obviously now
did an index scan)!!!!
This HAS bitten me before.
Questions:
If postgres knows the field is an int8, why do I have to cast it in my
query?

That is a long answer.

Any way I can avoid having to watch for this particular column (and 3
others in other tables) column in all my queries?

1. Automatically cast all your queries
2. Quote all parameters.

-- 
Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-667-4564 - jd@commandprompt.com - http://www.commandprompt.com
PostgreSQL Replicator -- production quality replication for PostgreSQL