Thread: BUG #16467: XX000 fun

BUG #16467: XX000 fun

From
PG Bug reporting form
Date:
The following bug has been logged on the website:

Bug reference:      16467
Logged by:          Andrey Marinchuk
Email address:      radist.nt@gmail.com
PostgreSQL version: 9.6.17
Operating system:   Red Hat 4.8.5-39
Description:

Reproduced in:
"PostgreSQL 9.6.17 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5
20150623 (Red Hat 4.8.5-39), 64-bit"
"PostgreSQL 10.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5
20150623 (Red Hat 4.8.5-28), 64-bit"
Not reproduced in 12.2.

Steps to reproduce:
1. open two connections (#1 and #2)
2. in connection #1 run script 0
3. in connection #1 run script 1
4. in connection #2 start transaction and run query: select
textXX000.testXX000(public.textXX000_internal('iteration #'), 1);
5. in connection #1 run script 1 again
6. in connection #2 start savepoint and run query: select
textXX000.testXX000(public.textXX000_internal('iteration #'), 2);
7. in connection #2 rollback to savepoint and run query: select
textXX000.testXX000(public.textXX000_internal('iteration #'), 3);

Expected: query in 6 executed without error
Actual (9.6.17):
crm_com=# begin;
BEGIN
crm_com=# \set VERBOSITY verbose
crm_com=# select textXX000.testXX000(public.textXX000_internal('iteration
#'), 1);
  testxx000
--------------
 iteration #1
(1 line)


crm_com=# savepoint sp;
SAVEPOINT
crm_com=# select textXX000.testXX000(public.textXX000_internal('iteration
#'), 2);
ERROR:  XX000: cache lookup failed for function 182081751
Location:  simplify_function, clauses.c:3894
crm_com=# rollback to sp;
ROLLBACK
crm_com=# select textXX000.testXX000(public.textXX000_internal('iteration
#'), 3);
  testxx000
--------------
 iteration #3
(1 line)

script 0:
create or replace function public.textXX000_internal(t in text, flag in
boolean default true) returns text as $$
select case when flag then t else (select nspname::text from
pg_catalog.pg_namespace where nspname = 'public') end
$$ language sql stable called on null input;
script 1:
BEGIN;
DROP SCHEMA IF EXISTS textXX000 CASCADE;
CREATE SCHEMA textXX000;
GRANT USAGE ON SCHEMA textXX000 TO public;
ALTER DEFAULT PRIVILEGES IN SCHEMA textXX000 GRANT EXECUTE ON FUNCTIONS TO
public;
SET SEARCH_PATH TO testXX000;
create or replace function textXX000.testXX000(t in text, i integer) returns
text as $$
begin
  return t || i;
end;
$$ language plpgsql stable returns null on null input;
END;


Additional info: also tested with PostgreSQL 9.6.3 via JDBC, see
https://github.com/pgjdbc/pgjdbc/issues/1786


Re: BUG #16467: XX000 fun

From
Kyotaro Horiguchi
Date:
At Wed, 27 May 2020 22:09:09 +0000, PG Bug reporting form <noreply@postgresql.org> wrote in 
> Steps to reproduce:
> 1. open two connections (#1 and #2)
> 2. in connection #1 run script 0
> 3. in connection #1 run script 1
> 4. in connection #2 start transaction and run query: select
> textXX000.testXX000(public.textXX000_internal('iteration #'), 1);
> 5. in connection #1 run script 1 again
> 6. in connection #2 start savepoint and run query: select
> textXX000.testXX000(public.textXX000_internal('iteration #'), 2);
> 7. in connection #2 rollback to savepoint and run query: select
> textXX000.testXX000(public.textXX000_internal('iteration #'), 3);

This boils down to the following.

S1. CREATE FUNCTION i() RETURNS int AS $$ SELECT count(*) FROM pg_class; $$ LANGUAGE SQL;
S1. CREATE FUNCTION o(int) RETURNS real AS $$ BEGIN RETURN $1::real; END; $$ LANGUAGE PLPGSQL;

S2. BEGIN;
S2. SELECT o(0);
S1. DROP FUNCTION o(int);
S1. CREATE FUNCTION o(int) ..;
S2. SELECT o(i());
ERROR:  cache lookup failed for function <oid of previous o()>

This happens in preprocess_targetlist becase of a stale OID for o() in
FuncExpr.

While processing arguments in simplify_function under
preprocess_expression, cache invalidation happenes while planning i().
So the funcid of the processing FuncExpr gets stale.  The stale procid
fires the error in preprocess_targetlist.

I'm not sure what to do for this, though..

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center