PostgreSQL Source Code  git master
pgrowlocks.c
Go to the documentation of this file.
1 /*
2  * contrib/pgrowlocks/pgrowlocks.c
3  *
4  * Copyright (c) 2005-2006 Tatsuo Ishii
5  *
6  * Permission to use, copy, modify, and distribute this software and
7  * its documentation for any purpose, without fee, and without a
8  * written agreement is hereby granted, provided that the above
9  * copyright notice and this paragraph and the following two
10  * paragraphs appear in all copies.
11  *
12  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
13  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
14  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
15  * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
16  * OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
21  * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
22  * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  */
24 
25 #include "postgres.h"
26 
27 #include "access/multixact.h"
28 #include "access/relscan.h"
29 #include "access/xact.h"
30 #include "catalog/namespace.h"
31 #include "catalog/pg_authid.h"
32 #include "funcapi.h"
33 #include "miscadmin.h"
34 #include "storage/bufmgr.h"
35 #include "storage/procarray.h"
36 #include "utils/acl.h"
37 #include "utils/builtins.h"
38 #include "utils/rel.h"
39 #include "utils/snapmgr.h"
40 #include "utils/tqual.h"
41 #include "utils/varlena.h"
42 
44 
46 
47 /* ----------
48  * pgrowlocks:
49  * returns tids of rows being locked
50  * ----------
51  */
52 
53 #define NCHARS 32
54 
55 typedef struct
56 {
59  int ncolumns;
60 } MyData;
61 
62 #define Atnum_tid 0
63 #define Atnum_xmax 1
64 #define Atnum_ismulti 2
65 #define Atnum_xids 3
66 #define Atnum_modes 4
67 #define Atnum_pids 5
68 
69 Datum
71 {
72  FuncCallContext *funcctx;
73  HeapScanDesc scan;
74  HeapTuple tuple;
75  TupleDesc tupdesc;
76  AttInMetadata *attinmeta;
77  Datum result;
78  MyData *mydata;
79  Relation rel;
80 
81  if (SRF_IS_FIRSTCALL())
82  {
83  text *relname;
84  RangeVar *relrv;
85  MemoryContext oldcontext;
86  AclResult aclresult;
87 
88  funcctx = SRF_FIRSTCALL_INIT();
89  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
90 
91  /* Build a tuple descriptor for our result type */
92  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
93  elog(ERROR, "return type must be a row type");
94 
95  attinmeta = TupleDescGetAttInMetadata(tupdesc);
96  funcctx->attinmeta = attinmeta;
97 
98  relname = PG_GETARG_TEXT_PP(0);
100  rel = relation_openrv(relrv, AccessShareLock);
101 
102  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
103  ereport(ERROR,
104  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
105  errmsg("\"%s\" is a partitioned table",
107  errdetail("Partitioned tables do not contain rows.")));
108  else if (rel->rd_rel->relkind != RELKIND_RELATION)
109  ereport(ERROR,
110  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
111  errmsg("\"%s\" is not a table",
112  RelationGetRelationName(rel))));
113 
114  /*
115  * check permissions: must have SELECT on table or be in
116  * pg_stat_scan_tables
117  */
118  aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
119  ACL_SELECT);
120  if (aclresult != ACLCHECK_OK)
122 
123  if (aclresult != ACLCHECK_OK)
124  aclcheck_error(aclresult, ACL_KIND_CLASS,
126 
127  scan = heap_beginscan(rel, GetActiveSnapshot(), 0, NULL);
128  mydata = palloc(sizeof(*mydata));
129  mydata->rel = rel;
130  mydata->scan = scan;
131  mydata->ncolumns = tupdesc->natts;
132  funcctx->user_fctx = mydata;
133 
134  MemoryContextSwitchTo(oldcontext);
135  }
136 
137  funcctx = SRF_PERCALL_SETUP();
138  attinmeta = funcctx->attinmeta;
139  mydata = (MyData *) funcctx->user_fctx;
140  scan = mydata->scan;
141 
142  /* scan the relation */
143  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
144  {
145  HTSU_Result htsu;
146  TransactionId xmax;
147  uint16 infomask;
148 
149  /* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
151 
152  htsu = HeapTupleSatisfiesUpdate(tuple,
153  GetCurrentCommandId(false),
154  scan->rs_cbuf);
155  xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
156  infomask = tuple->t_data->t_infomask;
157 
158  /*
159  * A tuple is locked if HTSU returns BeingUpdated.
160  */
161  if (htsu == HeapTupleBeingUpdated)
162  {
163  char **values;
164 
165  values = (char **) palloc(mydata->ncolumns * sizeof(char *));
166 
167  values[Atnum_tid] = (char *) DirectFunctionCall1(tidout,
168  PointerGetDatum(&tuple->t_self));
169 
170  values[Atnum_xmax] = palloc(NCHARS * sizeof(char));
171  snprintf(values[Atnum_xmax], NCHARS, "%d", xmax);
172  if (infomask & HEAP_XMAX_IS_MULTI)
173  {
174  MultiXactMember *members;
175  int nmembers;
176  bool first = true;
177  bool allow_old;
178 
179  values[Atnum_ismulti] = pstrdup("true");
180 
181  allow_old = HEAP_LOCKED_UPGRADED(infomask);
182  nmembers = GetMultiXactIdMembers(xmax, &members, allow_old,
183  false);
184  if (nmembers == -1)
185  {
186  values[Atnum_xids] = "{0}";
187  values[Atnum_modes] = "{transient upgrade status}";
188  values[Atnum_pids] = "{0}";
189  }
190  else
191  {
192  int j;
193 
194  values[Atnum_xids] = palloc(NCHARS * nmembers);
195  values[Atnum_modes] = palloc(NCHARS * nmembers);
196  values[Atnum_pids] = palloc(NCHARS * nmembers);
197 
198  strcpy(values[Atnum_xids], "{");
199  strcpy(values[Atnum_modes], "{");
200  strcpy(values[Atnum_pids], "{");
201 
202  for (j = 0; j < nmembers; j++)
203  {
204  char buf[NCHARS];
205 
206  if (!first)
207  {
208  strcat(values[Atnum_xids], ",");
209  strcat(values[Atnum_modes], ",");
210  strcat(values[Atnum_pids], ",");
211  }
212  snprintf(buf, NCHARS, "%d", members[j].xid);
213  strcat(values[Atnum_xids], buf);
214  switch (members[j].status)
215  {
217  snprintf(buf, NCHARS, "Update");
218  break;
220  snprintf(buf, NCHARS, "No Key Update");
221  break;
223  snprintf(buf, NCHARS, "For Update");
224  break;
226  snprintf(buf, NCHARS, "For No Key Update");
227  break;
229  snprintf(buf, NCHARS, "Share");
230  break;
232  snprintf(buf, NCHARS, "Key Share");
233  break;
234  }
235  strcat(values[Atnum_modes], buf);
236  snprintf(buf, NCHARS, "%d",
237  BackendXidGetPid(members[j].xid));
238  strcat(values[Atnum_pids], buf);
239 
240  first = false;
241  }
242 
243  strcat(values[Atnum_xids], "}");
244  strcat(values[Atnum_modes], "}");
245  strcat(values[Atnum_pids], "}");
246  }
247  }
248  else
249  {
250  values[Atnum_ismulti] = pstrdup("false");
251 
252  values[Atnum_xids] = palloc(NCHARS * sizeof(char));
253  snprintf(values[Atnum_xids], NCHARS, "{%d}", xmax);
254 
255  values[Atnum_modes] = palloc(NCHARS);
256  if (infomask & HEAP_XMAX_LOCK_ONLY)
257  {
258  if (HEAP_XMAX_IS_SHR_LOCKED(infomask))
259  snprintf(values[Atnum_modes], NCHARS, "{For Share}");
260  else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
261  snprintf(values[Atnum_modes], NCHARS, "{For Key Share}");
262  else if (HEAP_XMAX_IS_EXCL_LOCKED(infomask))
263  {
264  if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
265  snprintf(values[Atnum_modes], NCHARS, "{For Update}");
266  else
267  snprintf(values[Atnum_modes], NCHARS, "{For No Key Update}");
268  }
269  else
270  /* neither keyshare nor exclusive bit it set */
271  snprintf(values[Atnum_modes], NCHARS,
272  "{transient upgrade status}");
273  }
274  else
275  {
276  if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
277  snprintf(values[Atnum_modes], NCHARS, "{Update}");
278  else
279  snprintf(values[Atnum_modes], NCHARS, "{No Key Update}");
280  }
281 
282  values[Atnum_pids] = palloc(NCHARS * sizeof(char));
283  snprintf(values[Atnum_pids], NCHARS, "{%d}",
284  BackendXidGetPid(xmax));
285  }
286 
288 
289  /* build a tuple */
290  tuple = BuildTupleFromCStrings(attinmeta, values);
291 
292  /* make the tuple into a datum */
293  result = HeapTupleGetDatum(tuple);
294 
295  /*
296  * no need to pfree what we allocated; it's on a short-lived
297  * memory context anyway
298  */
299 
300  SRF_RETURN_NEXT(funcctx, result);
301  }
302  else
303  {
305  }
306  }
307 
308  heap_endscan(scan);
309  heap_close(mydata->rel, AccessShareLock);
310 
311  SRF_RETURN_DONE(funcctx);
312 }
#define NCHARS
Definition: pgrowlocks.c:53
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1198
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
PG_FUNCTION_INFO_V1(pgrowlocks)
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define HEAP_XMAX_LOCK_ONLY
Definition: htup_details.h:187
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1565
uint32 TransactionId
Definition: c.h:445
HTSU_Result HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, Buffer buffer)
Definition: tqual.c:460
Oid GetUserId(void)
Definition: miscinit.c:284
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define PointerGetDatum(X)
Definition: postgres.h:562
char * pstrdup(const char *in)
Definition: mcxt.c:1076
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
#define HEAP_LOCKED_UPGRADED(infomask)
Definition: htup_details.h:243
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
Datum pgrowlocks(PG_FUNCTION_ARGS)
Definition: pgrowlocks.c:70
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3023
#define heap_close(r, l)
Definition: heapam.h:97
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
Form_pg_class rd_rel
Definition: rel.h:114
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
int natts
Definition: tupdesc.h:79
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1118
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define HEAP_XMAX_IS_SHR_LOCKED(infomask)
Definition: htup_details.h:253
HeapTupleHeader t_data
Definition: htup.h:67
#define Atnum_tid
Definition: pgrowlocks.c:62
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
Relation rel
Definition: pgrowlocks.c:57
PG_MODULE_MAGIC
Definition: pgrowlocks.c:43
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:374
unsigned short uint16
Definition: c.h:295
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
static char * buf
Definition: pg_test_fsync.c:67
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
AttInMetadata * attinmeta
Definition: funcapi.h:100
int errdetail(const char *fmt,...)
Definition: elog.c:873
HTSU_Result
Definition: snapshot.h:121
#define RelationGetRelationName(relation)
Definition: rel.h:445
#define ereport(elevel, rest)
Definition: elog.h:122
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3206
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
int BackendXidGetPid(TransactionId xid)
Definition: procarray.c:2406
#define Atnum_modes
Definition: pgrowlocks.c:66
AclResult
Definition: acl.h:178
uintptr_t Datum
Definition: postgres.h:372
#define ACL_SELECT
Definition: parsenodes.h:73
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1069
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
#define HEAP_KEYS_UPDATED
Definition: htup_details.h:269
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1808
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:199
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4854
Buffer rs_cbuf
Definition: relscan.h:71
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:110
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:230
HeapScanDesc scan
Definition: pgrowlocks.c:58
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4422
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define Atnum_xids
Definition: pgrowlocks.c:65
void * user_fctx
Definition: funcapi.h:91
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
Definition: c.h:487
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define Atnum_pids
Definition: pgrowlocks.c:67
int ncolumns
Definition: pgrowlocks.c:59
#define HEAP_XMAX_IS_EXCL_LOCKED(infomask)
Definition: htup_details.h:255
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:680
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool onlyLock)
Definition: multixact.c:1202
#define elog
Definition: elog.h:219
#define DEFAULT_ROLE_STAT_SCAN_TABLES
Definition: pg_authid.h:110
#define HEAP_XMAX_IS_KEYSHR_LOCKED(infomask)
Definition: htup_details.h:257
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define RELKIND_RELATION
Definition: pg_class.h:160
HeapScanDesc heap_beginscan(Relation relation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: heapam.c:1397
Datum tidout(PG_FUNCTION_ARGS)
Definition: tid.c:105
#define RelationGetRelid(relation)
Definition: rel.h:425
#define Atnum_xmax
Definition: pgrowlocks.c:63
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define Atnum_ismulti
Definition: pgrowlocks.c:64
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295