BUG #15060: Row in table not found when using pg function in an expression

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: dejan(dot)petrovic(at)islonline(dot)com
Subject: BUG #15060: Row in table not found when using pg function in an expression
Date: 2018-02-12 12:59:04
Message-ID: 151844034484.1446.1721073839109092583@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 15060
Logged by: Dejan Petrovic
Email address: dejan(dot)petrovic(at)islonline(dot)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()

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Mark Scheffer 2018-02-12 13:37:47 Re: BUG #15060: Row in table not found when using pg function in an expression
Previous Message Andrew Gierth 2018-02-12 11:03:04 Re: BUG #15057: Issue with UNKNOW type when calling a PostgreSQL function from java code