Re: CREATE FUNCTION ... performance boost?

From: Richard Huxton <dev(at)archonet(dot)com>
To: Jan Danielsson <jan(dot)m(dot)danielsson(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: CREATE FUNCTION ... performance boost?
Date: 2007-06-29 16:32:51
Message-ID: 46853433.1070002@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jan Danielsson wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA512
>
> Hello all,
>
> In order to speed up commonly used queries, I use prepared
> statements. I assume that 'prepare' tells some database query planner to
> take a look at a query, and do all preparations for it, then store those
> preparations somewhere for when it's needed.

You might well be slowing things down. PREPARE/EXECUTE can't replan as
you change the parameters to the query. Now, in many cases that won't
matter, e.g. "SELECT * FROM foo WHERE x=? AND y=?" where there's a
unique index on x or y. What you are gaining is the cost of planning on
each call.

> When I use CREATE FUNCTION to create a function, does that also get
> "planned" automatically (plsql, specifically). My gut feeling is that it
> doesn't (mainly since it can be any external language).

Well, plpgsql will have its plans "compiled" on the first call. That
will last as long as the connection.

> The reason I'm asking is because I'd like to have permanently stored
> "prepared" statements in a database. On even intervals, I connect to it,
> and perform a number of queries. Many of them are the same statements
> being run over and over. And these prepared statements really are "hard
> coded", so they would do fine in my database initialization script.
>
> I assume that "functions" aren't what I am looking for.. But is is
> possible to store preparations some way so they survive past
> connection/disconnections?

Nope. Plans last only until the end of a session.

What you might be able to do is use a connection pool/proxy to maintain
connections to the db while you connect to the proxy. That way the
connection can be made to last longer.

Before you go to too much effort though, I'd check that you're really
going to gain a useful performance boost.

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Erik Jones 2007-06-29 17:47:07 Re: Standby servers and incrementally updated backups
Previous Message Merlin Moncure 2007-06-29 16:27:26 Re: CREATE FUNCTION ... performance boost?