Re: PostgreSQL sequence within function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Russ Brown <pickscrape(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: PostgreSQL sequence within function
Date: 2005-07-01 13:41:54
Message-ID: 28372.1120225314@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Russ Brown <pickscrape(at)gmail(dot)com> writes:
> This just made me think. If I was writing this function, I would have
> written it as an SQL function like this:

> CREATE or REPLACE FUNCTION getSeq() RETURNS int AS $$
> SELECT nextval('myseq');
> $$ LANGUAGE SQL;

> Does anybody know which version is actually better/faster/more optimal?

In recent releases the SQL version would be better, since it would
actually get "inlined" into the calling query and thus the function
overhead would be zero. However this only happens for a fairly narrow
set of cases (function returning scalar, not set, and there are
constraints as to strictness and volatility properties). A non-inlined
SQL function is probably slower than plpgsql, because the SQL-function
executor code isn't amazingly efficient (doesn't cache query plans from
one use to the next, for instance).

So the short answer is "it depends".

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-07-01 13:59:52 Re: question abut "order by" null fields
Previous Message Ajay Dalvi 2005-07-01 12:39:37 How to uninstall PostGreSql from linux