okbob / plpgsql_check

plpgsql_check is a linter tool (does source code static analyze) for the PostgreSQL language plpgsql (the native language for PostgreSQL store procedures).
https://groups.google.com/forum/#!forum/postgresql-extensions-hacking
Other
625 stars 52 forks source link

Server crashes when trying to execute select stmtid, exec_stmts, stmtname from plpgsql_profiler_function_statements_tb('covtest'); #177

Closed nkPit closed 2 weeks ago

nkPit commented 2 weeks ago

Hello!

After building postgres in the following way:

./configure --enable-tap-tests --enable-debug --enable-cassert && make -j8

And executing the query:

select * from plpgsql_check_function_tb('fx_testr');

create or replace function covtest(int)
returns int as $$
declare a int = $1;
begin
  a := a + 1;
  if a < 10 then
    a := a + 1;
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function;
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function();
drop function test_function;
drop function test_function();
  end if;
  a := a + 1;
  return a;
end;
$$ language plpgsql;

set plpgsql_check.profiler to on;

select covtest(10);

select stmtid, exec_stmts, stmtname from plpgsql_profiler_function_statements_tb('covtest');

the server crashes.

Coredump:

#0  0x00007fca8af709fc in pthread_kill () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007fca8af1c476 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00007fca8af027f3 in abort () from /lib/x86_64-linux-gnu/libc.so.6
#3  0x00005558911d1bd0 in ExceptionalCondition (conditionName=<optimized out>,
    fileName=<optimized out>, lineNumber=<optimized out>) at assert.c:66
#4  0x00007fca7fd6cad7 in profiler_stmt_walker (pinfo=0x7ffc93441230,
    mode=PLPGSQL_CHECK_STMT_WALKER_PREPARE_RESULT, stmt=0x555892dc2c98, parent_stmt=0x555892dc2cc8,
    description=0x7fca7fd833d8 "body", stmt_block_num=4, opts=0x7ffc93441300) at src/profiler.c:608
#5  0x00007fca7fd6ebdc in stmts_walker (pinfo=0x7ffc93441230,
    mode=PLPGSQL_CHECK_STMT_WALKER_PREPARE_RESULT, stmts=0x555892d80c78,
    parent_stmt=0x555892dc2cc8, description=0x7fca7fd833d8 "body", opts=0x7ffc93441300)
    at src/profiler.c:1440
#6  0x00007fca7fd6d363 in profiler_stmt_walker (pinfo=0x7ffc93441230,
    mode=PLPGSQL_CHECK_STMT_WALKER_PREPARE_RESULT, stmt=0x555892dc2cc8, parent_stmt=0x0,
    description=0x0, stmt_block_num=1, opts=0x7ffc93441300) at src/profiler.c:778
#7  0x00007fca7fd6f8a4 in plpgsql_check_iterate_over_profile (cinfo=0x7ffc93441540,
    mode=PLPGSQL_CHECK_STMT_WALKER_PREPARE_RESULT, ri=0x7ffc93441510, cs=0x0) at src/profiler.c:1903
#8  0x00007fca7fd7912d in profiler_function_statements_tb_internal (fnoid=16410,
    fcinfo=0x555892dc6638) at src/tablefunc.c:514
#9  0x00007fca7fd798d7 in plpgsql_profiler_function_statements_tb_name (fcinfo=0x555892dc6638)
    at src/tablefunc.c:665
#10 0x0000555890edbdbc in ExecMakeTableFunctionResult (setexpr=0x555892dcebe0,
    econtext=0x555892dceab0, argContext=<optimized out>, expectedDesc=0x555892dcc588,
    randomAccess=false) at execSRF.c:235
#11 0x0000555890eee2f1 in FunctionNext (node=0x555892dce8a0) at nodeFunctionscan.c:95
#12 0x0000555890edc6ec in ExecScan (node=0x555892dce8a0, accessMtd=0x555890eee000 <FunctionNext>,
    recheckMtd=0x555890eedff0 <FunctionRecheck>) at execScan.c:198
#13 0x0000555890ed288a in ExecProcNode (node=0x555892dce8a0) at ../../../src/include/executor/executor.h:273
#14 ExecutePlan (execute_once=<optimized out>, dest=0x555892debb68, direction=<optimized out>, numberTuples=0, sendTuples=<optimized out>, operation=CMD_SELECT, use_parallel_mode=<optimized out>, planstate=0x555892dce8a0, estate=0x555892dce678) at execMain.c:1670
#15 standard_ExecutorRun (queryDesc=0x555892cdba48, direction=<optimized out>, count=0, execute_once=<optimized out>) at execMain.c:365
#16 0x000055589109834f in PortalRunSelect (portal=0x555892d2aa18, forward=<optimized out>, count=0, dest=<optimized out>) at pquery.c:924
#17 0x0000555891099a31 in PortalRun (portal=portal@entry=0x555892d2aa18, count=count@entry=9223372036854775807, isTopLevel=isTopLevel@entry=true, run_once=run_once@entry=true, dest=dest@entry=0x555892debb68, altdest=altdest@entry=0x555892debb68, qc=0x7ffc93441a00) at pquery.c:768
#18 0x0000555891095904 in exec_simple_query (query_string=0x555892cb1638 "select stmtid, exec_stmts, stmtname from plpgsql_profiler_function_statements_tb('covtest');") at postgres.c:1278
#19 0x00005558910974a9 in PostgresMain (dbname=<optimized out>, username=<optimized out>) at postgres.c:4701
#20 0x0000555890ffe72f in BackendRun (port=0x555892ce03b0, port=0x555892ce03b0) at postmaster.c:4464
#21 BackendStartup (port=0x555892ce03b0) at postmaster.c:4192
#22 ServerLoop () at postmaster.c:1782
#23 0x0000555890fff753 in PostmasterMain (argc=argc@entry=3, argv=argv@entry=0x555892cabb70) at postmaster.c:1466
#24 0x0000555890d08651 in main (argc=3, argv=0x555892cabb70) at main.c:198

Postgres version:

postgres=# select version();
-------------------------------------------------------------------------------------------------------
 PostgreSQL 16.4 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit
(1 row)

plpgsql_check version:

postgres=# \dx plpgsql_check
                      List of installed extensions
     Name      | Version | Schema |             Description
---------------+---------+--------+--------------------------------------
 plpgsql_check | 2.7     | public | extended check for plpgsql functions
(1 row)

commit: 716bb058c0511fc320e44e1057e2e23d2547c042

Best regards, Nikita Kalinin Postgres Professional

okbob commented 2 weeks ago

should be fixed by be7e77e2f69c2466405977440d0867353cad43de

nkPit commented 2 weeks ago

Yes. That fixed the problem. Thank you!

okbob commented 2 weeks ago

thank you for bug report