mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
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.
This commit is contained in:
@ -7,7 +7,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_op.c,v 1.12 2008/01/01 19:45:53 momjian Exp $
|
* $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_op.c,v 1.12.2.1 2008/04/08 18:20:34 tgl Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -66,6 +66,39 @@ typedef struct
|
|||||||
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column);
|
static Datum tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check if datatype is the specified type or equivalent to it.
|
||||||
|
*
|
||||||
|
* Note: we could just do getBaseType() unconditionally, but since that's
|
||||||
|
* a relatively expensive catalog lookup that most users won't need, we
|
||||||
|
* try the straight comparison first.
|
||||||
|
*/
|
||||||
|
static bool
|
||||||
|
is_expected_type(Oid typid, Oid expected_type)
|
||||||
|
{
|
||||||
|
if (typid == expected_type)
|
||||||
|
return true;
|
||||||
|
typid = getBaseType(typid);
|
||||||
|
if (typid == expected_type)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if datatype is TEXT or binary-equivalent to it */
|
||||||
|
static bool
|
||||||
|
is_text_type(Oid typid)
|
||||||
|
{
|
||||||
|
/* varchar(n) and char(n) are binary-compatible with text */
|
||||||
|
if (typid == TEXTOID || typid == VARCHAROID || typid == BPCHAROID)
|
||||||
|
return true;
|
||||||
|
/* Allow domains over these types, too */
|
||||||
|
typid = getBaseType(typid);
|
||||||
|
if (typid == TEXTOID || typid == VARCHAROID || typid == BPCHAROID)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Order: haspos, len, word, for all positions (pos, weight)
|
* Order: haspos, len, word, for all positions (pos, weight)
|
||||||
*/
|
*/
|
||||||
@ -1102,7 +1135,8 @@ ts_stat_sql(text *txt, text *ws)
|
|||||||
|
|
||||||
if (SPI_tuptable == NULL ||
|
if (SPI_tuptable == NULL ||
|
||||||
SPI_tuptable->tupdesc->natts != 1 ||
|
SPI_tuptable->tupdesc->natts != 1 ||
|
||||||
SPI_gettypeid(SPI_tuptable->tupdesc, 1) != TSVECTOROID)
|
!is_expected_type(SPI_gettypeid(SPI_tuptable->tupdesc, 1),
|
||||||
|
TSVECTOROID))
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||||
errmsg("ts_stat query must return one tsvector column")));
|
errmsg("ts_stat query must return one tsvector column")));
|
||||||
@ -1227,21 +1261,6 @@ ts_stat2(PG_FUNCTION_ARGS)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Check if datatype is TEXT or binary-equivalent to it */
|
|
||||||
static bool
|
|
||||||
istexttype(Oid typid)
|
|
||||||
{
|
|
||||||
/* varchar(n) and char(n) are binary-compatible with text */
|
|
||||||
if (typid == TEXTOID || typid == VARCHAROID || typid == BPCHAROID)
|
|
||||||
return true;
|
|
||||||
/* Allow domains over these types, too */
|
|
||||||
typid = getBaseType(typid);
|
|
||||||
if (typid == TEXTOID || typid == VARCHAROID || typid == BPCHAROID)
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Triggers for automatic update of a tsvector column from text column(s)
|
* Triggers for automatic update of a tsvector column from text column(s)
|
||||||
*
|
*
|
||||||
@ -1309,7 +1328,8 @@ tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
|
|||||||
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
||||||
errmsg("tsvector column \"%s\" does not exist",
|
errmsg("tsvector column \"%s\" does not exist",
|
||||||
trigger->tgargs[0])));
|
trigger->tgargs[0])));
|
||||||
if (SPI_gettypeid(rel->rd_att, tsvector_attr_num) != TSVECTOROID)
|
if (!is_expected_type(SPI_gettypeid(rel->rd_att, tsvector_attr_num),
|
||||||
|
TSVECTOROID))
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
||||||
errmsg("column \"%s\" is not of tsvector type",
|
errmsg("column \"%s\" is not of tsvector type",
|
||||||
@ -1326,7 +1346,8 @@ tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
|
|||||||
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
||||||
errmsg("configuration column \"%s\" does not exist",
|
errmsg("configuration column \"%s\" does not exist",
|
||||||
trigger->tgargs[1])));
|
trigger->tgargs[1])));
|
||||||
if (SPI_gettypeid(rel->rd_att, config_attr_num) != REGCONFIGOID)
|
if (!is_expected_type(SPI_gettypeid(rel->rd_att, config_attr_num),
|
||||||
|
REGCONFIGOID))
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
||||||
errmsg("column \"%s\" is not of regconfig type",
|
errmsg("column \"%s\" is not of regconfig type",
|
||||||
@ -1371,7 +1392,7 @@ tsvector_update_trigger(PG_FUNCTION_ARGS, bool config_column)
|
|||||||
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
||||||
errmsg("column \"%s\" does not exist",
|
errmsg("column \"%s\" does not exist",
|
||||||
trigger->tgargs[i])));
|
trigger->tgargs[i])));
|
||||||
if (!istexttype(SPI_gettypeid(rel->rd_att, numattr)))
|
if (!is_text_type(SPI_gettypeid(rel->rd_att, numattr)))
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
||||||
errmsg("column \"%s\" is not of character type",
|
errmsg("column \"%s\" is not of character type",
|
||||||
|
Reference in New Issue
Block a user