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.

Referenced by pgrowlocks().

◆ Atnum_modes

#define Atnum_modes   4

Definition at line 61 of file pgrowlocks.c.

Referenced by pgrowlocks().

◆ Atnum_pids

#define Atnum_pids   5

Definition at line 62 of file pgrowlocks.c.

Referenced by pgrowlocks().

◆ Atnum_tid

#define Atnum_tid   0

Definition at line 57 of file pgrowlocks.c.

Referenced by pgrowlocks().

◆ Atnum_xids

#define Atnum_xids   3

Definition at line 60 of file pgrowlocks.c.

Referenced by pgrowlocks().

◆ Atnum_xmax

#define Atnum_xmax   1

Definition at line 58 of file pgrowlocks.c.

Referenced by pgrowlocks().

◆ NCHARS

#define NCHARS   32

Definition at line 55 of file pgrowlocks.c.

Referenced by pgrowlocks().

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.

References AccessShareLock, ACL_SELECT, aclcheck_error(), ACLCHECK_NO_PRIV, ACLCHECK_OK, ReturnSetInfo::allowedModes, Atnum_ismulti, Atnum_modes, Atnum_pids, Atnum_tid, Atnum_xids, Atnum_xmax, BackendXidGetPid(), buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BuildTupleFromCStrings(), DirectFunctionCall1, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, ForwardScanDirection, get_call_result_type(), get_relkind_objtype(), GetActiveSnapshot(), GetCurrentCommandId(), GetMultiXactIdMembers(), GetUserId(), 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(), is_member_of_role(), IsA, LockBuffer(), makeRangeVarFromNameList(), MemoryContextSwitchTo(), MultiXactStatusForKeyShare, MultiXactStatusForNoKeyUpdate, MultiXactStatusForShare, MultiXactStatusForUpdate, MultiXactStatusNoKeyUpdate, MultiXactStatusUpdate, NCHARS, palloc(), pg_class_aclcheck(), PG_GETARG_TEXT_PP, PointerGetDatum, pstrdup(), relation_openrv(), RelationGetRelationName, RelationGetRelid, relname, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, snprintf, status(), table_beginscan(), table_close(), table_endscan(), textToQualifiedNameList(), tidout(), TM_BeingModified, TupleDescGetAttInMetadata(), tuplestore_begin_heap(), tuplestore_puttuple(), TYPEFUNC_COMPOSITE, values, and work_mem.

66 {
68  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
69  bool randomAccess;
70  TupleDesc tupdesc;
71  Tuplestorestate *tupstore;
72  AttInMetadata *attinmeta;
73  Relation rel;
74  RangeVar *relrv;
75  TableScanDesc scan;
76  HeapScanDesc hscan;
77  HeapTuple tuple;
78  MemoryContext oldcontext;
79  AclResult aclresult;
80  char **values;
81 
82  /* check to see if caller supports us returning a tuplestore */
83  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
84  ereport(ERROR,
85  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
86  errmsg("set-valued function called in context that cannot accept a set")));
87  if (!(rsinfo->allowedModes & SFRM_Materialize))
88  ereport(ERROR,
89  (errcode(ERRCODE_SYNTAX_ERROR),
90  errmsg("materialize mode required, but it is not allowed in this context")));
91 
92  /* The tupdesc and tuplestore must be created in ecxt_per_query_memory */
94 
95  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
96  elog(ERROR, "return type must be a row type");
97 
98  randomAccess = (rsinfo->allowedModes & SFRM_Materialize_Random) != 0;
99  tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
100  rsinfo->returnMode = SFRM_Materialize;
101  rsinfo->setResult = tupstore;
102  rsinfo->setDesc = tupdesc;
103 
104  MemoryContextSwitchTo(oldcontext);
105 
106  /* Access the table */
108  rel = relation_openrv(relrv, AccessShareLock);
109 
110  if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
111  ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
112  errmsg("only heap AM is supported")));
113 
114  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
115  ereport(ERROR,
116  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
117  errmsg("\"%s\" is a partitioned table",
119  errdetail("Partitioned tables do not contain rows.")));
120  else if (rel->rd_rel->relkind != RELKIND_RELATION)
121  ereport(ERROR,
122  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
123  errmsg("\"%s\" is not a table",
124  RelationGetRelationName(rel))));
125 
126  /*
127  * check permissions: must have SELECT on table or be in
128  * pg_stat_scan_tables
129  */
130  aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
131  ACL_SELECT);
132  if (aclresult != ACLCHECK_OK)
133  aclresult = is_member_of_role(GetUserId(), DEFAULT_ROLE_STAT_SCAN_TABLES) ? ACLCHECK_OK : ACLCHECK_NO_PRIV;
134 
135  if (aclresult != ACLCHECK_OK)
136  aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
138 
139  /* Scan the relation */
140  scan = table_beginscan(rel, GetActiveSnapshot(), 0, NULL);
141  hscan = (HeapScanDesc) scan;
142 
143  attinmeta = TupleDescGetAttInMetadata(tupdesc);
144 
145  values = (char **) palloc(tupdesc->natts * sizeof(char *));
146 
147  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
148  {
149  TM_Result htsu;
150  TransactionId xmax;
151  uint16 infomask;
152 
153  /* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
154  LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
155 
156  htsu = HeapTupleSatisfiesUpdate(tuple,
157  GetCurrentCommandId(false),
158  hscan->rs_cbuf);
159  xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
160  infomask = tuple->t_data->t_infomask;
161 
162  /*
163  * A tuple is locked if HTSU returns BeingModified.
164  */
165  if (htsu == TM_BeingModified)
166  {
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 
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 */
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 
287  LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
288 
289  /* build a tuple */
290  tuple = BuildTupleFromCStrings(attinmeta, values);
291  tuplestore_puttuple(tupstore, tuple);
292  }
293  else
294  {
295  LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
296  }
297  }
298 
299  table_endscan(scan);
301  return (Datum) 0;
302 }
#define NCHARS
Definition: pgrowlocks.c:55
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define HEAP_XMAX_LOCK_ONLY
Definition: htup_details.h:196
uint32 TransactionId
Definition: c.h:521
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
char * pstrdup(const char *in)
Definition: mcxt.c:1187
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
#define HEAP_LOCKED_UPGRADED(infomask)
Definition: htup_details.h:252
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:610
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3061
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
NameData relname
Definition: pg_class.h:38
TM_Result HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, Buffer buffer)
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:73
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2116
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:308
#define HEAP_XMAX_IS_SHR_LOCKED(infomask)
Definition: htup_details.h:262
#define Atnum_tid
Definition: pgrowlocks.c:57
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:375
unsigned short uint16
Definition: c.h:374
#define ERROR
Definition: elog.h:43
static TableScanDesc table_beginscan(Relation rel, Snapshot snapshot, int nkeys, struct ScanKeyData *key)
Definition: tableam.h:755
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
static char * buf
Definition: pg_test_fsync.c:68
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1286
int errdetail(const char *fmt,...)
Definition: elog.c:954
#define RelationGetRelationName(relation)
Definition: rel.h:490
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3644
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
TM_Result
Definition: tableam.h:70
int BackendXidGetPid(TransactionId xid)
Definition: procarray.c:3040
#define Atnum_modes
Definition: pgrowlocks.c:61
AclResult
Definition: acl.h:177
uintptr_t Datum
Definition: postgres.h:367
#define ACL_SELECT
Definition: parsenodes.h:75
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2067
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3750
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
#define HEAP_KEYS_UPDATED
Definition: htup_details.h:278
int work_mem
Definition: globals.c:121
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:208
#define ereport(elevel,...)
Definition: elog.h:144
int allowedModes
Definition: execnodes.h:304
bool is_member_of_role(Oid member, Oid role)
Definition: acl.c:4916
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4563
Tuplestorestate * setResult
Definition: execnodes.h:309
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:863
static Datum values[MAXATTR]
Definition: bootstrap.c:165
ExprContext * econtext
Definition: execnodes.h:302
#define Atnum_xids
Definition: pgrowlocks.c:60
TupleDesc setDesc
Definition: execnodes.h:310
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:821
#define elog(elevel,...)
Definition: elog.h:214
ObjectType get_relkind_objtype(char relkind)
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:97
Definition: c.h:563
#define Atnum_pids
Definition: pgrowlocks.c:62
#define HEAP_XMAX_IS_EXCL_LOCKED(infomask)
Definition: htup_details.h:264
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:761
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool onlyLock)
Definition: multixact.c:1223
#define HEAP_XMAX_IS_KEYSHR_LOCKED(infomask)
Definition: htup_details.h:266
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define snprintf
Definition: port.h:215
Datum tidout(PG_FUNCTION_ARGS)
Definition: tid.c:107
#define RelationGetRelid(relation)
Definition: rel.h:456
#define Atnum_xmax
Definition: pgrowlocks.c:58
#define Atnum_ismulti
Definition: pgrowlocks.c:59

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 45 of file pgrowlocks.c.