Header And Logo

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

Defines | Functions | Variables

tid.c File Reference

#include "postgres.h"
#include <math.h>
#include <limits.h>
#include "access/heapam.h"
#include "access/sysattr.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "parser/parsetree.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/tqual.h"
Include dependency graph for tid.c:

Go to the source code of this file.

Defines

#define DatumGetItemPointer(X)   ((ItemPointer) DatumGetPointer(X))
#define ItemPointerGetDatum(X)   PointerGetDatum(X)
#define PG_GETARG_ITEMPOINTER(n)   DatumGetItemPointer(PG_GETARG_DATUM(n))
#define PG_RETURN_ITEMPOINTER(x)   return ItemPointerGetDatum(x)
#define LDELIM   '('
#define RDELIM   ')'
#define DELIM   ','
#define NTIDARGS   2

Functions

Datum tidin (PG_FUNCTION_ARGS)
Datum tidout (PG_FUNCTION_ARGS)
Datum tidrecv (PG_FUNCTION_ARGS)
Datum tidsend (PG_FUNCTION_ARGS)
Datum tideq (PG_FUNCTION_ARGS)
Datum tidne (PG_FUNCTION_ARGS)
Datum tidlt (PG_FUNCTION_ARGS)
Datum tidle (PG_FUNCTION_ARGS)
Datum tidgt (PG_FUNCTION_ARGS)
Datum tidge (PG_FUNCTION_ARGS)
Datum bttidcmp (PG_FUNCTION_ARGS)
Datum tidlarger (PG_FUNCTION_ARGS)
Datum tidsmaller (PG_FUNCTION_ARGS)
void setLastTid (const ItemPointer tid)
static Datum currtid_for_view (Relation viewrel, ItemPointer tid)
Datum currtid_byreloid (PG_FUNCTION_ARGS)
Datum currtid_byrelname (PG_FUNCTION_ARGS)

Variables

static ItemPointerData Current_last_tid = {{0, 0}, 0}

Define Documentation

#define DatumGetItemPointer (   X  )     ((ItemPointer) DatumGetPointer(X))

Definition at line 36 of file tid.c.

#define DELIM   ','

Definition at line 43 of file tid.c.

Referenced by tidin().

#define ItemPointerGetDatum (   X  )     PointerGetDatum(X)

Definition at line 37 of file tid.c.

#define LDELIM   '('

Definition at line 41 of file tid.c.

Referenced by tidin().

#define NTIDARGS   2

Definition at line 44 of file tid.c.

Referenced by tidin().

#define PG_GETARG_ITEMPOINTER (   n  )     DatumGetItemPointer(PG_GETARG_DATUM(n))
#define PG_RETURN_ITEMPOINTER (   x  )     return ItemPointerGetDatum(x)

Definition at line 39 of file tid.c.

Referenced by currtid_byrelname(), currtid_byreloid(), tidin(), tidlarger(), tidrecv(), and tidsmaller().

#define RDELIM   ')'

Definition at line 42 of file tid.c.

Referenced by tidin().


Function Documentation

Datum bttidcmp ( PG_FUNCTION_ARGS   ) 
Datum currtid_byrelname ( PG_FUNCTION_ARGS   ) 
Datum currtid_byreloid ( PG_FUNCTION_ARGS   ) 
static Datum currtid_for_view ( Relation  viewrel,
ItemPointer  tid 
) [static]

Definition at line 268 of file tid.c.

References AccessShareLock, RewriteRule::actions, tupleDesc::attrs, CMD_SELECT, currtid_byreloid(), DirectFunctionCall2, elog, ERROR, RewriteRule::event, TargetEntry::expr, get_tle_by_resno(), heap_close, i, IS_SPECIAL_VARNO, IsA, linitial, list_length(), NameStr, tupleDesc::natts, RuleLock::numLocks, ObjectIdGetDatum, PointerGetDatum, RelationData::rd_rules, RelationGetDescr, rt_fetch, Query::rtable, RuleLock::rules, Query::targetList, TIDOID, Var::varattno, and Var::varno.

Referenced by currtid_byrelname(), and currtid_byreloid().

{
    TupleDesc   att = RelationGetDescr(viewrel);
    RuleLock   *rulelock;
    RewriteRule *rewrite;
    int         i,
                natts = att->natts,
                tididx = -1;

    for (i = 0; i < natts; i++)
    {
        if (strcmp(NameStr(att->attrs[i]->attname), "ctid") == 0)
        {
            if (att->attrs[i]->atttypid != TIDOID)
                elog(ERROR, "ctid isn't of type TID");
            tididx = i;
            break;
        }
    }
    if (tididx < 0)
        elog(ERROR, "currtid cannot handle views with no CTID");
    rulelock = viewrel->rd_rules;
    if (!rulelock)
        elog(ERROR, "the view has no rules");
    for (i = 0; i < rulelock->numLocks; i++)
    {
        rewrite = rulelock->rules[i];
        if (rewrite->event == CMD_SELECT)
        {
            Query      *query;
            TargetEntry *tle;

            if (list_length(rewrite->actions) != 1)
                elog(ERROR, "only one select rule is allowed in views");
            query = (Query *) linitial(rewrite->actions);
            tle = get_tle_by_resno(query->targetList, tididx + 1);
            if (tle && tle->expr && IsA(tle->expr, Var))
            {
                Var        *var = (Var *) tle->expr;
                RangeTblEntry *rte;

                if (!IS_SPECIAL_VARNO(var->varno) &&
                    var->varattno == SelfItemPointerAttributeNumber)
                {
                    rte = rt_fetch(var->varno, query->rtable);
                    if (rte)
                    {
                        heap_close(viewrel, AccessShareLock);
                        return DirectFunctionCall2(currtid_byreloid, ObjectIdGetDatum(rte->relid), PointerGetDatum(tid));
                    }
                }
            }
            break;
        }
    }
    elog(ERROR, "currtid cannot handle this view");
    return (Datum) 0;
}

void setLastTid ( const ItemPointer  tid  ) 

Definition at line 257 of file tid.c.

Referenced by ExecInsert().

{
    Current_last_tid = *tid;
}

Datum tideq ( PG_FUNCTION_ARGS   ) 
Datum tidge ( PG_FUNCTION_ARGS   ) 
Datum tidgt ( PG_FUNCTION_ARGS   ) 
Datum tidin ( PG_FUNCTION_ARGS   ) 

Definition at line 51 of file tid.c.

References DELIM, ereport, errcode(), errmsg(), ERROR, i, ItemPointerSet, LDELIM, NTIDARGS, palloc(), PG_GETARG_CSTRING, PG_RETURN_ITEMPOINTER, and RDELIM.

Referenced by make_tuple_from_result_row().

{
    char       *str = PG_GETARG_CSTRING(0);
    char       *p,
               *coord[NTIDARGS];
    int         i;
    ItemPointer result;
    BlockNumber blockNumber;
    OffsetNumber offsetNumber;
    char       *badp;
    int         hold_offset;

    for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
        if (*p == DELIM || (*p == LDELIM && !i))
            coord[i++] = p + 1;

    if (i < NTIDARGS)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type tid: \"%s\"",
                        str)));

    errno = 0;
    blockNumber = strtoul(coord[0], &badp, 10);
    if (errno || *badp != DELIM)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type tid: \"%s\"",
                        str)));

    hold_offset = strtol(coord[1], &badp, 10);
    if (errno || *badp != RDELIM ||
        hold_offset > USHRT_MAX || hold_offset < 0)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                 errmsg("invalid input syntax for type tid: \"%s\"",
                        str)));

    offsetNumber = hold_offset;

    result = (ItemPointer) palloc(sizeof(ItemPointerData));

    ItemPointerSet(result, blockNumber, offsetNumber);

    PG_RETURN_ITEMPOINTER(result);
}

Datum tidlarger ( PG_FUNCTION_ARGS   ) 

Definition at line 230 of file tid.c.

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_ITEMPOINTER.

Datum tidle ( PG_FUNCTION_ARGS   ) 
Datum tidlt ( PG_FUNCTION_ARGS   ) 
Datum tidne ( PG_FUNCTION_ARGS   ) 
Datum tidout ( PG_FUNCTION_ARGS   ) 

Definition at line 103 of file tid.c.

References BlockIdGetBlockNumber, buf, ItemPointerData::ip_blkid, ItemPointerData::ip_posid, PG_GETARG_ITEMPOINTER, PG_RETURN_CSTRING, pstrdup(), and snprintf().

Referenced by pgrowlocks().

{
    ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
    BlockNumber blockNumber;
    OffsetNumber offsetNumber;
    char        buf[32];

    blockNumber = BlockIdGetBlockNumber(&(itemPtr->ip_blkid));
    offsetNumber = itemPtr->ip_posid;

    /* Perhaps someday we should output this as a record. */
    snprintf(buf, sizeof(buf), "(%u,%u)", blockNumber, offsetNumber);

    PG_RETURN_CSTRING(pstrdup(buf));
}

Datum tidrecv ( PG_FUNCTION_ARGS   ) 

Definition at line 123 of file tid.c.

References buf, ItemPointerSet, palloc(), PG_GETARG_POINTER, PG_RETURN_ITEMPOINTER, and pq_getmsgint().

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    ItemPointer result;
    BlockNumber blockNumber;
    OffsetNumber offsetNumber;

    blockNumber = pq_getmsgint(buf, sizeof(blockNumber));
    offsetNumber = pq_getmsgint(buf, sizeof(offsetNumber));

    result = (ItemPointer) palloc(sizeof(ItemPointerData));

    ItemPointerSet(result, blockNumber, offsetNumber);

    PG_RETURN_ITEMPOINTER(result);
}

Datum tidsend ( PG_FUNCTION_ARGS   ) 

Definition at line 144 of file tid.c.

References BlockIdGetBlockNumber, buf, ItemPointerData::ip_blkid, ItemPointerData::ip_posid, PG_GETARG_ITEMPOINTER, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendint().

{
    ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
    BlockId     blockId;
    BlockNumber blockNumber;
    OffsetNumber offsetNumber;
    StringInfoData buf;

    blockId = &(itemPtr->ip_blkid);
    blockNumber = BlockIdGetBlockNumber(blockId);
    offsetNumber = itemPtr->ip_posid;

    pq_begintypsend(&buf);
    pq_sendint(&buf, blockNumber, sizeof(blockNumber));
    pq_sendint(&buf, offsetNumber, sizeof(offsetNumber));
    PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

Datum tidsmaller ( PG_FUNCTION_ARGS   ) 

Definition at line 239 of file tid.c.

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_ITEMPOINTER.


Variable Documentation

ItemPointerData Current_last_tid = {{0, 0}, 0} [static]

Definition at line 254 of file tid.c.