From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: dejan.petrovic@islonline.com
Subject: BUG #15060: Row in table not found when using pg function in an expression
Date: Mon, 12 Feb 2018 12:59:04 +0000
Message-ID: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org> (raw)
The following bug has been logged on the website:
Bug reference: 15060
Logged by: Dejan Petrovic
Email address: dejan.petrovic@islonline.com
PostgreSQL version: 10.2
Operating system: CentOS 6
Description:
Hello,
I tested this in postgresql versions 9.1, 10.1 and 10.2 on centOS.
In short this is what happens (in a plpgsql function):
1.) An insert is done into 'bug' table
2.) A SELECT is done to make sure the INSERT was successful
3.) Another function (get_bug_id) is called which returns id based on
value.
When the function is called directly, it returns the id correctly. When it's
called in an expression, it does not find the inserted row and an exception
is raised.
I have prepared a minimal example:
CREATE TABLE public.bug
(
id integer NOT NULL,
text text,
CONSTRAINT bug_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.bug
OWNER TO postgres;
CREATE OR REPLACE FUNCTION public.get_bug_id(in_text text)
RETURNS integer AS
$BODY$
DECLARE
my_int int;
BEGIN
SELECT INTO my_int id from bug WHERE text = in_text;
IF NOT FOUND THEN
RAISE EXCEPTION 'row not found - BUG?';
END IF;
RETURN my_int;
END;
$BODY$
LANGUAGE plpgsql STABLE
COST 100;
ALTER FUNCTION public.get_bug_id(text)
OWNER TO postgres;
CREATE OR REPLACE FUNCTION test_bug()
RETURNS text AS
$BODY$
DECLARE
my_int int;
my_text text;
BEGIN
my_text := 'this is a bug';
INSERT INTO bug (id,text) VALUES (1,my_text);
SELECT INTO my_int id from bug WHERE text = my_text;
IF NOT FOUND THEN
RAISE EXCEPTION 'row does not exist';
END IF;
perform get_bug_id(my_text); -- This is OK - get_bug_id returns '1'
perform id FROM bug WHERE id = get_bug_id(my_text); -- This fails -
get_bug_id raises exception in version 10, works OK in version 9.1
RETURN 'OK';
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION test_bug()
OWNER TO postgres;
select test_bug()
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: noreply@postgresql.org, dejan.petrovic@islonline.com
Subject: Re: BUG #15060: Row in table not found when using pg function in an expression
In-Reply-To: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org>
References: <151844034484.1446.1721073839109092583@wrigleys.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox