Skip to content

Commit b7a2ab5

Browse files
committed
Fix memory arrangement of tsquery after removing stop words. It causes
a unused memory holes in tsquery. Per report by Richard Huxton <dev@archonet.com>. It was working well because in fact tsquery->size is not used for any kind of operation except comparing tsqueries. To prevent requirement of renew all stored tsquery optimization in CompareTSQ is removed.
1 parent ffeae03 commit b7a2ab5

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

src/backend/tsearch/to_tsany.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/tsearch/to_tsany.c,v 1.8 2008/01/01 19:45:52 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/tsearch/to_tsany.c,v 1.8.2.1 2008/03/07 15:29:27 teodor Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -350,6 +350,18 @@ to_tsquery_byid(PG_FUNCTION_ARGS)
350350
PG_RETURN_POINTER(query);
351351
}
352352
memcpy((void *) GETQUERY(query), (void *) res, len * sizeof(QueryItem));
353+
354+
if ( len != query->size ) {
355+
char *oldoperand = GETOPERAND(query);
356+
int4 lenoperand = VARSIZE(query) - (oldoperand - (char*)query);
357+
358+
Assert( len < query->size );
359+
360+
query->size = len;
361+
memcpy((void *) GETOPERAND(query), oldoperand, VARSIZE(query) - (oldoperand - (char*)query) );
362+
SET_VARSIZE(query, COMPUTESIZE( len, lenoperand ));
363+
}
364+
353365
pfree(res);
354366
PG_RETURN_TSQUERY(query);
355367
}
@@ -388,6 +400,18 @@ plainto_tsquery_byid(PG_FUNCTION_ARGS)
388400
PG_RETURN_POINTER(query);
389401
}
390402
memcpy((void *) GETQUERY(query), (void *) res, len * sizeof(QueryItem));
403+
404+
if ( len != query->size ) {
405+
char *oldoperand = GETOPERAND(query);
406+
int4 lenoperand = VARSIZE(query) - (oldoperand - (char*)query);
407+
408+
Assert( len < query->size );
409+
410+
query->size = len;
411+
memcpy((void *) GETOPERAND(query), oldoperand, lenoperand );
412+
SET_VARSIZE(query, COMPUTESIZE( len, lenoperand ));
413+
}
414+
391415
pfree(res);
392416
PG_RETURN_POINTER(query);
393417
}

src/backend/utils/adt/tsquery_op.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_op.c,v 1.3 2008/01/01 19:45:53 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_op.c,v 1.3.2.1 2008/03/07 15:29:27 teodor Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -141,27 +141,14 @@ tsquery_not(PG_FUNCTION_ARGS)
141141
static int
142142
CompareTSQ(TSQuery a, TSQuery b)
143143
{
144-
if (a->size != b->size)
145-
{
146-
return (a->size < b->size) ? -1 : 1;
147-
}
148-
else if (VARSIZE(a) != VARSIZE(b))
149-
{
150-
return (VARSIZE(a) < VARSIZE(b)) ? -1 : 1;
151-
}
152-
else
153-
{
154-
QTNode *an = QT2QTN(GETQUERY(a), GETOPERAND(a));
155-
QTNode *bn = QT2QTN(GETQUERY(b), GETOPERAND(b));
156-
int res = QTNodeCompare(an, bn);
157-
158-
QTNFree(an);
159-
QTNFree(bn);
144+
QTNode *an = QT2QTN(GETQUERY(a), GETOPERAND(a));
145+
QTNode *bn = QT2QTN(GETQUERY(b), GETOPERAND(b));
146+
int res = QTNodeCompare(an, bn);
160147

161-
return res;
162-
}
148+
QTNFree(an);
149+
QTNFree(bn);
163150

164-
return 0;
151+
return res;
165152
}
166153

167154
Datum

0 commit comments

Comments
 (0)