SELECT FOR UPDATE in function

From: Oleg Bartunov <oleg(at)sai(dot)msu(dot)su>
To: pgsql-hackers(at)postgreSQL(dot)org
Cc: tgl(at)sss(dot)pgh(dot)pa(dot)us
Subject: SELECT FOR UPDATE in function
Date: 1999-07-24 20:14:00
Message-ID: Pine.GSO.3.96.SK.990725000710.18633F-100000@ra
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Is't possible to use SELECT FOR UPDATE in functions ?
I have function for 'insert or update' which works ok, but as I have some
problem with duplicated records I tried as suggested by Tom Lane to use
SELECT FOR UPDATE instead of just select. Unfortunately it doesn't works:

ERROR: query didn't return correct # of attributes for *internal*

Here is a function:
CREATE FUNCTION "acc_hits" (int4) RETURNS int4 AS '
Declare
keyval Alias For $1;
cnt int4;
curtime datetime;
Begin
curtime := ''now'';
-- Select count into cnt from hits where msg_id = keyval FOR UPDATE;
Select count into cnt from hits where msg_id = keyval;
if Not Found then
cnt := 1;
-- first_access inserted on default, last_access is NULL
Insert Into hits (msg_id,count) values (keyval, cnt);
else
cnt := cnt + 1;
Update hits set count = cnt,last_access = curtime where msg_id = keyval;
End If;
return cnt;
End;
' LANGUAGE 'plpgsql';

Regards,

Oleg

PS.

Just to test:

create table hits (
msg_id int4 not null primary key,
count int4 not null,
first_access datetime default now(),
last_access datetime
);

select acc_hits(1);

_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg(at)sai(dot)msu(dot)su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 1999-07-24 20:15:19 Re: [HACKERS] RE: [INTERFACES] Re: SSL patch
Previous Message Mike Mascari 1999-07-24 19:02:26 Re: [HACKERS] Index not used on select (Is this more OR + LIKE?)