Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 56f3fb3

Browse files
committed
Restrict pgrowlocks function to superusers. (This might be too strict,
but no permissions check at all is certainly no good.) Clean up usage of some deprecated APIs.
1 parent 38c75ec commit 56f3fb3

File tree

4 files changed

+26
-61
lines changed

4 files changed

+26
-61
lines changed

contrib/pgrowlocks/README.pgrowlocks

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
$PostgreSQL: pgsql/contrib/pgrowlocks/README.pgrowlocks,v 1.1 2006/04/23 01:12:58 ishii Exp $
1+
$PostgreSQL: pgsql/contrib/pgrowlocks/README.pgrowlocks,v 1.2 2007/08/27 00:13:51 tgl Exp $
22

33
pgrowlocks README Tatsuo Ishii
44

55
1. What is pgrowlocks?
66

77
pgrowlocks shows row locking information for specified table.
88

9-
pgrowlocks returns following data type:
9+
pgrowlocks returns following columns:
1010

11-
CREATE TYPE pgrowlocks_type AS (
1211
locked_row TID, -- row TID
1312
lock_type TEXT, -- lock type
1413
locker XID, -- locking XID
1514
multi bool, -- multi XID?
1615
xids xid[], -- multi XIDs
1716
pids INTEGER[] -- locker's process id
18-
);
1917

2018
Here is a sample execution of pgrowlocks:
2119

@@ -62,14 +60,6 @@ test=# SELECT * FROM pgrowlocks('t1');
6260

6361
3. How to use pgrowlocks
6462

65-
The calling sequence for pgrowlocks is as follows:
66-
67-
CREATE OR REPLACE FUNCTION pgrowlocks(text) RETURNS pgrowlocks_type
68-
AS 'MODULE_PATHNAME', 'pgrowlocks'
69-
LANGUAGE 'c' WITH (isstrict);
70-
71-
The parameter is a name of table. pgrowlocks returns type pgrowlocks_type.
72-
7363
pgrowlocks grab AccessShareLock for the target table and read each
7464
row one by one to get the row locking information. You should
7565
notice that:

contrib/pgrowlocks/pgrowlocks.c

+15-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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 $
33
*
44
* Copyright (c) 2005-2006 Tatsuo Ishii
55
*
@@ -24,19 +24,15 @@
2424

2525
#include "postgres.h"
2626

27-
#include "funcapi.h"
2827
#include "access/heapam.h"
29-
#include "access/transam.h"
28+
#include "access/multixact.h"
3029
#include "access/xact.h"
3130
#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"
3434
#include "utils/builtins.h"
3535

36-
#ifdef HEAP_XMAX_SHARED_LOCK
37-
#include "access/multixact.h"
38-
#include "storage/procarray.h"
39-
#endif
4036

4137
PG_MODULE_MAGIC;
4238

@@ -47,22 +43,11 @@ extern Datum pgrowlocks(PG_FUNCTION_ARGS);
4743
/* ----------
4844
* pgrowlocks:
4945
* 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
5446
* ----------
5547
*/
5648

57-
#define DUMMY_TUPLE "public.pgrowlocks_type"
5849
#define NCHARS 32
5950

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-
6651
typedef struct
6752
{
6853
Relation rel;
@@ -82,6 +67,11 @@ pgrowlocks(PG_FUNCTION_ARGS)
8267
MyData *mydata;
8368
Relation rel;
8469

70+
if (!superuser())
71+
ereport(ERROR,
72+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
73+
(errmsg("must be superuser to use pgrowlocks"))));
74+
8575
if (SRF_IS_FIRSTCALL())
8676
{
8777
text *relname;
@@ -91,17 +81,17 @@ pgrowlocks(PG_FUNCTION_ARGS)
9181
funcctx = SRF_FIRSTCALL_INIT();
9282
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
9383

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+
9588
attinmeta = TupleDescGetAttInMetadata(tupdesc);
9689
funcctx->attinmeta = attinmeta;
9790

9891
relname = PG_GETARG_TEXT_P(0);
99-
#ifdef MAKERANGEVARFROMNAMELIST_HAS_TWO_ARGS
100-
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname, "pgrowlocks"));
101-
#else
10292
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
103-
#endif
10493
rel = heap_openrv(relrv, AccessShareLock);
94+
10595
scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
10696
mydata = palloc(sizeof(*mydata));
10797
mydata->rel = rel;
@@ -135,17 +125,12 @@ pgrowlocks(PG_FUNCTION_ARGS)
135125
i = 0;
136126
values[i++] = (char *) DirectFunctionCall1(tidout, PointerGetDatum(&tuple->t_self));
137127

138-
#ifdef HEAP_XMAX_SHARED_LOCK
139128
if (tuple->t_data->t_infomask & HEAP_XMAX_SHARED_LOCK)
140129
values[i++] = pstrdup("Shared");
141130
else
142131
values[i++] = pstrdup("Exclusive");
143-
#else
144-
values[i++] = pstrdup("Exclusive");
145-
#endif
146132
values[i] = palloc(NCHARS * sizeof(char));
147133
snprintf(values[i++], NCHARS, "%d", HeapTupleHeaderGetXmax(tuple->t_data));
148-
#ifdef HEAP_XMAX_SHARED_LOCK
149134
if (tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI)
150135
{
151136
TransactionId *xids;
@@ -198,11 +183,6 @@ pgrowlocks(PG_FUNCTION_ARGS)
198183
values[i] = palloc(NCHARS * sizeof(char));
199184
snprintf(values[i++], NCHARS, "{%d}", BackendXidGetPid(HeapTupleHeaderGetXmax(tuple->t_data)));
200185
}
201-
#else
202-
values[i++] = pstrdup("false");
203-
values[i++] = pstrdup("{}");
204-
values[i++] = pstrdup("{}");
205-
#endif
206186

207187
LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
208188

contrib/pgrowlocks/pgrowlocks.sql.in

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
-- Adjust this setting to control where the objects get created.
22
SET search_path = public;
33

4-
CREATE TYPE pgrowlocks_type AS (
5-
locked_row TID, -- row TID
6-
lock_type TEXT, -- lock type
7-
locker XID, -- locking XID
8-
multi bool, -- multi XID?
9-
xids xid[], -- multi XIDs
10-
pids INTEGER[] -- locker's process id
11-
);
12-
13-
CREATE OR REPLACE FUNCTION pgrowlocks(text)
14-
RETURNS setof pgrowlocks_type
4+
CREATE OR REPLACE FUNCTION pgrowlocks(IN relname text,
5+
OUT locked_row TID, -- row TID
6+
OUT lock_type TEXT, -- lock type
7+
OUT locker XID, -- locking XID
8+
OUT multi bool, -- multi XID?
9+
OUT xids xid[], -- multi XIDs
10+
OUT pids INTEGER[]) -- locker's process id
11+
RETURNS SETOF record
1512
AS 'MODULE_PATHNAME', 'pgrowlocks'
16-
LANGUAGE 'C' STRICT;
13+
LANGUAGE C STRICT;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
SET search_path = public;
22

33
DROP FUNCTION pgrowlocks(text);
4-
5-
DROP TYPE pgrowlocks_type;

0 commit comments

Comments
 (0)