Hello!

My colleague Victoria Shepard reported that pgbench might crash
during initialization with some values of shared_buffers and
max_worker_processes in conf.

After some research, found this happens when the LimitAdditionalPins() returns 
exactly zero.
In the current master, this will happen e.g. if shared_buffers = 10MB and 
max_worker_processes = 40.
Then the command "pgbench --initialize postgres" will lead to crash.
See the backtrace attached.

There is a fix in the patch applied. Please take a look on it.

With the best regards,

--
Anton A. Melnikov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
#1  0x00007f9169557859 in __GI_abort () at abort.c:79
        save_stage = 1
        act = {__sigaction_handler = {sa_handler = 0x7f91696eabf7, sa_sigaction 
= 0x7f91696eabf7}, sa_mask = {__val = {1, 140262515851294, 3, 140727328224084, 
12, 140262515851298, 2, 3487533463194566656, 7292515507211941683, 
94490218952736, 7291953849184874368, 0, 6147461878018348800, 140727328224176, 
94490235583952, 140727328225056}}, sa_flags = 938440736, sa_restorer = 
0x7ffda268ef80}
        sigs = {__val = {32, 0 <repeats 15 times>}}
#2  0x000055f03865f3a8 in ExceptionalCondition (conditionName=0x55f038846b8c 
"nblocks > 0", fileName=0x55f038846997 "md.c", lineNumber=534) at assert.c:66
No locals.
#3  0x000055f038479e41 in mdzeroextend (reln=0x55f038ed6e38, 
forknum=MAIN_FORKNUM, blocknum=0, nblocks=0, skipFsync=false) at md.c:534
        v = 0x55f038d96300
        curblocknum = 0
        remblocks = 0
        __func__ = "mdzeroextend"
#4  0x000055f03847c747 in smgrzeroextend (reln=0x55f038ed6e38, 
forknum=MAIN_FORKNUM, blocknum=0, nblocks=0, skipFsync=false) at smgr.c:525
No locals.
#5  0x000055f03842fc72 in ExtendBufferedRelShared (eb=..., fork=MAIN_FORKNUM, 
strategy=0x55f038e1d7a8, flags=8, extend_by=0, extend_upto=4294967295, 
buffers=0x7ffda268ba30, extended_by=0x7ffda268b8fc) at bufmgr.c:2057
        first_block = 0
        io_context = IOCONTEXT_BULKWRITE
        io_start = {ticks = 0}
        __func__ = "ExtendBufferedRelShared"
#6  0x000055f03842f512 in ExtendBufferedRelCommon (eb=..., fork=MAIN_FORKNUM, 
strategy=0x55f038e1d7a8, flags=8, extend_by=17, extend_upto=4294967295, 
buffers=0x7ffda268ba30, extended_by=0x7ffda268b9dc) at bufmgr.c:1805
        first_block = 22000
#7  0x000055f03842de78 in ExtendBufferedRelBy (eb=..., fork=MAIN_FORKNUM, 
strategy=0x55f038e1d7a8, flags=8, extend_by=17, buffers=0x7ffda268ba30, 
extended_by=0x7ffda268b9dc) at bufmgr.c:862
No locals.
#8  0x000055f037f773fa in RelationAddBlocks (relation=0x7f91655d97b8, 
bistate=0x55f038e1d778, num_pages=17, use_fsm=false, did_unlock=0x7ffda268bb8d) 
at hio.c:324
        victim_buffers = {1, 0, 953770752, 22000, -1570194736, 0, 955084344, 
22000, 955072168, 22000, 0, 0, -1570194800, 32765, 944220747, 22000, 16384, 0, 
955084344, 22000, 0, 0, 953770752, 22000, -1570194752, 32765, 944228643, 22000, 
-1570194752, 0, 955084344, 22000, 0, 0, 1700632504, 0, -1570194704, 32765, 
939296560, 22000, -1570194624, 0, 1700632504, 32657, 16384, 0, 0, 0, 
-1570194672, 32765, 943901980, 22000, 8000, 0, 1700632504, 32657, -1570194624, 
32765, 943923688, 22000, -1570194512, 0, 1700632504, 32657}
        first_block = 4294967295
        last_block = 4294967295
        extend_by_pages = 17
        not_in_fsm_pages = 17
        buffer = 22000
        page = 0xa268ba20 <error: Cannot access memory at address 0xa268ba20>
        __func__ = "RelationAddBlocks"
#9  0x000055f037f77d01 in RelationGetBufferForTuple (relation=0x7f91655d97b8, 
len=128, otherBuffer=0, options=6, bistate=0x55f038e1d778, 
vmbuffer=0x7ffda268bc2c, vmbuffer_other=0x0, num_pages=17) at hio.c:749
        use_fsm = false
        buffer = 0
        page = 0x6a268bc34 <error: Cannot access memory at address 0x6a268bc34>
        nearlyEmptyFreeSpace = 8016
        pageFreeSpace = 0
        saveFreeSpace = 0
        targetFreeSpace = 128
        targetBlock = 4294967295
        otherBlock = 4294967295
        unlockedTargetBuffer = 127
        recheckVmPins = false
        __func__ = "RelationGetBufferForTuple"
#10 0x000055f037f5e5e2 in heap_multi_insert (relation=0x7f91655d97b8, 
slots=0x55f038e37b08, ntuples=1000, cid=15, options=6, bistate=0x55f038e1d778) 
at heapam.c:2193
        buffer = 32657
        all_visible_cleared = false
        all_frozen_set = false
        nthispage = -1570194336
        xid = 734
        heaptuples = 0x55f038ed1e98
        i = 1000
        ndone = 0
        scratch = {data = 
"мh\242\001\000\000\000\204\352}e\221\177\000\000\340\274h\242\375\177\000\000\v|F8\360U\000\000\020\275h\242\001\000\000\000\204\352}e\221\177\000\000\020\275h\242\375\177\000\000\211\233F8\360U\000\000\020\275h\001\000\000\224\223\200\352}e\221\177\000\000`\267\241\000\000\000\000
 
\000\000\000\000\001\000\000\000\260\275h\242\375\177\000\000\242\352B8\004\000\000\000P\275h\242\375\177\000\000\342\333J8\360U\000\000\000\000\000\000\002\000\000\000\000\000\000\000\004\000\000\000\000\000\000\000p\177\000\000\330\344\336\070\360U\000\000\200\275h\242\375\177\000\000\333\334J8\360U\000\000\003\000\000\000\002\000\000\000\000\000\000\000\004\000\000\000\200\352}e\221\177\000\000\210"...,
 force_align_d = 3.4682125585537897e-314, force_align_i64 = 7019740368}
        page = 0x0
        vmbuffer = 0
        needwal = true
        saveFreeSpace = 0
        need_tuple_data = false
        need_cids = false
        starting_with_empty_page = false
        npages = 17
        npages_used = 0
#11 0x000055f038101376 in table_multi_insert (rel=0x7f91655d97b8, 
slots=0x55f038e37b08, nslots=1000, cid=15, options=6, bistate=0x55f038e1d778) 
at ../../../src/include/access/tableam.h:1455
No locals.
#12 0x000055f038101d6b in CopyMultiInsertBufferFlush (miinfo=0x7ffda268df10, 
buffer=0x55f038e37b08, processed=0x7ffda268de58) at copyfrom.c:416
        ti_options = 6
        oldcontext = 0x55f038d73bb0
        mycid = 15
        line_buf_valid = true
        save_cur_lineno = 1000
        cstate = 0x55f038d72c58
        estate = 0x55f038e25b70
        nused = 1000
        resultRelInfo = 0x55f038e1d2e8
        slots = 0x55f038e37b08
        i = -1570185872
#13 0x000055f0381020fa in CopyMultiInsertInfoFlush (miinfo=0x7ffda268df10, 
curr_rri=0x55f038e1d2e8, processed=0x7ffda268de58) at copyfrom.c:533
        buffer = 0x55f038e37b08
        lc__state = {l = 0x55f038e1dbb8, i = 0}
        lc = 0x55f038e1dbd0
#14 0x000055f0381036b5 in CopyFrom (cstate=0x55f038d72c58) at copyfrom.c:1204
        myslot = 0x55f038ed0f88
        skip_tuple = false
        resultRelInfo = 0x55f038e1d2e8
        target_resultRelInfo = 0x55f038e1d2e8
        prevResultRelInfo = 0x0
        estate = 0x55f038e25b70
        mtstate = 0x55f038e1d568
        econtext = 0x55f038e25d80
        singleslot = 0x0
        oldcontext = 0x55f038d73bb0
        proute = 0x0
        errcallback = {previous = 0x0, callback = 0x55f0381014e1 
<CopyFromErrorCallback>, arg = 0x55f038d72c58}
        mycid = 15
        ti_options = 6
        bistate = 0x0
        insertMethod = CIM_MULTI
        multiInsertInfo = {multiInsertBuffers = 0x55f038e1dbb8, bufferedTuples 
= 1000, bufferedBytes = 7893, cstate = 0x55f038d72c58, estate = 0x55f038e25b70, 
mycid = 15, ti_options = 6}
        processed = 0
        excluded = 0
        has_before_insert_row_trig = false
        has_instead_insert_row_trig = false
        leafpart_use_multi_insert = false
        __func__ = "CopyFrom"
#15 0x000055f0380ff554 in DoCopy (pstate=0x55f038d73cb0, stmt=0x55f038d478d0, 
stmt_location=0, stmt_len=0, processed=0x7ffda268e180) at copy.c:314
        cstate = 0x55f038d72c58
        is_from = true
        pipe = true
        rel = 0x7f91655d97b8
        relid = 16390
        query = 0x0
        whereClause = 0x0
        __func__ = "DoCopy"
#16 0x000055f0384895e9 in standard_ProcessUtility (pstmt=0x55f038d479f0, 
queryString=0x55f038d46d90 "copy pgbench_accounts from stdin with (freeze on)", 
readOnlyTree=false, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, 
dest=0x55f038d47cb0, qc=0x7ffda268e500) at utility.c:742
        processed = 120
        parsetree = 0x55f038d478d0
        isTopLevel = true
        isAtomicContext = true
        pstate = 0x55f038d73cb0
        readonly_flags = 1
        __func__ = "standard_ProcessUtility"
#17 0x000055f038488f3e in ProcessUtility (pstmt=0x55f038d479f0, 
queryString=0x55f038d46d90 "copy pgbench_accounts from stdin with (freeze on)", 
readOnlyTree=false, context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, 
dest=0x55f038d47cb0, qc=0x7ffda268e500) at utility.c:530
No locals.
#18 0x000055f038487a41 in PortalRunUtility (portal=0x55f038dc8250, 
pstmt=0x55f038d479f0, isTopLevel=true, setHoldSnapshot=false, 
dest=0x55f038d47cb0, qc=0x7ffda268e500) at pquery.c:1158
No locals.
#19 0x000055f038487ca9 in PortalRunMulti (portal=0x55f038dc8250, 
isTopLevel=true, setHoldSnapshot=false, dest=0x55f038d47cb0, 
altdest=0x55f038d47cb0, qc=0x7ffda268e500) at pquery.c:1315
        pstmt = 0x55f038d479f0
        stmtlist_item__state = {l = 0x55f038d47c60, i = 0}
        active_snapshot_set = false
        stmtlist_item = 0x55f038d47c78
#20 0x000055f03848712b in PortalRun (portal=0x55f038dc8250, 
count=9223372036854775807, isTopLevel=true, run_once=true, dest=0x55f038d47cb0, 
altdest=0x55f038d47cb0, qc=0x7ffda268e500) at pquery.c:791
        _save_exception_stack = 0x7ffda268e610
        _save_context_stack = 0x0
        _local_sigjmp_buf = {{__jmpbuf = {94490227245728, -222864991094357362, 
94490218952736, 140727328239488, 0, 0, -222864991285198194, 
-6272655314389220722}, __mask_was_saved = 0, __saved_mask = {__val = 
{94490226991425, 13, 112, 946280815, 94490233961760, 94490233957520, 
94490233961648, 112, 94490233961760, 140727328236592, 94490219061744, 
140727328236624, 9536418567, 0, 94490233961648, 94490234487376}}}}
        _do_rethrow = false
        result = false
        nprocessed = 18223879082613097102
        saveTopTransactionResourceOwner = 0x55f038d82ab0
        saveTopTransactionContext = 0x55f038d7c040
        saveActivePortal = 0x0
        saveResourceOwner = 0x55f038d82ab0
        savePortalContext = 0x0
        saveMemoryContext = 0x55f038d7c040
        __func__ = "PortalRun"
#21 0x000055f0384801fb in exec_simple_query (query_string=0x55f038d46d90 "copy 
pgbench_accounts from stdin with (freeze on)") at postgres.c:1274
        cmdtaglen = 4
        snapshot_set = false
        per_parsetree_context = 0x0
        plantree_list = 0x55f038d47c60
        parsetree = 0x55f038d47970
        commandTag = CMDTAG_COPY
        qc = {commandTag = CMDTAG_UNKNOWN, nprocessed = 0}
        querytree_list = 0x55f038d47c10
        portal = 0x55f038dc8250
        receiver = 0x55f038d47cb0
        format = 0
        cmdtagname = 0x55f038847779 "COPY"
        parsetree_item__state = {l = 0x55f038d479a0, i = 0}
        dest = DestRemote
        oldcontext = 0x55f038d7c040
        parsetree_list = 0x55f038d479a0
        parsetree_item = 0x55f038d479b8
        save_log_statement_stats = false
        was_logged = false
        use_implicit_block = false
        msec_str = 
"@\345h\242\375\177\000\000\370\303%8\360U\000\000\000\000\000\000\000\000\000\000\360\345h\242\375\177\000"
        __func__ = "exec_simple_query"
#22 0x000055f038484f71 in PostgresMain (dbname=0x55f038d811d8 "postgres", 
username=0x55f038d811b8 "postgres") at postgres.c:4637
        query_string = 0x55f038d46d90 "copy pgbench_accounts from stdin with 
(freeze on)"
        firstchar = 81
        input_message = {data = 0x55f038d46d90 "copy pgbench_accounts from 
stdin with (freeze on)", len = 50, maxlen = 1024, cursor = 50}
        local_sigjmp_buf = {{__jmpbuf = {94490227245728, -222864991150980466, 
94490218952736, 140727328239488, 0, 0, -222864991062900082, 
-6272655313480760690}, __mask_was_saved = 1, __saved_mask = {__val = {4194304, 
94490233920304, 94490234220712, 19720, 94490234204112, 8248, 94490233921712, 
94490233963008, 94490227245728, 140727328237264, 94490226991014, 
140724603453456, 94490233957520, 94490233920304, 94490233920304, 
140727328237296}}}}
        send_ready_for_query = false
        idle_in_transaction_timeout_enabled = false
        idle_session_timeout_enabled = false
        __func__ = "PostgresMain"
#23 0x000055f0383abd08 in BackendRun (port=0x55f038d73430) at postmaster.c:4433
No locals.
#24 0x000055f0383ab5db in BackendStartup (port=0x55f038d73430) at 
postmaster.c:4161
        bn = 0x55f038d73610
        pid = 0
        __func__ = "BackendStartup"
#25 0x000055f0383a7b54 in ServerLoop () at postmaster.c:1778
        port = 0x55f038d73430
        i = 0
        now = 1690140869
        last_lockfile_recheck_time = 1690140869
        last_touch_time = 1690140869
        events = {{pos = 2, events = 2, fd = 7, user_data = 0x0}, {pos = 
1508362478, events = 0, fd = 1781008296, user_data = 0x7ffda268e8e8}, {pos = 
1776849018, events = 32657, fd = -1570183104, user_data = 0x7ffda268e850}, {pos 
= 1781174769, events = 32657, fd = 12, user_data = 0x7f9166de6318}, {pos = 1, 
events = 0, fd = 0, user_data = 0x1}, {pos = 1781007424, events = 32657, fd = 
-1570181248, user_data = 0x7f9166de6318}, {pos = 1781007424, events = 32657, fd 
= 1781008296, user_data = 0x0}, {pos = 8, events = 0, fd = 938440736, user_data 
= 0x7f9100000001}, {pos = -1, events = 0, fd = 10830046, user_data = 
0x7f91695394a8}, {pos = 1776789760, events = 32657, fd = 1000, user_data = 
0x0}, {pos = 4096, events = 0, fd = 4096, user_data = 0x8}, {pos = 1690140868, 
events = 0, fd = 575921467, user_data = 0x7f9169ea30f8 <__libc_f...@got.plt>}, 
{pos = -1570181792, events = 32765, fd = 938440736, user_data = 
0x7ffda268ef80}, {pos = 0, events = 0, fd = 0, user_data = 0x7f916a2ae197 
<_dl_fixup+215>}, {pos = 1, events = 0, fd = 0, user_data = 0x7ffda268e910}, 
{pos = 1767085224, events = 32657, fd = -1570182000, user_data = 0x7f916a2b5c3e 
<_dl_runtime_resolve_xsavec+126>}, {pos = 0, events = 0, fd = 0, user_data = 
0x7ffda268ecd0}, {pos = 950299808, events = 22000, fd = 2, user_data = 
0x55f038a46ca0 <BlockSig>}, {pos = -1569509248, events = 32765, fd = 257, 
user_data = 0x8}, {pos = 1, events = 0, fd = 0, user_data = 0x2ffff00001fa0}, 
{pos = 111, events = 99, fd = 953639928, user_data = 0x2e302e302e373231}, {pos 
= 255, events = 0, fd = 4096, user_data = 0xffffffffffffffb0}, {pos = 0, events 
= 0, fd = 953644104, user_data = 0x55f038d73fc0}, {pos = 953634752, events = 
22000, fd = 4096, user_data = 0x1000}, {pos = 953424880, events = 22000, fd = 
1767599389, user_data = 0x10304}, {pos = 10787964, events = 0, fd = 0, 
user_data = 0x0}, {pos = 0, events = 0, fd = 0, user_data = 
0xffffffffffffffff}, {pos = -1, events = 4294967295, fd = 758657845, user_data 
= 0x3936310a61746164}, {pos = 0, events = 0, fd = 0, user_data = 0x7f9169721be0 
<main_arena+96>}, {pos = 0, events = 0, fd = 2, user_data = 0x0}, {pos = 0, 
events = 0, fd = 0, user_data = 0x0}, {pos = 0, events = 0, fd = 0, user_data = 
0x0}, {pos = 0, events = 0, fd = 0, user_data = 0x0}, {pos = 0, events = 0, fd 
= 0, user_data = 0x0}, {pos = 0, events = 0, fd = 0, user_data = 0x0}, {pos = 
0, events = 0, fd = 0, user_data = 0x0}, {pos = 784, events = 0, fd = 
953635008, user_data = 0x7ffda268eb60}, {pos = 946426213, events = 22000, fd = 
-1570182256, user_data = 0x55f038d74fd0}, {pos = 10, events = 0, fd = 5998700, 
user_data = 0x0}, {pos = 953634944, events = 22000, fd = 953634768, user_data = 
0x400}, {pos = 2, events = 0, fd = 6, user_data = 0x0}, {pos = 0, events = 0, 
fd = 0, user_data = 0x0}, {pos = 0, events = 0, fd = 0, user_data = 
0x7ffda268ebd0}, {pos = 946426596, events = 22000, fd = -1570182160, user_data 
= 0x55f038d74fd0}, {pos = 953644048, events = 22000, fd = 953634768, user_data 
= 0x400}, {pos = 953634768, events = 22000, fd = 950149200, user_data = 
0x138d46c90}, {pos = -1570182160, events = 32765, fd = 946472771, user_data = 
0x58}, {pos = -443934976, events = 1431317505, fd = -1570181872, user_data = 
0x55f038d41bf0}, {pos = 0, events = 0, fd = 953618896, user_data = 
0x7ffda268ef80}, {pos = 0, events = 0, fd = 953619096, user_data = 
0x7f91695c7c5a <_IO_flush_all_lockp+554>}, {pos = 1024, events = 1, fd = 
953619136, user_data = 0x7f91695c5fb0 <flush_cleanup>}, {pos = 0, events = 0, 
fd = 0, user_data = 0x0}, {pos = 947922173, events = 22000, fd = -443934976, 
user_data = 0x55f0386e02a0 <__libc_csu_init>}, {pos = 0, events = 0, fd = 0, 
user_data = 0x7f916961800d <__libc_fork+285>}, {pos = -1570181792, events = 
32765, fd = -443934976, user_data = 0x7ffda268ef80}, {pos = 943343810, events = 
22000, fd = 1024, user_data = 0x55f038d711d0}, {pos = 950149200, events = 
22000, fd = 953445520, user_data = 0x400000}, {pos = 946472771, events = 22000, 
fd = -1570181984, user_data = 0x55f038d711d0}, {pos = -1570181872, events = 
32765, fd = 953445520, user_data = 0x38d7d608}, {pos = 953634768, events = 
22000, fd = -1570181744, user_data = 0x55f038258159 <load_ident+444>}, {pos = 
-1570181248, events = 32765, fd = 0, user_data = 0x0}, {pos = 0, events = 2, fd 
= -1570181744, user_data = 0x55f0383acf62 <maybe_start_bgworkers+385>}, {pos = 
-1570181744, events = 32765, fd = 0, user_data = 0x0}, {pos = 953619936, events 
= 22000, fd = 0, user_data = 0x0}}
        nevents = 1
        __func__ = "ServerLoop"
#26 0x000055f0383a7429 in PostmasterMain (argc=3, argv=0x55f038d3daa0) at 
postmaster.c:1462
        opt = -1
        status = 0
        userDoption = 0x55f038d45c00 "/usr/local/pg8538-van/data"
        listen_addr_saved = true
        i = 64
        output_config_variable = 0x0
        __func__ = "PostmasterMain"
#27 0x000055f03826199b in main (argc=3, argv=0x55f038d3daa0) at main.c:198
        do_check_root = true
commit 9680dfad5a495c8ea1ee7db714fcec715bfa0fed
Author: Anton A. Melnikov <a.melni...@postgrespro.ru>
Date:   Sun Jul 23 22:24:03 2023 +0300

    Don't allow LimitAdditionalPins() to return zero.
    
    Reported-by Victoria Shepard.

diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index a7e3b9bb1d..df22aaa1c5 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -1767,7 +1767,7 @@ LimitAdditionalPins(uint32 *additional_pins)
 	 */
 	max_proportional_pins -= PrivateRefCountOverflowed + REFCOUNT_ARRAY_ENTRIES;
 
-	if (max_proportional_pins < 0)
+	if (max_proportional_pins <= 0)
 		max_proportional_pins = 1;
 
 	if (*additional_pins > max_proportional_pins)

Reply via email to