3333#include "utils/resowner.h"
3434#include "utils/snapmgr.h"
3535#include "utils/tqual.h"
36+ #include "utils/syscache.h"
3637
3738static bool xact_started = false;
3839static bool shutdown_requested = false;
@@ -48,24 +49,17 @@ Oid jsonbd_id_indoid = InvalidOid;
4849void jsonbd_worker_main (Datum arg );
4950void jsonbd_launcher_main (Datum arg );
5051static bool jsonbd_register_worker (int , Oid , int );
52+ static char * jsonbd_get_dictionary_name (Oid relid );
5153
5254#define JSONBD_DICTIONARY_REL "jsonbd_dictionary"
5355
54- static const char * sql_dictionary = \
55- "CREATE TABLE public." JSONBD_DICTIONARY_REL
56- " (cmopt OID NOT NULL,"
57- " id INT4 NOT NULL,"
58- " key TEXT NOT NULL);"
59- "CREATE UNIQUE INDEX jsonbd_dict_on_id ON " JSONBD_DICTIONARY_REL "(cmopt, id);"
60- "CREATE UNIQUE INDEX jsonbd_dict_on_key ON " JSONBD_DICTIONARY_REL " (cmopt, key);" ;
61-
6256static const char * sql_insert = \
63- "WITH t AS (SELECT (COALESCE(MAX(id), 0) + 1) new_id FROM "
64- JSONBD_DICTIONARY_REL " WHERE cmopt = %d) INSERT INTO " JSONBD_DICTIONARY_REL
57+ "WITH t AS (SELECT (COALESCE(MAX(id), 0) + 1) new_id FROM %s "
58+ " WHERE acoid = %d) INSERT INTO %s"
6559 " SELECT %d, t.new_id, '%s' FROM t RETURNING id" ;
6660
6761enum {
68- JSONBD_DICTIONARY_REL_ATT_CMOPT = 1 ,
62+ JSONBD_DICTIONARY_REL_ATT_ACOID = 1 ,
6963 JSONBD_DICTIONARY_REL_ATT_ID ,
7064 JSONBD_DICTIONARY_REL_ATT_KEY ,
7165 JSONBD_DICTIONARY_REL_ATT_COUNT
@@ -353,8 +347,17 @@ jsonbd_get_key_ids(Oid cmoptoid, char *buf, uint32 *idsbuf, int nkeys)
353347 Relation indrel ;
354348 int i ;
355349 Oid relid = jsonbd_get_dictionary_relid ();
356- bool spi_on = false;
350+ bool spi_on = false,
351+ failed = false;
357352 jsonbd_cached_cmopt * cmcache ;
353+ static char * relname = NULL ;
354+
355+ if (relname == NULL )
356+ {
357+ start_xact_command ();
358+ relname = jsonbd_get_dictionary_name (relid );
359+ finish_xact_command ();
360+ }
358361
359362 cmcache = get_cached_compression_options (cmoptoid );
360363
@@ -418,20 +421,27 @@ jsonbd_get_key_ids(Oid cmoptoid, char *buf, uint32 *idsbuf, int nkeys)
418421 /* still need to add */
419422 Datum datum ;
420423 bool isnull ;
421- char * sql2 = psprintf (sql_insert , cmoptoid , cmoptoid , buf );
424+ char * sql2 = psprintf (sql_insert , relname , cmoptoid ,
425+ relname , cmoptoid , buf );
422426
423427 /* TODO: maybe use bulk inserts instead of SPI */
424428 if (!spi_on )
425429 {
426430 /* lazy SPI initialization */
427431 if (SPI_connect () != SPI_OK_CONNECT )
428- elog (ERROR , "SPI_connect failed" );
432+ {
433+ failed = true;
434+ goto finish ;
435+ }
429436
430437 spi_on = true;
431438 }
432439
433440 if (SPI_exec (sql2 , 0 ) != SPI_OK_INSERT_RETURNING )
434- elog (ERROR , "SPI_exec failed" );
441+ {
442+ failed = true;
443+ goto finish ;
444+ }
435445
436446 pfree (sql2 );
437447
@@ -454,6 +464,7 @@ jsonbd_get_key_ids(Oid cmoptoid, char *buf, uint32 *idsbuf, int nkeys)
454464 while (* buf ++ != '\0' );
455465 }
456466
467+ finish :
457468 if (spi_on )
458469 SPI_finish ();
459470
@@ -463,6 +474,9 @@ jsonbd_get_key_ids(Oid cmoptoid, char *buf, uint32 *idsbuf, int nkeys)
463474 relation_close (rel , AccessShareLock );
464475 finish_xact_command ();
465476 }
477+
478+ if (failed )
479+ elog (ERROR , "get key ids error" );
466480}
467481
468482static char *
@@ -654,6 +668,9 @@ jsonbd_worker_main(Datum arg)
654668
655669 /* Initialize connection and local variables */
656670 seg = dsm_attach ((dsm_handle ) DatumGetInt32 (arg ));
671+ if (!seg )
672+ goto finish ;
673+
657674 init_worker (seg );
658675
659676 MemoryContextSwitchTo (worker_context );
@@ -746,6 +763,7 @@ jsonbd_worker_main(Datum arg)
746763 }
747764 }
748765
766+ finish :
749767 elog (LOG , "jsonbd dictionary worker has ended its work" );
750768 proc_exit (0 );
751769}
@@ -832,35 +850,16 @@ jsonbd_register_launcher(void)
832850Oid
833851jsonbd_get_dictionary_relid (void )
834852{
835- Oid relid ,
836- nspoid ;
853+ Oid relid ;
837854
838855 if (OidIsValid (jsonbd_dictionary_reloid ))
839856 return jsonbd_dictionary_reloid ;
840857
841858 start_xact_command ();
842859
843- nspoid = get_namespace_oid ("public" , false);
844- relid = get_relname_relid (JSONBD_DICTIONARY_REL , nspoid );
860+ relid = get_relname_relid (JSONBD_DICTIONARY_REL , get_jsonbd_schema ());
845861 if (relid == InvalidOid )
846- {
847- if (SPI_connect () != SPI_OK_CONNECT )
848- elog (ERROR , "SPI_connect failed" );
849-
850- if (SPI_execute (sql_dictionary , false, 0 ) != SPI_OK_UTILITY )
851- elog (ERROR , "could not create \"jsonbd\" dictionary" );
852-
853- SPI_finish ();
854- CommandCounterIncrement ();
855-
856- finish_xact_command ();
857- start_xact_command ();
858-
859- /* get just created table Oid */
860- relid = get_relname_relid (JSONBD_DICTIONARY_REL , nspoid );
861- jsonbd_id_indoid = InvalidOid ;
862- jsonbd_keys_indoid = InvalidOid ;
863- }
862+ elog (ERROR , "jsonbd dictionary relation does not exist" );
864863
865864 /* fill index Oids too */
866865 if (jsonbd_id_indoid == InvalidOid )
@@ -904,3 +903,33 @@ jsonbd_get_dictionary_relid(void)
904903 jsonbd_dictionary_reloid = relid ;
905904 return relid ;
906905}
906+
907+ static char *
908+ jsonbd_get_dictionary_name (Oid relid )
909+ {
910+ HeapTuple tp ;
911+ Form_pg_class reltup ;
912+ char * relname ;
913+ char * nspname ;
914+ char * result ;
915+ MemoryContext old_mcxt ;
916+
917+ tp = SearchSysCache1 (RELOID , ObjectIdGetDatum (relid ));
918+ if (!HeapTupleIsValid (tp ))
919+ elog (ERROR , "cache lookup failed for relation %u" , relid );
920+ reltup = (Form_pg_class ) GETSTRUCT (tp );
921+ relname = NameStr (reltup -> relname );
922+
923+ nspname = get_namespace_name (reltup -> relnamespace );
924+ if (!nspname )
925+ elog (ERROR , "cache lookup failed for namespace %u" ,
926+ reltup -> relnamespace );
927+
928+ old_mcxt = MemoryContextSwitchTo (TopMemoryContext );
929+ result = quote_qualified_identifier (nspname , relname );
930+ MemoryContextSwitchTo (old_mcxt );
931+
932+ ReleaseSysCache (tp );
933+
934+ return result ;
935+ }
0 commit comments