Re: FOR UPDATE

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: said assemlal <said(dot)assemlal(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: FOR UPDATE
Date: 2016-11-28 21:37:14
Message-ID: 20161128213714.d4nga45qpvouqkel@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

said assemlal wrote:
> Hello,
>
> PG: 9.4
> CentOS 6
>
> I am writing functions to lock results.
>
> Let's take an example:
>
> CREATE OR REPLACE FUNCTION usp_locking_my_result($1 VARCHAR(50), $2
> VARCHAR(50))
> RETURNS TEXT AS $$
> SELECT value
> FROM my_table
> WHERE field1 = $1 AND field2 = $2 FOR UPDATE;
> $$ LANGUAGE SQL;

Why do you want to lock these results?

> What happens if one of those arguments are empty and database finds
> results? ( I think they will locked )

What do you mean "empty"? If you pass the empty string, the query will
return rows that have the empty string in those columns. Note that the
NULL value is not the same as the empty string. If the function is
defined as STRICT (also spelled RETURNS NULL ON NULL INPUT), then the
function is not even called if you pass NULL arguments, and it simply
returns NULL without locking anything.

> Should I check the inputs and raise an exception if there are empty ?

Not necessarily ... depends on what you want to happen.

--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

In response to

  • FOR UPDATE at 2016-11-28 21:03:53 from said assemlal

Responses

Browse pgsql-general by date

  From Date Subject
Next Message said assemlal 2016-11-28 22:10:21 Re: FOR UPDATE
Previous Message Torsten Förtsch 2016-11-28 21:14:55 hot_standby_feedback