Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit38b602b

Browse files
Move dynamically-allocated LWLock tranche names to shared memory.
There are two ways for shared libraries to allocate their ownLWLock tranches. One way is to call RequestNamedLWLockTranche() ina shmem_request_hook, which requires the library to be loaded viashared_preload_libraries. The other way is to callLWLockNewTrancheId(), which is not subject to the samerestrictions. However, LWLockNewTrancheId() does require eachbackend to store the tranche's name in backend-local memory viaLWLockRegisterTranche(). This API is a little cumbersome and leadsto things like unhelpful pg_stat_activity.wait_event values inbackends that haven't loaded the library.This commit moves these LWLock tranche names to shared memory, thuseliminating the need for each backend to callLWLockRegisterTranche(). Instead, the tranche name must beprovided to LWLockNewTrancheId(), which immediately makes the nameavailable to all backends. Since the tranche name array isappend-only, lookups can ordinarily avoid locking as long as theirlocal copy of the LWLock counter is greater than the requestedtranche ID.One downside of this approach is that we now have a hard limit onboth the length of tranche names (NAMEDATALEN-1 bytes) and thenumber of dynamically-allocated tranches (256). Besides a limit ofNAMEDATALEN-1 bytes for tranche names registered viaRequestNamedLWLockTranche(), no such limits previously existed. Wecould avoid these new limits by using dynamic shared memory, butthe complexity involved didn't seem worth it. We brieflyconsidered making the tranche limit user-configurable butultimately decided against that, too. Since there is still a lotof time left in the v19 development cycle, it's possible we willrevisit this choice.Author: Sami Imseih <samimseih@gmail.com>Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>Reviewed-by: Rahila Syed <rahilasyed90@gmail.com>Reviewed-by: Andres Freund <andres@anarazel.de>Discussion:https://postgr.es/m/CAA5RZ0vvED3naph8My8Szv6DL4AxOVK3eTPS0qXsaKi%3DbVdW2A%40mail.gmail.com
1 parent7b0fb9f commit38b602b

File tree

11 files changed

+130
-174
lines changed

11 files changed

+130
-174
lines changed

‎contrib/pg_prewarm/autoprewarm.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ apw_init_state(void *ptr)
864864
{
865865
AutoPrewarmSharedState*state= (AutoPrewarmSharedState*)ptr;
866866

867-
LWLockInitialize(&state->lock,LWLockNewTrancheId());
867+
LWLockInitialize(&state->lock,LWLockNewTrancheId("autoprewarm"));
868868
state->bgworker_pid=InvalidPid;
869869
state->pid_using_dumpfile=InvalidPid;
870870
}
@@ -883,7 +883,6 @@ apw_init_shmem(void)
883883
sizeof(AutoPrewarmSharedState),
884884
apw_init_state,
885885
&found);
886-
LWLockRegisterTranche(apw_state->lock.tranche,"autoprewarm");
887886

888887
returnfound;
889888
}

‎doc/src/sgml/xfunc.sgml‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3759,7 +3759,7 @@ LWLockPadded *GetNamedLWLockTranche(const char *tranche_name)
37593759
<literal>shmem_request_hook</literal>. To do so, first allocate a
37603760
<literal>tranche_id</literal> by calling:
37613761
<programlisting>
3762-
int LWLockNewTrancheId(void)
3762+
int LWLockNewTrancheId(const char *name)
37633763
</programlisting>
37643764
Next, initialize each LWLock, passing the new
37653765
<literal>tranche_id</literal> as an argument:
@@ -3777,17 +3777,8 @@ void LWLockInitialize(LWLock *lock, int tranche_id)
37773777
</para>
37783778

37793779
<para>
3780-
Finally, each backend using the <literal>tranche_id</literal> should
3781-
associate it with a <literal>tranche_name</literal> by calling:
3782-
<programlisting>
3783-
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
3784-
</programlisting>
3785-
</para>
3786-
3787-
<para>
3788-
A complete usage example of <function>LWLockNewTrancheId</function>,
3789-
<function>LWLockInitialize</function>, and
3790-
<function>LWLockRegisterTranche</function> can be found in
3780+
A complete usage example of <function>LWLockNewTrancheId</function> and
3781+
<function>LWLockInitialize</function> can be found in
37913782
<filename>contrib/pg_prewarm/autoprewarm.c</filename> in the
37923783
<productname>PostgreSQL</productname> source tree.
37933784
</para>

‎src/backend/postmaster/launch_backend.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ typedef struct
101101
structInjectionPointsCtl*ActiveInjectionPoints;
102102
#endif
103103
intNamedLWLockTrancheRequests;
104-
NamedLWLockTranche*NamedLWLockTrancheArray;
104+
char**LWLockTrancheNames;
105105
int*LWLockCounter;
106106
LWLockPadded*MainLWLockArray;
107107
slock_t*ProcStructLock;
@@ -761,7 +761,7 @@ save_backend_variables(BackendParameters *param,
761761
#endif
762762

763763
param->NamedLWLockTrancheRequests=NamedLWLockTrancheRequests;
764-
param->NamedLWLockTrancheArray=NamedLWLockTrancheArray;
764+
param->LWLockTrancheNames=LWLockTrancheNames;
765765
param->LWLockCounter=LWLockCounter;
766766
param->MainLWLockArray=MainLWLockArray;
767767
param->ProcStructLock=ProcStructLock;
@@ -1022,7 +1022,7 @@ restore_backend_variables(BackendParameters *param)
10221022
#endif
10231023

10241024
NamedLWLockTrancheRequests=param->NamedLWLockTrancheRequests;
1025-
NamedLWLockTrancheArray=param->NamedLWLockTrancheArray;
1025+
LWLockTrancheNames=param->LWLockTrancheNames;
10261026
LWLockCounter=param->LWLockCounter;
10271027
MainLWLockArray=param->MainLWLockArray;
10281028
ProcStructLock=param->ProcStructLock;

‎src/backend/storage/ipc/dsm_registry.c‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ GetNamedDSA(const char *name, bool *found)
299299
entry->type=DSMR_ENTRY_TYPE_DSA;
300300

301301
/* Initialize the LWLock tranche for the DSA. */
302-
state->tranche=LWLockNewTrancheId();
303-
LWLockRegisterTranche(state->tranche,name);
302+
state->tranche=LWLockNewTrancheId(name);
304303

305304
/* Initialize the DSA. */
306305
ret=dsa_create(state->tranche);
@@ -321,9 +320,6 @@ GetNamedDSA(const char *name, bool *found)
321320
ereport(ERROR,
322321
(errmsg("requested DSA already attached to current process")));
323322

324-
/* Initialize existing LWLock tranche for the DSA. */
325-
LWLockRegisterTranche(state->tranche,name);
326-
327323
/* Attach to existing DSA. */
328324
ret=dsa_attach(state->handle);
329325
dsa_pin_mapping(ret);
@@ -378,8 +374,7 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found)
378374
entry->type=DSMR_ENTRY_TYPE_DSH;
379375

380376
/* Initialize the LWLock tranche for the hash table. */
381-
dsh_state->tranche=LWLockNewTrancheId();
382-
LWLockRegisterTranche(dsh_state->tranche,name);
377+
dsh_state->tranche=LWLockNewTrancheId(name);
383378

384379
/* Initialize the DSA for the hash table. */
385380
dsa=dsa_create(dsh_state->tranche);
@@ -409,9 +404,6 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found)
409404
ereport(ERROR,
410405
(errmsg("requested DSHash already attached to current process")));
411406

412-
/* Initialize existing LWLock tranche for the hash table. */
413-
LWLockRegisterTranche(dsh_state->tranche,name);
414-
415407
/* Attach to existing DSA for the hash table. */
416408
dsa=dsa_attach(dsh_state->dsa_handle);
417409
dsa_pin_mapping(dsa);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp