Skip to content

Commit 3e8c4c7

Browse files
committed
Improve warning messages from tsearch trigger function; clean up some
casting infelicities. Allow char(n) fields to be indexed. Per Bjoern Metzdorf.
1 parent 89caf56 commit 3e8c4c7

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

contrib/tsearch/txtidx.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ gettoken_txtidx(TI_IN_STATE * state)
207207
Datum
208208
txtidx_in(PG_FUNCTION_ARGS)
209209
{
210-
char *buf = (char *) PG_GETARG_POINTER(0);
210+
char *buf = PG_GETARG_CSTRING(0);
211211
TI_IN_STATE state;
212212
WordEntry *arr;
213213
int4 len = 0,
@@ -276,7 +276,7 @@ txtidx_in(PG_FUNCTION_ARGS)
276276
Datum
277277
txtidxsize(PG_FUNCTION_ARGS)
278278
{
279-
txtidx *in = (txtidx *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
279+
txtidx *in = (txtidx *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
280280
int4 ret = in->size;
281281

282282
PG_FREE_IF_COPY(in, 0);
@@ -286,7 +286,7 @@ txtidxsize(PG_FUNCTION_ARGS)
286286
Datum
287287
txtidx_out(PG_FUNCTION_ARGS)
288288
{
289-
txtidx *out = (txtidx *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
289+
txtidx *out = (txtidx *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
290290
char *outbuf;
291291
int4 i,
292292
j,
@@ -475,7 +475,7 @@ makevalue(PRSTEXT * prs)
475475
Datum
476476
txt2txtidx(PG_FUNCTION_ARGS)
477477
{
478-
text *in = (text *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
478+
text *in = PG_GETARG_TEXT_P(0);
479479
PRSTEXT prs;
480480
txtidx *out = NULL;
481481

@@ -511,7 +511,6 @@ tsearch(PG_FUNCTION_ARGS)
511511
PRSTEXT prs;
512512
Datum datum = (Datum) 0;
513513

514-
515514
if (!CALLED_AS_TRIGGER(fcinfo))
516515
elog(ERROR, "TSearch: Not fired by trigger manager");
517516

@@ -535,7 +534,7 @@ tsearch(PG_FUNCTION_ARGS)
535534
elog(ERROR, "TSearch: format tsearch(txtidx_field, text_field1,...)");
536535

537536
numidxattr = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
538-
if (numidxattr < 0)
537+
if (numidxattr == SPI_ERROR_NOATTRIBUTE)
539538
elog(ERROR, "TSearch: Can not find txtidx_field");
540539

541540
prs.lenwords = 32;
@@ -546,27 +545,35 @@ tsearch(PG_FUNCTION_ARGS)
546545
/* find all words in indexable column */
547546
for (i = 1; i < trigger->tgnargs; i++)
548547
{
549-
int4 numattr;
550-
text *txt_toasted,
551-
*txt;
552-
bool isnull;
548+
int numattr;
553549
Oid oidtype;
550+
Datum txt_datum;
551+
bool isnull;
552+
text *txt;
554553

555554
numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
555+
if (numattr == SPI_ERROR_NOATTRIBUTE)
556+
{
557+
elog(WARNING, "TSearch: can not find field '%s'",
558+
trigger->tgargs[i]);
559+
continue;
560+
}
556561
oidtype = SPI_gettypeid(rel->rd_att, numattr);
557-
if (numattr < 0 || (!(oidtype == TEXTOID || oidtype == VARCHAROID)))
562+
/* We assume char() and varchar() are binary-equivalent to text */
563+
if (!(oidtype == TEXTOID ||
564+
oidtype == VARCHAROID ||
565+
oidtype == BPCHAROID))
558566
{
559-
elog(WARNING, "TSearch: can not find field '%s'", trigger->tgargs[i]);
567+
elog(WARNING, "TSearch: '%s' is not of character type",
568+
trigger->tgargs[i]);
560569
continue;
561570
}
562-
txt_toasted = (text *) DatumGetPointer(SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull));
571+
txt_datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
563572
if (isnull)
564573
continue;
565-
txt = (text *) DatumGetPointer(PG_DETOAST_DATUM(PointerGetDatum(txt_toasted)));
574+
txt = DatumGetTextP(txt_datum);
566575

567576
parsetext(&prs, VARDATA(txt), VARSIZE(txt) - VARHDRSZ);
568-
if (txt != txt_toasted)
569-
pfree(txt);
570577
}
571578

572579
/* make txtidx value */

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