Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions | Variables

tsearch2.c File Reference

#include "postgres.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/syscache.h"
Include dependency graph for tsearch2.c:

Go to the source code of this file.

Defines

#define INSERT_ARGUMENT0(argument, isnull)
#define TextGetObjectId(infunction, text)
#define UNSUPPORTED_FUNCTION(name)

Functions

static Oid GetCurrentDict (void)
static Oid GetCurrentParser (void)
Datum tsa_lexize_byname (PG_FUNCTION_ARGS)
Datum tsa_lexize_bycurrent (PG_FUNCTION_ARGS)
Datum tsa_set_curdict (PG_FUNCTION_ARGS)
Datum tsa_set_curdict_byname (PG_FUNCTION_ARGS)
Datum tsa_token_type_current (PG_FUNCTION_ARGS)
Datum tsa_set_curprs (PG_FUNCTION_ARGS)
Datum tsa_set_curprs_byname (PG_FUNCTION_ARGS)
Datum tsa_parse_current (PG_FUNCTION_ARGS)
Datum tsa_set_curcfg (PG_FUNCTION_ARGS)
Datum tsa_set_curcfg_byname (PG_FUNCTION_ARGS)
Datum tsa_to_tsvector_name (PG_FUNCTION_ARGS)
Datum tsa_to_tsquery_name (PG_FUNCTION_ARGS)
Datum tsa_plainto_tsquery_name (PG_FUNCTION_ARGS)
Datum tsa_headline_byname (PG_FUNCTION_ARGS)
Datum tsa_ts_stat (PG_FUNCTION_ARGS)
Datum tsa_tsearch2 (PG_FUNCTION_ARGS)
Datum tsa_rewrite_accum (PG_FUNCTION_ARGS)
Datum tsa_rewrite_finish (PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1 (tsa_lexize_byname)
 PG_FUNCTION_INFO_V1 (tsa_lexize_bycurrent)
 PG_FUNCTION_INFO_V1 (tsa_set_curdict)
 PG_FUNCTION_INFO_V1 (tsa_set_curdict_byname)
 PG_FUNCTION_INFO_V1 (tsa_token_type_current)
 PG_FUNCTION_INFO_V1 (tsa_set_curprs)
 PG_FUNCTION_INFO_V1 (tsa_set_curprs_byname)
 PG_FUNCTION_INFO_V1 (tsa_parse_current)
 PG_FUNCTION_INFO_V1 (tsa_set_curcfg)
 PG_FUNCTION_INFO_V1 (tsa_set_curcfg_byname)
 PG_FUNCTION_INFO_V1 (tsa_to_tsvector_name)
 PG_FUNCTION_INFO_V1 (tsa_to_tsquery_name)
 PG_FUNCTION_INFO_V1 (tsa_plainto_tsquery_name)
 PG_FUNCTION_INFO_V1 (tsa_headline_byname)
 PG_FUNCTION_INFO_V1 (tsa_ts_stat)
 PG_FUNCTION_INFO_V1 (tsa_tsearch2)
 PG_FUNCTION_INFO_V1 (tsa_rewrite_accum)
 PG_FUNCTION_INFO_V1 (tsa_rewrite_finish)
 UNSUPPORTED_FUNCTION (tsa_dex_init)
 UNSUPPORTED_FUNCTION (tsa_dex_lexize)
 UNSUPPORTED_FUNCTION (tsa_snb_en_init)
 UNSUPPORTED_FUNCTION (tsa_snb_lexize)
 UNSUPPORTED_FUNCTION (tsa_snb_ru_init_koi8)
 UNSUPPORTED_FUNCTION (tsa_snb_ru_init_utf8)
 UNSUPPORTED_FUNCTION (tsa_snb_ru_init)
 UNSUPPORTED_FUNCTION (tsa_spell_init)
 UNSUPPORTED_FUNCTION (tsa_spell_lexize)
 UNSUPPORTED_FUNCTION (tsa_syn_init)
 UNSUPPORTED_FUNCTION (tsa_syn_lexize)
 UNSUPPORTED_FUNCTION (tsa_thesaurus_init)
 UNSUPPORTED_FUNCTION (tsa_thesaurus_lexize)
 UNSUPPORTED_FUNCTION (tsa_prsd_start)
 UNSUPPORTED_FUNCTION (tsa_prsd_getlexeme)
 UNSUPPORTED_FUNCTION (tsa_prsd_end)
 UNSUPPORTED_FUNCTION (tsa_prsd_lextype)
 UNSUPPORTED_FUNCTION (tsa_prsd_headline)
 UNSUPPORTED_FUNCTION (tsa_reset_tsearch)
 UNSUPPORTED_FUNCTION (tsa_get_covers)

Variables

 PG_MODULE_MAGIC
static Oid current_dictionary_oid = InvalidOid
static Oid current_parser_oid = InvalidOid

Define Documentation

#define INSERT_ARGUMENT0 (   argument,
  isnull 
)
Value:
do {                                                \
        int i;                                          \
        for (i = fcinfo->nargs; i > 0; i--)             \
        {                                               \
            fcinfo->arg[i] = fcinfo->arg[i-1];          \
            fcinfo->argnull[i] = fcinfo->argnull[i-1];  \
        }                                               \
        fcinfo->arg[0] = (argument);                    \
        fcinfo->argnull[0] = (isnull);                  \
        fcinfo->nargs++;                                \
    } while (0)

Definition at line 30 of file tsearch2.c.

Referenced by tsa_parse_current(), and tsa_token_type_current().

#define TextGetObjectId (   infunction,
  text 
)
#define UNSUPPORTED_FUNCTION (   name  ) 
Value:
Datum name(PG_FUNCTION_ARGS);                       \
    Datum                                               \
    name(PG_FUNCTION_ARGS)                              \
    {                                                   \
        ereport(ERROR,                                  \
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),\
                 errmsg("function %s is no longer supported", \
                        format_procedure(fcinfo->flinfo->fn_oid)), \
                 errhint("Switch to new tsearch functionality."))); \
        /* keep compiler quiet */                       \
        PG_RETURN_NULL();                               \
    }                                                   \
    PG_FUNCTION_INFO_V1(name)

Definition at line 47 of file tsearch2.c.


Function Documentation

static Oid GetCurrentDict ( void   )  [static]

Definition at line 537 of file tsearch2.c.

References current_dictionary_oid, ereport, errcode(), errhint(), errmsg(), ERROR, and InvalidOid.

Referenced by tsa_lexize_bycurrent().

{
    if (current_dictionary_oid == InvalidOid)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("no current dictionary"),
                 errhint("Execute SELECT set_curdict(...).")));

    return current_dictionary_oid;
}

static Oid GetCurrentParser ( void   )  [static]
PG_FUNCTION_INFO_V1 ( tsa_to_tsquery_name   ) 
PG_FUNCTION_INFO_V1 ( tsa_plainto_tsquery_name   ) 
PG_FUNCTION_INFO_V1 ( tsa_headline_byname   ) 
PG_FUNCTION_INFO_V1 ( tsa_ts_stat   ) 
PG_FUNCTION_INFO_V1 ( tsa_tsearch2   ) 
PG_FUNCTION_INFO_V1 ( tsa_rewrite_accum   ) 
PG_FUNCTION_INFO_V1 ( tsa_rewrite_finish   ) 
PG_FUNCTION_INFO_V1 ( tsa_lexize_byname   ) 
PG_FUNCTION_INFO_V1 ( tsa_lexize_bycurrent   ) 
PG_FUNCTION_INFO_V1 ( tsa_set_curdict   ) 
PG_FUNCTION_INFO_V1 ( tsa_set_curdict_byname   ) 
PG_FUNCTION_INFO_V1 ( tsa_token_type_current   ) 
PG_FUNCTION_INFO_V1 ( tsa_set_curprs   ) 
PG_FUNCTION_INFO_V1 ( tsa_set_curprs_byname   ) 
PG_FUNCTION_INFO_V1 ( tsa_parse_current   ) 
PG_FUNCTION_INFO_V1 ( tsa_set_curcfg   ) 
PG_FUNCTION_INFO_V1 ( tsa_set_curcfg_byname   ) 
PG_FUNCTION_INFO_V1 ( tsa_to_tsvector_name   ) 
Datum tsa_headline_byname ( PG_FUNCTION_ARGS   ) 

Definition at line 322 of file tsearch2.c.

References CStringGetDatum, DatumGetObjectId, DirectFunctionCall1, DirectFunctionCall3, DirectFunctionCall4, ObjectIdGetDatum, PG_GETARG_DATUM, PG_NARGS, regconfigin(), TextDatumGetCString, ts_headline_byid(), and ts_headline_byid_opt().

{
    Datum       arg0 = PG_GETARG_DATUM(0);
    Datum       arg1 = PG_GETARG_DATUM(1);
    Datum       arg2 = PG_GETARG_DATUM(2);
    Datum       result;
    Oid         config_oid;

    /* first parameter has to be converted to oid */
    config_oid = DatumGetObjectId(DirectFunctionCall1(regconfigin,
                                CStringGetDatum(TextDatumGetCString(arg0))));

    if (PG_NARGS() == 3)
        result = DirectFunctionCall3(ts_headline_byid,
                                   ObjectIdGetDatum(config_oid), arg1, arg2);
    else
    {
        Datum       arg3 = PG_GETARG_DATUM(3);

        result = DirectFunctionCall4(ts_headline_byid_opt,
                                     ObjectIdGetDatum(config_oid),
                                     arg1, arg2, arg3);
    }

    return result;
}

Datum tsa_lexize_bycurrent ( PG_FUNCTION_ARGS   ) 
Datum tsa_lexize_byname ( PG_FUNCTION_ARGS   ) 
Datum tsa_parse_current ( PG_FUNCTION_ARGS   ) 
Datum tsa_plainto_tsquery_name ( PG_FUNCTION_ARGS   ) 
Datum tsa_rewrite_accum ( PG_FUNCTION_ARGS   ) 

Definition at line 404 of file tsearch2.c.

References AggCheckCallContext(), ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, ArrayGetNItems(), DatumGetTSQuery, deconstruct_array(), elog, ERROR, findsubquery(), GETOPERAND, GETQUERY, HDRSIZETQ, MemoryContextAlloc(), MemoryContextSwitchTo(), NULL, pfree(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P_COPY, PG_GETARG_POINTER, PG_GETARG_TSQUERY, PG_RETURN_POINTER, PG_RETURN_TSQUERY, QT2QTN(), QTN2QT(), QTNBinary(), QTNFree(), QTNSort(), QTNTernary(), SET_VARSIZE, TSQueryData::size, TSQUERYOID, and VARSIZE.

{
    TSQuery     acc;
    ArrayType  *qa;
    TSQuery     q;
    QTNode     *qex = NULL,
               *subs = NULL,
               *acctree = NULL;
    bool        isfind = false;
    Datum      *elemsp;
    int         nelemsp;
    MemoryContext aggcontext;
    MemoryContext oldcontext;

    if (!AggCheckCallContext(fcinfo, &aggcontext))
        elog(ERROR, "tsa_rewrite_accum called in non-aggregate context");

    if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL)
    {
        acc = (TSQuery) MemoryContextAlloc(aggcontext, HDRSIZETQ);
        SET_VARSIZE(acc, HDRSIZETQ);
        acc->size = 0;
    }
    else
        acc = PG_GETARG_TSQUERY(0);

    if (PG_ARGISNULL(1) || PG_GETARG_POINTER(1) == NULL)
        PG_RETURN_TSQUERY(acc);
    else
        qa = PG_GETARG_ARRAYTYPE_P_COPY(1);

    if (ARR_NDIM(qa) != 1)
        elog(ERROR, "array must be one-dimensional, not %d dimensions",
             ARR_NDIM(qa));
    if (ArrayGetNItems(ARR_NDIM(qa), ARR_DIMS(qa)) != 3)
        elog(ERROR, "array must have three elements");
    if (ARR_ELEMTYPE(qa) != TSQUERYOID)
        elog(ERROR, "array must contain tsquery elements");

    deconstruct_array(qa, TSQUERYOID, -1, false, 'i', &elemsp, NULL, &nelemsp);

    q = DatumGetTSQuery(elemsp[0]);
    if (q->size == 0)
    {
        pfree(elemsp);
        PG_RETURN_POINTER(acc);
    }

    if (!acc->size)
    {
        if (VARSIZE(acc) > HDRSIZETQ)
        {
            pfree(elemsp);
            PG_RETURN_POINTER(acc);
        }
        else
            acctree = QT2QTN(GETQUERY(q), GETOPERAND(q));
    }
    else
        acctree = QT2QTN(GETQUERY(acc), GETOPERAND(acc));

    QTNTernary(acctree);
    QTNSort(acctree);

    q = DatumGetTSQuery(elemsp[1]);
    if (q->size == 0)
    {
        pfree(elemsp);
        PG_RETURN_POINTER(acc);
    }
    qex = QT2QTN(GETQUERY(q), GETOPERAND(q));
    QTNTernary(qex);
    QTNSort(qex);

    q = DatumGetTSQuery(elemsp[2]);
    if (q->size)
        subs = QT2QTN(GETQUERY(q), GETOPERAND(q));

    acctree = findsubquery(acctree, qex, subs, &isfind);

    if (isfind || !acc->size)
    {
        /* pfree( acc ); do not pfree(p), because nodeAgg.c will */
        if (acctree)
        {
            QTNBinary(acctree);
            oldcontext = MemoryContextSwitchTo(aggcontext);
            acc = QTN2QT(acctree);
            MemoryContextSwitchTo(oldcontext);
        }
        else
        {
            acc = (TSQuery) MemoryContextAlloc(aggcontext, HDRSIZETQ);
            SET_VARSIZE(acc, HDRSIZETQ);
            acc->size = 0;
        }
    }

    pfree(elemsp);
    QTNFree(qex);
    QTNFree(subs);
    QTNFree(acctree);

    PG_RETURN_TSQUERY(acc);
}

Datum tsa_rewrite_finish ( PG_FUNCTION_ARGS   ) 

Definition at line 511 of file tsearch2.c.

References HDRSIZETQ, NULL, palloc(), pfree(), PG_ARGISNULL, PG_GETARG_TSQUERY, PG_RETURN_POINTER, SET_VARSIZE, TSQueryData::size, and VARSIZE.

{
    TSQuery     acc = PG_GETARG_TSQUERY(0);
    TSQuery     rewrited;

    if (acc == NULL || PG_ARGISNULL(0) || acc->size == 0)
    {
        rewrited = (TSQuery) palloc(HDRSIZETQ);
        SET_VARSIZE(rewrited, HDRSIZETQ);
        rewrited->size = 0;
    }
    else
    {
        rewrited = (TSQuery) palloc(VARSIZE(acc));
        memcpy(rewrited, acc, VARSIZE(acc));
        pfree(acc);
    }

    PG_RETURN_POINTER(rewrited);
}

Datum tsa_set_curcfg ( PG_FUNCTION_ARGS   ) 
Datum tsa_set_curcfg_byname ( PG_FUNCTION_ARGS   ) 

Definition at line 264 of file tsearch2.c.

References name, PG_GETARG_TEXT_PP, PG_RETURN_VOID, PGC_S_SESSION, PGC_USERSET, SetConfigOption(), and text_to_cstring().

{
    text       *arg0 = PG_GETARG_TEXT_PP(0);
    char       *name;

    name = text_to_cstring(arg0);

    SetConfigOption("default_text_search_config", name,
                    PGC_USERSET, PGC_S_SESSION);

    PG_RETURN_VOID();
}

Datum tsa_set_curdict ( PG_FUNCTION_ARGS   ) 

Definition at line 170 of file tsearch2.c.

References current_dictionary_oid, elog, ERROR, ObjectIdGetDatum, PG_GETARG_OID, PG_RETURN_VOID, SearchSysCacheExists(), and TSDICTOID.

{
    Oid         dict_oid = PG_GETARG_OID(0);

    if (!SearchSysCacheExists(TSDICTOID,
                              ObjectIdGetDatum(dict_oid),
                              0, 0, 0))
        elog(ERROR, "cache lookup failed for text search dictionary %u",
             dict_oid);

    current_dictionary_oid = dict_oid;

    PG_RETURN_VOID();
}

Datum tsa_set_curdict_byname ( PG_FUNCTION_ARGS   ) 
Datum tsa_set_curprs ( PG_FUNCTION_ARGS   ) 

Definition at line 209 of file tsearch2.c.

References current_parser_oid, elog, ERROR, ObjectIdGetDatum, PG_GETARG_OID, PG_RETURN_VOID, SearchSysCacheExists(), and TSPARSEROID.

{
    Oid         parser_oid = PG_GETARG_OID(0);

    if (!SearchSysCacheExists(TSPARSEROID,
                              ObjectIdGetDatum(parser_oid),
                              0, 0, 0))
        elog(ERROR, "cache lookup failed for text search parser %u",
             parser_oid);

    current_parser_oid = parser_oid;

    PG_RETURN_VOID();
}

Datum tsa_set_curprs_byname ( PG_FUNCTION_ARGS   ) 
Datum tsa_to_tsquery_name ( PG_FUNCTION_ARGS   ) 

Definition at line 293 of file tsearch2.c.

References DirectFunctionCall2, ObjectIdGetDatum, PG_GETARG_DATUM, PG_GETARG_TEXT_PP, regconfigin(), TextGetObjectId, and to_tsquery_byid().

{
    text       *cfgname = PG_GETARG_TEXT_PP(0);
    Datum       arg1 = PG_GETARG_DATUM(1);
    Oid         config_oid;

    config_oid = TextGetObjectId(regconfigin, cfgname);

    return DirectFunctionCall2(to_tsquery_byid,
                               ObjectIdGetDatum(config_oid), arg1);
}

Datum tsa_to_tsvector_name ( PG_FUNCTION_ARGS   ) 
Datum tsa_token_type_current ( PG_FUNCTION_ARGS   ) 
Datum tsa_ts_stat ( PG_FUNCTION_ARGS   ) 
Datum tsa_tsearch2 ( PG_FUNCTION_ARGS   ) 

Definition at line 359 of file tsearch2.c.

References CALLED_AS_TRIGGER, elog, ERROR, GetConfigOptionByName(), i, NULL, palloc(), pfree(), pstrdup(), TriggerData::tg_trigger, Trigger::tgargs, Trigger::tgnargs, and tsvector_update_trigger_byid().

{
    TriggerData *trigdata;
    Trigger    *trigger;
    char      **tgargs,
              **tgargs_old;
    int         i;
    Datum       res;

    /* Check call context */
    if (!CALLED_AS_TRIGGER(fcinfo))     /* internal error */
        elog(ERROR, "tsvector_update_trigger: not fired by trigger manager");

    trigdata = (TriggerData *) fcinfo->context;
    trigger = trigdata->tg_trigger;

    if (trigger->tgnargs < 2)
        elog(ERROR, "TSearch: format tsearch2(tsvector_field, text_field1,...)");

    /* create space for configuration name */
    tgargs = (char **) palloc((trigger->tgnargs + 1) * sizeof(char *));
    tgargs[0] = trigger->tgargs[0];
    for (i = 1; i < trigger->tgnargs; i++)
        tgargs[i + 1] = trigger->tgargs[i];

    tgargs[1] = pstrdup(GetConfigOptionByName("default_text_search_config",
                                              NULL));
    tgargs_old = trigger->tgargs;
    trigger->tgargs = tgargs;
    trigger->tgnargs++;

    res = tsvector_update_trigger_byid(fcinfo);

    /* restore old trigger data */
    trigger->tgargs = tgargs_old;
    trigger->tgnargs--;

    pfree(tgargs[1]);
    pfree(tgargs);

    return res;
}

UNSUPPORTED_FUNCTION ( tsa_syn_lexize   ) 
UNSUPPORTED_FUNCTION ( tsa_prsd_end   ) 
UNSUPPORTED_FUNCTION ( tsa_dex_init   ) 
UNSUPPORTED_FUNCTION ( tsa_snb_lexize   ) 
UNSUPPORTED_FUNCTION ( tsa_prsd_getlexeme   ) 
UNSUPPORTED_FUNCTION ( tsa_syn_init   ) 
UNSUPPORTED_FUNCTION ( tsa_prsd_lextype   ) 
UNSUPPORTED_FUNCTION ( tsa_snb_ru_init_utf8   ) 
UNSUPPORTED_FUNCTION ( tsa_snb_ru_init_koi8   ) 
UNSUPPORTED_FUNCTION ( tsa_get_covers   ) 
UNSUPPORTED_FUNCTION ( tsa_reset_tsearch   ) 
UNSUPPORTED_FUNCTION ( tsa_prsd_headline   ) 
UNSUPPORTED_FUNCTION ( tsa_spell_lexize   ) 
UNSUPPORTED_FUNCTION ( tsa_snb_en_init   ) 
UNSUPPORTED_FUNCTION ( tsa_snb_ru_init   ) 
UNSUPPORTED_FUNCTION ( tsa_spell_init   ) 
UNSUPPORTED_FUNCTION ( tsa_prsd_start   ) 
UNSUPPORTED_FUNCTION ( tsa_thesaurus_lexize   ) 
UNSUPPORTED_FUNCTION ( tsa_dex_lexize   ) 
UNSUPPORTED_FUNCTION ( tsa_thesaurus_init   ) 

Variable Documentation

Oid current_dictionary_oid = InvalidOid [static]

Definition at line 26 of file tsearch2.c.

Referenced by GetCurrentDict(), tsa_set_curdict(), and tsa_set_curdict_byname().

Oid current_parser_oid = InvalidOid [static]

Definition at line 27 of file tsearch2.c.

Referenced by GetCurrentParser(), tsa_set_curprs(), and tsa_set_curprs_byname().

Definition at line 24 of file tsearch2.c.