neondatabase / neon

Neon: Serverless Postgres. We separated storage and compute to offer autoscaling, code-like database branching, and scale to zero.
https://neon.tech
Apache License 2.0
14.78k stars 430 forks source link

Creating all extensions in a loop crashes Postgres #5428

Closed bayandin closed 1 year ago

bayandin commented 1 year ago

Steps to reproduce

Create a new project, and run the following query:

DO $$
DECLARE
    r RECORD;
BEGIN
    FOR r IN (SELECT * FROM pg_available_extensions WHERE name NOT IN ('adminpack', 'amcheck', 'dblink', 'dict_xsyn', 'embedding', 'file_fdw', 'hnsw', 'kq_imcx', 'neon', 'neon_rmgr', 'old_snapshot', 'pageinspect', 'pg_buffercache', 'pg_cron'))
    LOOP
        BEGIN
            RAISE NOTICE 'Attempting to install extension: %', r.name;
            EXECUTE 'CREATE EXTENSION IF NOT EXISTS "' || r.name || '" CASCADE;';
        EXCEPTION
            WHEN OTHERS THEN
                RAISE WARNING 'Failed to install extension: %, error: %', r.name, SQLERRM;
        END;
    END LOOP;
END $$;

Expected result

Postgres doesn't segfault

Actual result

Postgres segfaults

Environment

Logs, links

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f15d508f1ce in pldbgapi2_stmt_beg (estate=0x7ffc7b991010, stmt=0x56023b5734e8) at src/pldbgapi2.c:1060
[Current thread is 1 (Thread 0x7f16751532c0 (LWP 63))]
#0  0x00007f15d508f1ce in pldbgapi2_stmt_beg (estate=0x7ffc7b991010, stmt=0x56023b5734e8) at src/pldbgapi2.c:1060
#1  0x00007f15edb428eb in exec_stmts (estate=0x7ffc7b991010, stmts=0x56023b573538) at pl_exec.c:2008
#2  0x00007f15edb46a0a in exec_for_query (estate=estate@entry=0x7ffc7b991010, stmt=stmt@entry=0x56023b5707c8, portal=0x56023b4e16f8, prefetch_ok=<optimized out>, prefetch_ok@entry=true) at pl_exec.c:5962
#3  0x00007f15edb43849 in exec_stmt_fors (stmt=0x56023b5707c8, estate=0x7ffc7b991010) at pl_exec.c:2832
#4  exec_stmts (estate=0x7ffc7b991010, stmts=0x56023b573588) at pl_exec.c:2055
#5  0x00007f15edb45433 in exec_stmt_block (estate=0x7ffc7b991010, block=0x56023b5735d8) at pl_exec.c:1942
#6  0x00007f15edb4550d in exec_toplevel_block (estate=estate@entry=0x7ffc7b991010, block=0x56023b5735d8) at pl_exec.c:1633
#7  0x00007f15edb45d52 in plpgsql_exec_function (func=func@entry=0x56023b56ba80, fcinfo=fcinfo@entry=0x7ffc7b991190, simple_eval_estate=simple_eval_estate@entry=0x56023b579588, simple_eval_resowner=simple_eval_resowner@entry=0x56023b48c058, procedure_resowner=procedure_resowner@entry=0x56023b48c058, atomic=<optimized out>) at pl_exec.c:622
#8  0x00007f15edb4fb66 in plpgsql_inline_handler (fcinfo=<optimized out>) at pl_handler.c:368
#9  0x0000560239a46f00 in FunctionCall1Coll (flinfo=0x7ffc7b991320, collation=<optimized out>, arg1=<optimized out>) at fmgr.c:1124
#10 0x0000560239a4749f in OidFunctionCall1Coll (functionId=<optimized out>, collation=collation@entry=0, arg1=94567584349032) at fmgr.c:1402
#11 0x0000560239778835 in ExecuteDoStmt (pstate=pstate@entry=0x56023b45ba58, stmt=stmt@entry=0x56023b40f1f8, atomic=atomic@entry=false) at functioncmds.c:2148
#12 0x000056023992a0d3 in standard_ProcessUtility (pstmt=0x56023b40f728, queryString=0x56023b40df68 "DO $$ \nDECLARE \n    r RECORD;\nBEGIN \n    FOR r IN (SELECT * FROM pg_available_extensions WHERE name NOT IN ('adminpack', 'amcheck', 'dblink', 'dict_xsyn', 'embedding', 'file_fdw', 'hnsw', 'kq_imcx', '"..., readOnlyTree=<optimized out>, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x56023b40f7b8, qc=0x7ffc7b9919e0) at utility.c:714
#13 0x00007f167431e3b7 in pgss_ProcessUtility (pstmt=0x56023b40f728, queryString=0x56023b40df68 "DO $$ \nDECLARE \n    r RECORD;\nBEGIN \n    FOR r IN (SELECT * FROM pg_available_extensions WHERE name NOT IN ('adminpack', 'amcheck', 'dblink', 'dict_xsyn', 'embedding', 'file_fdw', 'hnsw', 'kq_imcx', '"..., readOnlyTree=<optimized out>, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x56023b40f7b8, qc=0x7ffc7b9919e0) at pg_stat_statements.c:1141
#14 0x00007f1674310dc4 in loader_process_utility_hook (pstmt=0x56023b40f728, query_string=0x56023b40df68 "DO $$ \nDECLARE \n    r RECORD;\nBEGIN \n    FOR r IN (SELECT * FROM pg_available_extensions WHERE name NOT IN ('adminpack', 'amcheck', 'dblink', 'dict_xsyn', 'embedding', 'file_fdw', 'hnsw', 'kq_imcx', '"..., readonly_tree=<optimized out>, context=PROCESS_UTILITY_TOPLEVEL, params=<optimized out>, queryEnv=<optimized out>, dest=0x56023b40f7b8, completion_tag=0x7ffc7b9919e0) at /timescaledb-src/src/loader/loader.c:583
#15 0x000056023992881f in PortalRunUtility (portal=portal@entry=0x56023b4e15e8, pstmt=pstmt@entry=0x56023b40f728, isTopLevel=isTopLevel@entry=true, setHoldSnapshot=setHoldSnapshot@entry=false, dest=0x56023b40f7b8, qc=0x7ffc7b9919e0) at pquery.c:1158
#16 0x000056023992894b in PortalRunMulti (portal=portal@entry=0x56023b4e15e8, isTopLevel=isTopLevel@entry=true, setHoldSnapshot=setHoldSnapshot@entry=false, dest=dest@entry=0x56023b40f7b8, altdest=altdest@entry=0x56023b40f7b8, qc=qc@entry=0x7ffc7b9919e0) at pquery.c:1322
#17 0x0000560239928e1d in PortalRun (portal=portal@entry=0x56023b4e15e8, count=count@entry=9223372036854775807, isTopLevel=isTopLevel@entry=true, run_once=run_once@entry=true, dest=dest@entry=0x56023b40f7b8, altdest=altdest@entry=0x56023b40f7b8, qc=0x7ffc7b9919e0) at pquery.c:791
#18 0x0000560239925293 in exec_simple_query (query_string=0x56023b40df68 "DO $$ \nDECLARE \n    r RECORD;\nBEGIN \n    FOR r IN (SELECT * FROM pg_available_extensions WHERE name NOT IN ('adminpack', 'amcheck', 'dblink', 'dict_xsyn', 'embedding', 'file_fdw', 'hnsw', 'kq_imcx', '"...) at postgres.c:1252
#19 0x00005602399273df in PostgresMain (dbname=<optimized out>, username=<optimized out>) at postgres.c:4608
#20 0x00005602398abb12 in BackendRun (port=<optimized out>, port=<optimized out>) at postmaster.c:4516
#21 BackendStartup (port=<optimized out>) at postmaster.c:4244
#22 ServerLoop () at postmaster.c:1811
#23 0x00005602398acb63 in PostmasterMain (argc=argc@entry=3, argv=argv@entry=0x56023b4076e0) at postmaster.c:1483
#24 0x000056023963f27e in main (argc=3, argv=0x56023b4076e0) at main.c:242
bayandin commented 1 year ago

It turns out it's a plpgsql_check issue: https://github.com/okbob/plpgsql_check/issues/155

Ref https://neondb.slack.com/archives/C04DGM6SMTM/p1695991788447819