PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tid.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * tid.c
4  * Functions for the built-in type tuple id
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/tid.c
12  *
13  * NOTES
14  * input routine largely stolen from boxin().
15  *
16  *-------------------------------------------------------------------------
17  */
18 #include "postgres.h"
19 
20 #include <math.h>
21 #include <limits.h>
22 
23 #include "access/heapam.h"
24 #include "access/sysattr.h"
25 #include "catalog/namespace.h"
26 #include "catalog/pg_type.h"
27 #include "libpq/pqformat.h"
28 #include "miscadmin.h"
29 #include "parser/parsetree.h"
30 #include "utils/acl.h"
31 #include "utils/builtins.h"
32 #include "utils/rel.h"
33 #include "utils/snapmgr.h"
34 #include "utils/tqual.h"
35 #include "utils/varlena.h"
36 
37 
38 #define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
39 #define ItemPointerGetDatum(X) PointerGetDatum(X)
40 #define PG_GETARG_ITEMPOINTER(n) DatumGetItemPointer(PG_GETARG_DATUM(n))
41 #define PG_RETURN_ITEMPOINTER(x) return ItemPointerGetDatum(x)
42 
43 #define LDELIM '('
44 #define RDELIM ')'
45 #define DELIM ','
46 #define NTIDARGS 2
47 
48 /* ----------------------------------------------------------------
49  * tidin
50  * ----------------------------------------------------------------
51  */
52 Datum
54 {
55  char *str = PG_GETARG_CSTRING(0);
56  char *p,
57  *coord[NTIDARGS];
58  int i;
60  BlockNumber blockNumber;
61  OffsetNumber offsetNumber;
62  char *badp;
63  int hold_offset;
64 
65  for (i = 0, p = str; *p && i < NTIDARGS && *p != RDELIM; p++)
66  if (*p == DELIM || (*p == LDELIM && !i))
67  coord[i++] = p + 1;
68 
69  if (i < NTIDARGS)
70  ereport(ERROR,
71  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
72  errmsg("invalid input syntax for type %s: \"%s\"",
73  "tid", str)));
74 
75  errno = 0;
76  blockNumber = strtoul(coord[0], &badp, 10);
77  if (errno || *badp != DELIM)
78  ereport(ERROR,
79  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
80  errmsg("invalid input syntax for type %s: \"%s\"",
81  "tid", str)));
82 
83  hold_offset = strtol(coord[1], &badp, 10);
84  if (errno || *badp != RDELIM ||
85  hold_offset > USHRT_MAX || hold_offset < 0)
86  ereport(ERROR,
87  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
88  errmsg("invalid input syntax for type %s: \"%s\"",
89  "tid", str)));
90 
91  offsetNumber = hold_offset;
92 
93  result = (ItemPointer) palloc(sizeof(ItemPointerData));
94 
95  ItemPointerSet(result, blockNumber, offsetNumber);
96 
97  PG_RETURN_ITEMPOINTER(result);
98 }
99 
100 /* ----------------------------------------------------------------
101  * tidout
102  * ----------------------------------------------------------------
103  */
104 Datum
106 {
107  ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
108  BlockNumber blockNumber;
109  OffsetNumber offsetNumber;
110  char buf[32];
111 
112  blockNumber = BlockIdGetBlockNumber(&(itemPtr->ip_blkid));
113  offsetNumber = itemPtr->ip_posid;
114 
115  /* Perhaps someday we should output this as a record. */
116  snprintf(buf, sizeof(buf), "(%u,%u)", blockNumber, offsetNumber);
117 
119 }
120 
121 /*
122  * tidrecv - converts external binary format to tid
123  */
124 Datum
126 {
129  BlockNumber blockNumber;
130  OffsetNumber offsetNumber;
131 
132  blockNumber = pq_getmsgint(buf, sizeof(blockNumber));
133  offsetNumber = pq_getmsgint(buf, sizeof(offsetNumber));
134 
135  result = (ItemPointer) palloc(sizeof(ItemPointerData));
136 
137  ItemPointerSet(result, blockNumber, offsetNumber);
138 
139  PG_RETURN_ITEMPOINTER(result);
140 }
141 
142 /*
143  * tidsend - converts tid to binary format
144  */
145 Datum
147 {
148  ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
149  BlockId blockId;
150  BlockNumber blockNumber;
151  OffsetNumber offsetNumber;
153 
154  blockId = &(itemPtr->ip_blkid);
155  blockNumber = BlockIdGetBlockNumber(blockId);
156  offsetNumber = itemPtr->ip_posid;
157 
158  pq_begintypsend(&buf);
159  pq_sendint(&buf, blockNumber, sizeof(blockNumber));
160  pq_sendint(&buf, offsetNumber, sizeof(offsetNumber));
162 }
163 
164 /*****************************************************************************
165  * PUBLIC ROUTINES *
166  *****************************************************************************/
167 
168 Datum
170 {
173 
174  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) == 0);
175 }
176 
177 Datum
179 {
182 
183  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) != 0);
184 }
185 
186 Datum
188 {
191 
192  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) < 0);
193 }
194 
195 Datum
197 {
200 
201  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) <= 0);
202 }
203 
204 Datum
206 {
209 
210  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) > 0);
211 }
212 
213 Datum
215 {
218 
219  PG_RETURN_BOOL(ItemPointerCompare(arg1, arg2) >= 0);
220 }
221 
222 Datum
224 {
227 
228  PG_RETURN_INT32(ItemPointerCompare(arg1, arg2));
229 }
230 
231 Datum
233 {
236 
237  PG_RETURN_ITEMPOINTER(ItemPointerCompare(arg1, arg2) >= 0 ? arg1 : arg2);
238 }
239 
240 Datum
242 {
245 
246  PG_RETURN_ITEMPOINTER(ItemPointerCompare(arg1, arg2) <= 0 ? arg1 : arg2);
247 }
248 
249 
250 /*
251  * Functions to get latest tid of a specified tuple.
252  *
253  * Maybe these implementations should be moved to another place
254  */
255 
256 static ItemPointerData Current_last_tid = {{0, 0}, 0};
257 
258 void
260 {
261  Current_last_tid = *tid;
262 }
263 
264 /*
265  * Handle CTIDs of views.
266  * CTID should be defined in the view and it must
267  * correspond to the CTID of a base relation.
268  */
269 static Datum
271 {
272  TupleDesc att = RelationGetDescr(viewrel);
273  RuleLock *rulelock;
274  RewriteRule *rewrite;
275  int i,
276  natts = att->natts,
277  tididx = -1;
278 
279  for (i = 0; i < natts; i++)
280  {
281  if (strcmp(NameStr(att->attrs[i]->attname), "ctid") == 0)
282  {
283  if (att->attrs[i]->atttypid != TIDOID)
284  elog(ERROR, "ctid isn't of type TID");
285  tididx = i;
286  break;
287  }
288  }
289  if (tididx < 0)
290  elog(ERROR, "currtid cannot handle views with no CTID");
291  rulelock = viewrel->rd_rules;
292  if (!rulelock)
293  elog(ERROR, "the view has no rules");
294  for (i = 0; i < rulelock->numLocks; i++)
295  {
296  rewrite = rulelock->rules[i];
297  if (rewrite->event == CMD_SELECT)
298  {
299  Query *query;
300  TargetEntry *tle;
301 
302  if (list_length(rewrite->actions) != 1)
303  elog(ERROR, "only one select rule is allowed in views");
304  query = (Query *) linitial(rewrite->actions);
305  tle = get_tle_by_resno(query->targetList, tididx + 1);
306  if (tle && tle->expr && IsA(tle->expr, Var))
307  {
308  Var *var = (Var *) tle->expr;
309  RangeTblEntry *rte;
310 
311  if (!IS_SPECIAL_VARNO(var->varno) &&
313  {
314  rte = rt_fetch(var->varno, query->rtable);
315  if (rte)
316  {
317  heap_close(viewrel, AccessShareLock);
319  }
320  }
321  }
322  break;
323  }
324  }
325  elog(ERROR, "currtid cannot handle this view");
326  return (Datum) 0;
327 }
328 
329 Datum
331 {
332  Oid reloid = PG_GETARG_OID(0);
335  Relation rel;
336  AclResult aclresult;
337  Snapshot snapshot;
338 
339  result = (ItemPointer) palloc(sizeof(ItemPointerData));
340  if (!reloid)
341  {
342  *result = Current_last_tid;
343  PG_RETURN_ITEMPOINTER(result);
344  }
345 
346  rel = heap_open(reloid, AccessShareLock);
347 
348  aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
349  ACL_SELECT);
350  if (aclresult != ACLCHECK_OK)
351  aclcheck_error(aclresult, ACL_KIND_CLASS,
353 
354  if (rel->rd_rel->relkind == RELKIND_VIEW)
355  return currtid_for_view(rel, tid);
356 
357  ItemPointerCopy(tid, result);
358 
359  snapshot = RegisterSnapshot(GetLatestSnapshot());
360  heap_get_latest_tid(rel, snapshot, result);
361  UnregisterSnapshot(snapshot);
362 
364 
365  PG_RETURN_ITEMPOINTER(result);
366 }
367 
368 Datum
370 {
371  text *relname = PG_GETARG_TEXT_PP(0);
374  RangeVar *relrv;
375  Relation rel;
376  AclResult aclresult;
377  Snapshot snapshot;
378 
380  rel = heap_openrv(relrv, AccessShareLock);
381 
382  aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
383  ACL_SELECT);
384  if (aclresult != ACLCHECK_OK)
385  aclcheck_error(aclresult, ACL_KIND_CLASS,
387 
388  if (rel->rd_rel->relkind == RELKIND_VIEW)
389  return currtid_for_view(rel, tid);
390 
391  result = (ItemPointer) palloc(sizeof(ItemPointerData));
392  ItemPointerCopy(tid, result);
393 
394  snapshot = RegisterSnapshot(GetLatestSnapshot());
395  heap_get_latest_tid(rel, snapshot, result);
396  UnregisterSnapshot(snapshot);
397 
399 
400  PG_RETURN_ITEMPOINTER(result);
401 }
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:52
Datum tidlt(PG_FUNCTION_ARGS)
Definition: tid.c:187
#define BlockIdGetBlockNumber(blockId)
Definition: block.h:115
Datum bttidcmp(PG_FUNCTION_ARGS)
Definition: tid.c:223
#define IsA(nodeptr, _type_)
Definition: nodes.h:573
int numLocks
Definition: prs2lock.h:42
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:858
#define RelationGetDescr(relation)
Definition: rel.h:429
#define PG_GETARG_ITEMPOINTER(n)
Definition: tid.c:40
Oid GetUserId(void)
Definition: miscinit.c:283
#define LDELIM
Definition: tid.c:43
Datum tidsmaller(PG_FUNCTION_ARGS)
Definition: tid.c:241
#define PointerGetDatum(X)
Definition: postgres.h:562
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:359
char * pstrdup(const char *in)
Definition: mcxt.c:1077
StringInfoData * StringInfo
Definition: stringinfo.h:46
#define RDELIM
Definition: tid.c:44
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define PG_RETURN_INT32(x)
Definition: fmgr.h:306
#define AccessShareLock
Definition: lockdefs.h:36
void heap_get_latest_tid(Relation relation, Snapshot snapshot, ItemPointer tid)
Definition: heapam.c:2170
int errcode(int sqlerrcode)
Definition: elog.c:575
#define NTIDARGS
Definition: tid.c:46
AttrNumber varattno
Definition: primnodes.h:168
return result
Definition: formatting.c:1618
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:233
uint32 BlockNumber
Definition: block.h:31
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:2913
#define heap_close(r, l)
Definition: heapam.h:97
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:321
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:163
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:379
int natts
Definition: tupdesc.h:73
List * targetList
Definition: parsenodes.h:131
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:265
uint16 OffsetNumber
Definition: off.h:24
ItemPointerData * ItemPointer
Definition: itemptr.h:48
static Datum currtid_for_view(Relation viewrel, ItemPointer tid)
Definition: tid.c:270
Datum tidsend(PG_FUNCTION_ARGS)
Definition: tid.c:146
#define TIDOID
Definition: pg_type.h:332
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:157
#define linitial(l)
Definition: pg_list.h:110
List * rtable
Definition: parsenodes.h:128
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
Datum currtid_byreloid(PG_FUNCTION_ARGS)
Definition: tid.c:330
static ItemPointerData Current_last_tid
Definition: tid.c:256
BlockIdData ip_blkid
Definition: itemptr.h:38
#define PG_RETURN_ITEMPOINTER(x)
Definition: tid.c:41
static char * buf
Definition: pg_test_fsync.c:65
Datum tidrecv(PG_FUNCTION_ARGS)
Definition: tid.c:125
#define PG_GETARG_OID(n)
Definition: fmgr.h:232
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3382
CmdType event
Definition: prs2lock.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:437
RewriteRule ** rules
Definition: prs2lock.h:43
List * actions
Definition: prs2lock.h:29
void setLastTid(const ItemPointer tid)
Definition: tid.c:259
#define ereport(elevel, rest)
Definition: elog.h:122
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3182
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:900
Datum tidge(PG_FUNCTION_ARGS)
Definition: tid.c:214
Index varno
Definition: primnodes.h:166
Datum tidle(PG_FUNCTION_ARGS)
Definition: tid.c:196
Datum tidlarger(PG_FUNCTION_ARGS)
Definition: tid.c:232
AclResult
Definition: acl.h:170
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:311
uintptr_t Datum
Definition: postgres.h:372
#define ACL_SELECT
Definition: parsenodes.h:66
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
Relation heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1315
#define DELIM
Definition: tid.c:45
Datum tidin(PG_FUNCTION_ARGS)
Definition: tid.c:53
Expr * expr
Definition: primnodes.h:1352
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:314
RuleLock * rd_rules
Definition: rel.h:118
static int list_length(const List *l)
Definition: pg_list.h:89
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:375
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4405
Datum currtid_byrelname(PG_FUNCTION_ARGS)
Definition: tid.c:369
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
#define RELKIND_VIEW
Definition: pg_class.h:164
int i
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define NameStr(name)
Definition: c.h:499
Datum tidgt(PG_FUNCTION_ARGS)
Definition: tid.c:205
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:234
Datum tidne(PG_FUNCTION_ARGS)
Definition: tid.c:178
Definition: c.h:439
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
#define SelfItemPointerAttributeNumber
Definition: sysattr.h:21
#define elog
Definition: elog.h:219
OffsetNumber ip_posid
Definition: itemptr.h:39
Datum tidout(PG_FUNCTION_ARGS)
Definition: tid.c:105
#define RelationGetRelid(relation)
Definition: rel.h:417
Datum tideq(PG_FUNCTION_ARGS)
Definition: tid.c:169
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:578
#define ItemPointerSet(pointer, blockNumber, offNum)
Definition: itemptr.h:86
#define ItemPointerCopy(fromPointer, toPointer)
Definition: itemptr.h:120