PostgreSQL Source Code  git master
tid.c File Reference
#include "postgres.h"
#include <math.h>
#include <limits.h>
#include "access/heapam.h"
#include "access/sysattr.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/builtins.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 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

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

◆ DatumGetItemPointer

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

Definition at line 40 of file tid.c.

◆ DELIM

#define DELIM   ','

Definition at line 47 of file tid.c.

◆ ItemPointerGetDatum

#define ItemPointerGetDatum (   X)    PointerGetDatum(X)

Definition at line 41 of file tid.c.

◆ LDELIM

#define LDELIM   '('

Definition at line 45 of file tid.c.

◆ NTIDARGS

#define NTIDARGS   2

Definition at line 48 of file tid.c.

◆ PG_GETARG_ITEMPOINTER

#define PG_GETARG_ITEMPOINTER (   n)    DatumGetItemPointer(PG_GETARG_DATUM(n))

Definition at line 42 of file tid.c.

◆ PG_RETURN_ITEMPOINTER

#define PG_RETURN_ITEMPOINTER (   x)    return ItemPointerGetDatum(x)

Definition at line 43 of file tid.c.

◆ RDELIM

#define RDELIM   ')'

Definition at line 46 of file tid.c.

Function Documentation

◆ bttidcmp()

Datum bttidcmp ( PG_FUNCTION_ARGS  )

Definition at line 234 of file tid.c.

235 {
238 
239  PG_RETURN_INT32(ItemPointerCompare(arg1, arg2));
240 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
#define PG_GETARG_ITEMPOINTER(n)
Definition: tid.c:42

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_INT32.

◆ currtid_byrelname()

Datum currtid_byrelname ( PG_FUNCTION_ARGS  )

Definition at line 412 of file tid.c.

413 {
416  ItemPointer result;
417  RangeVar *relrv;
418  Relation rel;
419 
421  rel = table_openrv(relrv, AccessShareLock);
422 
423  /* grab the latest tuple version associated to this CTID */
424  result = currtid_internal(rel, tid);
425 
427 
428  PG_RETURN_ITEMPOINTER(result);
429 }
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define AccessShareLock
Definition: lockdefs.h:36
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3108
NameData relname
Definition: pg_class.h:38
Definition: c.h:622
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: table.c:102
static ItemPointer currtid_internal(Relation rel, ItemPointer tid)
Definition: tid.c:300
#define PG_RETURN_ITEMPOINTER(x)
Definition: tid.c:43
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3657

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 340 of file tid.c.

341 {
342  TupleDesc att = RelationGetDescr(viewrel);
343  RuleLock *rulelock;
344  RewriteRule *rewrite;
345  int i,
346  natts = att->natts,
347  tididx = -1;
348 
349  for (i = 0; i < natts; i++)
350  {
351  Form_pg_attribute attr = TupleDescAttr(att, i);
352 
353  if (strcmp(NameStr(attr->attname), "ctid") == 0)
354  {
355  if (attr->atttypid != TIDOID)
356  elog(ERROR, "ctid isn't of type TID");
357  tididx = i;
358  break;
359  }
360  }
361  if (tididx < 0)
362  elog(ERROR, "currtid cannot handle views with no CTID");
363  rulelock = viewrel->rd_rules;
364  if (!rulelock)
365  elog(ERROR, "the view has no rules");
366  for (i = 0; i < rulelock->numLocks; i++)
367  {
368  rewrite = rulelock->rules[i];
369  if (rewrite->event == CMD_SELECT)
370  {
371  Query *query;
372  TargetEntry *tle;
373 
374  if (list_length(rewrite->actions) != 1)
375  elog(ERROR, "only one select rule is allowed in views");
376  query = (Query *) linitial(rewrite->actions);
377  tle = get_tle_by_resno(query->targetList, tididx + 1);
378  if (tle && tle->expr && IsA(tle->expr, Var))
379  {
380  Var *var = (Var *) tle->expr;
381  RangeTblEntry *rte;
382 
383  if (!IS_SPECIAL_VARNO(var->varno) &&
385  {
386  rte = rt_fetch(var->varno, query->rtable);
387  if (rte)
388  {
389  ItemPointer result;
390  Relation rel;
391 
392  rel = table_open(rte->relid, AccessShareLock);
393  result = currtid_internal(rel, tid);
395  return result;
396  }
397  }
398  }
399  break;
400  }
401  }
402  elog(ERROR, "currtid cannot handle this view");
403  return NULL;
404 }
#define NameStr(name)
Definition: c.h:681
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
int i
Definition: isn.c:73
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
@ CMD_SELECT
Definition: nodes.h:721
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:207
static int list_length(const List *l)
Definition: pg_list.h:149
#define linitial(l)
Definition: pg_list.h:174
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:189
#define RelationGetDescr(relation)
Definition: rel.h:515
List * rtable
Definition: parsenodes.h:148
List * targetList
Definition: parsenodes.h:155
RuleLock * rd_rules
Definition: rel.h:113
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:1716
Definition: primnodes.h:196
AttrNumber varattno
Definition: primnodes.h:200
int varno
Definition: primnodes.h:198
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#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 300 of file tid.c.

301 {
302  ItemPointer result;
303  AclResult aclresult;
304  Snapshot snapshot;
305  TableScanDesc scan;
306 
307  result = (ItemPointer) palloc(sizeof(ItemPointerData));
308 
309  aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
310  ACL_SELECT);
311  if (aclresult != ACLCHECK_OK)
312  aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
314 
315  if (rel->rd_rel->relkind == RELKIND_VIEW)
316  return currtid_for_view(rel, tid);
317 
318  if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
319  elog(ERROR, "cannot look at latest visible tid for relation \"%s.%s\"",
322 
323  ItemPointerCopy(tid, result);
324 
325  snapshot = RegisterSnapshot(GetLatestSnapshot());
326  scan = table_beginscan_tid(rel, snapshot);
327  table_tuple_get_latest_tid(scan, result);
328  table_endscan(scan);
329  UnregisterSnapshot(snapshot);
330 
331  return result;
332 }
AclResult
Definition: acl.h:181
@ ACLCHECK_OK
Definition: acl.h:182
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3512
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:5007
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:161
ItemPointerData * ItemPointer
Definition: itemptr.h:49
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3326
void * palloc(Size size)
Definition: mcxt.c:1068
Oid GetUserId(void)
Definition: miscinit.c:492
ObjectType get_relkind_objtype(char relkind)
#define ACL_SELECT
Definition: parsenodes.h:83
#define RelationGetRelid(relation)
Definition: rel.h:489
#define RelationGetRelationName(relation)
Definition: rel.h:523
#define RelationGetNamespace(relation)
Definition: rel.h:530
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:325
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:869
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:827
Form_pg_class rd_rel
Definition: rel.h:109
void table_tuple_get_latest_tid(TableScanDesc scan, ItemPointer tid)
Definition: tableam.c:246
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:993
static TableScanDesc table_beginscan_tid(Relation rel, Snapshot snapshot)
Definition: tableam.h:969
static ItemPointer currtid_for_view(Relation viewrel, ItemPointer tid)
Definition: tid.c:340

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 261 of file tid.c.

262 {
264 
265  /*
266  * While you'll probably have a lot of trouble with a compiler that
267  * insists on appending pad space to struct ItemPointerData, we can at
268  * least make this code work, by not using sizeof(ItemPointerData).
269  * Instead rely on knowing the sizes of the component fields.
270  */
271  return hash_any((unsigned char *) key,
272  sizeof(BlockIdData) + sizeof(OffsetNumber));
273 }
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 276 of file tid.c.

277 {
279  uint64 seed = PG_GETARG_INT64(1);
280 
281  /* As above */
282  return hash_any_extended((unsigned char *) key,
283  sizeof(BlockIdData) + sizeof(OffsetNumber),
284  seed);
285 }
#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 180 of file tid.c.

181 {
184 
185  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) == 0);
186 }
#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 225 of file tid.c.

226 {
229 
230  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) >= 0);
231 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidgt()

Datum tidgt ( PG_FUNCTION_ARGS  )

Definition at line 216 of file tid.c.

217 {
220 
221  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) > 0);
222 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidin()

Datum tidin ( PG_FUNCTION_ARGS  )

Definition at line 57 of file tid.c.

58 {
59  char *str = PG_GETARG_CSTRING(0);
60  char *p,
61  *coord[NTIDARGS];
62  int i;
63  ItemPointer result;
64  BlockNumber blockNumber;
65  OffsetNumber offsetNumber;
66  char *badp;
67  unsigned long cvt;
68 
69  for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
70  if (*p == DELIM || (*p == LDELIM && i == 0))
71  coord[i++] = p + 1;
72 
73  if (i < NTIDARGS)
74  ereport(ERROR,
75  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
76  errmsg("invalid input syntax for type %s: \"%s\"",
77  "tid", str)));
78 
79  errno = 0;
80  cvt = strtoul(coord[0], &badp, 10);
81  if (errno || *badp != DELIM)
82  ereport(ERROR,
83  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
84  errmsg("invalid input syntax for type %s: \"%s\"",
85  "tid", str)));
86  blockNumber = (BlockNumber) cvt;
87 
88  /*
89  * Cope with possibility that unsigned long is wider than BlockNumber, in
90  * which case strtoul will not raise an error for some values that are out
91  * of the range of BlockNumber. (See similar code in oidin().)
92  */
93 #if SIZEOF_LONG > 4
94  if (cvt != (unsigned long) blockNumber &&
95  cvt != (unsigned long) ((int32) blockNumber))
96  ereport(ERROR,
97  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
98  errmsg("invalid input syntax for type %s: \"%s\"",
99  "tid", str)));
100 #endif
101 
102  cvt = strtoul(coord[1], &badp, 10);
103  if (errno || *badp != RDELIM ||
104  cvt > USHRT_MAX)
105  ereport(ERROR,
106  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
107  errmsg("invalid input syntax for type %s: \"%s\"",
108  "tid", str)));
109  offsetNumber = (OffsetNumber) cvt;
110 
111  result = (ItemPointer) palloc(sizeof(ItemPointerData));
112 
113  ItemPointerSet(result, blockNumber, offsetNumber);
114 
115  PG_RETURN_ITEMPOINTER(result);
116 }
uint32 BlockNumber
Definition: block.h:31
signed int int32
Definition: c.h:429
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:127
#define DELIM
Definition: tid.c:47
#define NTIDARGS
Definition: tid.c:48
#define RDELIM
Definition: tid.c:46
#define LDELIM
Definition: tid.c:45

References DELIM, ereport, errcode(), errmsg(), ERROR, 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 243 of file tid.c.

244 {
247 
248  PG_RETURN_ITEMPOINTER(ItemPointerCompare(arg1, arg2) >= 0 ? arg1 : arg2);
249 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_ITEMPOINTER.

◆ tidle()

Datum tidle ( PG_FUNCTION_ARGS  )

Definition at line 207 of file tid.c.

208 {
211 
212  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) <= 0);
213 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidlt()

Datum tidlt ( PG_FUNCTION_ARGS  )

Definition at line 198 of file tid.c.

199 {
202 
203  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) < 0);
204 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidne()

Datum tidne ( PG_FUNCTION_ARGS  )

Definition at line 189 of file tid.c.

190 {
193 
194  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) != 0);
195 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_BOOL.

◆ tidout()

Datum tidout ( PG_FUNCTION_ARGS  )

Definition at line 123 of file tid.c.

124 {
125  ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
126  BlockNumber blockNumber;
127  OffsetNumber offsetNumber;
128  char buf[32];
129 
130  blockNumber = ItemPointerGetBlockNumberNoCheck(itemPtr);
131  offsetNumber = ItemPointerGetOffsetNumberNoCheck(itemPtr);
132 
133  /* Perhaps someday we should output this as a record. */
134  snprintf(buf, sizeof(buf), "(%u,%u)", blockNumber, offsetNumber);
135 
137 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define ItemPointerGetBlockNumberNoCheck(pointer)
Definition: itemptr.h:89
#define ItemPointerGetOffsetNumberNoCheck(pointer)
Definition: itemptr.h:108
char * pstrdup(const char *in)
Definition: mcxt.c:1305
static char * buf
Definition: pg_test_fsync.c:67
#define snprintf
Definition: port.h:225

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 143 of file tid.c.

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

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

◆ tidsend()

Datum tidsend ( PG_FUNCTION_ARGS  )

Definition at line 164 of file tid.c.

165 {
166  ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
168 
173 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137

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 252 of file tid.c.

253 {
256 
257  PG_RETURN_ITEMPOINTER(ItemPointerCompare(arg1, arg2) <= 0 ? arg1 : arg2);
258 }

References ItemPointerCompare(), PG_GETARG_ITEMPOINTER, and PG_RETURN_ITEMPOINTER.