OSDN Git Service

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