PostgreSQL Source Code  git master
session.h File Reference
#include "lib/dshash.h"
Include dependency graph for session.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Session
 

Typedefs

typedef struct Session Session
 

Functions

void InitializeSession (void)
 
dsm_handle GetSessionDsmHandle (void)
 
void AttachSession (dsm_handle handle)
 
void DetachSession (void)
 

Variables

SessionCurrentSession
 

Typedef Documentation

◆ Session

Function Documentation

◆ AttachSession()

void AttachSession ( dsm_handle  handle)

Definition at line 155 of file session.c.

References Session::area, dsa_attach_in_place(), dsa_pin_mapping(), dsm_attach(), dsm_pin_mapping(), dsm_segment_address(), elog, ERROR, MemoryContextSwitchTo(), Session::segment, SESSION_KEY_DSA, SESSION_KEY_RECORD_TYPMOD_REGISTRY, SESSION_MAGIC, SharedRecordTypmodRegistryAttach(), shm_toc_attach(), shm_toc_lookup(), and TopMemoryContext.

Referenced by ParallelWorkerMain().

156 {
157  dsm_segment *seg;
158  shm_toc *toc;
159  void *dsa_space;
160  void *typmod_registry_space;
161  dsa_area *dsa;
162  MemoryContext old_context;
163 
165 
166  /* Attach to the DSM segment. */
167  seg = dsm_attach(handle);
168  if (seg == NULL)
169  elog(ERROR, "could not attach to per-session DSM segment");
171 
172  /* Attach to the DSA area. */
173  dsa_space = shm_toc_lookup(toc, SESSION_KEY_DSA, false);
174  dsa = dsa_attach_in_place(dsa_space, seg);
175 
176  /* Make them available via the current session. */
177  CurrentSession->segment = seg;
178  CurrentSession->area = dsa;
179 
180  /* Attach to the shared record typmod registry. */
181  typmod_registry_space =
184  typmod_registry_space);
185 
186  /* Remain attached until end of backend or DetachSession(). */
187  dsm_pin_mapping(seg);
188  dsa_pin_mapping(dsa);
189 
190  MemoryContextSwitchTo(old_context);
191 }
#define SESSION_KEY_RECORD_TYPMOD_REGISTRY
Definition: session.c:45
Session * CurrentSession
Definition: session.c:48
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:533
dsa_area * dsa_attach_in_place(void *place, dsm_segment *segment)
Definition: dsa.c:541
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void SharedRecordTypmodRegistryAttach(SharedRecordTypmodRegistry *registry)
Definition: typcache.c:2009
#define ERROR
Definition: elog.h:43
void dsm_pin_mapping(dsm_segment *seg)
Definition: dsm.c:765
dsa_area * area
Definition: session.h:28
#define SESSION_KEY_DSA
Definition: session.c:44
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:631
MemoryContext TopMemoryContext
Definition: mcxt.c:44
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition: shm_toc.c:64
#define SESSION_MAGIC
Definition: session.c:30
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:938
Definition: dsa.c:354
#define elog(elevel,...)
Definition: elog.h:226
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
dsm_segment * segment
Definition: session.h:27

◆ DetachSession()

void DetachSession ( void  )

Definition at line 201 of file session.c.

References Session::area, dsa_detach(), dsm_detach(), and Session::segment.

Referenced by ParallelWorkerMain().

202 {
203  /* Runs detach hooks. */
205  CurrentSession->segment = NULL;
207  CurrentSession->area = NULL;
208 }
Session * CurrentSession
Definition: session.c:48
dsa_area * area
Definition: session.h:28
void dsa_detach(dsa_area *area)
Definition: dsa.c:1913
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:664
dsm_segment * segment
Definition: session.h:27

◆ GetSessionDsmHandle()

dsm_handle GetSessionDsmHandle ( void  )

Definition at line 70 of file session.c.

References Session::area, dsa_create_in_place(), dsa_pin_mapping(), dsm_create(), DSM_CREATE_NULL_IF_MAXSEGMENTS, DSM_HANDLE_INVALID, dsm_pin_mapping(), dsm_segment_address(), dsm_segment_handle(), LWTRANCHE_SESSION_DSA, MemoryContextSwitchTo(), Session::segment, SESSION_DSA_SIZE, SESSION_KEY_DSA, SESSION_KEY_RECORD_TYPMOD_REGISTRY, SESSION_MAGIC, SharedRecordTypmodRegistryEstimate(), SharedRecordTypmodRegistryInit(), shm_toc_allocate(), shm_toc_create(), shm_toc_estimate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_initialize_estimator, shm_toc_insert(), and TopMemoryContext.

Referenced by InitializeParallelDSM().

71 {
72  shm_toc_estimator estimator;
73  shm_toc *toc;
74  dsm_segment *seg;
75  size_t typmod_registry_size;
76  size_t size;
77  void *dsa_space;
78  void *typmod_registry_space;
79  dsa_area *dsa;
80  MemoryContext old_context;
81 
82  /*
83  * If we have already created a session-scope DSM segment in this backend,
84  * return its handle. The same segment will be used for the rest of this
85  * backend's lifetime.
86  */
87  if (CurrentSession->segment != NULL)
89 
90  /* Otherwise, prepare to set one up. */
92  shm_toc_initialize_estimator(&estimator);
93 
94  /* Estimate space for the per-session DSA area. */
95  shm_toc_estimate_keys(&estimator, 1);
97 
98  /* Estimate space for the per-session record typmod registry. */
99  typmod_registry_size = SharedRecordTypmodRegistryEstimate();
100  shm_toc_estimate_keys(&estimator, 1);
101  shm_toc_estimate_chunk(&estimator, typmod_registry_size);
102 
103  /* Set up segment and TOC. */
104  size = shm_toc_estimate(&estimator);
106  if (seg == NULL)
107  {
108  MemoryContextSwitchTo(old_context);
109 
110  return DSM_HANDLE_INVALID;
111  }
113  dsm_segment_address(seg),
114  size);
115 
116  /* Create per-session DSA area. */
117  dsa_space = shm_toc_allocate(toc, SESSION_DSA_SIZE);
118  dsa = dsa_create_in_place(dsa_space,
121  seg);
122  shm_toc_insert(toc, SESSION_KEY_DSA, dsa_space);
123 
124 
125  /* Create session-scoped shared record typmod registry. */
126  typmod_registry_space = shm_toc_allocate(toc, typmod_registry_size);
128  typmod_registry_space, seg, dsa);
130  typmod_registry_space);
131 
132  /*
133  * If we got this far, we can pin the shared memory so it stays mapped for
134  * the rest of this backend's life. If we don't make it this far, cleanup
135  * callbacks for anything we installed above (ie currently
136  * SharedRecordTypmodRegistry) will run when the DSM segment is detached
137  * by CurrentResourceOwner so we aren't left with a broken CurrentSession.
138  */
139  dsm_pin_mapping(seg);
140  dsa_pin_mapping(dsa);
141 
142  /* Make segment and area available via CurrentSession. */
143  CurrentSession->segment = seg;
144  CurrentSession->area = dsa;
145 
146  MemoryContextSwitchTo(old_context);
147 
148  return dsm_segment_handle(seg);
149 }
#define SESSION_KEY_RECORD_TYPMOD_REGISTRY
Definition: session.c:45
shm_toc * shm_toc_create(uint64 magic, void *address, Size nbytes)
Definition: shm_toc.c:40
#define SESSION_DSA_SIZE
Definition: session.c:39
Session * CurrentSession
Definition: session.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:966
Size shm_toc_estimate(shm_toc_estimator *e)
Definition: shm_toc.c:263
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
void SharedRecordTypmodRegistryInit(SharedRecordTypmodRegistry *registry, dsm_segment *segment, dsa_area *area)
Definition: typcache.c:1910
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
void dsm_pin_mapping(dsm_segment *seg)
Definition: dsm.c:765
dsa_area * area
Definition: session.h:28
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
#define SESSION_KEY_DSA
Definition: session.c:44
#define shm_toc_initialize_estimator(e)
Definition: shm_toc.h:49
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:631
dsa_area * dsa_create_in_place(void *place, size_t size, int tranche_id, dsm_segment *segment)
Definition: dsa.c:469
MemoryContext TopMemoryContext
Definition: mcxt.c:44
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:437
size_t SharedRecordTypmodRegistryEstimate(void)
Definition: typcache.c:1888
#define SESSION_MAGIC
Definition: session.c:30
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:938
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
Definition: dsa.c:354
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
dsm_segment * segment
Definition: session.h:27

◆ InitializeSession()

void InitializeSession ( void  )

Definition at line 54 of file session.c.

References MemoryContextAllocZero(), and TopMemoryContext.

Referenced by InitPostgres().

55 {
57 }
Session * CurrentSession
Definition: session.c:48
MemoryContext TopMemoryContext
Definition: mcxt.c:44
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:814

Variable Documentation

◆ CurrentSession