Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d78bf69

Browse files
committed
multiple databases issue fixed
1 parent 9603608 commit d78bf69

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

dsm_array.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,32 @@ free_dsm_array(DsmArray *arr)
258258
arr->length = 0;
259259
}
260260

261+
void
262+
resize_dsm_array(DsmArray *arr, size_t entry_size, size_t length)
263+
{
264+
void *array_data;
265+
size_t array_data_size;
266+
void *buffer;
267+
268+
/* Copy data from array to temporary buffer */
269+
array_data = dsm_array_get_pointer(arr);
270+
array_data_size = arr->length * entry_size;
271+
buffer = palloc(array_data_size);
272+
memcpy(buffer, array_data, array_data_size);
273+
274+
/* Free array */
275+
free_dsm_array(arr);
276+
277+
/* Allocate new array */
278+
alloc_dsm_array(arr, entry_size, length);
279+
280+
/* Copy data to new array */
281+
array_data = dsm_array_get_pointer(arr);
282+
memcpy(array_data, buffer, array_data_size);
283+
284+
pfree(buffer);
285+
}
286+
261287
void *
262288
dsm_array_get_pointer(const DsmArray* arr)
263289
{

init.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,58 @@ void
7575
load_config(void)
7676
{
7777
bool new_segment_created;
78+
Oid *databases;
7879

7980
initialization_needed = false;
8081
new_segment_created = init_dsm_segment(INITIAL_BLOCKS_COUNT, 32);
8182

82-
/* if config is not loaded */
83+
/* If dsm segment just created */
8384
if (new_segment_created)
8485
{
85-
LWLockAcquire(pmstate->load_config_lock, LW_EXCLUSIVE);
86-
load_relations_hashtable(new_segment_created);
87-
LWLockRelease(pmstate->load_config_lock);
86+
/*
87+
* Allocate databases array and put current database
88+
* oid into it. This array contains databases oids
89+
* that have already been cached (to prevent repeat caching)
90+
*/
91+
LWLockAcquire(pmstate->dsm_init_lock, LW_EXCLUSIVE);
92+
93+
if (&pmstate->databases.length > 0)
94+
free_dsm_array(&pmstate->databases);
95+
alloc_dsm_array(&pmstate->databases, sizeof(Oid), 1);
96+
databases = (Oid *) dsm_array_get_pointer(&pmstate->databases);
97+
databases[0] = MyDatabaseId;
98+
99+
LWLockRelease(pmstate->dsm_init_lock);
100+
}
101+
else
102+
{
103+
int databases_count = pmstate->databases.length;
104+
int i;
105+
106+
/* Check if we already cached config for current database */
107+
LWLockAcquire(pmstate->dsm_init_lock, LW_EXCLUSIVE);
108+
109+
databases = (Oid *) dsm_array_get_pointer(&pmstate->databases);
110+
for(i=0; i<databases_count; i++)
111+
if (databases[i] == MyDatabaseId)
112+
{
113+
// LWLockRelease(pmstate->load_config_lock);
114+
LWLockRelease(pmstate->dsm_init_lock);
115+
return;
116+
}
117+
118+
/* Put current database oid to databases list */
119+
resize_dsm_array(&pmstate->databases, sizeof(Oid), databases_count + 1);
120+
databases = (Oid *) dsm_array_get_pointer(&pmstate->databases);
121+
databases[databases_count] = MyDatabaseId;
122+
123+
LWLockRelease(pmstate->dsm_init_lock);
88124
}
125+
126+
/* Load cache */
127+
LWLockAcquire(pmstate->load_config_lock, LW_EXCLUSIVE);
128+
load_relations_hashtable(new_segment_created);
129+
LWLockRelease(pmstate->load_config_lock);
89130
}
90131

91132
/*

pathman.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ typedef struct RangeRelation
109109

110110
typedef struct PathmanState
111111
{
112-
LWLock *load_config_lock;
113-
LWLock *dsm_init_lock;
114-
LWLock *edit_partitions_lock;
112+
LWLock *load_config_lock;
113+
LWLock *dsm_init_lock;
114+
LWLock *edit_partitions_lock;
115+
DsmArray databases;
115116
} PathmanState;
116117

117118
PathmanState *pmstate;
@@ -156,6 +157,7 @@ bool init_dsm_segment(size_t blocks_count, size_t block_size);
156157
void init_dsm_table(size_t block_size, size_t start, size_t end);
157158
void alloc_dsm_array(DsmArray *arr, size_t entry_size, size_t length);
158159
void free_dsm_array(DsmArray *arr);
160+
void resize_dsm_array(DsmArray *arr, size_t entry_size, size_t length);
159161
void *dsm_array_get_pointer(const DsmArray* arr);
160162
dsm_handle get_dsm_array_segment(void);
161163
void attach_dsm_array_segment(void);

0 commit comments

Comments
 (0)