@@ -70,7 +70,7 @@ jsonbd_shmem_size(void)
70
70
shm_toc_estimator e ;
71
71
Size size ;
72
72
73
- Assert (jsonbd_nworkers != -1 );
73
+ Assert (jsonbd_nworkers > 0 );
74
74
shm_toc_initialize_estimator (& e );
75
75
76
76
shm_toc_estimate_chunk (& e , sizeof (jsonbd_shm_hdr ));
@@ -83,17 +83,26 @@ jsonbd_shmem_size(void)
83
83
shm_toc_estimate_chunk (& e , jsonbd_get_queue_size ());
84
84
}
85
85
86
- /* 3 keys each worker + 3 for header (header itself and two queues) */
87
- shm_toc_estimate_keys (& e , MAX_JSONBD_WORKERS * 3 + 3 );
86
+ /* 3 keys each worker + 3 for header (header itself, launcher and its two queues) */
87
+ shm_toc_estimate_keys (& e , MAX_JSONBD_WORKERS * 3 + 4 );
88
88
size = shm_toc_estimate (& e );
89
89
return size ;
90
90
}
91
91
92
+ /*
93
+ * Initialize worker shm block
94
+ *
95
+ * About keys in toc:
96
+ * 0 - for header
97
+ * 1..MAX_JSONBD_WORKERS - workers
98
+ * MAX_JSONBD_WORKERS + 1 - launcher
99
+ * MAX_JSONBD_WORKERS + 2 .. - queues
100
+ */
92
101
static void
93
102
jsonbd_init_worker (shm_toc * toc , jsonbd_shm_worker * wd , int worker_num ,
94
103
size_t queue_size )
95
104
{
96
- static int mqkey = MAX_JSONBD_WORKERS + 1 ;
105
+ static int mqkey = MAX_JSONBD_WORKERS + 2 ;
97
106
98
107
/* each worker will have two mq, for input and output */
99
108
wd -> mqin = shm_mq_create (shm_toc_allocate (toc , queue_size ), queue_size );
@@ -109,7 +118,7 @@ jsonbd_init_worker(shm_toc *toc, jsonbd_shm_worker *wd, int worker_num,
109
118
shm_mq_clean_sender (wd -> mqout );
110
119
111
120
if (worker_num )
112
- shm_toc_insert (toc , i + 1 , wd );
121
+ shm_toc_insert (toc , worker_num , wd );
113
122
114
123
shm_toc_insert (toc , mqkey ++ , wd -> mqin );
115
124
shm_toc_insert (toc , mqkey ++ , wd -> mqout );
@@ -141,7 +150,7 @@ jsonbd_shmem_startup_hook(void)
141
150
hdr = shm_toc_allocate (toc , sizeof (jsonbd_shm_hdr ));
142
151
hdr -> workers_ready = 0 ;
143
152
hdr -> launcher_sem = PGSemaphoreCreate ();
144
- jsonbd_init_worker (toc , & hdr -> launcher , sizeof (Oid ));
153
+ jsonbd_init_worker (toc , & hdr -> launcher , MAX_JSONBD_WORKERS + 1 , sizeof (Oid ));
145
154
146
155
for (i = 0 ; i < MAX_DATABASES ; i ++ )
147
156
sem_init (& hdr -> workers_sem [i ], 1 , jsonbd_nworkers );
@@ -176,9 +185,8 @@ _PG_init(void)
176
185
177
186
if (jsonbd_nworkers )
178
187
{
179
- int i ;
180
188
RequestAddinShmemSpace (jsonbd_shmem_size ());
181
- jsonbd_register_worker_launcher ();
189
+ jsonbd_register_launcher ();
182
190
}
183
191
else elog (LOG , "jsonbd: workers are disabled" );
184
192
}
@@ -281,8 +289,7 @@ jsonbd_communicate(shm_mq_iovec *iov, int iov_len,
281
289
shm_mq_result resmq ;
282
290
shm_mq_handle * mqh ;
283
291
jsonbd_shm_hdr * hdr ;
284
- jsonbd_shm_worker * wd ,
285
- * wd_inner ;
292
+ jsonbd_shm_worker * wd ;
286
293
287
294
char * res ;
288
295
Size reslen ;
@@ -312,7 +319,11 @@ jsonbd_communicate(shm_mq_iovec *iov, int iov_len,
312
319
for (j = i ; j < (i + jsonbd_nworkers ); j ++ )
313
320
{
314
321
wd = shm_toc_lookup (toc , j + 1 , false);
315
- Assert (wd -> dboid == MyDatabaseId );
322
+
323
+ if (wd -> dboid != MyDatabaseId )
324
+ /* somehow not all workers started for this database */
325
+ break ;
326
+
316
327
if (pg_atomic_test_set_flag (& wd -> busy ))
317
328
goto comm ;
318
329
}
@@ -355,7 +366,7 @@ jsonbd_communicate(shm_mq_iovec *iov, int iov_len,
355
366
elog (ERROR , "jsonbd: workers launcher was detached" );
356
367
}
357
368
358
- goto comm :
369
+ comm :
359
370
detached = false;
360
371
361
372
/* send data */
@@ -599,7 +610,7 @@ packJsonbValue(JsonbValue *val, int header_size, int *len)
599
610
600
611
/* Compress jsonb using dictionary */
601
612
static struct varlena *
602
- jsonbd_compress (AttributeCompression * ac , const struct varlena * data )
613
+ jsonbd_compress (CompressionMethodOptions * cmoptions , const struct varlena * data )
603
614
{
604
615
int size ;
605
616
JsonbIteratorToken r ;
@@ -668,7 +679,7 @@ jsonbd_compress(AttributeCompression *ac, const struct varlena *data)
668
679
Assert (offset == len );
669
680
670
681
/* retrieve or generate ids */
671
- jsonbd_worker_get_key_ids (ac -> cmoptoid , buf , len , idsbuf , nkeys );
682
+ jsonbd_worker_get_key_ids (cmoptions -> cmoptoid , buf , len , idsbuf , nkeys );
672
683
673
684
/* replace the old keys with encoded ids */
674
685
for (i = 0 ; i < nkeys ; i ++ )
@@ -704,7 +715,7 @@ jsonbd_configure(Form_pg_attribute attr, List *options)
704
715
}
705
716
706
717
static struct varlena *
707
- jsonbd_decompress (AttributeCompression * ac , const struct varlena * data )
718
+ jsonbd_decompress (CompressionMethodOptions * cmoptions , const struct varlena * data )
708
719
{
709
720
JsonbIteratorToken r ;
710
721
JsonbValue v ,
@@ -752,7 +763,7 @@ jsonbd_decompress(AttributeCompression *ac, const struct varlena *data)
752
763
}
753
764
754
765
/* retrieve keys */
755
- buf = jsonbd_worker_get_keys (ac -> cmoptoid , compression_buffers -> idsbuf , nkeys , & buflen );
766
+ buf = jsonbd_worker_get_keys (cmoptions -> cmoptoid , compression_buffers -> idsbuf , nkeys , & buflen );
756
767
if (buf == NULL )
757
768
elog (ERROR , "jsonbd: decompression error" );
758
769
0 commit comments