PostgreSQL transaction locking problem

From: "Jeff Martin" <jeff(at)dgjc(dot)org>
To: <pgsql-general(at)postgresql(dot)org>
Subject: PostgreSQL transaction locking problem
Date: 2002-02-02 21:42:58
Message-ID: NEBBLNMDMLIJEILLDFNBKEAPCFAA.jeff@dgjc.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I cannot get locking to operate as documented and as I understand it to
work. I have created a test block of code below that should allow multiple
processes to execute the "TestInsert()" concurrently. However, I get an
error "cannot insert duplicate key". My source code follows....

/* create the test table */
DROP TABLE Test;
CREATE TABLE Test ( CONSTRAINT Test_Id PRIMARY KEY (Id), Id int8 NOT NULL );

/* insert test record with unique Id value */
DROP FUNCTION TestInsert();
CREATE FUNCTION TestInsert() RETURNS int8
AS '
DECLARE
newid int8;
BEGIN
LOCK TABLE Test IN EXCLUSIVE MODE;
SELECT INTO newid Id FROM Test ORDER BY Id DESC FOR UPDATE OF Test LIMIT 1;
IF NOT FOUND THEN newid=1; ELSE newid=newid+1; END IF;
INSERT INTO Test (Id) VALUES (newid);
RETURN 1;
END; '
LANGUAGE 'plpgsql';

/* call TestInsert() */
/* This function should be able to operate concurrently BUT CANNOT */
BEGIN; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SELECT TestInsert();
END;

Thanks for any help,

Jeff
Jeff Martin
704 Torrey Lane, Apartment D
Boalsburg, PA 16827
H814-466-7791
jeff(at)dgjc(dot)org
www.dgjc.org

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Martin 2002-02-02 21:43:03 ERROR: OUTER JOIN is not yet supported
Previous Message Jeff Martin 2002-02-02 21:42:49 How to best scroll through a list of database records?