Re: FOR UPDATE - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: FOR UPDATE
Date
Msg-id 20161128213714.d4nga45qpvouqkel@alvherre.pgsql
Whole thread Raw
In response to FOR UPDATE  (said assemlal <said.assemlal@gmail.com>)
Responses Re: FOR UPDATE
List 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


pgsql-general by date:

Previous
From: Torsten Förtsch
Date:
Subject: hot_standby_feedback
Next
From: said assemlal
Date:
Subject: Re: FOR UPDATE