1
1
/*
2
- * $PostgreSQL: pgsql/contrib/pgrowlocks/pgrowlocks.c,v 1.5 2006/10/04 00:29:46 momjian Exp $
2
+ * $PostgreSQL: pgsql/contrib/pgrowlocks/pgrowlocks.c,v 1.6 2007/08/27 00:13:51 tgl Exp $
3
3
*
4
4
* Copyright (c) 2005-2006 Tatsuo Ishii
5
5
*
24
24
25
25
#include "postgres.h"
26
26
27
- #include "funcapi.h"
28
27
#include "access/heapam.h"
29
- #include "access/transam .h"
28
+ #include "access/multixact .h"
30
29
#include "access/xact.h"
31
30
#include "catalog/namespace.h"
32
- #include "catalog/pg_type.h"
33
- #include "storage/proc.h"
31
+ #include "funcapi.h"
32
+ #include "miscadmin.h"
33
+ #include "storage/procarray.h"
34
34
#include "utils/builtins.h"
35
35
36
- #ifdef HEAP_XMAX_SHARED_LOCK
37
- #include "access/multixact.h"
38
- #include "storage/procarray.h"
39
- #endif
40
36
41
37
PG_MODULE_MAGIC ;
42
38
@@ -47,22 +43,11 @@ extern Datum pgrowlocks(PG_FUNCTION_ARGS);
47
43
/* ----------
48
44
* pgrowlocks:
49
45
* returns tids of rows being locked
50
- *
51
- * C FUNCTION definition
52
- * pgrowlocks(text) returns set of pgrowlocks_type
53
- * see pgrowlocks.sql for pgrowlocks_type
54
46
* ----------
55
47
*/
56
48
57
- #define DUMMY_TUPLE "public.pgrowlocks_type"
58
49
#define NCHARS 32
59
50
60
- /*
61
- * define this if makeRangeVarFromNameList() has two arguments. As far
62
- * as I know, this only happens in 8.0.x.
63
- */
64
- #undef MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
65
-
66
51
typedef struct
67
52
{
68
53
Relation rel ;
@@ -82,6 +67,11 @@ pgrowlocks(PG_FUNCTION_ARGS)
82
67
MyData * mydata ;
83
68
Relation rel ;
84
69
70
+ if (!superuser ())
71
+ ereport (ERROR ,
72
+ (errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
73
+ (errmsg ("must be superuser to use pgrowlocks" ))));
74
+
85
75
if (SRF_IS_FIRSTCALL ())
86
76
{
87
77
text * relname ;
@@ -91,17 +81,17 @@ pgrowlocks(PG_FUNCTION_ARGS)
91
81
funcctx = SRF_FIRSTCALL_INIT ();
92
82
oldcontext = MemoryContextSwitchTo (funcctx -> multi_call_memory_ctx );
93
83
94
- tupdesc = RelationNameGetTupleDesc (DUMMY_TUPLE );
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
+
95
88
attinmeta = TupleDescGetAttInMetadata (tupdesc );
96
89
funcctx -> attinmeta = attinmeta ;
97
90
98
91
relname = PG_GETARG_TEXT_P (0 );
99
- #ifdef MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
100
- relrv = makeRangeVarFromNameList (textToQualifiedNameList (relname , "pgrowlocks" ));
101
- #else
102
92
relrv = makeRangeVarFromNameList (textToQualifiedNameList (relname ));
103
- #endif
104
93
rel = heap_openrv (relrv , AccessShareLock );
94
+
105
95
scan = heap_beginscan (rel , SnapshotNow , 0 , NULL );
106
96
mydata = palloc (sizeof (* mydata ));
107
97
mydata -> rel = rel ;
@@ -135,17 +125,12 @@ pgrowlocks(PG_FUNCTION_ARGS)
135
125
i = 0 ;
136
126
values [i ++ ] = (char * ) DirectFunctionCall1 (tidout , PointerGetDatum (& tuple -> t_self ));
137
127
138
- #ifdef HEAP_XMAX_SHARED_LOCK
139
128
if (tuple -> t_data -> t_infomask & HEAP_XMAX_SHARED_LOCK )
140
129
values [i ++ ] = pstrdup ("Shared" );
141
130
else
142
131
values [i ++ ] = pstrdup ("Exclusive" );
143
- #else
144
- values [i ++ ] = pstrdup ("Exclusive" );
145
- #endif
146
132
values [i ] = palloc (NCHARS * sizeof (char ));
147
133
snprintf (values [i ++ ], NCHARS , "%d" , HeapTupleHeaderGetXmax (tuple -> t_data ));
148
- #ifdef HEAP_XMAX_SHARED_LOCK
149
134
if (tuple -> t_data -> t_infomask & HEAP_XMAX_IS_MULTI )
150
135
{
151
136
TransactionId * xids ;
@@ -198,11 +183,6 @@ pgrowlocks(PG_FUNCTION_ARGS)
198
183
values [i ] = palloc (NCHARS * sizeof (char ));
199
184
snprintf (values [i ++ ], NCHARS , "{%d}" , BackendXidGetPid (HeapTupleHeaderGetXmax (tuple -> t_data )));
200
185
}
201
- #else
202
- values [i ++ ] = pstrdup ("false" );
203
- values [i ++ ] = pstrdup ("{}" );
204
- values [i ++ ] = pstrdup ("{}" );
205
- #endif
206
186
207
187
LockBuffer (scan -> rs_cbuf , BUFFER_LOCK_UNLOCK );
208
188
0 commit comments