Re: Basic locking question

From: "Joshua D(dot) Drake" <jd(at)commandprompt(dot)com>
To: Ben <bench(at)silentmedia(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Basic locking question
Date: 2005-09-06 22:13:58
Message-ID: 431E14A6.50304@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ben wrote:
> I'm not very familiar with explicitly locking things in SQL, so this may
> be a dumb question, but.....
>
> I've got a stored proc that takes a text key and returns an ID. If the
> key isn't in a lookup table, it adds it and will return the
> auto-generated serial number. If it's already there, it simply returns
> the existing serial number.
>
> I'm concerned about multiple sessions calling this at the same time with
> the same key. While I *could* just put in a unique constraint and let
> things fail, that's hardly graceful. It seems I should be able to lock
> the lookup table, but I don't really know what mode to lock that table
> in. Will ROW EXCLUSIVE keep the same key from being entered twice?

You are probably looking for select for update:

http://www.postgresql.org/docs/current/static/sql-select.html#SQL-FOR-UPDATE

>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend

--
Your PostgreSQL solutions company - Command Prompt, Inc. 1.800.492.2240
PostgreSQL Replication, Consulting, Custom Programming, 24x7 support
Managed Services, Shared and Dedicated Hosting
Co-Authors: plPHP, plPerlNG - http://www.commandprompt.com/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jim C. Nasby 2005-09-06 22:19:58 Re: How to write jobs in postgresql
Previous Message Tom Lane 2005-09-06 22:13:25 Re: Help with row locks on 7.4 to 8.0 migration