Return-Path: postman
Received: by postgres.Berkeley.EDU (5.61/1.29)
	id AA00735; Mon, 1 Nov 93 11:27:24 -0800
Resent-From: postman (POSTGRES mailing list)
Resent-Message-Id: <9311011927.AA00735@postgres.Berkeley.EDU>
Sender: owner-postman@postgres.Berkeley.EDU
X-Return-Path: nobody@lth.se
Received: from nic.lth.se by postgres.Berkeley.EDU (5.61/1.29)
	id AA00725; Mon, 1 Nov 93 11:27:15 -0800
Received: by mail.lth.se (Smail3.1.28.1 #2)
	id m0ou4uM-000MVeC; Mon, 1 Nov 93 20:27 MET
X-Sender: nobody@lth.se
To: postgres@postgres.Berkeley.EDU
Path: postgres.Berkeley.EDU!postman
From: witr@rwwa.COM
Newsgroups: lth.lists.postgres
Subject: Concurrency and locking
Date: 1 Nov 1993 20:27:15 +0100
Organization: Lund Institute of Technology, Sweden
Lines: 46
X-Sender: nobody@news.lth.se
Message-Id: <9310141511.AA02499@relay2.UU.NET>
Resent-To: postgres-dist
Resent-Date: Mon, 01 Nov 93 11:27:22 PST

The following short pgperl program demonstrates `shortcomings' with
perl's concurrency and locking.  When I run two or more copies of it
I get *both* stale data updates and deadlocks.

This has been discussed before, but I don't understand what can or
should be done to remedy this so that this sort of thing works as one
would expect, that is it should demonstrate *neither* deadlocks or
stale data problems.

#!/usr/local/bin/pgperl

# Test postgres concurrency.
# 1) Createdb concur
# 2) Monitor concur
#    destroy TEST \g
#    create TEST (tkey = text, tvalue = int4) \g
#    append TEST (tkey = "foo", tvalue = 1) \g
# 3) Run two or more copies of this program.

$dbname = "concur";
&PQsetdb ($dbname);

while (1) {
  &PQexec("begin");
  $query = "retrieve portal p (t.tvalue) from t in TEST where t.tkey =
\"foo\" ";
  &PQexec($query);
  &PQexec("fetch all in p");

  $p = &PQparray("p");
  $n = &PQntuples($p);

  $value = &PQgetvalue($p, $n-1, 0);
  $value += 1;

  print("Replacing tvalue with $value.\n");
  $query = "replace TEST(tvalue=$value) where TEST.tkey = \"foo\" ";
  &PQexec($query);

  &PQexec ("close p");
  &PQexec ("end");
  &PQclear("p");
}
---
 Robert Withrow, Tel: +1 617 598 4480, Fax: +1 617 598 4430, Net: witr@rwwa.COM
 R.W. Withrow Associates, 21 Railroad Ave, Swampscott MA 01907-1821 USA
