Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w9P4V-001PGu-2j for pgsql-hackers@arkaria.postgresql.org; Sun, 05 Apr 2026 15:07:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w9P4U-003URy-0V for pgsql-hackers@arkaria.postgresql.org; Sun, 05 Apr 2026 15:07:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w9P4T-003URq-2R for pgsql-hackers@lists.postgresql.org; Sun, 05 Apr 2026 15:07:30 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w9P4R-00000000hBt-3uYQ for pgsql-hackers@postgresql.org; Sun, 05 Apr 2026 15:07:29 +0000 Received: from [10.0.2.15] (unknown [130.41.208.1]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4fpbRJ5LPTz49Q1P; Sun, 05 Apr 2026 18:07:24 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1775401645; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y0RCoP1fhadXinafmYvbzu/fFIA4qHLhQydT5z0rIc8=; b=kU8cfbK5gqkrQqf1JZ2FmjO4Vlv6qZxf04H/sD2qqKiC7CX6fPpYz2kJ9aRa8MqAyP7bi2 B5fXYqUPrUtmQbqGRu1wBerqQGdpMFxe1XE1qq7Z9GrtDKaCAKNOEwFgbWMVI5wPMEzaDP HNgYJ6JFo4pj1qxEekHSYZRj/wf3emJmkbtc6NtNgzjOdvLKiUqgJHR9WVMhosx8OZ6auY 77ftnbv23gi7FgU5L4kcpJLabMGtovnec0x80+Nyn1TSdD9pq27KYfzjsg8K1bg7xb6MEm LSFLLb1BwyZ52AlFml6+TVEmGXKEiI9EKwxZIvp2pzhoPhmL8cGiOPR5YQw/dw== ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1775401645; b=ZLvgmJdLQrgUgCg3PkvezZU7rA3zxBXYyMpgYJn7VN4uvJ5YEv572TBOF+labvxECSnbc5 g7m3yc6yq+z39zG7Xl93Wozfc3W1ncembWyTA+FQDhTu9f3UcqAiveGYNCT8D6pOnTS35g ktpfZlL6C3TC8DlNNSPwNIFa7pxzElnljGSv76ADJPVTK3LCeczmpOrfdfB8d33B25Ye0Q l+Pz1RYi/moxz8YUzzVHgv698SMhcJZP2lKbS/REaDn3z++sKI/UQ7lYYjMRanb5amk8Oa DYiR0V8l4CdNtBO+uVG5+CN+vgnwSdNYw7825a9+VW+zJd2T/VAPPoylKlvwzQ== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1775401645; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y0RCoP1fhadXinafmYvbzu/fFIA4qHLhQydT5z0rIc8=; b=DCMKHr0kRfSknItChqFV+salA2TT/LqXoHEC/0jyXCzxr7qOIFtc0xM1Oj/1a2Viz3wrEU t7ds0IZVZRhqMVgRGc5G2pF5zvFXpg3PdK4rnLLimvjp4Ww+h7V6Y6wC8f/ZsVOY61ZN3n XVY74GZEzn7bZjdUIjupFpaK5eNT4sFQMQMaMTFZQGFYvXoHUMG9B/o0jn+PtCEqMLO4vt 7TkzVwLTIjGywwOXRstjtc+4FzEINKpD5X1jDa8nzoMm8Pu55cP2fgSXeRF45mSMtYcP8P Tkjxx+uvY/FbDXF1pKxsf6VOuL+kA47Sp0/9FVjQVZ73hj0Enx2N3/doEtMb/w== Message-ID: Date: Sun, 5 Apr 2026 18:07:23 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Better shared data structure management and resizable shared data structures To: Matthias van de Meent Cc: Ashutosh Bapat , Robert Haas , Andres Freund , pgsql-hackers , chaturvedipalak1911@gmail.com References: <113724ab-0028-493f-9605-6e8570f0939f@iki.fi> <791c3f18-f4de-4d84-ac6b-c7ccc074dd38@iki.fi> <9d919bd9-94dd-4bda-8ccf-ebced4178c53@iki.fi> <7d3ba240-9350-4dfc-bbe1-be6584aee236@iki.fi> <1c3a07a7-158d-4800-927c-2641c73277d8@iki.fi> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 05/04/2026 02:17, Matthias van de Meent wrote: > Formatting: >> + ShmemRequestHash(.name = "pg_stat_statements hash", >> + .nelems = pgss_max, >> + .hash_info.keysize = sizeof(pgssHashKey), >> + .hash_info.entrysize = sizeof(pgssEntry), >> + .hash_flags = HASH_ELEM | HASH_BLOBS, >> + .ptr = &pgss_hash, >> + ); > (note that additional unit of indentation for the closing bracket) > > Is this malformatting caused by pgindent? If so, could you see if > there's a better way of defining ShmemRequestHash/Struct that doesn't > have this indent as output? Yeah, that's pgindent. Matter of taste, but I think that looks fine. An alternative is to put the closing bracket on the same line with the last argument and drop the trailing comma: ShmemRequestStruct(.name = "pg_stat_statements", .size = sizeof(pgssSharedState), .ptr = (void **) &pgss); That looks OK to me too. >> + pgss->extent = 0; >> + pgss->n_writers = 0; >> + pgss->gc_count = 0; >> + pgss->stats.dealloc = 0; > > Shmem is said to be zero-initialized, should we remove the manual > zero-initialization? > >> + on_shmem_exit(pgss_shmem_shutdown, (Datum) 0); > See my upthread comment about adding optional on_shmem_exit callbacks > to ShmemCallbacks. > > 0005: LGTM > > 0006: I don't think it is a great idea to make the LwLock machinery > the first to get allocation requests: > It has the RequestNamedLWLockTranche infrastructure, which can only > register new requests while process_shmem_requests_in_progress, and > making it request its memory ahead of everything else is likely to > cause an undersized tranche to be allocated. Good catch. I think the easiest fix is to call process_shmem_requests() before ShmemCallRequestCallbacks() at postmaster startup. That's kind of how it was before: process_shmem_requests() was called before all the *ShmemSize() and *ShmemInit() functions in core. > You could make sure that this isn't an issue by maintaining a flag > in lwlock.c that's set when the shmem request is made (and reset on > shmem exit), which must be false when RequestNamedLWLockTranche() is > called, and if not then it should throw an error. I'll change it so that the number of locks calculated in LWLockShmemRequest() is stored in a global variable, and LWLockShmemInit() has an Assert() to cross-checks with that. That catches the bug and seems like a good cross-check in general. This isn't the only place where we need to pass information from the request callback to the init callback. I've used a global variable for that here, and also between ProcGlobalShmemRequest() and ProcGlobalShmemRequest(). An alternative might be to use the callback arg pointers that are currently unused, but I'm not sure how to make that ergonomic. The current 'arg' isn't very helpful for that, so perhaps the signatures should look like this instead: static void LWLockShmemRequest(Datum *init_arg) { int numLocks; numLocks = NUM_FIXED_LWLOCKS + NumLWLocksForNamedTranches(); /* pass the calculated numLocks value to LWLockShmemInit() */ *init_arg = Int32GetDatum(numLocks); ... } static void LWLockShmemInit(Datum init_arg) { int numLocks = DatumGetIn32(numLocks); ... } If you need to pass more than a single Datum, you can allocate a struct and pass it via PointerGetDatum(). At that point global variables might feel simpler again though. > 0010: Not looked at everything yet, but a few comment: > >> +++ b/src/include/access/slru.h > > With the changes in the signatures for most/all SLRU functions from a > hidden-by-typedef pointer to a visible pointer type, maybe this could > be an opportunity to swap them to `const SlruDesc *ctl` wherever > possible? I don't think there are many backend-local changes that > happen to SlruDescs once we've properly started the backend. I'm happy > to provide an incremental patch if you'd like me to spend cycles on it > if you're busy. Yeah sounds like a good idea. >> +++ b/src/backend/access/transam/clog.c > >> + SimpleLruRequest(.desc = &XactSlruDesc, >> + .name = "transaction", >> + .Dir = "pg_xact", >> + .long_segment_names = false, >> + >> + .nslots = CLOGShmemBuffers(), >> + .nlsns = CLOG_LSNS_PER_PAGE, >> + >> + .sync_handler = SYNC_HANDLER_CLOG, >> + .PagePrecedes = CLOGPagePrecedes, >> + .errdetail_for_io_error = clog_errdetail_for_io_error, > > That awfully inconsistent field name styling is ... awful, but not > this patch's fault. If something can be done about it in a cheap > fashion in this patch, that'd be great, but I won't hold it against > you if that's skipped. :-D. - Heikki