OSDN Git Service

302bb5c39c97742ac4e78f90a2b1312643c98b54
[pg-rex/syncrep.git] / contrib / pgrowlocks / pgrowlocks.c
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/xact.h"
31 #include "catalog/namespace.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/tqual.h"
39
40
41 PG_MODULE_MAGIC;
42
43 PG_FUNCTION_INFO_V1(pgrowlocks);
44
45 extern Datum pgrowlocks(PG_FUNCTION_ARGS);
46
47 /* ----------
48  * pgrowlocks:
49  * returns tids of rows being locked
50  * ----------
51  */
52
53 #define NCHARS 32
54
55 typedef struct
56 {
57         Relation        rel;
58         HeapScanDesc scan;
59         int                     ncolumns;
60 } MyData;
61
62 Datum
63 pgrowlocks(PG_FUNCTION_ARGS)
64 {
65         FuncCallContext *funcctx;
66         HeapScanDesc scan;
67         HeapTuple       tuple;
68         TupleDesc       tupdesc;
69         AttInMetadata *attinmeta;
70         Datum           result;
71         MyData     *mydata;
72         Relation        rel;
73
74         if (SRF_IS_FIRSTCALL())
75         {
76                 text       *relname;
77                 RangeVar   *relrv;
78                 MemoryContext oldcontext;
79                 AclResult       aclresult;
80
81                 funcctx = SRF_FIRSTCALL_INIT();
82                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
83
84                 /* Build a tuple descriptor for our result type */
85                 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
86                         elog(ERROR, "return type must be a row type");
87
88                 attinmeta = TupleDescGetAttInMetadata(tupdesc);
89                 funcctx->attinmeta = attinmeta;
90
91                 relname = PG_GETARG_TEXT_P(0);
92                 relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
93                 rel = heap_openrv(relrv, AccessShareLock);
94
95                 /* check permissions: must have SELECT on table */
96                 aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
97                                                                           ACL_SELECT);
98                 if (aclresult != ACLCHECK_OK)
99                         aclcheck_error(aclresult, ACL_KIND_CLASS,
100                                                    RelationGetRelationName(rel));
101
102                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
103                 mydata = palloc(sizeof(*mydata));
104                 mydata->rel = rel;
105                 mydata->scan = scan;
106                 mydata->ncolumns = tupdesc->natts;
107                 funcctx->user_fctx = mydata;
108
109                 MemoryContextSwitchTo(oldcontext);
110         }
111
112         funcctx = SRF_PERCALL_SETUP();
113         attinmeta = funcctx->attinmeta;
114         mydata = (MyData *) funcctx->user_fctx;
115         scan = mydata->scan;
116
117         /* scan the relation */
118         while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
119         {
120                 /* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
121                 LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
122
123                 if (HeapTupleSatisfiesUpdate(tuple->t_data,
124                                                                          GetCurrentCommandId(false),
125                                                                          scan->rs_cbuf) == HeapTupleBeingUpdated)
126                 {
127
128                         char      **values;
129                         int                     i;
130
131                         values = (char **) palloc(mydata->ncolumns * sizeof(char *));
132
133                         i = 0;
134                         values[i++] = (char *) DirectFunctionCall1(tidout, PointerGetDatum(&tuple->t_self));
135
136                         if (tuple->t_data->t_infomask & HEAP_XMAX_SHARED_LOCK)
137                                 values[i++] = pstrdup("Shared");
138                         else
139                                 values[i++] = pstrdup("Exclusive");
140                         values[i] = palloc(NCHARS * sizeof(char));
141                         snprintf(values[i++], NCHARS, "%d", HeapTupleHeaderGetXmax(tuple->t_data));
142                         if (tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI)
143                         {
144                                 TransactionId *xids;
145                                 int                     nxids;
146                                 int                     j;
147                                 int                     isValidXid = 0;         /* any valid xid ever exists? */
148
149                                 values[i++] = pstrdup("true");
150                                 nxids = GetMultiXactIdMembers(HeapTupleHeaderGetXmax(tuple->t_data), &xids);
151                                 if (nxids == -1)
152                                 {
153                                         elog(ERROR, "GetMultiXactIdMembers returns error");
154                                 }
155
156                                 values[i] = palloc(NCHARS * nxids);
157                                 values[i + 1] = palloc(NCHARS * nxids);
158                                 strcpy(values[i], "{");
159                                 strcpy(values[i + 1], "{");
160
161                                 for (j = 0; j < nxids; j++)
162                                 {
163                                         char            buf[NCHARS];
164
165                                         if (TransactionIdIsInProgress(xids[j]))
166                                         {
167                                                 if (isValidXid)
168                                                 {
169                                                         strcat(values[i], ",");
170                                                         strcat(values[i + 1], ",");
171                                                 }
172                                                 snprintf(buf, NCHARS, "%d", xids[j]);
173                                                 strcat(values[i], buf);
174                                                 snprintf(buf, NCHARS, "%d", BackendXidGetPid(xids[j]));
175                                                 strcat(values[i + 1], buf);
176
177                                                 isValidXid = 1;
178                                         }
179                                 }
180
181                                 strcat(values[i], "}");
182                                 strcat(values[i + 1], "}");
183                                 i++;
184                         }
185                         else
186                         {
187                                 values[i++] = pstrdup("false");
188                                 values[i] = palloc(NCHARS * sizeof(char));
189                                 snprintf(values[i++], NCHARS, "{%d}", HeapTupleHeaderGetXmax(tuple->t_data));
190
191                                 values[i] = palloc(NCHARS * sizeof(char));
192                                 snprintf(values[i++], NCHARS, "{%d}", BackendXidGetPid(HeapTupleHeaderGetXmax(tuple->t_data)));
193                         }
194
195                         LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
196
197                         /* build a tuple */
198                         tuple = BuildTupleFromCStrings(attinmeta, values);
199
200                         /* make the tuple into a datum */
201                         result = HeapTupleGetDatum(tuple);
202
203                         /* Clean up */
204                         for (i = 0; i < mydata->ncolumns; i++)
205                                 pfree(values[i]);
206                         pfree(values);
207
208                         SRF_RETURN_NEXT(funcctx, result);
209                 }
210                 else
211                 {
212                         LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
213                 }
214         }
215
216         heap_endscan(scan);
217         heap_close(mydata->rel, AccessShareLock);
218
219         SRF_RETURN_DONE(funcctx);
220 }