PostgreSQL Source Code  git master
tid.c File Reference
#include "postgres.h"
#include <math.h>
#include <limits.h>
#include "access/sysattr.h"
#include "access/table.h"
#include "access/tableam.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "parser/parsetree.h"
#include "utils/acl.h"
#include "utils/fmgrprotos.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/varlena.h"
Include dependency graph for tid.c:

Go to the source code of this file.

Macros

#define LDELIM   '('
 
#define RDELIM   ')'
 
#define DELIM   ','
 
#define NTIDARGS   2
 

Functions

static ItemPointer currtid_for_view (Relation viewrel, ItemPointer tid)
 
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)
 
Datum hashtid (PG_FUNCTION_ARGS)
 
Datum hashtidextended (PG_FUNCTION_ARGS)
 
static ItemPointer currtid_internal (Relation rel, ItemPointer tid)
 
Datum currtid_byrelname (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ DELIM

#define DELIM   ','

Definition at line 42 of file tid.c.

◆ LDELIM

#define LDELIM   '('

Definition at line 40 of file tid.c.

◆ NTIDARGS

#define NTIDARGS   2

Definition at line 43 of file tid.c.

◆ RDELIM

#define RDELIM   ')'

Definition at line 41 of file tid.c.

Function Documentation

◆ bttidcmp()

Datum bttidcmp ( PG_FUNCTION_ARGS  )

Definition at line 230 of file tid.c.

231 {
234 
235  PG_RETURN_INT32(ItemPointerCompare(arg1, arg2));
236 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:51
#define PG_GETARG_ITEMPOINTER(n)
Definition: itemptr.h:242

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_INT32.

◆ currtid_byrelname()

Datum currtid_byrelname ( PG_FUNCTION_ARGS  )

Definition at line 408 of file tid.c.

409 {
412  ItemPointer result;
413  RangeVar *relrv;
414  Relation rel;
415 
417  rel = table_openrv(relrv, AccessShareLock);
418 
419  /* grab the latest tuple version associated to this CTID */
420  result = currtid_internal(rel, tid);
421 
423 
424  PG_RETURN_ITEMPOINTER(result);
425 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_ITEMPOINTER(x)
Definition: itemptr.h:243
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(const List *names)
Definition: namespace.c:3539
NameData relname
Definition: pg_class.h:38
Definition: c.h:674
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: table.c:83
static ItemPointer currtid_internal(Relation rel, ItemPointer tid)
Definition: tid.c:296
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3398

References AccessShareLock, currtid_internal(), makeRangeVarFromNameList(), PG_GETARG_ITEMPOINTER, PG_GETARG_TEXT_PP, PG_RETURN_ITEMPOINTER, relname, table_close(), table_openrv(), and textToQualifiedNameList().

◆ currtid_for_view()

static ItemPointer currtid_for_view ( Relation  viewrel,
ItemPointer  tid 
)
static

Definition at line 336 of file tid.c.

337 {
338  TupleDesc att = RelationGetDescr(viewrel);
339  RuleLock *rulelock;
340  RewriteRule *rewrite;
341  int i,
342  natts = att->natts,
343  tididx = -1;
344 
345  for (i = 0; i < natts; i++)
346  {
347  Form_pg_attribute attr = TupleDescAttr(att, i);
348 
349  if (strcmp(NameStr(attr->attname), "ctid") == 0)
350  {
351  if (attr->atttypid != TIDOID)
352  elog(ERROR, "ctid isn't of type TID");
353  tididx = i;
354  break;
355  }
356  }
357  if (tididx < 0)
358  elog(ERROR, "currtid cannot handle views with no CTID");
359  rulelock = viewrel->rd_rules;
360  if (!rulelock)
361  elog(ERROR, "the view has no rules");
362  for (i = 0; i < rulelock->numLocks; i++)
363  {
364  rewrite = rulelock->rules[i];
365  if (rewrite->event == CMD_SELECT)
366  {
367  Query *query;
368  TargetEntry *tle;
369 
370  if (list_length(rewrite->actions) != 1)
371  elog(ERROR, "only one select rule is allowed in views");
372  query = (Query *) linitial(rewrite->actions);
373  tle = get_tle_by_resno(query->targetList, tididx + 1);
374  if (tle && tle->expr && IsA(tle->expr, Var))
375  {
376  Var *var = (Var *) tle->expr;
377  RangeTblEntry *rte;
378 
379  if (!IS_SPECIAL_VARNO(var->varno) &&
381  {
382  rte = rt_fetch(var->varno, query->rtable);
383  if (rte)
384  {
385  ItemPointer result;
386  Relation rel;
387 
388  rel = table_open(rte->relid, AccessShareLock);
389  result = currtid_internal(rel, tid);
391  return result;
392  }
393  }
394  }
395  break;
396  }
397  }
398  elog(ERROR, "currtid cannot handle this view");
399  return NULL;
400 }
#define NameStr(name)
Definition: c.h:733
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
@ CMD_SELECT
Definition: nodes.h:255
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:227
#define RelationGetDescr(relation)
Definition: rel.h:531
List * rtable
Definition: parsenodes.h:168
List * targetList
Definition: parsenodes.h:190
RuleLock * rd_rules
Definition: rel.h:115
CmdType event
Definition: prs2lock.h:27
List * actions
Definition: prs2lock.h:29
RewriteRule ** rules
Definition: prs2lock.h:43
int numLocks
Definition: prs2lock.h:42
Expr * expr
Definition: primnodes.h:1943
Definition: primnodes.h:234
AttrNumber varattno
Definition: primnodes.h:246
int varno
Definition: primnodes.h:241
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References AccessShareLock, RewriteRule::actions, CMD_SELECT, currtid_internal(), elog, ERROR, RewriteRule::event, TargetEntry::expr, get_tle_by_resno(), i, if(), IS_SPECIAL_VARNO, IsA, linitial, list_length(), NameStr, TupleDescData::natts, RuleLock::numLocks, RelationData::rd_rules, RelationGetDescr, rt_fetch, Query::rtable, RuleLock::rules, SelfItemPointerAttributeNumber, table_close(), table_open(), Query::targetList, TupleDescAttr, Var::varattno, and Var::varno.

Referenced by currtid_internal().

◆ currtid_internal()

static ItemPointer currtid_internal ( Relation  rel,
ItemPointer  tid 
)
static

Definition at line 296 of file tid.c.

297 {
298  ItemPointer result;
299  AclResult aclresult;
300  Snapshot snapshot;
301  TableScanDesc scan;
302 
303  result = (ItemPointer) palloc(sizeof(ItemPointerData));
304 
305  aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
306  ACL_SELECT);
307  if (aclresult != ACLCHECK_OK)
308  aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
310 
311  if (rel->rd_rel->relkind == RELKIND_VIEW)
312  return currtid_for_view(rel, tid);
313 
314  if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
315  elog(ERROR, "cannot look at latest visible tid for relation \"%s.%s\"",
318 
319  ItemPointerCopy(tid, result);
320 
321  snapshot = RegisterSnapshot(GetLatestSnapshot());
322  scan = table_beginscan_tid(rel, snapshot);
323  table_tuple_get_latest_tid(scan, result);
324  table_endscan(scan);
325  UnregisterSnapshot(snapshot);
326 
327  return result;
328 }
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2688
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4079
ItemPointerData * ItemPointer
Definition: itemptr.h:49
static void ItemPointerCopy(const ItemPointerData *fromPointer, ItemPointerData *toPointer)
Definition: itemptr.h:172
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3322
void * palloc(Size size)
Definition: mcxt.c:1304
Oid GetUserId(void)
Definition: miscinit.c:514
ObjectType get_relkind_objtype(char relkind)
#define ACL_SELECT
Definition: parsenodes.h:77
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RelationGetNamespace(relation)
Definition: rel.h:546
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:291
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:836
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:794
Form_pg_class rd_rel
Definition: rel.h:111
void table_tuple_get_latest_tid(TableScanDesc scan, ItemPointer tid)
Definition: tableam.c:235
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1009
static TableScanDesc table_beginscan_tid(Relation rel, Snapshot snapshot)
Definition: tableam.h:985
static ItemPointer currtid_for_view(Relation viewrel, ItemPointer tid)
Definition: tid.c:336

References ACL_SELECT, aclcheck_error(), ACLCHECK_OK, currtid_for_view(), elog, ERROR, get_namespace_name(), get_relkind_objtype(), GetLatestSnapshot(), GetUserId(), ItemPointerCopy(), palloc(), pg_class_aclcheck(), RelationData::rd_rel, RegisterSnapshot(), RelationGetNamespace, RelationGetRelationName, RelationGetRelid, table_beginscan_tid(), table_endscan(), table_tuple_get_latest_tid(), and UnregisterSnapshot().

Referenced by currtid_byrelname(), and currtid_for_view().

◆ hashtid()

Datum hashtid ( PG_FUNCTION_ARGS  )

Definition at line 257 of file tid.c.

258 {
260 
261  /*
262  * While you'll probably have a lot of trouble with a compiler that
263  * insists on appending pad space to struct ItemPointerData, we can at
264  * least make this code work, by not using sizeof(ItemPointerData).
265  * Instead rely on knowing the sizes of the component fields.
266  */
267  return hash_any((unsigned char *) key,
268  sizeof(BlockIdData) + sizeof(OffsetNumber));
269 }
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
uint16 OffsetNumber
Definition: off.h:24

References hash_any(), sort-test::key, and PG_GETARG_ITEMPOINTER.

◆ hashtidextended()

Datum hashtidextended ( PG_FUNCTION_ARGS  )

Definition at line 272 of file tid.c.

273 {
275  uint64 seed = PG_GETARG_INT64(1);
276 
277  /* As above */
278  return hash_any_extended((unsigned char *) key,
279  sizeof(BlockIdData) + sizeof(OffsetNumber),
280  seed);
281 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37

References hash_any_extended(), sort-test::key, PG_GETARG_INT64, and PG_GETARG_ITEMPOINTER.

◆ tideq()

Datum tideq ( PG_FUNCTION_ARGS  )

Definition at line 176 of file tid.c.

177 {
180 
181  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) == 0);
182 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidge()

Datum tidge ( PG_FUNCTION_ARGS  )

Definition at line 221 of file tid.c.

222 {
225 
226  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) >= 0);
227 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidgt()

Datum tidgt ( PG_FUNCTION_ARGS  )

Definition at line 212 of file tid.c.

213 {
216 
217  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) > 0);
218 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidin()

Datum tidin ( PG_FUNCTION_ARGS  )

Definition at line 52 of file tid.c.

53 {
54  char *str = PG_GETARG_CSTRING(0);
55  Node *escontext = fcinfo->context;
56  char *p,
57  *coord[NTIDARGS];
58  int i;
59  ItemPointer result;
60  BlockNumber blockNumber;
61  OffsetNumber offsetNumber;
62  char *badp;
63  unsigned long cvt;
64 
65  for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
66  if (*p == DELIM || (*p == LDELIM && i == 0))
67  coord[i++] = p + 1;
68 
69  if (i < NTIDARGS)
70  ereturn(escontext, (Datum) 0,
71  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
72  errmsg("invalid input syntax for type %s: \"%s\"",
73  "tid", str)));
74 
75  errno = 0;
76  cvt = strtoul(coord[0], &badp, 10);
77  if (errno || *badp != DELIM)
78  ereturn(escontext, (Datum) 0,
79  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
80  errmsg("invalid input syntax for type %s: \"%s\"",
81  "tid", str)));
82  blockNumber = (BlockNumber) cvt;
83 
84  /*
85  * Cope with possibility that unsigned long is wider than BlockNumber, in
86  * which case strtoul will not raise an error for some values that are out
87  * of the range of BlockNumber. (See similar code in oidin().)
88  */
89 #if SIZEOF_LONG > 4
90  if (cvt != (unsigned long) blockNumber &&
91  cvt != (unsigned long) ((int32) blockNumber))
92  ereturn(escontext, (Datum) 0,
93  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
94  errmsg("invalid input syntax for type %s: \"%s\"",
95  "tid", str)));
96 #endif
97 
98  cvt = strtoul(coord[1], &badp, 10);
99  if (errno || *badp != RDELIM ||
100  cvt > USHRT_MAX)
101  ereturn(escontext, (Datum) 0,
102  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
103  errmsg("invalid input syntax for type %s: \"%s\"",
104  "tid", str)));
105  offsetNumber = (OffsetNumber) cvt;
106 
107  result = (ItemPointer) palloc(sizeof(ItemPointerData));
108 
109  ItemPointerSet(result, blockNumber, offsetNumber);
110 
111  PG_RETURN_ITEMPOINTER(result);
112 }
uint32 BlockNumber
Definition: block.h:31
signed int int32
Definition: c.h:481
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
uintptr_t Datum
Definition: postgres.h:64
Definition: nodes.h:129
#define DELIM
Definition: tid.c:42
#define NTIDARGS
Definition: tid.c:43
#define RDELIM
Definition: tid.c:41
#define LDELIM
Definition: tid.c:40

References DELIM, ereturn, errcode(), errmsg(), i, ItemPointerSet(), LDELIM, NTIDARGS, palloc(), PG_GETARG_CSTRING, PG_RETURN_ITEMPOINTER, RDELIM, and generate_unaccent_rules::str.

Referenced by make_tuple_from_result_row().

◆ tidlarger()

Datum tidlarger ( PG_FUNCTION_ARGS  )

Definition at line 239 of file tid.c.

240 {
243 
244  PG_RETURN_ITEMPOINTER(ItemPointerCompare(arg1, arg2) >= 0 ? arg1 : arg2);
245 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_ITEMPOINTER.

◆ tidle()

Datum tidle ( PG_FUNCTION_ARGS  )

Definition at line 203 of file tid.c.

204 {
207 
208  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) <= 0);
209 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidlt()

Datum tidlt ( PG_FUNCTION_ARGS  )

Definition at line 194 of file tid.c.

195 {
198 
199  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) < 0);
200 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidne()

Datum tidne ( PG_FUNCTION_ARGS  )

Definition at line 185 of file tid.c.

186 {
189 
190  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) != 0);
191 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidout()

Datum tidout ( PG_FUNCTION_ARGS  )

Definition at line 119 of file tid.c.

120 {
121  ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
122  BlockNumber blockNumber;
123  OffsetNumber offsetNumber;
124  char buf[32];
125 
126  blockNumber = ItemPointerGetBlockNumberNoCheck(itemPtr);
127  offsetNumber = ItemPointerGetOffsetNumberNoCheck(itemPtr);
128 
129  /* Perhaps someday we should output this as a record. */
130  snprintf(buf, sizeof(buf), "(%u,%u)", blockNumber, offsetNumber);
131 
133 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
static OffsetNumber ItemPointerGetOffsetNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:114
static BlockNumber ItemPointerGetBlockNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:93
char * pstrdup(const char *in)
Definition: mcxt.c:1683
static char * buf
Definition: pg_test_fsync.c:73
#define snprintf
Definition: port.h:238

References buf, ItemPointerGetBlockNumberNoCheck(), ItemPointerGetOffsetNumberNoCheck(), PG_GETARG_ITEMPOINTER, PG_RETURN_CSTRING, pstrdup(), and snprintf.

Referenced by pgrowlocks().

◆ tidrecv()

Datum tidrecv ( PG_FUNCTION_ARGS  )

Definition at line 139 of file tid.c.

140 {
142  ItemPointer result;
143  BlockNumber blockNumber;
144  OffsetNumber offsetNumber;
145 
146  blockNumber = pq_getmsgint(buf, sizeof(blockNumber));
147  offsetNumber = pq_getmsgint(buf, sizeof(offsetNumber));
148 
149  result = (ItemPointer) palloc(sizeof(ItemPointerData));
150 
151  ItemPointerSet(result, blockNumber, offsetNumber);
152 
153  PG_RETURN_ITEMPOINTER(result);
154 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:415
StringInfoData * StringInfo
Definition: stringinfo.h:54

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

◆ tidsend()

Datum tidsend ( PG_FUNCTION_ARGS  )

Definition at line 160 of file tid.c.

161 {
162  ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
164 
169 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:136

References buf, ItemPointerGetBlockNumberNoCheck(), ItemPointerGetOffsetNumberNoCheck(), PG_GETARG_ITEMPOINTER, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint16(), and pq_sendint32().

◆ tidsmaller()

Datum tidsmaller ( PG_FUNCTION_ARGS  )

Definition at line 248 of file tid.c.

249 {
252 
253  PG_RETURN_ITEMPOINTER(ItemPointerCompare(arg1, arg2) <= 0 ? arg1 : arg2);
254 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_ITEMPOINTER.