Branch data Line data Source code
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/heapam.h"
28 : : #include "access/multixact.h"
29 : : #include "access/relscan.h"
30 : : #include "access/tableam.h"
31 : : #include "access/xact.h"
32 : : #include "access/xlog.h"
33 : : #include "catalog/namespace.h"
34 : : #include "catalog/pg_am_d.h"
35 : : #include "catalog/pg_authid.h"
36 : : #include "funcapi.h"
37 : : #include "miscadmin.h"
38 : : #include "storage/bufmgr.h"
39 : : #include "storage/procarray.h"
40 : : #include "utils/acl.h"
41 : : #include "utils/fmgrprotos.h"
42 : : #include "utils/rel.h"
43 : : #include "utils/snapmgr.h"
44 : : #include "utils/tuplestore.h"
45 : : #include "utils/varlena.h"
46 : :
47 : 2 : PG_MODULE_MAGIC_EXT(
48 : : .name = "pgrowlocks",
49 : : .version = PG_VERSION
50 : : );
51 : :
52 : 2 : PG_FUNCTION_INFO_V1(pgrowlocks);
53 : :
54 : : /* ----------
55 : : * pgrowlocks:
56 : : * returns tids of rows being locked
57 : : * ----------
58 : : */
59 : :
60 : : #define NCHARS 32
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
70 : 12 : pgrowlocks(PG_FUNCTION_ARGS)
71 : : {
72 : 12 : text *relname = PG_GETARG_TEXT_PP(0);
73 : 12 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
74 : : AttInMetadata *attinmeta;
75 : : Relation rel;
76 : : RangeVar *relrv;
77 : : TableScanDesc scan;
78 : : HeapScanDesc hscan;
79 : : HeapTuple tuple;
80 : : AclResult aclresult;
81 : : char **values;
82 : :
83 : 12 : InitMaterializedSRF(fcinfo, 0);
84 : :
85 : : /* Access the table */
86 : 12 : relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
87 : 12 : rel = relation_openrv(relrv, AccessShareLock);
88 : :
89 [ - + ]: 12 : if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
90 [ # # ]: 0 : ereport(ERROR,
91 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
92 : : errmsg("\"%s\" is a partitioned table",
93 : : RelationGetRelationName(rel)),
94 : : errdetail("Partitioned tables do not contain rows.")));
95 [ - + ]: 12 : else if (rel->rd_rel->relkind != RELKIND_RELATION)
96 [ # # ]: 0 : ereport(ERROR,
97 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
98 : : errmsg("\"%s\" is not a table",
99 : : RelationGetRelationName(rel))));
100 [ - + ]: 12 : else if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
101 [ # # ]: 0 : ereport(ERROR,
102 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
103 : : errmsg("only heap AM is supported")));
104 : :
105 : : /*
106 : : * check permissions: must have SELECT on table or be in
107 : : * pg_stat_scan_tables
108 : : */
109 : 12 : aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
110 : : ACL_SELECT);
111 [ - + ]: 12 : if (aclresult != ACLCHECK_OK)
112 : 0 : aclresult = has_privs_of_role(GetUserId(), ROLE_PG_STAT_SCAN_TABLES) ? ACLCHECK_OK : ACLCHECK_NO_PRIV;
113 : :
114 [ - + ]: 12 : if (aclresult != ACLCHECK_OK)
115 : 0 : aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
116 : 0 : RelationGetRelationName(rel));
117 : :
118 [ - + - - ]: 12 : if (!RelationIsPermanent(rel) && RecoveryInProgress())
119 [ # # ]: 0 : ereport(ERROR,
120 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
121 : : errmsg("cannot access temporary or unlogged relations during recovery")));
122 : :
123 : : /* Scan the relation */
124 : 12 : scan = table_beginscan(rel, GetActiveSnapshot(), 0, NULL, SO_NONE);
125 : 12 : hscan = (HeapScanDesc) scan;
126 : :
127 : 12 : attinmeta = TupleDescGetAttInMetadata(rsinfo->setDesc);
128 : :
129 : 12 : values = palloc_array(char *, rsinfo->setDesc->natts);
130 : :
131 [ + + ]: 36 : while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
132 : : {
133 : : TM_Result htsu;
134 : : TransactionId xmax;
135 : : uint16 infomask;
136 : :
137 : : /* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
138 : 24 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
139 : :
140 : 24 : htsu = HeapTupleSatisfiesUpdate(tuple,
141 : : GetCurrentCommandId(false),
142 : : hscan->rs_cbuf);
143 : 24 : xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
144 : 24 : infomask = tuple->t_data->t_infomask;
145 : :
146 : : /*
147 : : * A tuple is locked if HTSU returns BeingModified.
148 : : */
149 [ + + ]: 24 : if (htsu == TM_BeingModified)
150 : : {
151 : 22 : values[Atnum_tid] = DatumGetCString(DirectFunctionCall1(tidout,
152 : : PointerGetDatum(&tuple->t_self)));
153 : :
154 : 22 : values[Atnum_xmax] = palloc_array(char, NCHARS);
155 : 22 : snprintf(values[Atnum_xmax], NCHARS, "%u", xmax);
156 [ + + ]: 22 : if (infomask & HEAP_XMAX_IS_MULTI)
157 : : {
158 : : MultiXactMember *members;
159 : : int nmembers;
160 : 8 : bool first = true;
161 : : bool allow_old;
162 : :
163 : 8 : values[Atnum_ismulti] = pstrdup("true");
164 : :
165 : 8 : allow_old = HEAP_LOCKED_UPGRADED(infomask);
166 : 8 : nmembers = GetMultiXactIdMembers(xmax, &members, allow_old,
167 : : false);
168 [ - + ]: 8 : if (nmembers == -1)
169 : : {
170 : 0 : values[Atnum_xids] = "{0}";
171 : 0 : values[Atnum_modes] = "{transient upgrade status}";
172 : 0 : values[Atnum_pids] = "{0}";
173 : : }
174 : : else
175 : : {
176 : : int j;
177 : :
178 : 8 : values[Atnum_xids] = palloc(NCHARS * nmembers);
179 : 8 : values[Atnum_modes] = palloc(NCHARS * nmembers);
180 : 8 : values[Atnum_pids] = palloc(NCHARS * nmembers);
181 : :
182 : 8 : strcpy(values[Atnum_xids], "{");
183 : 8 : strcpy(values[Atnum_modes], "{");
184 : 8 : strcpy(values[Atnum_pids], "{");
185 : :
186 [ + + ]: 24 : for (j = 0; j < nmembers; j++)
187 : : {
188 : : char buf[NCHARS];
189 : :
190 [ + + ]: 16 : if (!first)
191 : : {
192 : 8 : strcat(values[Atnum_xids], ",");
193 : 8 : strcat(values[Atnum_modes], ",");
194 : 8 : strcat(values[Atnum_pids], ",");
195 : : }
196 : 16 : snprintf(buf, NCHARS, "%u", members[j].xid);
197 : 16 : strcat(values[Atnum_xids], buf);
198 [ + + + + : 16 : switch (members[j].status)
+ + - ]
199 : : {
200 : 1 : case MultiXactStatusUpdate:
201 : 1 : snprintf(buf, NCHARS, "Update");
202 : 1 : break;
203 : 1 : case MultiXactStatusNoKeyUpdate:
204 : 1 : snprintf(buf, NCHARS, "No Key Update");
205 : 1 : break;
206 : 2 : case MultiXactStatusForUpdate:
207 : 2 : snprintf(buf, NCHARS, "For Update");
208 : 2 : break;
209 : 2 : case MultiXactStatusForNoKeyUpdate:
210 : 2 : snprintf(buf, NCHARS, "For No Key Update");
211 : 2 : break;
212 : 2 : case MultiXactStatusForShare:
213 : 2 : snprintf(buf, NCHARS, "For Share");
214 : 2 : break;
215 : 8 : case MultiXactStatusForKeyShare:
216 : 8 : snprintf(buf, NCHARS, "For Key Share");
217 : 8 : break;
218 : : }
219 : 16 : strcat(values[Atnum_modes], buf);
220 : 16 : snprintf(buf, NCHARS, "%d",
221 : 16 : BackendXidGetPid(members[j].xid));
222 : 16 : strcat(values[Atnum_pids], buf);
223 : :
224 : 16 : first = false;
225 : : }
226 : :
227 : 8 : strcat(values[Atnum_xids], "}");
228 : 8 : strcat(values[Atnum_modes], "}");
229 : 8 : strcat(values[Atnum_pids], "}");
230 : : }
231 : : }
232 : : else
233 : : {
234 : 14 : values[Atnum_ismulti] = pstrdup("false");
235 : :
236 : 14 : values[Atnum_xids] = palloc_array(char, NCHARS);
237 : 14 : snprintf(values[Atnum_xids], NCHARS, "{%u}", xmax);
238 : :
239 : 14 : values[Atnum_modes] = palloc(NCHARS);
240 [ + + ]: 14 : if (infomask & HEAP_XMAX_LOCK_ONLY)
241 : : {
242 [ + + ]: 12 : if (HEAP_XMAX_IS_SHR_LOCKED(infomask))
243 : 2 : snprintf(values[Atnum_modes], NCHARS, "{For Share}");
244 [ + + ]: 10 : else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
245 : 6 : snprintf(values[Atnum_modes], NCHARS, "{For Key Share}");
246 [ + - ]: 4 : else if (HEAP_XMAX_IS_EXCL_LOCKED(infomask))
247 : : {
248 [ + + ]: 4 : if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
249 : 2 : snprintf(values[Atnum_modes], NCHARS, "{For Update}");
250 : : else
251 : 2 : snprintf(values[Atnum_modes], NCHARS, "{For No Key Update}");
252 : : }
253 : : else
254 : : /* neither keyshare nor exclusive bit it set */
255 : 0 : snprintf(values[Atnum_modes], NCHARS,
256 : : "{transient upgrade status}");
257 : : }
258 : : else
259 : : {
260 [ + + ]: 2 : if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
261 : 1 : snprintf(values[Atnum_modes], NCHARS, "{Update}");
262 : : else
263 : 1 : snprintf(values[Atnum_modes], NCHARS, "{No Key Update}");
264 : : }
265 : :
266 : 14 : values[Atnum_pids] = palloc_array(char, NCHARS);
267 : 14 : snprintf(values[Atnum_pids], NCHARS, "{%d}",
268 : : BackendXidGetPid(xmax));
269 : : }
270 : :
271 : 22 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
272 : :
273 : : /* build a tuple */
274 : 22 : tuple = BuildTupleFromCStrings(attinmeta, values);
275 : 22 : tuplestore_puttuple(rsinfo->setResult, tuple);
276 : : }
277 : : else
278 : : {
279 : 2 : LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
280 : : }
281 : : }
282 : :
283 : 12 : table_endscan(scan);
284 : 12 : table_close(rel, AccessShareLock);
285 : 12 : return (Datum) 0;
286 : : }
|