pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Graham Vickrage <graham@digitalplanit.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Use of indexes in plpgsql functions
Date: Fri, 15 Dec 2000 17:54:28 -0500
Message-ID: <13994.976920868@sss.pgh.pa.us> (raw)
In-Reply-To: <NDBBJABDILOPAOOMFJHOIEHACGAA.graham@digitalplanit.com>
References: <NDBBJABDILOPAOOMFJHOIEHACGAA.graham@digitalplanit.com>

"Graham Vickrage" <graham@digitalplanit.com> writes:
>> CREATE FUNCTION get_url_hits (varchar, int4) RETURNS int4 AS '
>> DECLARE
>> num INT4;
>> BEGIN
>> SELECT count(*) INTO num FROM statistics WHERE url = $1 and
>> website_id = $2;

>  [ is slow ]

A possible gotcha is if the url and website_id columns are declared as
something other than varchar and int4 respectively.  The planner's not
very smart about optimizing cross-datatype comparisons into indexscans.
When you write out a query with constants you are protected from this
because the constants have their types adjusted, but when you write "url
= $1" you'd best make sure $1 is declared exactly the same way as url.

7.1 fixes some instances of this gotcha, but not all of 'em IIRC.

			regards, tom lane



view thread (3+ messages)

Message-ID: <13994.976920868@sss.pgh.pa.us>
Permalink:  ../13994.976920868@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/13994.976920868@sss.pgh.pa.us

 · 

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-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, graham@digitalplanit.com
  Subject: Re: Use of indexes in plpgsql functions
  In-Reply-To: <13994.976920868@sss.pgh.pa.us>
  References: <NDBBJABDILOPAOOMFJHOIEHACGAA.graham@digitalplanit.com> <13994.976920868@sss.pgh.pa.us>

* 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