Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 256E4632B36 for ; Mon, 22 Feb 2010 16:29:20 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 28115-07 for ; Mon, 22 Feb 2010 20:29:08 +0000 (UTC) X-Greylist: delayed 00:24:13.837969 by SQLgrey-1.7.6 Received: from mail-fx0-f223.google.com (mail-fx0-f223.google.com [209.85.220.223]) by mail.postgresql.org (Postfix) with ESMTP id 535AB63291D for ; Mon, 22 Feb 2010 16:29:08 -0400 (AST) Received: by fxm23 with SMTP id 23so2883013fxm.3 for ; Mon, 22 Feb 2010 12:29:06 -0800 (PST) Received: by 10.87.38.2 with SMTP id q2mr8899435fgj.0.1266868693600; Mon, 22 Feb 2010 11:58:13 -0800 (PST) Received: from ?10.10.20.22? (178.131.241.83.in-addr.dgcsystems.net [83.241.131.178]) by mx.google.com with ESMTPS id l19sm2331917fgb.10.2010.02.22.11.58.11 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 22 Feb 2010 11:58:12 -0800 (PST) Cc: pgsql-performance@postgresql.org Message-Id: <8AD3F6B6-BCCF-46E1-B255-9EB8E761653B@gluefinance.com> From: Joel Jacobson To: "Pierre C" In-Reply-To: Content-Type: multipart/alternative; boundary=Apple-Mail-20-798681795 Mime-Version: 1.0 (Apple Message framework v936) Subject: Re: plpgsql plan cache Date: Mon, 22 Feb 2010 20:58:10 +0100 References: <38E2265E-B38E-4EBD-BB87-23B46EF4FD1C@gluefinance.com> X-Mailer: Apple Mail (2.936) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.714 tagged_above=-10 required=5 tests=BAYES_00=-2.599, HTML_FONT_FACE_BAD=0.884, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201002/302 X-Sequence-Number: 37723 --Apple-Mail-20-798681795 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit db=# \d FlagValueAccountingTransactions Table "public.flagvalueaccountingtransactions" Column | Type | Modifiers ---------------------+-------------------------- + -------------------------------------------------------------------------- flagvalueid | integer | not null eventid | integer | not null transactionid | integer | not null recorddate | timestamp with time zone | not null debitaccountnumber | integer | not null creditaccountnumber | integer | not null debitaccountname | character varying | not null creditaccountname | character varying | not null amount | numeric | not null currency | character(3) | not null seqid | integer | not null default nextval('seqflagvalueaccountingtransactions'::regclass) undone | smallint | undoneseqid | integer | Indexes: "flagvalueaccountingtransactions_pkey" PRIMARY KEY, btree (seqid) "index_flagvalueaccountingtransactions_eventid" btree (eventid) "index_flagvalueaccountingtransactions_flagvalueid" btree (flagvalueid) "index_flagvalueaccountingtransactions_recorddate" btree (recorddate) db=# EXPLAIN ANALYZE SELECT SUM(Amount) FROM FlagValueAccountingTransactions WHERE FlagValueID = 182903 AND (RecordDate >= '2008-10-21' AND RecordDate < '2008-10-22') AND CreditAccountName = 'CLIENT_BALANCES' AND Currency = 'SEK'; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=1291.74..1291.75 rows=1 width=7) (actual time=1.812..1.812 rows=1 loops=1) -> Index Scan using index_flagvalueaccountingtransactions_recorddate on flagvalueaccountingtransactions (cost=0.00..1291.68 rows=25 width=7) (actual time=1.055..1.807 rows=1 loops=1) Index Cond: ((recorddate >= '2008-10-21 00:00:00+02'::timestamp with time zone) AND (recorddate < '2008-10-22 00:00:00+02'::timestamp with time zone)) Filter: ((flagvalueid = 182903) AND ((creditaccountname)::text = 'CLIENT_BALANCES'::text) AND (currency = 'SEK'::bpchar)) Total runtime: 1.847 ms (5 rows) db=# PREPARE myplan (integer,date,date,varchar,char(3)) AS SELECT SUM(Amount) FROM FlagValueAccountingTransactions WHERE FlagValueID = $1 AND RecordDate >= $2 AND RecordDate < $3 AND DebitAccountName = $4 AND Currency = $5;PREPARE PREPARE db=# EXPLAIN ANALYZE EXECUTE myplan(182903,'2008-10-21','2008-10-22','CLIENT_BALANCES','SEK'); QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=3932.75..3932.76 rows=1 width=7) (actual time=175.792..175.792 rows=1 loops=1) -> Bitmap Heap Scan on flagvalueaccountingtransactions (cost=2283.91..3932.74 rows=1 width=7) (actual time=175.747..175.767 rows=4 loops=1) Recheck Cond: ((recorddate >= $2) AND (recorddate < $3) AND (flagvalueid = $1)) Filter: (((debitaccountname)::text = ($4)::text) AND (currency = $5)) -> BitmapAnd (cost=2283.91..2283.91 rows=582 width=0) (actual time=175.714..175.714 rows=0 loops=1) -> Bitmap Index Scan on index_flagvalueaccountingtransactions_recorddate (cost=0.00..395.97 rows=21536 width=0) (actual time=1.158..1.158 rows=3432 loops=1) Index Cond: ((recorddate >= $2) AND (recorddate < $3)) -> Bitmap Index Scan on index_flagvalueaccountingtransactions_flagvalueid (cost=0.00..1887.69 rows=116409 width=0) (actual time=174.132..174.132 rows=1338824 loops=1) Index Cond: (flagvalueid = $1) Total runtime: 175.879 ms (10 rows) Hm, it is strange the query planner is using two different strategies for the same query? On Feb 22, 2010, at 8:42 PM, Pierre C wrote: > >> I cannot understand why the index is not being used when in the >> plpgsql function? >> I even tried to make a test function containing nothing more than >> the single query. Still the index is not being used. >> When running the same query in the sql prompt, the index is in use >> though. > > Please post the following : > > - EXPLAIN ANALYZE your query directly in psql > - PREPARE testq AS your query > - EXPLAIN ANALYZE EXECUTE testq( your parameters ) --Apple-Mail-20-798681795 Content-Type: text/html; charset=US-ASCII Content-Transfer-Encoding: quoted-printable
db=3D# \d = FlagValueAccountingTransactions
   =                     =                Table = "public.flagvalueaccountingtransactions"
   =     Column        |       =     Type           |     =                     =        Modifiers           =                     =   
---------------------+--------------------------+------------------= --------------------------------------------------------
 flagvalueid         | integer   =                | not = null
 eventid         =     | integer               =    | not null
 transactionid       | integer     =              | not = null
 recorddate         =  | timestamp with time zone | not = null
 debitaccountnumber  | integer =                  | not = null
 creditaccountnumber | integer   =                | not = null
 debitaccountname    | = character varying        | not = null
 creditaccountname   | character = varying        | not = null
 amount         =      | numeric             =      | not null
 currency =            | character(3)     =         | not null
 seqid   =             | integer       =            | not null default = nextval('seqflagvalueaccountingtransactions'::regclass)
 undone              | = smallint                 = | 
 undoneseqid         = | integer                 =  | 
Indexes:
   =  "flagvalueaccountingtransactions_pkey" PRIMARY KEY, btree = (seqid)
   =  "index_flagvalueaccountingtransactions_eventid" btree = (eventid)
   =  "index_flagvalueaccountingtransactions_flagvalueid" btree = (flagvalueid)
   =  "index_flagvalueaccountingtransactions_recorddate" btree = (recorddate)

db=3D# EXPLAIN ANALYZE SELECT SUM(Amount) FROM = FlagValueAccountingTransactions WHERE FlagValueID =3D 182903 AND = (RecordDate >=3D '2008-10-21' AND RecordDate < '2008-10-22') AND = CreditAccountName =3D 'CLIENT_BALANCES' AND Currency =3D = 'SEK';

           =                     =                     =                     =                   QUERY = PLAN                   =                     =                     =                     =         =   
---------------------------------------------------= --------------------------------------------------------------------------= ---------------------------------------------------------------
=  Aggregate  (cost=3D1291.74..1291.75 rows=3D1 width=3D7) = (actual time=3D1.812..1.812 rows=3D1 loops=3D1)
   = ->  Index Scan using = index_flagvalueaccountingtransactions_recorddate on = flagvalueaccountingtransactions  (cost=3D0.00..1291.68 rows=3D25 = width=3D7) (actual time=3D1.055..1.807 rows=3D1 = loops=3D1)
         Index Cond: = ((recorddate >=3D '2008-10-21 00:00:00+02'::timestamp with time zone) = AND (recorddate < '2008-10-22 00:00:00+02'::timestamp with time = zone))
         Filter: ((flagvalueid = =3D 182903) AND ((creditaccountname)::text =3D 'CLIENT_BALANCES'::text) = AND (currency =3D 'SEK'::bpchar))
 Total runtime: 1.847 = ms
(5 rows)

db=3D# PREPARE = myplan (integer,date,date,varchar,char(3)) AS SELECT SUM(Amount) FROM = FlagValueAccountingTransactions WHERE FlagValueID =3D $1 AND RecordDate = >=3D $2 AND RecordDate < $3 AND DebitAccountName  =3D $4 AND = Currency =3D = $5;PREPARE
PREPARE

db=3D# EXPLAIN = ANALYZE EXECUTE = myplan(182903,'2008-10-21','2008-10-22','CLIENT_BALANCES','SEK');

             =                     =                     =                     =               QUERY PLAN   =                     =                     =                     =                     =   =   
---------------------------------------------------= --------------------------------------------------------------------------= -----------------------------------------------------------
&nbs= p;Aggregate  (cost=3D3932.75..3932.76 rows=3D1 width=3D7) (actual = time=3D175.792..175.792 rows=3D1 loops=3D1)
   -> =  Bitmap Heap Scan on flagvalueaccountingtransactions =  (cost=3D2283.91..3932.74 rows=3D1 width=3D7) (actual = time=3D175.747..175.767 rows=3D4 loops=3D1)
   =       Recheck Cond: ((recorddate >=3D $2) AND = (recorddate < $3) AND (flagvalueid =3D $1))
   =       Filter: (((debitaccountname)::text =3D ($4)::text) = AND (currency =3D $5))
         -> =  BitmapAnd  (cost=3D2283.91..2283.91 rows=3D582 width=3D0) = (actual time=3D175.714..175.714 rows=3D0 loops=3D1)
  =             ->  Bitmap Index Scan = on index_flagvalueaccountingtransactions_recorddate =  (cost=3D0.00..395.97 rows=3D21536 width=3D0) (actual = time=3D1.158..1.158 rows=3D3432 loops=3D1)
     =                 Index Cond: = ((recorddate >=3D $2) AND (recorddate < = $3))
               = ->  Bitmap Index Scan on = index_flagvalueaccountingtransactions_flagvalueid =  (cost=3D0.00..1887.69 rows=3D116409 width=3D0) (actual = time=3D174.132..174.132 rows=3D1338824 loops=3D1)       =               Index Cond: = (flagvalueid =3D $1)
 Total runtime: 175.879 = ms
(10 = rows)



H= m, it is strange the query planner is using two different strategies for = the same query?



I cannot understand why = the index is not being used when in the plpgsql = function?
I even tried to make = a test function containing nothing more than the single query. Still the = index is not being used.
When = running the same query in the sql prompt, the index is in use = though.

Please post the following :

- EXPLAIN = ANALYZE your query directly in psql
- PREPARE testq AS your = query
- EXPLAIN ANALYZE EXECUTE testq( your parameters = )

= --Apple-Mail-20-798681795--