From: Teodor Sigaev Date: Fri, 7 Mar 2008 15:29:27 +0000 (+0000) Subject: Fix memory arrangement of tsquery after removing stop words. It causes X-Git-Url: http://waps.l3s.uni-hannover.de/gitweb/?a=commitdiff_plain;h=36c3b62a8d34ce701274fefb31e0ba787cff5bf4;p=users%2Fbernd%2Fpostgres.git Fix memory arrangement of tsquery after removing stop words. It causes a unused memory holes in tsquery. Per report by Richard Huxton . 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. --- diff --git a/src/backend/tsearch/to_tsany.c b/src/backend/tsearch/to_tsany.c index 71f4db3a54..9fcae7959e 100644 --- a/src/backend/tsearch/to_tsany.c +++ b/src/backend/tsearch/to_tsany.c @@ -350,6 +350,18 @@ to_tsquery_byid(PG_FUNCTION_ARGS) PG_RETURN_POINTER(query); } memcpy((void *) GETQUERY(query), (void *) res, len * sizeof(QueryItem)); + + if ( len != query->size ) { + char *oldoperand = GETOPERAND(query); + int4 lenoperand = VARSIZE(query) - (oldoperand - (char*)query); + + Assert( len < query->size ); + + query->size = len; + memcpy((void *) GETOPERAND(query), oldoperand, VARSIZE(query) - (oldoperand - (char*)query) ); + SET_VARSIZE(query, COMPUTESIZE( len, lenoperand )); + } + pfree(res); PG_RETURN_TSQUERY(query); } @@ -388,6 +400,18 @@ plainto_tsquery_byid(PG_FUNCTION_ARGS) PG_RETURN_POINTER(query); } memcpy((void *) GETQUERY(query), (void *) res, len * sizeof(QueryItem)); + + if ( len != query->size ) { + char *oldoperand = GETOPERAND(query); + int4 lenoperand = VARSIZE(query) - (oldoperand - (char*)query); + + Assert( len < query->size ); + + query->size = len; + memcpy((void *) GETOPERAND(query), oldoperand, lenoperand ); + SET_VARSIZE(query, COMPUTESIZE( len, lenoperand )); + } + pfree(res); PG_RETURN_POINTER(query); } diff --git a/src/backend/utils/adt/tsquery_op.c b/src/backend/utils/adt/tsquery_op.c index 0af69964e8..34889c4b00 100644 --- a/src/backend/utils/adt/tsquery_op.c +++ b/src/backend/utils/adt/tsquery_op.c @@ -141,27 +141,14 @@ tsquery_not(PG_FUNCTION_ARGS) static int CompareTSQ(TSQuery a, TSQuery b) { - if (a->size != b->size) - { - return (a->size < b->size) ? -1 : 1; - } - else if (VARSIZE(a) != VARSIZE(b)) - { - return (VARSIZE(a) < VARSIZE(b)) ? -1 : 1; - } - else - { - QTNode *an = QT2QTN(GETQUERY(a), GETOPERAND(a)); - QTNode *bn = QT2QTN(GETQUERY(b), GETOPERAND(b)); - int res = QTNodeCompare(an, bn); - - QTNFree(an); - QTNFree(bn); + QTNode *an = QT2QTN(GETQUERY(a), GETOPERAND(a)); + QTNode *bn = QT2QTN(GETQUERY(b), GETOPERAND(b)); + int res = QTNodeCompare(an, bn); - return res; - } + QTNFree(an); + QTNFree(bn); - return 0; + return res; } Datum