Skip to content

Commit 553bc41

Browse files
committed
1 add namespaces as Tom suggest http://www.pgsql.ru/db/mw/msg.html?mid=1987703
2 remove select qeury in inserts
1 parent 7cb55d2 commit 553bc41

File tree

11 files changed

+233
-113
lines changed

11 files changed

+233
-113
lines changed

contrib/tsearch2/common.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
#include "postgres.h"
2+
3+
#include "postgres.h"
4+
#include "fmgr.h"
5+
#include "catalog/pg_proc.h"
6+
#include "catalog/pg_namespace.h"
7+
#include "utils/syscache.h"
8+
9+
#include "ts_cfg.h"
10+
#include "dict.h"
11+
#include "wparser.h"
12+
#include "snmap.h"
13+
#include "common.h"
14+
#include "tsvector.h"
15+
16+
17+
218
#include "common.h"
319
#include "wparser.h"
420
#include "ts_cfg.h"
521
#include "dict.h"
622

23+
24+
Oid TSNSP_FunctionOid = InvalidOid;
25+
26+
727
text *
828
char2text(char *in)
929
{
@@ -100,3 +120,45 @@ text_cmp(text *a, text *b)
100120
return (int) VARSIZE(a) - (int) VARSIZE(b);
101121

102122
}
123+
124+
char*
125+
get_namespace(Oid funcoid) {
126+
HeapTuple tuple;
127+
Form_pg_proc proc;
128+
Form_pg_namespace nsp;
129+
Oid nspoid;
130+
char *txt;
131+
132+
tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
133+
if (!HeapTupleIsValid(tuple))
134+
elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
135+
proc=(Form_pg_proc) GETSTRUCT(tuple);
136+
nspoid = proc->pronamespace;
137+
ReleaseSysCache(tuple);
138+
139+
tuple = SearchSysCache(NAMESPACEOID, ObjectIdGetDatum(nspoid), 0, 0, 0);
140+
if (!HeapTupleIsValid(tuple))
141+
elog(ERROR, "cache lookup failed for namespace oid %u", nspoid);
142+
nsp = (Form_pg_namespace) GETSTRUCT(tuple);
143+
txt = pstrdup( NameStr((nsp->nspname)) );
144+
ReleaseSysCache(tuple);
145+
146+
return txt;
147+
}
148+
149+
Oid
150+
get_oidnamespace(Oid funcoid) {
151+
HeapTuple tuple;
152+
Form_pg_proc proc;
153+
Oid nspoid;
154+
155+
tuple = SearchSysCache(PROCOID, ObjectIdGetDatum(funcoid), 0, 0, 0);
156+
if (!HeapTupleIsValid(tuple))
157+
elog(ERROR, "cache lookup failed for proc oid %u", funcoid);
158+
proc=(Form_pg_proc) GETSTRUCT(tuple);
159+
nspoid = proc->pronamespace;
160+
ReleaseSysCache(tuple);
161+
162+
return nspoid;
163+
}
164+

contrib/tsearch2/common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ int text_cmp(text *a, text *b);
2121

2222
void ts_error(int state, const char *format,...);
2323

24+
extern Oid TSNSP_FunctionOid; /* oid of called function, needed only for determ namespace, no more */
25+
char* get_namespace(Oid funcoid);
26+
Oid get_oidnamespace(Oid funcoid);
27+
28+
#define SET_FUNCOID() do { \
29+
if ( fcinfo->flinfo && fcinfo->flinfo->fn_oid != InvalidOid ) \
30+
TSNSP_FunctionOid = fcinfo->flinfo->fn_oid; \
31+
} while(0)
32+
2433
#endif

contrib/tsearch2/dict.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@
1919

2020
/*********top interface**********/
2121

22-
static void *plan_getdict = NULL;
23-
2422
void
2523
init_dict(Oid id, DictInfo * dict)
2624
{
2725
Oid arg[1];
2826
bool isnull;
2927
Datum pars[1];
3028
int stat;
29+
void *plan;
30+
char buf[1024];
31+
char *nsp = get_namespace(TSNSP_FunctionOid);
3132

3233
arg[0] = OIDOID;
3334
pars[0] = ObjectIdGetDatum(id);
3435

3536
memset(dict, 0, sizeof(DictInfo));
3637
SPI_connect();
37-
if (!plan_getdict)
38-
{
39-
plan_getdict = SPI_saveplan(SPI_prepare("select dict_init, dict_initoption, dict_lexize from pg_ts_dict where oid = $1", 1, arg));
40-
if (!plan_getdict)
41-
ts_error(ERROR, "SPI_prepare() failed");
42-
}
38+
sprintf(buf,"select dict_init, dict_initoption, dict_lexize from %s.pg_ts_dict where oid = $1", nsp);
39+
pfree(nsp);
40+
plan= SPI_prepare(buf, 1, arg);
41+
if (!plan)
42+
ts_error(ERROR, "SPI_prepare() failed");
4343

44-
stat = SPI_execp(plan_getdict, pars, " ", 1);
44+
stat = SPI_execp(plan, pars, " ", 1);
4545
if (stat < 0)
4646
ts_error(ERROR, "SPI_execp return %d", stat);
4747
if (SPI_processed > 0)
@@ -63,6 +63,7 @@ init_dict(Oid id, DictInfo * dict)
6363
}
6464
else
6565
ts_error(ERROR, "No dictionary with id %d", id);
66+
SPI_freeplan(plan);
6667
SPI_finish();
6768
}
6869

@@ -133,8 +134,6 @@ finddict(Oid id)
133134
return finddict(id); /* qsort changed order!! */ ;
134135
}
135136

136-
static void *plan_name2id = NULL;
137-
138137
Oid
139138
name2id_dict(text *name)
140139
{
@@ -143,28 +142,31 @@ name2id_dict(text *name)
143142
Datum pars[1];
144143
int stat;
145144
Oid id = findSNMap_t(&(DList.name2id_map), name);
145+
void *plan;
146+
char buf[1024], *nsp;
146147

147148
arg[0] = TEXTOID;
148149
pars[0] = PointerGetDatum(name);
149150

150151
if (id)
151152
return id;
152153

154+
nsp = get_namespace(TSNSP_FunctionOid);
153155
SPI_connect();
154-
if (!plan_name2id)
155-
{
156-
plan_name2id = SPI_saveplan(SPI_prepare("select oid from pg_ts_dict where dict_name = $1", 1, arg));
157-
if (!plan_name2id)
158-
ts_error(ERROR, "SPI_prepare() failed");
159-
}
156+
sprintf(buf,"select oid from %s.pg_ts_dict where dict_name = $1", nsp);
157+
pfree(nsp);
158+
plan= SPI_prepare(buf, 1, arg);
159+
if (!plan)
160+
ts_error(ERROR, "SPI_prepare() failed");
160161

161-
stat = SPI_execp(plan_name2id, pars, " ", 1);
162+
stat = SPI_execp(plan, pars, " ", 1);
162163
if (stat < 0)
163164
ts_error(ERROR, "SPI_execp return %d", stat);
164165
if (SPI_processed > 0)
165166
id = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull));
166167
else
167168
ts_error(ERROR, "No dictionary with name '%s'", text2char(name));
169+
SPI_freeplan(plan);
168170
SPI_finish();
169171
addSNMap_t(&(DList.name2id_map), name, id);
170172
return id;
@@ -179,12 +181,14 @@ Datum
179181
lexize(PG_FUNCTION_ARGS)
180182
{
181183
text *in = PG_GETARG_TEXT_P(1);
182-
DictInfo *dict = finddict(PG_GETARG_OID(0));
184+
DictInfo *dict;
183185
char **res,
184186
**ptr;
185187
Datum *da;
186188
ArrayType *a;
187189

190+
SET_FUNCOID();
191+
dict = finddict(PG_GETARG_OID(0));
188192

189193
ptr = res = (char **) DatumGetPointer(
190194
FunctionCall3(&(dict->lexize_info),
@@ -241,8 +245,8 @@ lexize_byname(PG_FUNCTION_ARGS)
241245
{
242246
text *dictname = PG_GETARG_TEXT_P(0);
243247
Datum res;
248+
SET_FUNCOID();
244249

245-
strdup("simple");
246250
res = DirectFunctionCall3(
247251
lexize,
248252
ObjectIdGetDatum(name2id_dict(dictname)),
@@ -263,6 +267,7 @@ Datum set_curdict(PG_FUNCTION_ARGS);
263267
Datum
264268
set_curdict(PG_FUNCTION_ARGS)
265269
{
270+
SET_FUNCOID();
266271
finddict(PG_GETARG_OID(0));
267272
currect_dictionary_id = PG_GETARG_OID(0);
268273
PG_RETURN_VOID();
@@ -274,7 +279,7 @@ Datum
274279
set_curdict_byname(PG_FUNCTION_ARGS)
275280
{
276281
text *dictname = PG_GETARG_TEXT_P(0);
277-
282+
SET_FUNCOID();
278283
DirectFunctionCall1(
279284
set_curdict,
280285
ObjectIdGetDatum(name2id_dict(dictname))
@@ -289,7 +294,7 @@ Datum
289294
lexize_bycurrent(PG_FUNCTION_ARGS)
290295
{
291296
Datum res;
292-
297+
SET_FUNCOID();
293298
if (currect_dictionary_id == 0)
294299
ereport(ERROR,
295300
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),

contrib/tsearch2/query.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ TS_execute(ITEM * curitem, void *checkval, bool calcnot, bool (*chkcond) (void *
469469
Datum
470470
rexectsq(PG_FUNCTION_ARGS)
471471
{
472+
SET_FUNCOID();
472473
return DirectFunctionCall2(
473474
exectsq,
474475
PG_GETARG_DATUM(1),
@@ -483,7 +484,7 @@ exectsq(PG_FUNCTION_ARGS)
483484
QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
484485
CHKVAL chkval;
485486
bool result;
486-
487+
SET_FUNCOID();
487488
if (!val->size || !query->size)
488489
{
489490
PG_FREE_IF_COPY(val, 0);
@@ -638,6 +639,7 @@ static QUERYTYPE *
638639
Datum
639640
tsquery_in(PG_FUNCTION_ARGS)
640641
{
642+
SET_FUNCOID();
641643
PG_RETURN_POINTER(queryin((char *) PG_GETARG_POINTER(0), pushval_asis, 0));
642644
}
643645

@@ -863,6 +865,7 @@ to_tsquery(PG_FUNCTION_ARGS)
863865
QUERYTYPE *query;
864866
ITEM *res;
865867
int4 len;
868+
SET_FUNCOID();
866869

867870
str = text2char(in);
868871
PG_FREE_IF_COPY(in, 1);
@@ -884,7 +887,9 @@ Datum
884887
to_tsquery_name(PG_FUNCTION_ARGS)
885888
{
886889
text *name = PG_GETARG_TEXT_P(0);
887-
Datum res = DirectFunctionCall2(to_tsquery,
890+
Datum res;
891+
SET_FUNCOID();
892+
res = DirectFunctionCall2(to_tsquery,
888893
Int32GetDatum(name2id_cfg(name)),
889894
PG_GETARG_DATUM(1));
890895

@@ -895,6 +900,7 @@ to_tsquery_name(PG_FUNCTION_ARGS)
895900
Datum
896901
to_tsquery_current(PG_FUNCTION_ARGS)
897902
{
903+
SET_FUNCOID();
898904
PG_RETURN_DATUM(DirectFunctionCall2(to_tsquery,
899905
Int32GetDatum(get_currcfg()),
900906
PG_GETARG_DATUM(0)));

contrib/tsearch2/snmap.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
static int
1414
compareSNMapEntry(const void *a, const void *b)
1515
{
16-
return strcmp(((SNMapEntry *) a)->key, ((SNMapEntry *) b)->key);
16+
if ( ((SNMapEntry *) a)->nsp < ((SNMapEntry *) b)->nsp )
17+
return -1;
18+
else if ( ((SNMapEntry *) a)->nsp > ((SNMapEntry *) b)->nsp )
19+
return 1;
20+
else
21+
return strcmp(((SNMapEntry *) a)->key, ((SNMapEntry *) b)->key);
1722
}
1823

1924
void
@@ -37,6 +42,7 @@ addSNMap(SNMap * map, char *key, Oid value)
3742
ereport(ERROR,
3843
(errcode(ERRCODE_OUT_OF_MEMORY),
3944
errmsg("out of memory")));
45+
map->list[map->len].nsp = get_oidnamespace(TSNSP_FunctionOid);
4046
map->list[map->len].value = value;
4147
map->len++;
4248
if (map->len > 1)
@@ -59,6 +65,7 @@ findSNMap(SNMap * map, char *key)
5965
SNMapEntry ks;
6066

6167
ks.key = key;
68+
ks.nsp = get_oidnamespace(TSNSP_FunctionOid);
6269
ks.value = 0;
6370

6471
if (map->len == 0 || !map->list)

contrib/tsearch2/snmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ typedef struct
77
{
88
char *key;
99
Oid value;
10+
Oid nsp;
1011
} SNMapEntry;
1112

1213
typedef struct

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy