Re: data integrity

From: Jason Earl <jdearl(at)yahoo(dot)com>
To: Janning Vygen <vygen(at)gmx(dot)de>, "Martn" "Marqus" <martin(at)bugs(dot)unl(dot)edu(dot)ar>, Jelle Ouwerkerk <jelle(at)openface(dot)ca>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: data integrity
Date: 2001-11-06 18:55:30
Message-ID: 20011106185530.66930.qmail@web10004.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Keep reading, I have commented below.

--- Janning Vygen <vygen(at)gmx(dot)de> wrote:
> Am Dienstag, 6. November 2001 00:17 schrieb Martn
> Marqus:
> > On Lun 05 Nov 2001 18:06, you wrote:
> > > Hi
> > >
> > > I'm using postgresql to manage a web site.
> > >
> > > Problem: How are people dealing with data
> integrity issues such
> > > as stale data when writing web-based apps? For
> instance:
> > >
> > > The value of X is 7.
> > >
> > > Step (1) User A selects X.
> > > Step (2) User B selects X.
> > > Step (3) User A updates X to 8.
> > > Step (4) User B updates X to 10 under the
> assumption that X is
> > > still 7.
> >
> > Lock it with a SELECT ...... FOR UPDATE. The rows
> that are going to
> > be updated will be locked, but not the entire
> table. :-)
>
> I dont think that FOR UPDATE helps in this
> situation, but it might be
> a misunderstanding.
>
> FOR UPDATE locks the rows of the table for an
> update. right. but it
> removes the lock if the transaction commits.
>
> In a web based application you cant enforce a
> transaction between the
> sending of a form and the exceuting of a submit.

And even if you could, you probably wouldn't want to.
If you did, it would allow folks to lock up rows in
your database indefinitely simply by requestin a form
and closing their web browser. Not only that, but it
would require that each database user get their own
PostgreSQL connection, and your middle ware layer
would have to map requests to the correct connection.

In the end it is *much* easier to simply make each
request a transaction. If an error is raised, then
everything is rolled back, if not then everything is
committed and the page is served up.

If you *really* need something like this it is better
not to use PostgreSQL's transaction mechanism and
instead create an extra table that holds the primary
key of the records you want locked and a timestamp
(and probably the user id or session id of the person
who has created the lock). That way you can easily
check this table for which records are being updated
and you can also easily time out locks.

This is especially useful in those cases where
updating a record takes a lot of time. If all you are
doing is updating a simple integer then a locking
table is not likely to help. Each individual will
hold the lock for such a short period of time that the
chances of collision are low.

> Its an application problem: the data i see in my
> frontend are never
> realtime. they might have change in the last
> seconds. But who cares.
> If i want to have a value of 8 and another one wants
> a value of 10
> its a question of whom granting write access.

This sort of a setup is certainly easiest. However,
sometimes it is useful to alert the user to the fact
that someone else is also working on this particular
row. Creating a separate table to track which rows
are being accessed is fairly straightforward and not
particular expensive. Trying to use PostgreSQL's
locking mechanisms (on the other hand) is a short road
to disaster.

Janning is certainly correct, however, when he says
that ultimately it comes down to who has write access.
Even using a locking table there is nothing to stop
someone changing that row right after I finish
changing it. They won't be able to change it *while*
I am working on it, but they'll be able to change it
the second I hit "submit."

Jason

__________________________________________________
Do You Yahoo!?
Find a job, post your resume.
http://careers.yahoo.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message wsheldah 2001-11-06 18:59:08 Re: Howto change column length
Previous Message Tom Lane 2001-11-06 18:54:09 Re: Use of Serial Datatype and Sequence Issue