Fix memory arrangement of tsquery after removing stop words. It causes
authorTeodor Sigaev
Fri, 7 Mar 2008 15:29:27 +0000 (15:29 +0000)
committerTeodor Sigaev
Fri, 7 Mar 2008 15:29:27 +0000 (15:29 +0000)
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.

src/backend/tsearch/to_tsany.c
src/backend/utils/adt/tsquery_op.c

index 5c954fa43167cb155454d155745a908ca1f60196..dbcfe814306b3301082b7d3f7e1b0421dcaf09ac 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/tsearch/to_tsany.c,v 1.8 2008/01/01 19:45:52 momjian Exp $
+ *   $PostgreSQL: pgsql/src/backend/tsearch/to_tsany.c,v 1.8.2.1 2008/03/07 15:29:27 teodor Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -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);
 }
index bf50b9d4c3191352b072e1576e0b3f9ec3bbd42a..3e5b8d300574d64b4ad6aaafecbfed2024ab2342 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_op.c,v 1.3 2008/01/01 19:45:53 momjian Exp $
+ *   $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_op.c,v 1.3.2.1 2008/03/07 15:29:27 teodor Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -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