12
12
#include "access/sysattr.h"
13
13
#include "catalog/pg_extension.h"
14
14
#include "catalog/indexing.h"
15
+ #include "catalog/namespace.h"
15
16
#include "commands/extension.h"
16
17
#include "executor/spi.h"
17
18
#include "port/atomics.h"
@@ -154,6 +155,8 @@ jsonbc_get_keys_slow(Oid cmoptoid, uint32 *ids, int nkeys, size_t *reslen)
154
155
Relation rel ,
155
156
idxrel ;
156
157
158
+ start_xact_command ();
159
+
157
160
rel = relation_open (relid , AccessShareLock );
158
161
idxrel = index_open (jsonbc_id_indoid , AccessShareLock );
159
162
@@ -211,6 +214,8 @@ jsonbc_get_keys_slow(Oid cmoptoid, uint32 *ids, int nkeys, size_t *reslen)
211
214
index_close (idxrel , AccessShareLock );
212
215
relation_close (rel , AccessShareLock );
213
216
217
+ finish_xact_command ();
218
+
214
219
* reslen = keyptr - keys ;
215
220
return keys ;
216
221
}
@@ -226,8 +231,8 @@ jsonbc_get_key_ids_slow(Oid cmoptoid, char *buf, uint32 *idsbuf, int nkeys)
226
231
227
232
int i ;
228
233
Oid relid = jsonbc_get_dictionary_relid ();
229
- char * nspc = get_namespace_name (get_extension_schema ());
230
234
235
+ start_xact_command ();
231
236
rel = relation_open (relid , ShareLock );
232
237
233
238
if (SPI_connect () != SPI_OK_CONNECT )
@@ -239,18 +244,18 @@ jsonbc_get_key_ids_slow(Oid cmoptoid, char *buf, uint32 *idsbuf, int nkeys)
239
244
bool isnull ;
240
245
char * sql ;
241
246
242
- sql = psprintf ("SELECT id FROM %s .jsonbc_dictionary WHERE cmopt = %d"
243
- " AND key = '%s'" , nspc , cmoptoid , buf );
247
+ sql = psprintf ("SELECT id FROM public .jsonbc_dictionary WHERE cmopt = %d"
248
+ " AND key = '%s'" , cmoptoid , buf );
244
249
245
250
if (SPI_exec (sql , 0 ) != SPI_OK_SELECT )
246
251
elog (ERROR , "SPI_exec failed" );
247
252
248
253
if (SPI_processed == 0 )
249
254
{
250
255
char * sql2 = psprintf ("with t as (select (coalesce(max(id), 0) + 1) new_id from "
251
- "%s .jsonbc_dictionary where cmopt = %d) insert into %s .jsonbc_dictionary"
256
+ "public .jsonbc_dictionary where cmopt = %d) insert into public .jsonbc_dictionary"
252
257
" select %d, t.new_id, '%s' from t returning id" ,
253
- nspc , cmoptoid , nspc , cmoptoid , buf );
258
+ cmoptoid , cmoptoid , buf );
254
259
255
260
if (SPI_exec (sql2 , 0 ) != SPI_OK_INSERT_RETURNING )
256
261
elog (ERROR , "SPI_exec failed" );
@@ -274,6 +279,7 @@ jsonbc_get_key_ids_slow(Oid cmoptoid, char *buf, uint32 *idsbuf, int nkeys)
274
279
}
275
280
SPI_finish ();
276
281
relation_close (rel , ShareLock );
282
+ finish_xact_command ();
277
283
}
278
284
279
285
static char *
@@ -296,7 +302,7 @@ jsonbc_cmd_get_ids(int nkeys, Oid cmoptoid, char *buf, size_t *buflen)
296
302
ErrorData * error ;
297
303
MemoryContextSwitchTo (old_mcxt );
298
304
error = CopyErrorData ();
299
- elog (LOG , "jsonbc: error occured %s" , error -> message );
305
+ elog (LOG , "jsonbc: error occured: %s" , error -> message );
300
306
FlushErrorState ();
301
307
pfree (error );
302
308
@@ -316,16 +322,14 @@ jsonbc_cmd_get_keys(int nkeys, Oid cmoptoid, uint32 *ids, size_t *reslen)
316
322
317
323
PG_TRY ();
318
324
{
319
- start_xact_command ();
320
325
keys = jsonbc_get_keys_slow (cmoptoid , ids , nkeys , reslen );
321
- finish_xact_command ();
322
326
}
323
327
PG_CATCH ();
324
328
{
325
329
ErrorData * error ;
326
330
MemoryContextSwitchTo (old_mcxt );
327
331
error = CopyErrorData ();
328
- elog (LOG , "jsonbc: error occured %s" , error -> message );
332
+ elog (LOG , "jsonbc: error occured: %s" , error -> message );
329
333
FlushErrorState ();
330
334
pfree (error );
331
335
}
@@ -399,11 +403,13 @@ worker_main(Datum arg)
399
403
iov .data = "" ;
400
404
iov .len = 1 ;
401
405
}
406
+ break ;
402
407
default :
403
408
elog (NOTICE , "jsonbc: got unknown command" );
404
409
}
405
410
406
411
shm_mq_detach (mqh );
412
+ shm_mq_clean_sender (worker_state -> mqin );
407
413
408
414
mqh = shm_mq_attach (worker_state -> mqout , NULL , NULL );
409
415
resmq = shm_mq_sendv (mqh , & iov , 1 , false);
@@ -436,24 +442,32 @@ worker_main(Datum arg)
436
442
static Oid
437
443
jsonbc_get_dictionary_relid (void )
438
444
{
439
- Oid relid ;
445
+ Oid relid ,
446
+ nspoid ;
440
447
441
448
if (OidIsValid (jsonbc_dictionary_reloid ))
442
449
return jsonbc_dictionary_reloid ;
443
450
444
- relid = get_relname_relid (JSONBC_DICTIONARY_REL , InvalidOid );
451
+ start_xact_command ();
452
+
453
+ nspoid = get_namespace_oid ("public" , false);
454
+ relid = get_relname_relid (JSONBC_DICTIONARY_REL , nspoid );
445
455
if (relid == InvalidOid )
446
456
{
447
457
if (SPI_connect () != SPI_OK_CONNECT )
448
458
elog (ERROR , "SPI_connect failed" );
449
459
450
- if (SPI_exec (sql_dictionary , 0 ) != SPI_OK_UTILITY )
460
+ if (SPI_execute (sql_dictionary , false , 0 ) != SPI_OK_UTILITY )
451
461
elog (ERROR , "could not create \"jsonbc\" dictionary" );
452
462
453
463
SPI_finish ();
464
+ CommandCounterIncrement ();
465
+
466
+ finish_xact_command ();
467
+ start_xact_command ();
454
468
455
469
/* get just created table Oid */
456
- relid = get_relname_relid (JSONBC_DICTIONARY_REL , InvalidOid );
470
+ relid = get_relname_relid (JSONBC_DICTIONARY_REL , nspoid );
457
471
jsonbc_id_indoid = InvalidOid ;
458
472
jsonbc_keys_indoid = InvalidOid ;
459
473
}
@@ -487,6 +501,8 @@ jsonbc_get_dictionary_relid(void)
487
501
relation_close (rel , NoLock );
488
502
}
489
503
504
+ finish_xact_command ();
505
+
490
506
/* check we did fill global variables */
491
507
Assert (OidIsValid (jsonbc_id_indoid ));
492
508
Assert (OidIsValid (jsonbc_keys_indoid ));
0 commit comments