problem with create function and drop type

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

problem with create function and drop type

chenyanfei
This post was updated on .
【Version】
I use the source code compile the database.

postgres=# select version();
                                    version
--------------------------------------------------------------------------------
 PostgreSQL 14devel on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.0, 64-bit
(1 row)

【Problem】
When we test the concurrent commands, we encounter a problem about the the function.  The test case like this:

drop function if existstest_function;
drop type if exists test_type;
create type test_type as (avarchar);
create or replace function test_function(t1 test_type[])
returns integer as $$
begin
end;
$$ LANGUAGE plpgsql;

We get the error when we query thefunction definition

postgres=# drop function if exists test_function;
DROP FUNCTION
postgres=# drop type if exists test_type;
NOTICE:  type "test_type" does not exist, skipping
DROP TYPE
postgres=# create type test_type as (avarchar);
CREATE TYPE
postgres=# create or replace function test_function(t1 test_type[])
postgres-# returns integer as $$
postgres$# begin
postgres$# end;
postgres$# $$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# \df
2020-11-1300:18:49.567 CST [11000] ERROR:  cache lookup failed for type 16389
2020-11-13 00:18:49.567 CST [11000] STATEMENT:  SELECT n.nspname as
"Schema",          p.proname as "Name",        
pg_catalog.pg_get_function_result(p.oid) as "Result data type",        
pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types",        
CASE p.prokind          WHEN 'a' THEN 'agg'          WHEN 'w' THEN 'window'        
WHEN 'p' THEN 'proc'          ELSE 'func'         END as "Type"        FROM
pg_catalog.pg_proc p             LEFT JOIN pg_catalog.pg_namespace n ON
n.oid = p.pronamespace        WHERE pg_catalog.pg_function_is_visible(p.oid)            
AND n.nspname <> 'pg_catalog'              AND n.nspname <>
'information_schema'        ORDER BY 1, 2, 4;
ERROR:  cache lookup failed for type 16389

【Analyze】
To reprocedure the problem, we anlayze our scripts like below. Accounting for the concurrent commands, we use the gdb tool to simulate two sessions.So we set a break point at  pl_handler.c:514

                /* Test-compile the function */
                plpgsql_compile(fake_fcinfo, true);

                /*
                 * Disconnect from SPI manager
                 */
                if ((rc = SPI_finish()) != SPI_OK_FINISH)
                        elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));

When the create command execute success afeter plpgsql_compile function, in other session we execute "drop type if exists test_type;"  The drop command is successfully execute.
postgres=# drop function if exists test_function;
NOTICE:  function test_function() does not exist, skipping
DROP FUNCTION
postgres=# drop type if exists test_type;
DROP TYPE

I think the problem is that the drop type command can not query any depend informations through the pg_depend because the create command doesnot commit now, so it can drop the type. But the create command has done the last check work through plpgsql_compile function and will commit it now. However, because of the depend type has been dropped, so the function can not work now. But it also shows create successfully.