BUG #18656: "STABLE" function sometimes does not see changes

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: panso8(at)gmail(dot)com
Subject: BUG #18656: "STABLE" function sometimes does not see changes
Date: 2024-10-15 09:58:35
Message-ID: 18656-cade1780866ef66c@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: 18656
Logged by: Alexander Alehin
Email address: panso8(at)gmail(dot)com
PostgreSQL version: 16.4
Operating system: Debian 11
Description:

CREATE TABLE stbl_states(
id int primary key,
state text
);

CREATE TABLE stbl_logs(
ts timestamp default clock_timestamp(),
log text
);

CREATE or replace FUNCTION stbl_get_state(p_id int) RETURNS text
LANGUAGE sql STABLE AS
$$
select coalesce(k.state, 'off') from stbl_states k where k.id = p_id;
$$;

CREATE or replace PROCEDURE stbl_log(p_message text)
LANGUAGE plpgsql AS
$$
begin
insert into stbl_logs(log) values (p_message);
end;
$$;

CREATE or replace PROCEDURE stbl_update_state()
LANGUAGE plpgsql AS
$$
declare
s text;
begin
update stbl_states set state='on' where id=1;
s := stbl_get_state(1);
call stbl_log(s);
call stbl_log(stbl_get_state(1));
insert into stbl_logs(log) values (stbl_get_state(1));
exception when others then
s := SQLERRM;
call stbl_log(s);
end;
$$;

insert into stbl_states(id) values(1);
call stbl_update_state();
select log from stbl_logs order by ts;

---------------------------------------------------------------

log
-----
on
off
on
(3 rows)

But expected:
on
on
on

If "STABLE" or "exception ..." is removed, it will work as expected.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2024-10-15 11:00:01 BUG #18657: Using JSON_OBJECTAGG with volatile function leads to segfault
Previous Message Alvaro Herrera 2024-10-15 09:30:39 Re: BUG #18647: INSERT statements execute functions twice.