PostgreSQL Source Code  git master
pgrowlocks.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/multixact.h"
#include "access/relscan.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_authid.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/varlena.h"
Include dependency graph for pgrowlocks.c:

Go to the source code of this file.

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

◆ Atnum_ismulti

#define Atnum_ismulti   2

Definition at line 59 of file pgrowlocks.c.

◆ Atnum_modes

#define Atnum_modes   4

Definition at line 61 of file pgrowlocks.c.

◆ Atnum_pids

#define Atnum_pids   5

Definition at line 62 of file pgrowlocks.c.

◆ Atnum_tid

#define Atnum_tid   0

Definition at line 57 of file pgrowlocks.c.

◆ Atnum_xids

#define Atnum_xids   3

Definition at line 60 of file pgrowlocks.c.

◆ Atnum_xmax

#define Atnum_xmax   1

Definition at line 58 of file pgrowlocks.c.

◆ NCHARS

#define NCHARS   32

Definition at line 55 of file pgrowlocks.c.

Function Documentation

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( pgrowlocks  )

◆ pgrowlocks()

Datum pgrowlocks ( PG_FUNCTION_ARGS  )

Definition at line 65 of file pgrowlocks.c.

66 {
68  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
69  AttInMetadata *attinmeta;
70  Relation rel;
71  RangeVar *relrv;
72  TableScanDesc scan;
73  HeapScanDesc hscan;
74  HeapTuple tuple;
75  AclResult aclresult;
76  char **values;
77 
78  SetSingleFuncCall(fcinfo, 0);
79 
80  /* Access the table */
82  rel = relation_openrv(relrv, AccessShareLock);
83 
84  if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
85  ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
86  errmsg("only heap AM is supported")));
87 
88  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
89  ereport(ERROR,
90  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
91  errmsg("\"%s\" is a partitioned table",
93  errdetail("Partitioned tables do not contain rows.")));
94  else if (rel->rd_rel->relkind != RELKIND_RELATION)
95  ereport(ERROR,
96  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
97  errmsg("\"%s\" is not a table",
99 
100  /*
101  * check permissions: must have SELECT on table or be in
102  * pg_stat_scan_tables
103  */
104  aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
105  ACL_SELECT);
106  if (aclresult != ACLCHECK_OK)
107  aclresult = has_privs_of_role(GetUserId(), ROLE_PG_STAT_SCAN_TABLES) ? ACLCHECK_OK : ACLCHECK_NO_PRIV;
108 
109  if (aclresult != ACLCHECK_OK)
110  aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
112 
113  /* Scan the relation */
114  scan = table_beginscan(rel, GetActiveSnapshot(), 0, NULL);
115  hscan = (HeapScanDesc) scan;
116 
117  attinmeta = TupleDescGetAttInMetadata(rsinfo->setDesc);
118 
119  values = (char **) palloc(rsinfo->setDesc->natts * sizeof(char *));
120 
121  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
122  {
123  TM_Result htsu;
124  TransactionId xmax;
125  uint16 infomask;
126 
127  /* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
128  LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
129 
130  htsu = HeapTupleSatisfiesUpdate(tuple,
131  GetCurrentCommandId(false),
132  hscan->rs_cbuf);
133  xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
134  infomask = tuple->t_data->t_infomask;
135 
136  /*
137  * A tuple is locked if HTSU returns BeingModified.
138  */
139  if (htsu == TM_BeingModified)
140  {
142  PointerGetDatum(&tuple->t_self));
143 
144  values[Atnum_xmax] = palloc(NCHARS * sizeof(char));
145  snprintf(values[Atnum_xmax], NCHARS, "%u", xmax);
146  if (infomask & HEAP_XMAX_IS_MULTI)
147  {
148  MultiXactMember *members;
149  int nmembers;
150  bool first = true;
151  bool allow_old;
152 
153  values[Atnum_ismulti] = pstrdup("true");
154 
155  allow_old = HEAP_LOCKED_UPGRADED(infomask);
156  nmembers = GetMultiXactIdMembers(xmax, &members, allow_old,
157  false);
158  if (nmembers == -1)
159  {
160  values[Atnum_xids] = "{0}";
161  values[Atnum_modes] = "{transient upgrade status}";
162  values[Atnum_pids] = "{0}";
163  }
164  else
165  {
166  int j;
167 
168  values[Atnum_xids] = palloc(NCHARS * nmembers);
169  values[Atnum_modes] = palloc(NCHARS * nmembers);
170  values[Atnum_pids] = palloc(NCHARS * nmembers);
171 
172  strcpy(values[Atnum_xids], "{");
173  strcpy(values[Atnum_modes], "{");
174  strcpy(values[Atnum_pids], "{");
175 
176  for (j = 0; j < nmembers; j++)
177  {
178  char buf[NCHARS];
179 
180  if (!first)
181  {
182  strcat(values[Atnum_xids], ",");
183  strcat(values[Atnum_modes], ",");
184  strcat(values[Atnum_pids], ",");
185  }
186  snprintf(buf, NCHARS, "%u", members[j].xid);
187  strcat(values[Atnum_xids], buf);
188  switch (members[j].status)
189  {
191  snprintf(buf, NCHARS, "Update");
192  break;
194  snprintf(buf, NCHARS, "No Key Update");
195  break;
197  snprintf(buf, NCHARS, "For Update");
198  break;
200  snprintf(buf, NCHARS, "For No Key Update");
201  break;
203  snprintf(buf, NCHARS, "Share");
204  break;
206  snprintf(buf, NCHARS, "Key Share");
207  break;
208  }
209  strcat(values[Atnum_modes], buf);
210  snprintf(buf, NCHARS, "%d",
211  BackendXidGetPid(members[j].xid));
212  strcat(values[Atnum_pids], buf);
213 
214  first = false;
215  }
216 
217  strcat(values[Atnum_xids], "}");
218  strcat(values[Atnum_modes], "}");
219  strcat(values[Atnum_pids], "}");
220  }
221  }
222  else
223  {
224  values[Atnum_ismulti] = pstrdup("false");
225 
226  values[Atnum_xids] = palloc(NCHARS * sizeof(char));
227  snprintf(values[Atnum_xids], NCHARS, "{%u}", xmax);
228 
230  if (infomask & HEAP_XMAX_LOCK_ONLY)
231  {
232  if (HEAP_XMAX_IS_SHR_LOCKED(infomask))
233  snprintf(values[Atnum_modes], NCHARS, "{For Share}");
234  else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
235  snprintf(values[Atnum_modes], NCHARS, "{For Key Share}");
236  else if (HEAP_XMAX_IS_EXCL_LOCKED(infomask))
237  {
238  if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
239  snprintf(values[Atnum_modes], NCHARS, "{For Update}");
240  else
241  snprintf(values[Atnum_modes], NCHARS, "{For No Key Update}");
242  }
243  else
244  /* neither keyshare nor exclusive bit it set */
246  "{transient upgrade status}");
247  }
248  else
249  {
250  if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
251  snprintf(values[Atnum_modes], NCHARS, "{Update}");
252  else
253  snprintf(values[Atnum_modes], NCHARS, "{No Key Update}");
254  }
255 
256  values[Atnum_pids] = palloc(NCHARS * sizeof(char));
257  snprintf(values[Atnum_pids], NCHARS, "{%d}",
258  BackendXidGetPid(xmax));
259  }
260 
261  LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
262 
263  /* build a tuple */
264  tuple = BuildTupleFromCStrings(attinmeta, values);
265  tuplestore_puttuple(rsinfo->setResult, tuple);
266  }
267  else
268  {
269  LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
270  }
271  }
272 
273  table_endscan(scan);
275  return (Datum) 0;
276 }
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4951
AclResult
Definition: acl.h:181
@ ACLCHECK_NO_PRIV
Definition: acl.h:183
@ 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
static Datum values[MAXATTR]
Definition: bootstrap.c:156
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4156
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
unsigned short uint16
Definition: c.h:440
uint32 TransactionId
Definition: c.h:587
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2135
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2086
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
void SetSingleFuncCall(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1296
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:79
TM_Result HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, Buffer buffer)
#define HEAP_XMAX_IS_EXCL_LOCKED(infomask)
Definition: htup_details.h:260
#define HEAP_XMAX_IS_KEYSHR_LOCKED(infomask)
Definition: htup_details.h:262
#define HEAP_KEYS_UPDATED
Definition: htup_details.h:274
#define HEAP_XMAX_LOCK_ONLY
Definition: htup_details.h:196
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:208
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:370
#define HEAP_XMAX_IS_SHR_LOCKED(infomask)
Definition: htup_details.h:258
#define HEAP_LOCKED_UPGRADED(infomask)
Definition: htup_details.h:248
int j
Definition: isn.c:74
#define AccessShareLock
Definition: lockdefs.h:36
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void * palloc(Size size)
Definition: mcxt.c:1068
Oid GetUserId(void)
Definition: miscinit.c:492
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool onlyLock)
Definition: multixact.c:1223
@ MultiXactStatusForShare
Definition: multixact.h:44
@ MultiXactStatusForNoKeyUpdate
Definition: multixact.h:45
@ MultiXactStatusNoKeyUpdate
Definition: multixact.h:48
@ MultiXactStatusUpdate
Definition: multixact.h:50
@ MultiXactStatusForUpdate
Definition: multixact.h:46
@ MultiXactStatusForKeyShare
Definition: multixact.h:43
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3108
ObjectType get_relkind_objtype(char relkind)
#define ACL_SELECT
Definition: parsenodes.h:83
NameData relname
Definition: pg_class.h:38
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
static char * buf
Definition: pg_test_fsync.c:67
#define Atnum_modes
Definition: pgrowlocks.c:61
#define NCHARS
Definition: pgrowlocks.c:55
#define Atnum_xids
Definition: pgrowlocks.c:60
#define Atnum_ismulti
Definition: pgrowlocks.c:59
#define Atnum_tid
Definition: pgrowlocks.c:57
#define Atnum_pids
Definition: pgrowlocks.c:62
#define Atnum_xmax
Definition: pgrowlocks.c:58
#define snprintf
Definition: port.h:225
uintptr_t Datum
Definition: postgres.h:411
#define PointerGetDatum(X)
Definition: postgres.h:600
int BackendXidGetPid(TransactionId xid)
Definition: procarray.c:3230
#define RelationGetRelid(relation)
Definition: rel.h:489
#define RelationGetRelationName(relation)
Definition: rel.h:523
@ ForwardScanDirection
Definition: sdir.h:26
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:803
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
TupleDesc setDesc
Definition: execnodes.h:317
Tuplestorestate * setResult
Definition: execnodes.h:316
Definition: c.h:622
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key)
Definition: tableam.h:885
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:993
TM_Result
Definition: tableam.h:72
@ TM_BeingModified
Definition: tableam.h:99
Datum tidout(PG_FUNCTION_ARGS)
Definition: tid.c:123
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3657
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:814

References AccessShareLock, ACL_SELECT, aclcheck_error(), ACLCHECK_NO_PRIV, ACLCHECK_OK, Atnum_ismulti, Atnum_modes, Atnum_pids, Atnum_tid, Atnum_xids, Atnum_xmax, BackendXidGetPid(), buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BuildTupleFromCStrings(), DirectFunctionCall1, ereport, errcode(), errdetail(), errmsg(), ERROR, ForwardScanDirection, get_relkind_objtype(), GetActiveSnapshot(), GetCurrentCommandId(), GetMultiXactIdMembers(), GetUserId(), has_privs_of_role(), heap_getnext(), HEAP_KEYS_UPDATED, HEAP_LOCKED_UPGRADED, HEAP_XMAX_IS_EXCL_LOCKED, HEAP_XMAX_IS_KEYSHR_LOCKED, HEAP_XMAX_IS_MULTI, HEAP_XMAX_IS_SHR_LOCKED, HEAP_XMAX_LOCK_ONLY, HeapTupleHeaderGetRawXmax, HeapTupleSatisfiesUpdate(), j, LockBuffer(), makeRangeVarFromNameList(), MultiXactStatusForKeyShare, MultiXactStatusForNoKeyUpdate, MultiXactStatusForShare, MultiXactStatusForUpdate, MultiXactStatusNoKeyUpdate, MultiXactStatusUpdate, TupleDescData::natts, NCHARS, palloc(), pg_class_aclcheck(), PG_GETARG_TEXT_PP, PointerGetDatum, pstrdup(), relation_openrv(), RelationGetRelationName, RelationGetRelid, relname, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SetSingleFuncCall(), snprintf, status(), table_beginscan(), table_close(), table_endscan(), textToQualifiedNameList(), tidout(), TM_BeingModified, TupleDescGetAttInMetadata(), tuplestore_puttuple(), and values.

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 45 of file pgrowlocks.c.