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
6666static 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
11031136if (SPI_tuptable == NULL ||
11041137SPI_tuptable -> tupdesc -> natts != 1 ||
1105- SPI_gettypeid (SPI_tuptable -> tupdesc ,1 )!= TSVECTOROID )
1138+ !is_expected_type (SPI_gettypeid (SPI_tuptable -> tupdesc ,1 ),
1139+ TSVECTOROID ))
11061140ereport (ERROR ,
11071141(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
11081142errmsg ("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 ),
13101329errmsg ("tsvector column \"%s\" does not exist" ,
13111330trigger -> 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 ))
13131333ereport (ERROR ,
13141334(errcode (ERRCODE_DATATYPE_MISMATCH ),
13151335errmsg ("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 ),
13271347errmsg ("configuration column \"%s\" does not exist" ,
13281348trigger -> 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 ))
13301351ereport (ERROR ,
13311352(errcode (ERRCODE_DATATYPE_MISMATCH ),
13321353errmsg ("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 ),
13721393errmsg ("column \"%s\" does not exist" ,
13731394trigger -> tgargs [i ])));
1374- if (!istexttype (SPI_gettypeid (rel -> rd_att ,numattr )))
1395+ if (!is_text_type (SPI_gettypeid (rel -> rd_att ,numattr )))
13751396ereport (ERROR ,
13761397(errcode (ERRCODE_DATATYPE_MISMATCH ),
13771398errmsg ("column \"%s\" is not of character type" ,