2
2
3
3
#include "postgres.h"
4
4
#include "fmgr.h"
5
+ #include "miscadmin.h"
5
6
#include "pgstat.h"
6
7
7
8
#include "access/genam.h"
30
31
static bool xact_started = false;
31
32
static bool shutdown_requested = false;
32
33
static jsonbc_shm_worker * worker_state ;
33
- static shm_mq_handle * worker_mq_handle_in ;
34
- static shm_mq_handle * worker_mq_handle_out ;
35
34
36
35
Oid jsonbc_dictionary_reloid = InvalidOid ;
37
36
Oid jsonbc_keys_indoid = InvalidOid ;
@@ -82,16 +81,15 @@ init_local_variables(int worker_num)
82
81
83
82
/* input mq */
84
83
shm_mq_set_receiver (worker_state -> mqin , MyProc );
85
- worker_mq_handle_in = shm_mq_attach (worker_state -> mqin , NULL , NULL );
86
84
87
85
/* output mq */
88
86
shm_mq_set_sender (worker_state -> mqout , MyProc );
89
- worker_mq_handle_out = shm_mq_attach (worker_state -> mqout , NULL , NULL );
90
87
91
88
/* not busy at start */
92
89
pg_atomic_clear_flag (& worker_state -> busy );
93
90
94
- elog (LOG , "jsonbc dictionary worker %d started" , worker_num + 1 );
91
+ elog (LOG , "jsonbc dictionary worker %d started with pid: %d" ,
92
+ worker_num + 1 , MyProcPid );
95
93
}
96
94
97
95
static void
@@ -224,9 +222,14 @@ jsonbc_get_keys_slow(Oid cmoptoid, uint32 *ids, int nkeys, size_t *reslen)
224
222
static void
225
223
jsonbc_get_key_ids_slow (Oid cmoptoid , char * buf , uint32 * idsbuf , int nkeys )
226
224
{
225
+ Relation rel ;
226
+
227
227
int i ;
228
+ Oid relid = jsonbc_get_dictionary_relid ();
228
229
char * nspc = get_namespace_name (get_extension_schema ());
229
230
231
+ rel = relation_open (relid , ShareLock );
232
+
230
233
if (SPI_connect () != SPI_OK_CONNECT )
231
234
elog (ERROR , "SPI_connect failed" );
232
235
@@ -270,6 +273,7 @@ jsonbc_get_key_ids_slow(Oid cmoptoid, char *buf, uint32 *idsbuf, int nkeys)
270
273
pfree (sql );
271
274
}
272
275
SPI_finish ();
276
+ relation_close (rel , ShareLock );
273
277
}
274
278
275
279
static char *
@@ -361,8 +365,12 @@ worker_main(Datum arg)
361
365
Size nbytes ;
362
366
void * data ;
363
367
364
- shm_mq_result resmq = shm_mq_receive (worker_mq_handle_in , & nbytes ,
365
- & data , true);
368
+ shm_mq_handle * mqh ;
369
+ shm_mq_result resmq ;
370
+
371
+ mqh = shm_mq_attach (worker_state -> mqin , NULL , NULL );
372
+ resmq = shm_mq_receive (mqh , & nbytes , & data , true);
373
+
366
374
if (resmq == SHM_MQ_SUCCESS )
367
375
{
368
376
JsonbcCommand cmd ;
@@ -393,10 +401,14 @@ worker_main(Datum arg)
393
401
elog (NOTICE , "jsonbc: got unknown command" );
394
402
}
395
403
396
- resmq = shm_mq_sendv (worker_mq_handle_out , & iov , 1 , false);
404
+ shm_mq_detach (mqh );
405
+
406
+ mqh = shm_mq_attach (worker_state -> mqout , NULL , NULL );
407
+ resmq = shm_mq_sendv (mqh , & iov , 1 , false);
397
408
if (resmq != SHM_MQ_SUCCESS )
398
409
elog (NOTICE , "jsonbc: backend detached early" );
399
410
411
+ shm_mq_detach (mqh );
400
412
pfree ((void * ) iov .data );
401
413
}
402
414
@@ -412,8 +424,6 @@ worker_main(Datum arg)
412
424
ResetLatch (& MyProc -> procLatch );
413
425
}
414
426
415
- shm_mq_detach (worker_mq_handle_in );
416
- shm_mq_detach (worker_mq_handle_out );
417
427
elog (LOG , "jsonbc dictionary worker has ended its work" );
418
428
proc_exit (0 );
419
429
}
0 commit comments