@@ -342,6 +342,7 @@ to_tsquery_byid(PG_FUNCTION_ARGS)
342342if (query -> size == 0 )
343343PG_RETURN_TSQUERY (query );
344344
345+ /* clean out any stopword placeholders from the tree */
345346res = clean_fakeval (GETQUERY (query ),& len );
346347if (!res )
347348{
@@ -351,6 +352,10 @@ to_tsquery_byid(PG_FUNCTION_ARGS)
351352}
352353memcpy ((void * )GETQUERY (query ), (void * )res ,len * sizeof (QueryItem ));
353354
355+ /*
356+ * Removing the stopword placeholders might've resulted in fewer
357+ * QueryItems. If so, move the operands up accordingly.
358+ */
354359if (len != query -> size )
355360{
356361char * oldoperand = GETOPERAND (query );
@@ -359,7 +364,7 @@ to_tsquery_byid(PG_FUNCTION_ARGS)
359364Assert (len < query -> size );
360365
361366query -> size = len ;
362- memcpy ((void * )GETOPERAND (query ),oldoperand ,VARSIZE (query )- (oldoperand - (char * )query ));
367+ memmove ((void * )GETOPERAND (query ),oldoperand ,VARSIZE (query )- (oldoperand - (char * )query ));
363368SET_VARSIZE (query ,COMPUTESIZE (len ,lenoperand ));
364369}
365370