diff --git a/src/backend/tsearch/wparser_def.c b/src/backend/tsearch/wparser_def.c
index 840a44ec00..23e4e9d98a 100644
--- a/src/backend/tsearch/wparser_def.c
+++ b/src/backend/tsearch/wparser_def.c
@@ -2417,7 +2417,8 @@ mark_hl_fragments(HeadlineParsedText *prs, TSQuery query, List *locations,
/* show the first min_words words if we have not marked anything */
if (num_f <= 0)
{
- startpos = endpos = curlen = 0;
+ startpos = curlen = 0;
+ endpos = -1;
for (i = 0; i < prs->curwords && curlen < min_words; i++)
{
if (!NONWORDTOKEN(prs->words[i].type))
@@ -2571,7 +2572,7 @@ mark_hl_words(HeadlineParsedText *prs, TSQuery query, List *locations,
if (bestlen < 0)
{
curlen = 0;
- pose = 0;
+ pose = -1;
for (i = 0; i < prs->curwords && curlen < min_words; i++)
{
if (!NONWORDTOKEN(prs->words[i].type))
@@ -2601,7 +2602,6 @@ prsd_headline(PG_FUNCTION_ARGS)
HeadlineParsedText *prs = (HeadlineParsedText *) PG_GETARG_POINTER(0);
List *prsoptions = (List *) PG_GETARG_POINTER(1);
TSQuery query = PG_GETARG_TSQUERY(2);
- hlCheck ch;
List *locations;
/* default option values: */
@@ -2671,10 +2671,17 @@ prsd_headline(PG_FUNCTION_ARGS)
}
/* Locate words and phrases matching the query */
- ch.words = prs->words;
- ch.len = prs->curwords;
- locations = TS_execute_locations(GETQUERY(query), &ch, TS_EXEC_EMPTY,
- checkcondition_HL);
+ if (query->size > 0)
+ {
+ hlCheck ch;
+
+ ch.words = prs->words;
+ ch.len = prs->curwords;
+ locations = TS_execute_locations(GETQUERY(query), &ch, TS_EXEC_EMPTY,
+ checkcondition_HL);
+ }
+ else
+ locations = NIL; /* empty query matches nothing */
/* Apply appropriate headline selector */
if (max_fragments == 0)
diff --git a/src/test/regress/expected/tsearch.out b/src/test/regress/expected/tsearch.out
index 0e68245743..6b3ad38f39 100644
--- a/src/test/regress/expected/tsearch.out
+++ b/src/test/regress/expected/tsearch.out
@@ -2127,6 +2127,27 @@ to_tsquery('english','Lorem') && phraseto_tsquery('english','ullamcorper urna'),
Lorem ipsum urna. Nullam nullam ullamcorper urna
(1 row)
+-- Edge cases with empty query
+SELECT ts_headline('english',
+'', ''::tsquery);
+NOTICE: text-search query doesn't contain lexemes: ""
+LINE 2: '', ''::tsquery);
+ ^
+ ts_headline
+-------------
+
+(1 row)
+
+SELECT ts_headline('english',
+'foo bar', ''::tsquery);
+NOTICE: text-search query doesn't contain lexemes: ""
+LINE 2: 'foo bar', ''::tsquery);
+ ^
+ ts_headline
+-------------
+ foo bar
+(1 row)
+
--Rewrite sub system
CREATE TABLE test_tsquery (txtkeyword TEXT, txtsample TEXT);
\set ECHO none
diff --git a/src/test/regress/sql/tsearch.sql b/src/test/regress/sql/tsearch.sql
index b56477a813..f6f5fb5c27 100644
--- a/src/test/regress/sql/tsearch.sql
+++ b/src/test/regress/sql/tsearch.sql
@@ -640,6 +640,12 @@ SELECT ts_headline('english',
to_tsquery('english','Lorem') && phraseto_tsquery('english','ullamcorper urna'),
'MaxFragments=100, MaxWords=100, MinWords=1');
+-- Edge cases with empty query
+SELECT ts_headline('english',
+'', ''::tsquery);
+SELECT ts_headline('english',
+'foo bar', ''::tsquery);
+
--Rewrite sub system
CREATE TABLE test_tsquery (txtkeyword TEXT, txtsample TEXT);