Skip to content

Commit 635aaab

Browse files
committed
Fix tsvector_update_trigger() to be domain-friendly: it needs to allow all
the columns it works with to be domains over the expected type, not just exactly the expected type. In passing, fix ts_stat() the same way. Per report from Markus Wollny.
1 parent db5f60c commit 635aaab

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/backend/utils/adt/tsvector_op.c

Lines changed: 41 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/tsvector_op.c,v 1.14 2008/03/25 22:42:44 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_op.c,v 1.15 2008/04/08 18:20:29 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -66,6 +66,39 @@ typedef struct
6666
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column);
6767

6868

69+
/*
70+
* Check if datatype is the specified type or equivalent to it.
71+
*
72+
* Note: we could just do getBaseType() unconditionally, but since that's
73+
* a relatively expensive catalog lookup that most users won't need, we
74+
* try the straight comparison first.
75+
*/
76+
static bool
77+
is_expected_type(Oid typid, Oid expected_type)
78+
{
79+
if (typid == expected_type)
80+
return true;
81+
typid = getBaseType(typid);
82+
if (typid == expected_type)
83+
return true;
84+
return false;
85+
}
86+
87+
/* Check if datatype is TEXT or binary-equivalent to it */
88+
static bool
89+
is_text_type(Oid typid)
90+
{
91+
/* varchar(n) and char(n) are binary-compatible with text */
92+
if (typid == TEXTOID || typid == VARCHAROID || typid == BPCHAROID)
93+
return true;
94+
/* Allow domains over these types, too */
95+
typid = getBaseType(typid);
96+
if (typid == TEXTOID || typid == VARCHAROID || typid == BPCHAROID)
97+
return true;
98+
return false;
99+
}
100+
101+
69102
/*
70103
* Order: haspos, len, word, for all positions (pos, weight)
71104
*/
@@ -1102,7 +1135,8 @@ ts_stat_sql(text *txt, text *ws)
11021135

11031136
if (SPI_tuptable == NULL ||
11041137
SPI_tuptable->tupdesc->natts != 1 ||
1105-
SPI_gettypeid(SPI_tuptable->tupdesc, 1) != TSVECTOROID)
1138+
!is_expected_type(SPI_gettypeid(SPI_tuptable->tupdesc, 1),
1139+
TSVECTOROID))
11061140
ereport(ERROR,
11071141
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
11081142
errmsg("ts_stat query must return one tsvector column")));
@@ -1227,21 +1261,6 @@ ts_stat2(PG_FUNCTION_ARGS)
12271261
}
12281262

12291263

1230-
/* Check if datatype is TEXT or binary-equivalent to it */
1231-
static bool
1232-
istexttype(Oid typid)
1233-
{
1234-
/* varchar(n) and char(n) are binary-compatible with text */
1235-
if (typid == TEXTOID || typid == VARCHAROID || typid == BPCHAROID)
1236-
return true;
1237-
/* Allow domains over these types, too */
1238-
typid = getBaseType(typid);
1239-
if (typid == TEXTOID || typid == VARCHAROID || typid == BPCHAROID)
1240-
return true;
1241-
return false;
1242-
}
1243-
1244-
12451264
/*
12461265
* Triggers for automatic update of a tsvector column from text column(s)
12471266
*
@@ -1309,7 +1328,8 @@ tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
13091328
(errcode(ERRCODE_UNDEFINED_COLUMN),
13101329
errmsg("tsvector column \"%s\" does not exist",
13111330
trigger->tgargs[0])));
1312-
if (SPI_gettypeid(rel->rd_att, tsvector_attr_num) != TSVECTOROID)
1331+
if (!is_expected_type(SPI_gettypeid(rel->rd_att, tsvector_attr_num),
1332+
TSVECTOROID))
13131333
ereport(ERROR,
13141334
(errcode(ERRCODE_DATATYPE_MISMATCH),
13151335
errmsg("column \"%s\" is not of tsvector type",
@@ -1326,7 +1346,8 @@ tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
13261346
(errcode(ERRCODE_UNDEFINED_COLUMN),
13271347
errmsg("configuration column \"%s\" does not exist",
13281348
trigger->tgargs[1])));
1329-
if (SPI_gettypeid(rel->rd_att, config_attr_num) != REGCONFIGOID)
1349+
if (!is_expected_type(SPI_gettypeid(rel->rd_att, config_attr_num),
1350+
REGCONFIGOID))
13301351
ereport(ERROR,
13311352
(errcode(ERRCODE_DATATYPE_MISMATCH),
13321353
errmsg("column \"%s\" is not of regconfig type",
@@ -1371,7 +1392,7 @@ tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
13711392
(errcode(ERRCODE_UNDEFINED_COLUMN),
13721393
errmsg("column \"%s\" does not exist",
13731394
trigger->tgargs[i])));
1374-
if (!istexttype(SPI_gettypeid(rel->rd_att, numattr)))
1395+
if (!is_text_type(SPI_gettypeid(rel->rd_att, numattr)))
13751396
ereport(ERROR,
13761397
(errcode(ERRCODE_DATATYPE_MISMATCH),
13771398
errmsg("column \"%s\" is not of character type",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy