| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* tsvector.c |
| 4 |
* I/O functions for tsvector |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* |
| 8 |
* |
| 9 |
* IDENTIFICATION |
| 10 |
* src/backend/utils/adt/tsvector.c |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "postgres.h" |
| 16 |
|
| 17 |
#include "common/int.h" |
| 18 |
#include "libpq/pqformat.h" |
| 19 |
#include "nodes/miscnodes.h" |
| 20 |
#include "tsearch/ts_locale.h" |
| 21 |
#include "tsearch/ts_utils.h" |
| 22 |
#include "utils/fmgrprotos.h" |
| 23 |
#include "utils/memutils.h" |
| 24 |
#include "varatt.h" |
| 25 |
|
| 26 |
typedef struct |
| 27 |
{ |
| 28 |
WordEntry entry; /* must be first, see compareentry */ |
| 29 |
WordEntryPos *pos; |
| 30 |
int poslen; /* number of elements in pos */ |
| 31 |
} WordEntryIN; |
| 32 |
|
| 33 |
|
| 34 |
/* Compare two WordEntryPos values for qsort */ |
| 35 |
int |
| 36 |
compareWordEntryPos(const void *a, const void *b) |
| 37 |
{ |
| 38 |
int apos = WEP_GETPOS(*(const WordEntryPos *) a); |
| 39 |
int bpos = WEP_GETPOS(*(const WordEntryPos *) b); |
| 40 |
|
| 41 |
return pg_cmp_s32(apos, bpos); |
| 42 |
} |
| 43 |
|
| 44 |
/* |
| 45 |
* Removes duplicate pos entries. If there's two entries with same pos but |
| 46 |
* different weight, the higher weight is retained, so we can't use |
| 47 |
* qunique here. |
| 48 |
* |
| 49 |
* Returns new length. |
| 50 |
*/ |
| 51 |
static int |
| 52 |
uniquePos(WordEntryPos *a, int l) |
| 53 |
{ |
| 54 |
WordEntryPos *ptr, |
| 55 |
*res; |
| 56 |
|
| 57 |
if (l <= 1) |
| 58 |
return l; |
| 59 |
|
| 60 |
qsort(a, l, sizeof(WordEntryPos), compareWordEntryPos); |
| 61 |
|
| 62 |
res = a; |
| 63 |
ptr = a + 1; |
| 64 |
while (ptr - a < l) |
| 65 |
{ |
| 66 |
if (WEP_GETPOS(*ptr) != WEP_GETPOS(*res)) |
| 67 |
{ |
| 68 |
res++; |
| 69 |
*res = *ptr; |
| 70 |
if (res - a >= MAXNUMPOS - 1 || |
| 71 |
WEP_GETPOS(*res) == MAXENTRYPOS - 1) |
| 72 |
break; |
| 73 |
} |
| 74 |
else if (WEP_GETWEIGHT(*ptr) > WEP_GETWEIGHT(*res)) |
| 75 |
WEP_SETWEIGHT(*res, WEP_GETWEIGHT(*ptr)); |
| 76 |
ptr++; |
| 77 |
} |
| 78 |
|
| 79 |
return res + 1 - a; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* Compare two WordEntry structs for qsort_arg. This can also be used on |
| 84 |
* WordEntryIN structs, since those have WordEntry as their first field. |
| 85 |
*/ |
| 86 |
static int |
| 87 |
compareentry(const void *va, const void *vb, void *arg) |
| 88 |
{ |
| 89 |
const WordEntry *a = (const WordEntry *) va; |
| 90 |
const WordEntry *b = (const WordEntry *) vb; |
| 91 |
char *BufferStr = (char *) arg; |
| 92 |
|
| 93 |
return tsCompareString(&BufferStr[a->pos], a->len, |
| 94 |
&BufferStr[b->pos], b->len, |
| 95 |
false); |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* Sort an array of WordEntryIN, remove duplicates. |
| 100 |
* *outbuflen receives the amount of space needed for strings and positions. |
| 101 |
*/ |
| 102 |
static int |
| 103 |
uniqueentry(WordEntryIN *a, int l, char *buf, int *outbuflen) |
| 104 |
{ |
| 105 |
int buflen; |
| 106 |
WordEntryIN *ptr, |
| 107 |
*res; |
| 108 |
|
| 109 |
Assert(l >= 1); |
| 110 |
|
| 111 |
if (l > 1) |
| 112 |
qsort_arg(a, l, sizeof(WordEntryIN), compareentry, buf); |
| 113 |
|
| 114 |
buflen = 0; |
| 115 |
res = a; |
| 116 |
ptr = a + 1; |
| 117 |
while (ptr - a < l) |
| 118 |
{ |
| 119 |
if (!(ptr->entry.len == res->entry.len && |
| 120 |
strncmp(&buf[ptr->entry.pos], &buf[res->entry.pos], |
| 121 |
res->entry.len) == 0)) |
| 122 |
{ |
| 123 |
/* done accumulating data into *res, count space needed */ |
| 124 |
buflen += res->entry.len; |
| 125 |
if (res->entry.haspos) |
| 126 |
{ |
| 127 |
res->poslen = uniquePos(res->pos, res->poslen); |
| 128 |
buflen = SHORTALIGN(buflen); |
| 129 |
buflen += res->poslen * sizeof(WordEntryPos) + sizeof(uint16); |
| 130 |
} |
| 131 |
res++; |
| 132 |
if (res != ptr) |
| 133 |
memcpy(res, ptr, sizeof(WordEntryIN)); |
| 134 |
} |
| 135 |
else if (ptr->entry.haspos) |
| 136 |
{ |
| 137 |
if (res->entry.haspos) |
| 138 |
{ |
| 139 |
/* append ptr's positions to res's positions */ |
| 140 |
int newlen = ptr->poslen + res->poslen; |
| 141 |
|
| 142 |
res->pos = (WordEntryPos *) |
| 143 |
repalloc(res->pos, newlen * sizeof(WordEntryPos)); |
| 144 |
memcpy(&res->pos[res->poslen], ptr->pos, |
| 145 |
ptr->poslen * sizeof(WordEntryPos)); |
| 146 |
res->poslen = newlen; |
| 147 |
pfree(ptr->pos); |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
/* just give ptr's positions to pos */ |
| 152 |
res->entry.haspos = 1; |
| 153 |
res->pos = ptr->pos; |
| 154 |
res->poslen = ptr->poslen; |
| 155 |
} |
| 156 |
} |
| 157 |
ptr++; |
| 158 |
} |
| 159 |
|
| 160 |
/* count space needed for last item */ |
| 161 |
buflen += res->entry.len; |
| 162 |
if (res->entry.haspos) |
| 163 |
{ |
| 164 |
res->poslen = uniquePos(res->pos, res->poslen); |
| 165 |
buflen = SHORTALIGN(buflen); |
| 166 |
buflen += res->poslen * sizeof(WordEntryPos) + sizeof(uint16); |
| 167 |
} |
| 168 |
|
| 169 |
*outbuflen = buflen; |
| 170 |
return res + 1 - a; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
Datum |
| 175 |
tsvectorin(PG_FUNCTION_ARGS) |
| 176 |
{ |
| 177 |
char *buf = PG_GETARG_CSTRING(0); |
| 178 |
Node *escontext = fcinfo->context; |
| 179 |
TSVectorParseState state; |
| 180 |
WordEntryIN *arr; |
| 181 |
int totallen; |
| 182 |
int arrlen; /* allocated size of arr */ |
| 183 |
WordEntry *inarr; |
| 184 |
int len = 0; |
| 185 |
TSVector in; |
| 186 |
int i; |
| 187 |
char *token; |
| 188 |
int toklen; |
| 189 |
WordEntryPos *pos; |
| 190 |
int poslen; |
| 191 |
char *strbuf; |
| 192 |
int stroff; |
| 193 |
|
| 194 |
/* |
| 195 |
* Tokens are appended to tmpbuf, cur is a pointer to the end of used |
| 196 |
* space in tmpbuf. |
| 197 |
*/ |
| 198 |
char *tmpbuf; |
| 199 |
char *cur; |
| 200 |
int buflen = 256; /* allocated size of tmpbuf */ |
| 201 |
|
| 202 |
state = init_tsvector_parser(buf, 0, escontext); |
| 203 |
|
| 204 |
arrlen = 64; |
| 205 |
arr = (WordEntryIN *) palloc(sizeof(WordEntryIN) * arrlen); |
| 206 |
cur = tmpbuf = (char *) palloc(buflen); |
| 207 |
|
| 208 |
while (gettoken_tsvector(state, &token, &toklen, &pos, &poslen, NULL)) |
| 209 |
{ |
| 210 |
if (toklen >= MAXSTRLEN) |
| 211 |
ereturn(escontext, (Datum) 0, |
| 212 |
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 213 |
errmsg("word is too long (%ld bytes, max %ld bytes)", |
| 214 |
(long) toklen, |
| 215 |
(long) (MAXSTRLEN - 1)))); |
| 216 |
|
| 217 |
if (cur - tmpbuf > MAXSTRPOS) |
| 218 |
ereturn(escontext, (Datum) 0, |
| 219 |
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 220 |
errmsg("string is too long for tsvector (%zu bytes, max %zu bytes)", |
| 221 |
(size_t) (cur - tmpbuf), (size_t) MAXSTRPOS))); |
| 222 |
|
| 223 |
/* |
| 224 |
* Enlarge buffers if needed |
| 225 |
*/ |
| 226 |
if (len >= arrlen) |
| 227 |
{ |
| 228 |
arrlen *= 2; |
| 229 |
arr = (WordEntryIN *) |
| 230 |
repalloc(arr, sizeof(WordEntryIN) * arrlen); |
| 231 |
} |
| 232 |
while ((cur - tmpbuf) + toklen >= buflen) |
| 233 |
{ |
| 234 |
int dist = cur - tmpbuf; |
| 235 |
|
| 236 |
buflen *= 2; |
| 237 |
tmpbuf = (char *) repalloc(tmpbuf, buflen); |
| 238 |
cur = tmpbuf + dist; |
| 239 |
} |
| 240 |
arr[len].entry.len = toklen; |
| 241 |
arr[len].entry.pos = cur - tmpbuf; |
| 242 |
memcpy(cur, token, toklen); |
| 243 |
cur += toklen; |
| 244 |
|
| 245 |
if (poslen != 0) |
| 246 |
{ |
| 247 |
arr[len].entry.haspos = 1; |
| 248 |
arr[len].pos = pos; |
| 249 |
arr[len].poslen = poslen; |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
arr[len].entry.haspos = 0; |
| 254 |
arr[len].pos = NULL; |
| 255 |
arr[len].poslen = 0; |
| 256 |
} |
| 257 |
len++; |
| 258 |
} |
| 259 |
|
| 260 |
close_tsvector_parser(state); |
| 261 |
|
| 262 |
/* Did gettoken_tsvector fail? */ |
| 263 |
if (SOFT_ERROR_OCCURRED(escontext)) |
| 264 |
PG_RETURN_NULL(); |
| 265 |
|
| 266 |
if (len > 0) |
| 267 |
len = uniqueentry(arr, len, tmpbuf, &buflen); |
| 268 |
else |
| 269 |
buflen = 0; |
| 270 |
|
| 271 |
if (buflen > MAXSTRPOS) |
| 272 |
ereturn(escontext, (Datum) 0, |
| 273 |
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 274 |
errmsg("string is too long for tsvector (%zu bytes, max %zu bytes)", |
| 275 |
(size_t) buflen, (size_t) MAXSTRPOS))); |
| 276 |
|
| 277 |
totallen = CALCDATASIZE(len, buflen); |
| 278 |
in = (TSVector) palloc0(totallen); |
| 279 |
SET_VARSIZE(in, totallen); |
| 280 |
in->size = len; |
| 281 |
inarr = ARRPTR(in); |
| 282 |
strbuf = STRPTR(in); |
| 283 |
stroff = 0; |
| 284 |
for (i = 0; i < len; i++) |
| 285 |
{ |
| 286 |
memcpy(strbuf + stroff, &tmpbuf[arr[i].entry.pos], arr[i].entry.len); |
| 287 |
arr[i].entry.pos = stroff; |
| 288 |
stroff += arr[i].entry.len; |
| 289 |
if (arr[i].entry.haspos) |
| 290 |
{ |
| 291 |
/* This should be unreachable because of MAXNUMPOS restrictions */ |
| 292 |
if (arr[i].poslen > 0xFFFF) |
| 293 |
elog(ERROR, "positions array too long"); |
| 294 |
|
| 295 |
/* Copy number of positions */ |
| 296 |
stroff = SHORTALIGN(stroff); |
| 297 |
*(uint16 *) (strbuf + stroff) = (uint16) arr[i].poslen; |
| 298 |
stroff += sizeof(uint16); |
| 299 |
|
| 300 |
/* Copy positions */ |
| 301 |
memcpy(strbuf + stroff, arr[i].pos, arr[i].poslen * sizeof(WordEntryPos)); |
| 302 |
stroff += arr[i].poslen * sizeof(WordEntryPos); |
| 303 |
|
| 304 |
pfree(arr[i].pos); |
| 305 |
} |
| 306 |
inarr[i] = arr[i].entry; |
| 307 |
} |
| 308 |
|
| 309 |
Assert((strbuf + stroff - (char *) in) == totallen); |
| 310 |
|
| 311 |
PG_RETURN_TSVECTOR(in); |
| 312 |
} |
| 313 |
|
| 314 |
Datum |
| 315 |
tsvectorout(PG_FUNCTION_ARGS) |
| 316 |
{ |
| 317 |
TSVector out = PG_GETARG_TSVECTOR(0); |
| 318 |
char *outbuf; |
| 319 |
int32 i, |
| 320 |
pp; |
| 321 |
size_t lenbuf; |
| 322 |
WordEntry *ptr = ARRPTR(out); |
| 323 |
char *curin, |
| 324 |
*curout; |
| 325 |
const char *curend; |
| 326 |
|
| 327 |
lenbuf = out->size * 2 /* '' */ + out->size - 1 /* space */ + 2 /* \0 */ ; |
| 328 |
for (i = 0; i < out->size; i++) |
| 329 |
{ |
| 330 |
lenbuf += ptr[i].len * 2 /* allow for escapes */ ; |
| 331 |
if (ptr[i].haspos) |
| 332 |
lenbuf += 1 /* : */ + 7 /* int2 + , + weight */ * POSDATALEN(out, &(ptr[i])); |
| 333 |
} |
| 334 |
|
| 335 |
curout = outbuf = (char *) palloc(lenbuf); |
| 336 |
for (i = 0; i < out->size; i++) |
| 337 |
{ |
| 338 |
curin = STRPTR(out) + ptr->pos; |
| 339 |
curend = curin + ptr->len; |
| 340 |
if (i != 0) |
| 341 |
*curout++ = ' '; |
| 342 |
*curout++ = '\''; |
| 343 |
while (curin < curend) |
| 344 |
{ |
| 345 |
int len = pg_mblen_range(curin, curend); |
| 346 |
|
| 347 |
if (t_iseq(curin, '\'')) |
| 348 |
*curout++ = '\''; |
| 349 |
else if (t_iseq(curin, '\\')) |
| 350 |
*curout++ = '\\'; |
| 351 |
|
| 352 |
while (len--) |
| 353 |
*curout++ = *curin++; |
| 354 |
} |
| 355 |
|
| 356 |
*curout++ = '\''; |
| 357 |
if ((pp = POSDATALEN(out, ptr)) != 0) |
| 358 |
{ |
| 359 |
WordEntryPos *wptr; |
| 360 |
|
| 361 |
*curout++ = ':'; |
| 362 |
wptr = POSDATAPTR(out, ptr); |
| 363 |
while (pp) |
| 364 |
{ |
| 365 |
curout += sprintf(curout, "%d", WEP_GETPOS(*wptr)); |
| 366 |
switch (WEP_GETWEIGHT(*wptr)) |
| 367 |
{ |
| 368 |
case 3: |
| 369 |
*curout++ = 'A'; |
| 370 |
break; |
| 371 |
case 2: |
| 372 |
*curout++ = 'B'; |
| 373 |
break; |
| 374 |
case 1: |
| 375 |
*curout++ = 'C'; |
| 376 |
break; |
| 377 |
case 0: |
| 378 |
default: |
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
if (pp > 1) |
| 383 |
*curout++ = ','; |
| 384 |
pp--; |
| 385 |
wptr++; |
| 386 |
} |
| 387 |
} |
| 388 |
ptr++; |
| 389 |
} |
| 390 |
|
| 391 |
*curout = '\0'; |
| 392 |
PG_FREE_IF_COPY(out, 0); |
| 393 |
PG_RETURN_CSTRING(outbuf); |
| 394 |
} |
| 395 |
|
| 396 |
/* |
| 397 |
* Binary Input / Output functions. The binary format is as follows: |
| 398 |
* |
| 399 |
* uint32 number of lexemes |
| 400 |
* |
| 401 |
* for each lexeme: |
| 402 |
* lexeme text in client encoding, null-terminated |
| 403 |
* uint16 number of positions |
| 404 |
* for each position: |
| 405 |
* uint16 WordEntryPos |
| 406 |
*/ |
| 407 |
|
| 408 |
Datum |
| 409 |
tsvectorsend(PG_FUNCTION_ARGS) |
| 410 |
{ |
| 411 |
TSVector vec = PG_GETARG_TSVECTOR(0); |
| 412 |
StringInfoData buf; |
| 413 |
int i, |
| 414 |
j; |
| 415 |
WordEntry *weptr = ARRPTR(vec); |
| 416 |
|
| 417 |
pq_begintypsend(&buf); |
| 418 |
|
| 419 |
pq_sendint32(&buf, vec->size); |
| 420 |
for (i = 0; i < vec->size; i++) |
| 421 |
{ |
| 422 |
uint16 npos; |
| 423 |
|
| 424 |
/* |
| 425 |
* the strings in the TSVector array are not null-terminated, so we |
| 426 |
* have to send the null-terminator separately |
| 427 |
*/ |
| 428 |
pq_sendtext(&buf, STRPTR(vec) + weptr->pos, weptr->len); |
| 429 |
pq_sendbyte(&buf, '\0'); |
| 430 |
|
| 431 |
npos = POSDATALEN(vec, weptr); |
| 432 |
pq_sendint16(&buf, npos); |
| 433 |
|
| 434 |
if (npos > 0) |
| 435 |
{ |
| 436 |
WordEntryPos *wepptr = POSDATAPTR(vec, weptr); |
| 437 |
|
| 438 |
for (j = 0; j < npos; j++) |
| 439 |
pq_sendint16(&buf, wepptr[j]); |
| 440 |
} |
| 441 |
weptr++; |
| 442 |
} |
| 443 |
|
| 444 |
PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); |
| 445 |
} |
| 446 |
|
| 447 |
Datum |
| 448 |
tsvectorrecv(PG_FUNCTION_ARGS) |
| 449 |
{ |
| 450 |
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); |
| 451 |
TSVector vec; |
| 452 |
int i; |
| 453 |
int32 nentries; |
| 454 |
int datalen; /* number of bytes used in the variable size |
| 455 |
* area after fixed size TSVector header and |
| 456 |
* WordEntries */ |
| 457 |
Size hdrlen; |
| 458 |
Size len; /* allocated size of vec */ |
| 459 |
bool needSort = false; |
| 460 |
|
| 461 |
nentries = pq_getmsgint(buf, sizeof(int32)); |
| 462 |
|
| 463 |
/* We disallow empty lexemes, so more than MAXSTRPOS of them can't fit */ |
| 464 |
if (nentries < 0 || nentries > MAXSTRPOS) |
| 465 |
elog(ERROR, "invalid size of tsvector"); |
| 466 |
|
| 467 |
hdrlen = DATAHDRSIZE + sizeof(WordEntry) * nentries; |
| 468 |
|
| 469 |
len = hdrlen * 2; /* times two to make some room for lexemes */ |
| 470 |
vec = (TSVector) palloc0(len); |
| 471 |
vec->size = nentries; |
| 472 |
|
| 473 |
datalen = 0; |
| 474 |
for (i = 0; i < nentries; i++) |
| 475 |
{ |
| 476 |
const char *lexeme; |
| 477 |
uint16 npos; |
| 478 |
size_t lex_len; |
| 479 |
|
| 480 |
lexeme = pq_getmsgstring(buf); |
| 481 |
npos = (uint16) pq_getmsgint(buf, sizeof(uint16)); |
| 482 |
|
| 483 |
/* sanity checks */ |
| 484 |
|
| 485 |
lex_len = strlen(lexeme); |
| 486 |
if (lex_len == 0) |
| 487 |
elog(ERROR, "invalid tsvector: empty lexeme"); |
| 488 |
if (lex_len > MAXSTRLEN) |
| 489 |
elog(ERROR, "invalid tsvector: lexeme too long"); |
| 490 |
|
| 491 |
if (datalen > MAXSTRPOS) |
| 492 |
elog(ERROR, "invalid tsvector: maximum total lexeme length exceeded"); |
| 493 |
|
| 494 |
if (npos > MAXNUMPOS) |
| 495 |
elog(ERROR, "unexpected number of tsvector positions"); |
| 496 |
|
| 497 |
/* |
| 498 |
* Looks valid. Fill the WordEntry struct, and copy lexeme. |
| 499 |
* |
| 500 |
* But make sure the buffer is large enough first. |
| 501 |
*/ |
| 502 |
while (hdrlen + SHORTALIGN(datalen + lex_len) + |
| 503 |
sizeof(uint16) + npos * sizeof(WordEntryPos) >= len) |
| 504 |
{ |
| 505 |
len *= 2; |
| 506 |
vec = (TSVector) repalloc(vec, len); |
| 507 |
} |
| 508 |
|
| 509 |
vec->entries[i].haspos = (npos > 0) ? 1 : 0; |
| 510 |
vec->entries[i].len = lex_len; |
| 511 |
vec->entries[i].pos = datalen; |
| 512 |
|
| 513 |
memcpy(STRPTR(vec) + datalen, lexeme, lex_len); |
| 514 |
|
| 515 |
datalen += lex_len; |
| 516 |
|
| 517 |
if (i > 0 && compareentry(&vec->entries[i], |
| 518 |
&vec->entries[i - 1], |
| 519 |
STRPTR(vec)) <= 0) |
| 520 |
needSort = true; |
| 521 |
|
| 522 |
/* Receive positions */ |
| 523 |
if (npos > 0) |
| 524 |
{ |
| 525 |
uint16 j; |
| 526 |
WordEntryPos *wepptr; |
| 527 |
|
| 528 |
/* |
| 529 |
* Pad to 2-byte alignment if necessary. Though we used palloc0 |
| 530 |
* for the initial allocation, subsequent repalloc'd memory areas |
| 531 |
* are not initialized to zero. |
| 532 |
*/ |
| 533 |
if (datalen != SHORTALIGN(datalen)) |
| 534 |
{ |
| 535 |
*(STRPTR(vec) + datalen) = '\0'; |
| 536 |
datalen = SHORTALIGN(datalen); |
| 537 |
} |
| 538 |
|
| 539 |
memcpy(STRPTR(vec) + datalen, &npos, sizeof(uint16)); |
| 540 |
|
| 541 |
wepptr = POSDATAPTR(vec, &vec->entries[i]); |
| 542 |
for (j = 0; j < npos; j++) |
| 543 |
{ |
| 544 |
wepptr[j] = (WordEntryPos) pq_getmsgint(buf, sizeof(WordEntryPos)); |
| 545 |
if (j > 0 && WEP_GETPOS(wepptr[j]) <= WEP_GETPOS(wepptr[j - 1])) |
| 546 |
elog(ERROR, "position information is misordered"); |
| 547 |
} |
| 548 |
|
| 549 |
datalen += sizeof(uint16) + npos * sizeof(WordEntryPos); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
/* |
| 554 |
* Enforce that datalen is still within MAXSTRPOS, ie the last lexeme |
| 555 |
* didn't go past that. We could allow that, since no "pos" field |
| 556 |
* overflowed, but tsvectorrecv shouldn't accept values that other |
| 557 |
* tsvector-constructing routines wouldn't. |
| 558 |
*/ |
| 559 |
if (datalen > MAXSTRPOS) |
| 560 |
elog(ERROR, "invalid tsvector: maximum total lexeme length exceeded"); |
| 561 |
|
| 562 |
SET_VARSIZE(vec, hdrlen + datalen); |
| 563 |
|
| 564 |
if (needSort) |
| 565 |
qsort_arg(ARRPTR(vec), vec->size, sizeof(WordEntry), |
| 566 |
compareentry, STRPTR(vec)); |
| 567 |
|
| 568 |
PG_RETURN_TSVECTOR(vec); |
| 569 |
} |