Header And Logo

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

Functions

pg_conversion.c File Reference

#include "postgres.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_conversion_fn.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/fmgroids.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
Include dependency graph for pg_conversion.c:

Go to the source code of this file.

Functions

Oid ConversionCreate (const char *conname, Oid connamespace, Oid conowner, int32 conforencoding, int32 contoencoding, Oid conproc, bool def)
void RemoveConversionById (Oid conversionOid)
Oid FindDefaultConversion (Oid name_space, int32 for_encoding, int32 to_encoding)

Function Documentation

Oid ConversionCreate ( const char *  conname,
Oid  connamespace,
Oid  conowner,
int32  conforencoding,
int32  contoencoding,
Oid  conproc,
bool  def 
)

Definition at line 41 of file pg_conversion.c.

References Anum_pg_conversion_condefault, Anum_pg_conversion_conforencoding, Anum_pg_conversion_conname, Anum_pg_conversion_connamespace, Anum_pg_conversion_conowner, Anum_pg_conversion_conproc, Anum_pg_conversion_contoencoding, Assert, BoolGetDatum, CatalogUpdateIndexes(), ObjectAddress::classId, CONNAMENSP, ConversionRelationId, DEPENDENCY_NORMAL, elog, ereport, errcode(), errmsg(), ERROR, FindDefaultConversion(), heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), HeapTupleGetOid, i, Int32GetDatum, InvokeObjectPostCreateHook, NameGetDatum, namestrcpy(), NULL, ObjectAddress::objectId, ObjectIdGetDatum, ObjectAddress::objectSubId, OidIsValid, pg_encoding_to_char(), PointerGetDatum, RelationData::rd_att, recordDependencyOn(), recordDependencyOnCurrentExtension(), recordDependencyOnOwner(), RowExclusiveLock, SearchSysCacheExists2, simple_heap_insert(), and values.

Referenced by CreateConversionCommand().

{
    int         i;
    Relation    rel;
    TupleDesc   tupDesc;
    HeapTuple   tup;
    bool        nulls[Natts_pg_conversion];
    Datum       values[Natts_pg_conversion];
    NameData    cname;
    Oid         oid;
    ObjectAddress myself,
                referenced;

    /* sanity checks */
    if (!conname)
        elog(ERROR, "no conversion name supplied");

    /* make sure there is no existing conversion of same name */
    if (SearchSysCacheExists2(CONNAMENSP,
                              PointerGetDatum(conname),
                              ObjectIdGetDatum(connamespace)))
        ereport(ERROR,
                (errcode(ERRCODE_DUPLICATE_OBJECT),
                 errmsg("conversion \"%s\" already exists", conname)));

    if (def)
    {
        /*
         * make sure there is no existing default <for encoding><to encoding>
         * pair in this name space
         */
        if (FindDefaultConversion(connamespace,
                                  conforencoding,
                                  contoencoding))
            ereport(ERROR,
                    (errcode(ERRCODE_DUPLICATE_OBJECT),
                     errmsg("default conversion for %s to %s already exists",
                            pg_encoding_to_char(conforencoding),
                            pg_encoding_to_char(contoencoding))));
    }

    /* open pg_conversion */
    rel = heap_open(ConversionRelationId, RowExclusiveLock);
    tupDesc = rel->rd_att;

    /* initialize nulls and values */
    for (i = 0; i < Natts_pg_conversion; i++)
    {
        nulls[i] = false;
        values[i] = (Datum) NULL;
    }

    /* form a tuple */
    namestrcpy(&cname, conname);
    values[Anum_pg_conversion_conname - 1] = NameGetDatum(&cname);
    values[Anum_pg_conversion_connamespace - 1] = ObjectIdGetDatum(connamespace);
    values[Anum_pg_conversion_conowner - 1] = ObjectIdGetDatum(conowner);
    values[Anum_pg_conversion_conforencoding - 1] = Int32GetDatum(conforencoding);
    values[Anum_pg_conversion_contoencoding - 1] = Int32GetDatum(contoencoding);
    values[Anum_pg_conversion_conproc - 1] = ObjectIdGetDatum(conproc);
    values[Anum_pg_conversion_condefault - 1] = BoolGetDatum(def);

    tup = heap_form_tuple(tupDesc, values, nulls);

    /* insert a new tuple */
    oid = simple_heap_insert(rel, tup);
    Assert(OidIsValid(oid));

    /* update the index if any */
    CatalogUpdateIndexes(rel, tup);

    myself.classId = ConversionRelationId;
    myself.objectId = HeapTupleGetOid(tup);
    myself.objectSubId = 0;

    /* create dependency on conversion procedure */
    referenced.classId = ProcedureRelationId;
    referenced.objectId = conproc;
    referenced.objectSubId = 0;
    recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);

    /* create dependency on namespace */
    referenced.classId = NamespaceRelationId;
    referenced.objectId = connamespace;
    referenced.objectSubId = 0;
    recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);

    /* create dependency on owner */
    recordDependencyOnOwner(ConversionRelationId, HeapTupleGetOid(tup),
                            conowner);

    /* dependency on extension */
    recordDependencyOnCurrentExtension(&myself, false);

    /* Post creation hook for new conversion */
    InvokeObjectPostCreateHook(ConversionRelationId, HeapTupleGetOid(tup), 0);

    heap_freetuple(tup);
    heap_close(rel, RowExclusiveLock);

    return oid;
}

Oid FindDefaultConversion ( Oid  name_space,
int32  for_encoding,
int32  to_encoding 
)

Definition at line 191 of file pg_conversion.c.

References CONDEFAULT, GETSTRUCT, i, Int32GetDatum, catclist::members, catclist::n_members, ObjectIdGetDatum, ReleaseSysCacheList, SearchSysCacheList3, and catctup::tuple.

Referenced by ConversionCreate(), and FindDefaultConversionProc().

{
    CatCList   *catlist;
    HeapTuple   tuple;
    Form_pg_conversion body;
    Oid         proc = InvalidOid;
    int         i;

    catlist = SearchSysCacheList3(CONDEFAULT,
                                  ObjectIdGetDatum(name_space),
                                  Int32GetDatum(for_encoding),
                                  Int32GetDatum(to_encoding));

    for (i = 0; i < catlist->n_members; i++)
    {
        tuple = &catlist->members[i]->tuple;
        body = (Form_pg_conversion) GETSTRUCT(tuple);
        if (body->condefault)
        {
            proc = body->conproc;
            break;
        }
    }
    ReleaseSysCacheList(catlist);
    return proc;
}

void RemoveConversionById ( Oid  conversionOid  ) 

Definition at line 154 of file pg_conversion.c.

References BTEqualStrategyNumber, ConversionRelationId, elog, ERROR, ForwardScanDirection, heap_beginscan(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HeapTupleIsValid, ObjectIdAttributeNumber, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), simple_heap_delete(), SnapshotNow, and HeapTupleData::t_self.

Referenced by doDeletion().

{
    Relation    rel;
    HeapTuple   tuple;
    HeapScanDesc scan;
    ScanKeyData scanKeyData;

    ScanKeyInit(&scanKeyData,
                ObjectIdAttributeNumber,
                BTEqualStrategyNumber, F_OIDEQ,
                ObjectIdGetDatum(conversionOid));

    /* open pg_conversion */
    rel = heap_open(ConversionRelationId, RowExclusiveLock);

    scan = heap_beginscan(rel, SnapshotNow,
                          1, &scanKeyData);

    /* search for the target tuple */
    if (HeapTupleIsValid(tuple = heap_getnext(scan, ForwardScanDirection)))
        simple_heap_delete(rel, &tuple->t_self);
    else
        elog(ERROR, "could not find tuple for conversion %u", conversionOid);
    heap_endscan(scan);
    heap_close(rel, RowExclusiveLock);
}