pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hiroshi Inoue <Inoue@tpf.co.jp>
To: Forest Wilkinson <fspam@home.com>
Cc: pgsql-sql@hub.org
Subject: Re: SQL functions not locking properly?
Date: Tue, 26 Sep 2000 11:42:11 +0900
Message-ID: <39D00D03.757EB407@tpf.co.jp> (raw)
References: <p87vss0kh2sroma0fpts3dsac5tlt1pdh4@4ax.com>

Forest Wilkinson wrote:
> 
> I'm having a problem with functions written in SQL.  Specifically, they
> don't seem to be adhering to Postgres locking rules.  For the record, I'm
> using postgres 7.0.2, installed from RPMs, on Red Hat 6.2.
> 
> Here's what I'm seeing:
> (psql input represented by '<<'; output represented by '>>'.)
> 
> session1<< create table idseq
> session1<<   (
> session1<<   name varchar(32) not null,
> session1<<   id int8 not null default 0
> session1<<   );
> session1>> CREATE
> 
> session1<< insert into idseq values ('myid');
> session1>> INSERT 20700 1
> 
> Each row in the table is supposed to represent a named numeric sequence,
> much like the sequences built into postgres.  (Mine use an int8 though,
> so their values can be much higher.)
> 
> session1<< create function nextid( varchar(32)) returns int8 as '
> session1<<   select * from idseq where name = $1::text for update;
> session1<<   update idseq set id = id + 1 where name = $1::text;
> session1<<   select id from idseq where name = $1::text;

Queries in a function are executed under the 
snapshot of the top level query.
So SELECT could never see changes made by
other backends once the top level query
started. If you change the definition of above
function to return setof int8,you may see 2
rows returned. 
 
Maybe you could add 'for update' clause to your
above query.

> session1<<   ' language 'sql';
> session1>> CREATE
>

Regards.

Hiroshi Inoue



view thread (4+ messages)  latest in thread

Message-ID: <39D00D03.757EB407@tpf.co.jp>
Permalink:  ../39D00D03.757EB407@tpf.co.jp/
Also on:    postgresql.org/message-id/39D00D03.757EB407@tpf.co.jp

 · 

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: Inoue@tpf.co.jp, fspam@home.com, pgsql-sql@hub.org
  Subject: Re: SQL functions not locking properly?
  In-Reply-To: <39D00D03.757EB407@tpf.co.jp>
  References: <p87vss0kh2sroma0fpts3dsac5tlt1pdh4@4ax.com> <39D00D03.757EB407@tpf.co.jp>

* 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