PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgrowlocks.c File Reference
#include "postgres.h"
#include "access/multixact.h"
#include "access/relscan.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/procarray.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/tqual.h"
#include "utils/varlena.h"
Include dependency graph for pgrowlocks.c:

Go to the source code of this file.

Data Structures

struct  MyData
 

Macros

#define NCHARS   32
 
#define Atnum_tid   0
 
#define Atnum_xmax   1
 
#define Atnum_ismulti   2
 
#define Atnum_xids   3
 
#define Atnum_modes   4
 
#define Atnum_pids   5
 

Functions

 PG_FUNCTION_INFO_V1 (pgrowlocks)
 
Datum pgrowlocks (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

#define Atnum_ismulti   2

Definition at line 63 of file pgrowlocks.c.

Referenced by pgrowlocks().

#define Atnum_modes   4

Definition at line 65 of file pgrowlocks.c.

Referenced by pgrowlocks().

#define Atnum_pids   5

Definition at line 66 of file pgrowlocks.c.

Referenced by pgrowlocks().

#define Atnum_tid   0

Definition at line 61 of file pgrowlocks.c.

Referenced by pgrowlocks().

#define Atnum_xids   3

Definition at line 64 of file pgrowlocks.c.

Referenced by pgrowlocks().

#define Atnum_xmax   1

Definition at line 62 of file pgrowlocks.c.

Referenced by pgrowlocks().

#define NCHARS   32

Definition at line 52 of file pgrowlocks.c.

Referenced by pgrowlocks().

Function Documentation

PG_FUNCTION_INFO_V1 ( pgrowlocks  )
Datum pgrowlocks ( PG_FUNCTION_ARGS  )

Definition at line 69 of file pgrowlocks.c.

References AccessShareLock, ACL_KIND_CLASS, ACL_SELECT, aclcheck_error(), ACLCHECK_OK, Atnum_ismulti, Atnum_modes, Atnum_pids, Atnum_tid, Atnum_xids, Atnum_xmax, FuncCallContext::attinmeta, BackendXidGetPid(), buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BuildTupleFromCStrings(), DirectFunctionCall1, elog, ERROR, ForwardScanDirection, get_call_result_type(), GetActiveSnapshot(), GetCurrentCommandId(), GetMultiXactIdMembers(), GetUserId(), heap_beginscan(), heap_close, heap_endscan(), heap_getnext(), HEAP_KEYS_UPDATED, HEAP_LOCKED_UPGRADED, heap_openrv(), HEAP_XMAX_IS_EXCL_LOCKED, HEAP_XMAX_IS_KEYSHR_LOCKED, HEAP_XMAX_IS_MULTI, HEAP_XMAX_IS_SHR_LOCKED, HEAP_XMAX_LOCK_ONLY, HeapTupleBeingUpdated, HeapTupleGetDatum, HeapTupleHeaderGetRawXmax, HeapTupleSatisfiesUpdate(), LockBuffer(), makeRangeVarFromNameList(), MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, MultiXactStatusForKeyShare, MultiXactStatusForNoKeyUpdate, MultiXactStatusForShare, MultiXactStatusForUpdate, MultiXactStatusNoKeyUpdate, MultiXactStatusUpdate, tupleDesc::natts, NCHARS, MyData::ncolumns, NULL, palloc(), pg_class_aclcheck(), PG_GETARG_TEXT_P, PointerGetDatum, pstrdup(), MyData::rel, RelationGetRelationName, RelationGetRelid, HeapScanDescData::rs_cbuf, MyData::scan, snprintf(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, status(), HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_self, textToQualifiedNameList(), tidout(), TupleDescGetAttInMetadata(), TYPEFUNC_COMPOSITE, FuncCallContext::user_fctx, and values.

70 {
71  FuncCallContext *funcctx;
72  HeapScanDesc scan;
73  HeapTuple tuple;
74  TupleDesc tupdesc;
75  AttInMetadata *attinmeta;
76  Datum result;
77  MyData *mydata;
78  Relation rel;
79 
80  if (SRF_IS_FIRSTCALL())
81  {
82  text *relname;
83  RangeVar *relrv;
84  MemoryContext oldcontext;
85  AclResult aclresult;
86 
87  funcctx = SRF_FIRSTCALL_INIT();
88  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
89 
90  /* Build a tuple descriptor for our result type */
91  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
92  elog(ERROR, "return type must be a row type");
93 
94  attinmeta = TupleDescGetAttInMetadata(tupdesc);
95  funcctx->attinmeta = attinmeta;
96 
97  relname = PG_GETARG_TEXT_P(0);
99  rel = heap_openrv(relrv, AccessShareLock);
100 
101  /* check permissions: must have SELECT on table */
102  aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
103  ACL_SELECT);
104  if (aclresult != ACLCHECK_OK)
105  aclcheck_error(aclresult, ACL_KIND_CLASS,
107 
108  scan = heap_beginscan(rel, GetActiveSnapshot(), 0, NULL);
109  mydata = palloc(sizeof(*mydata));
110  mydata->rel = rel;
111  mydata->scan = scan;
112  mydata->ncolumns = tupdesc->natts;
113  funcctx->user_fctx = mydata;
114 
115  MemoryContextSwitchTo(oldcontext);
116  }
117 
118  funcctx = SRF_PERCALL_SETUP();
119  attinmeta = funcctx->attinmeta;
120  mydata = (MyData *) funcctx->user_fctx;
121  scan = mydata->scan;
122 
123  /* scan the relation */
124  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
125  {
126  HTSU_Result htsu;
127  TransactionId xmax;
128  uint16 infomask;
129 
130  /* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
132 
133  htsu = HeapTupleSatisfiesUpdate(tuple,
134  GetCurrentCommandId(false),
135  scan->rs_cbuf);
136  xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
137  infomask = tuple->t_data->t_infomask;
138 
139  /*
140  * A tuple is locked if HTSU returns BeingUpdated.
141  */
142  if (htsu == HeapTupleBeingUpdated)
143  {
144  char **values;
145 
146  values = (char **) palloc(mydata->ncolumns * sizeof(char *));
147 
148  values[Atnum_tid] = (char *) DirectFunctionCall1(tidout,
149  PointerGetDatum(&tuple->t_self));
150 
151  values[Atnum_xmax] = palloc(NCHARS * sizeof(char));
152  snprintf(values[Atnum_xmax], NCHARS, "%d", xmax);
153  if (infomask & HEAP_XMAX_IS_MULTI)
154  {
155  MultiXactMember *members;
156  int nmembers;
157  bool first = true;
158  bool allow_old;
159 
160  values[Atnum_ismulti] = pstrdup("true");
161 
162  allow_old = HEAP_LOCKED_UPGRADED(infomask);
163  nmembers = GetMultiXactIdMembers(xmax, &members, allow_old,
164  false);
165  if (nmembers == -1)
166  {
167  values[Atnum_xids] = "{0}";
168  values[Atnum_modes] = "{transient upgrade status}";
169  values[Atnum_pids] = "{0}";
170  }
171  else
172  {
173  int j;
174 
175  values[Atnum_xids] = palloc(NCHARS * nmembers);
176  values[Atnum_modes] = palloc(NCHARS * nmembers);
177  values[Atnum_pids] = palloc(NCHARS * nmembers);
178 
179  strcpy(values[Atnum_xids], "{");
180  strcpy(values[Atnum_modes], "{");
181  strcpy(values[Atnum_pids], "{");
182 
183  for (j = 0; j < nmembers; j++)
184  {
185  char buf[NCHARS];
186 
187  if (!first)
188  {
189  strcat(values[Atnum_xids], ",");
190  strcat(values[Atnum_modes], ",");
191  strcat(values[Atnum_pids], ",");
192  }
193  snprintf(buf, NCHARS, "%d", members[j].xid);
194  strcat(values[Atnum_xids], buf);
195  switch (members[j].status)
196  {
198  snprintf(buf, NCHARS, "Update");
199  break;
201  snprintf(buf, NCHARS, "No Key Update");
202  break;
204  snprintf(buf, NCHARS, "For Update");
205  break;
207  snprintf(buf, NCHARS, "For No Key Update");
208  break;
210  snprintf(buf, NCHARS, "Share");
211  break;
213  snprintf(buf, NCHARS, "Key Share");
214  break;
215  }
216  strcat(values[Atnum_modes], buf);
217  snprintf(buf, NCHARS, "%d",
218  BackendXidGetPid(members[j].xid));
219  strcat(values[Atnum_pids], buf);
220 
221  first = false;
222  }
223 
224  strcat(values[Atnum_xids], "}");
225  strcat(values[Atnum_modes], "}");
226  strcat(values[Atnum_pids], "}");
227  }
228  }
229  else
230  {
231  values[Atnum_ismulti] = pstrdup("false");
232 
233  values[Atnum_xids] = palloc(NCHARS * sizeof(char));
234  snprintf(values[Atnum_xids], NCHARS, "{%d}", xmax);
235 
236  values[Atnum_modes] = palloc(NCHARS);
237  if (infomask & HEAP_XMAX_LOCK_ONLY)
238  {
239  if (HEAP_XMAX_IS_SHR_LOCKED(infomask))
240  snprintf(values[Atnum_modes], NCHARS, "{For Share}");
241  else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
242  snprintf(values[Atnum_modes], NCHARS, "{For Key Share}");
243  else if (HEAP_XMAX_IS_EXCL_LOCKED(infomask))
244  {
245  if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
246  snprintf(values[Atnum_modes], NCHARS, "{For Update}");
247  else
248  snprintf(values[Atnum_modes], NCHARS, "{For No Key Update}");
249  }
250  else
251  /* neither keyshare nor exclusive bit it set */
252  snprintf(values[Atnum_modes], NCHARS,
253  "{transient upgrade status}");
254  }
255  else
256  {
257  if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
258  snprintf(values[Atnum_modes], NCHARS, "{Update}");
259  else
260  snprintf(values[Atnum_modes], NCHARS, "{No Key Update}");
261  }
262 
263  values[Atnum_pids] = palloc(NCHARS * sizeof(char));
264  snprintf(values[Atnum_pids], NCHARS, "{%d}",
265  BackendXidGetPid(xmax));
266  }
267 
269 
270  /* build a tuple */
271  tuple = BuildTupleFromCStrings(attinmeta, values);
272 
273  /* make the tuple into a datum */
274  result = HeapTupleGetDatum(tuple);
275 
276  /*
277  * no need to pfree what we allocated; it's on a short-lived
278  * memory context anyway
279  */
280 
281  SRF_RETURN_NEXT(funcctx, result);
282  }
283  else
284  {
286  }
287  }
288 
289  heap_endscan(scan);
290  heap_close(mydata->rel, AccessShareLock);
291 
292  SRF_RETURN_DONE(funcctx);
293 }
#define NCHARS
Definition: pgrowlocks.c:52
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define HEAP_XMAX_LOCK_ONLY
Definition: htup_details.h:182
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1581
uint32 TransactionId
Definition: c.h:394
HTSU_Result HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, Buffer buffer)
Definition: tqual.c:460
Oid GetUserId(void)
Definition: miscinit.c:282
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:564
char * pstrdup(const char *in)
Definition: mcxt.c:1165
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:834
#define HEAP_LOCKED_UPGRADED(infomask)
Definition: htup_details.h:238
#define AccessShareLock
Definition: lockdefs.h:36
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:2857
#define heap_close(r, l)
Definition: heapam.h:97
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
int natts
Definition: tupdesc.h:73
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:1115
#define HEAP_XMAX_IS_SHR_LOCKED(infomask)
Definition: htup_details.h:248
HeapTupleHeader t_data
Definition: htup.h:67
#define Atnum_tid
Definition: pgrowlocks.c:61
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
Relation rel
Definition: pgrowlocks.c:56
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:369
unsigned short uint16
Definition: c.h:264
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
static char * buf
Definition: pg_test_fsync.c:65
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3378
AttInMetadata * attinmeta
Definition: funcapi.h:99
HTSU_Result
Definition: snapshot.h:119
#define RelationGetRelationName(relation)
Definition: rel.h:433
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3071
int BackendXidGetPid(TransactionId xid)
Definition: procarray.c:2389
#define Atnum_modes
Definition: pgrowlocks.c:65
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:374
#define ACL_SELECT
Definition: parsenodes.h:66
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:1068
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
#define HEAP_KEYS_UPDATED
Definition: htup_details.h:264
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1781
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:194
Relation heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1315
Buffer rs_cbuf
Definition: relscan.h:70
#define NULL
Definition: c.h:226
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
HeapScanDesc scan
Definition: pgrowlocks.c:57
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4397
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#define Atnum_xids
Definition: pgrowlocks.c:64
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:891
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
Definition: c.h:435
#define Atnum_pids
Definition: pgrowlocks.c:66
int ncolumns
Definition: pgrowlocks.c:58
#define HEAP_XMAX_IS_EXCL_LOCKED(infomask)
Definition: htup_details.h:250
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:686
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool onlyLock)
Definition: multixact.c:1202
#define elog
Definition: elog.h:219
#define HEAP_XMAX_IS_KEYSHR_LOCKED(infomask)
Definition: htup_details.h:252
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
HeapScanDesc heap_beginscan(Relation relation, Snapshot snapshot, int nkeys, ScanKey key)
Definition: heapam.c:1394
Datum tidout(PG_FUNCTION_ARGS)
Definition: tid.c:105
#define RelationGetRelid(relation)
Definition: rel.h:413
#define Atnum_xmax
Definition: pgrowlocks.c:62
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define Atnum_ismulti
Definition: pgrowlocks.c:63
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287

Variable Documentation

PG_MODULE_MAGIC

Definition at line 42 of file pgrowlocks.c.