Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcf7ee63

Browse files
committed
Adapt for SRF(Set Returning Function).
1 parent6415ffe commitcf7ee63

File tree

4 files changed

+196
-89
lines changed

4 files changed

+196
-89
lines changed

‎contrib/pgstattuple/README.pgstattuple

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
pgstattuple README2001/10/01 Tatsuo Ishii
1+
pgstattuple README2002/08/22 Tatsuo Ishii
22

33
1. What is pgstattuple?
44

5-
pgstattuple returns the percentage of the "dead" tuples of a
6-
table. This will help users to judge if vacuum is needed.
7-
8-
In addition, pgstattuple prints more detailed information using
9-
NOTICE.
10-
11-
test=# select pgstattuple('tellers');
12-
NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 320 (0.01MB, 18.75%) free/reusable space: 0.01MB (18.06%) overhead: 62.02%
13-
pgstattuple
14-
-------------
15-
18.75
16-
(1 row)
17-
18-
Above example shows tellers table includes 18.75% dead tuples.
19-
20-
physical lengthphysical size of the table in MB
21-
live tuplesinformation on the live tuples
22-
dead tuplesinformation on the dead tuples
23-
free/reusable spaceavailable space
24-
overheadoverhead space
5+
pgstattuple returns the table length, percentage of the "dead"
6+
tuples of a table and other info. This may help users to determine
7+
whether vacuum is necessary or not. Here is an example session:
8+
9+
test=# \x
10+
Expanded display is on.
11+
test=# select * from pgstattuple('pg_proc');
12+
-[ RECORD 1 ]------+-------
13+
table_len | 458752
14+
tuple_count | 1470
15+
tuple_len | 438896
16+
tuple_percent | 95.67
17+
dead_tuple_count | 11
18+
dead_tuple_len | 3157
19+
dead_tuple_percent | 0.69
20+
free_space | 8932
21+
free_percent | 1.95
22+
23+
24+
Here are explanations for each column:
25+
26+
table_len-- physical table length in bytes
27+
tuple_count-- number of live tuples
28+
tuple_len-- total tuples length in bytes
29+
tuple_percent-- live tuples in %
30+
dead_tuple_len-- total dead tuples length in bytes
31+
dead_tuple_percent-- dead tuples in %
32+
free_space-- free space in bytes
33+
free_percent-- free space in %
2534

2635
2. Installing pgstattuple
2736

@@ -31,12 +40,15 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
3140

3241
3. Using pgstattuple
3342

34-
pgstattuple can be called as a function:
43+
pgstattuple may be called as a SRF (set returning function) and is
44+
defined as follows:
3545

36-
pgstattuple(TEXT) RETURNS FLOAT8
46+
CREATE OR REPLACE FUNCTION pgstattuple(text) RETURNS SETOF pgstattuple_view
47+
AS 'MODULE_PATHNAME', 'pgstattuple'
48+
LANGUAGE 'c' WITH (isstrict);
3749

38-
The argument is the table name.pgstattuple returns the percentage
39-
of the "dead" tuples of a table.
50+
The argument is the table name.Note that pgstattuple never
51+
returns more than 1 tuple.
4052

4153
4. Notes
4254

‎contrib/pgstattuple/README.pgstattuple.euc_jp

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
$Id: README.pgstattuple.euc_jp,v 1.2 2001/12/20 01:56:08 ishii Exp $
1+
$Id: README.pgstattuple.euc_jp,v 1.3 2002/08/23 08:19:49 ishii Exp $
22

3-
pgstattuple README2001/10/01 �а�ã��
3+
pgstattuple README2002/08/22 �а�ã��
44

55
1. pgstattuple�Ȥ�
66

77
pgstattuple�ϡ�UPDATE��DELETE�Ǻ��줿�ơ��֥�Υ����ΰ���礭����
88
�ơ��֥뼫�Τ�ʪ��Ū���礭�����Ф���ѡ�����ơ������ֵѤ��ޤ�����
99
�ޤꡤ�ֵ��ͤ��礭����С�����������ߤ�¿���Τǡ�vacuum�򤫤���ɬ
10-
�פ�����Ȥ���Ƚ�Ǥν����ˤʤ�櫓�Ǥ���
11-
12-
��������ǤϾ����̤����ʤ��Τǡ�NOTICE��å������Ǥ�����ʾ�����
13-
���Ǥ˽��Ϥ��ޤ���
14-
15-
test=# select pgstattuple('tellers');
16-
NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 320 (0.01MB, 18.75%) free/reusable space: 0.01MB (18.06%) overhead: 62.02%
17-
pgstattuple
18-
-------------
19-
18.75
20-
(1 row)
21-
22-
���μ¹���Ǥϡ�19%�ۤɤ����ߤˤʤäƤ��뤳�Ȥ��Ǥ��ޤ���NOTICE���
23-
�������θ�����񤤤Ƥ����ޤ���
24-
25-
physical length�ơ��֥��ʪ��Ū�ʥ�������MBñ�̤�ɽ��
26-
live tuples���ߤǤϤʤ����ץ�˴ؤ�����󡥥��ץ������
27-
���ץ뤬�����ΰ�ι�ס��ơ��֥����Τ��Ф���
28-
��Ψ��ɽ�����ޤ���
29-
dead tuples���ߤˤʤä����ץ�˴ؤ������
30-
free/reusable space���Ѳ�ǽ��̤�����ΰ������Ѳ�ǽ�ΰ�
31-
overhead�����Τ�����ΰ褬�ơ��֥����Τ�������Ψ
10+
�פ�����Ȥ���Ƚ�Ǥν����ˤʤ�櫓�Ǥ�������ʳ��ˤ⤤�������ʾ���
11+
���֤�ޤ���
12+
13+
test=# \x
14+
Expanded display is on.
15+
test=# select * from pgstattuple('pg_proc');
16+
-[ RECORD 1 ]------+-------
17+
table_len | 458752
18+
tuple_count | 1470
19+
tuple_len | 438896
20+
tuple_percent | 95.67
21+
dead_tuple_count | 11
22+
dead_tuple_len | 3157
23+
dead_tuple_percent | 0.69
24+
free_space | 8932
25+
free_percent | 1.95
26+
27+
�ƹ��ܤ������Ǥ���
28+
29+
table_len-- �ơ��֥��ʪ��Ū���礭��(�Х���)
30+
tuple_count-- ���ץ��
31+
tuple_len-- ���ץ�Ĺ�ι��(�Х���)
32+
tuple_percent-- ���ץ�γ�硥table_len���Ф���tuple_len����Ψ��
33+
dead_tuple_len-- �ǥåɥ��ץ��
34+
dead_tuple_percent-- �ǥåɥ��ץ�γ�硥table_len���Ф���tuple_len����Ψ��
35+
free_space-- �����Ѳ�ǽ���ΰ�(�Х���)
36+
free_percent-- �����Ѳ�ǽ���ΰ补table_len���Ф���free_space����Ψ��
3237

3338
2. pgstattuple�Υ��󥹥ȡ���
3439

@@ -47,12 +52,14 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
4752

4853
pgstattuple�θƤӽФ������ϰʲ��Ǥ���
4954

50-
pgstattuple(NAME) RETURNS FLOAT8
55+
CREATE OR REPLACE FUNCTION pgstattuple(text) RETURNS SETOF pgstattuple_view
56+
AS 'MODULE_PATHNAME', 'pgstattuple'
57+
LANGUAGE 'c' WITH (isstrict);
5158

5259
������: �ơ��֥�̾
5360

54-
�ؿ�������UPDATE��DELETE�Ǻ��줿�ơ��֥�Υ����ΰ���礭���ǡ
55-
ơ��֥��ʪ��Ū���礭�����Ф�����(�ѡ������)���ֵѤ��ޤ���
61+
�ؿ�������pgstattuple_view���Ǥ����ޤ���pgstattuple��1�Ԥ����Ԥ
62+
֤��ޤ���
5663

5764
�ʤ���pgstattuple�ϥơ��֥��AccessShareLock���������ʤ��Τǡ�
5865
pgstattuple ��¹���˳����ơ��֥�˹���������ȯ������ȡ�������
@@ -69,6 +76,10 @@ NOTICE: physical length: 0.08MB live tuples: 20 (0.00MB, 1.17%) dead tuples: 32
6976

7077
5. ��������
7178

79+
2002/08/23
80+
81+
SRF(Set Returning Function)��Ȥä�7.3�Ѥ˽񤭴�����
82+
7283
2001/12/20 Tom Lane�ˤ�뽤��
7384

7485
Fix pgstattuple to acquire a read lock on the target table. This

‎contrib/pgstattuple/pgstattuple.c

Lines changed: 105 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* $Header: /cvsroot/pgsql/contrib/pgstattuple/pgstattuple.c,v 1.6 2002/05/20 23:51:40 tgl Exp $
2+
* $Header: /cvsroot/pgsql/contrib/pgstattuple/pgstattuple.c,v 1.7 2002/08/23 08:19:49 ishii Exp $
33
*
4-
* Copyright (c) 2001 Tatsuo Ishii
4+
* Copyright (c) 2001,2002 Tatsuo Ishii
55
*
66
* Permission to use, copy, modify, and distribute this software and
77
* its documentation for any purpose, without fee, and without a
@@ -28,6 +28,7 @@
2828
#include"access/heapam.h"
2929
#include"access/transam.h"
3030
#include"catalog/namespace.h"
31+
#include"funcapi.h"
3132
#include"utils/builtins.h"
3233

3334

@@ -37,16 +38,22 @@ extern Datum pgstattuple(PG_FUNCTION_ARGS);
3738

3839
/* ----------
3940
* pgstattuple:
40-
* returnsthe percentage ofdead tuples
41+
* returnslive/dead tuples info
4142
*
4243
* C FUNCTION definition
43-
* pgstattuple(NAME) returns FLOAT8
44+
* pgstattuple(TEXT) returns setof pgstattuple_view
45+
* see pgstattuple.sql for pgstattuple_view
4446
* ----------
4547
*/
48+
49+
#defineDUMMY_TUPLE "pgstattuple_view"
50+
#defineNCOLUMNS 9
51+
#defineNCHARS 32
52+
4653
Datum
4754
pgstattuple(PG_FUNCTION_ARGS)
4855
{
49-
text*relname=PG_GETARG_TEXT_P(0);
56+
text*relname;
5057
RangeVar*relrv;
5158
Relationrel;
5259
HeapScanDescscan;
@@ -55,7 +62,7 @@ pgstattuple(PG_FUNCTION_ARGS)
5562
BlockNumberblock=0;/* next block to count free space in */
5663
BlockNumbertupblock;
5764
Bufferbuffer;
58-
doubletable_len;
65+
uint64table_len;
5966
uint64tuple_len=0;
6067
uint64dead_tuple_len=0;
6168
uint64tuple_count=0;
@@ -65,13 +72,67 @@ pgstattuple(PG_FUNCTION_ARGS)
6572
uint64free_space=0;/* free/reusable space in bytes */
6673
doublefree_percent;/* free/reusable space in % */
6774

68-
relrv=makeRangeVarFromNameList(textToQualifiedNameList(relname,
69-
"pgstattuple"));
75+
FuncCallContext*funcctx;
76+
intcall_cntr;
77+
intmax_calls;
78+
TupleDesctupdesc;
79+
TupleTableSlot*slot;
80+
AttInMetadata*attinmeta;
81+
82+
char**values;
83+
inti;
84+
Datumresult;
85+
86+
/* stuff done only on the first call of the function */
87+
if(SRF_IS_FIRSTCALL())
88+
{
89+
/* create a function context for cross-call persistence */
90+
funcctx=SRF_FIRSTCALL_INIT();
91+
92+
/* total number of tuples to be returned */
93+
funcctx->max_calls=1;
94+
95+
/*
96+
* Build a tuple description for a pgstattupe_view tuple
97+
*/
98+
tupdesc=RelationNameGetTupleDesc(DUMMY_TUPLE);
99+
100+
/* allocate a slot for a tuple with this tupdesc */
101+
slot=TupleDescGetSlot(tupdesc);
102+
103+
/* assign slot to function context */
104+
funcctx->slot=slot;
105+
106+
/*
107+
* Generate attribute metadata needed later to produce tuples from raw
108+
* C strings
109+
*/
110+
attinmeta=TupleDescGetAttInMetadata(tupdesc);
111+
funcctx->attinmeta=attinmeta;
112+
}
113+
114+
/* stuff done on every call of the function */
115+
funcctx=SRF_PERCALL_SETUP();
116+
call_cntr=funcctx->call_cntr;
117+
max_calls=funcctx->max_calls;
118+
slot=funcctx->slot;
119+
attinmeta=funcctx->attinmeta;
120+
121+
/* Are we done? */
122+
if (call_cntr >=max_calls)
123+
{
124+
SRF_RETURN_DONE(funcctx);
125+
}
126+
127+
/* open relation */
128+
relname=PG_GETARG_TEXT_P(0);
129+
relrv=makeRangeVarFromNameList(textToQualifiedNameList(relname,"pgstattuple"));
70130
rel=heap_openrv(relrv,AccessShareLock);
71131

72132
nblocks=RelationGetNumberOfBlocks(rel);
73133
scan=heap_beginscan(rel,SnapshotAny,0,NULL);
74134

135+
/* scan the relation */
75136
while ((tuple=heap_getnext(scan,ForwardScanDirection))!=NULL)
76137
{
77138
if (HeapTupleSatisfiesNow(tuple->t_data))
@@ -113,7 +174,7 @@ pgstattuple(PG_FUNCTION_ARGS)
113174

114175
heap_close(rel,AccessShareLock);
115176

116-
table_len= (double)nblocks*BLCKSZ;
177+
table_len= (uint64)nblocks*BLCKSZ;
117178

118179
if (nblocks==0)
119180
{
@@ -128,28 +189,39 @@ pgstattuple(PG_FUNCTION_ARGS)
128189
free_percent= (double)free_space*100.0 /table_len;
129190
}
130191

131-
elog(DEBUG3,"physical length: %.2fMB live tuples: %.0f (%.2fMB, %.2f%%) dead tuples: %.0f (%.2fMB, %.2f%%) free/reusable space: %.2fMB (%.2f%%) overhead: %.2f%%",
132-
133-
table_len / (1024*1024),/* physical length in MB */
134-
135-
(double)tuple_count,/* number of live tuples */
136-
(double)tuple_len / (1024*1024),/* live tuples in MB */
137-
tuple_percent,/* live tuples in % */
138-
139-
(double)dead_tuple_count,/* number of dead tuples */
140-
(double)dead_tuple_len / (1024*1024),/* dead tuples in MB */
141-
dead_tuple_percent,/* dead tuples in % */
142-
143-
(double)free_space / (1024*1024),/* free/available space in
144-
* MB */
145-
146-
free_percent,/* free/available space in % */
147-
148-
/* overhead in % */
149-
(nblocks==0) ?0.0 :100.0
150-
-tuple_percent
151-
-dead_tuple_percent
152-
-free_percent);
153-
154-
PG_RETURN_FLOAT8(dead_tuple_percent);
192+
/*
193+
* Prepare a values array for storage in our slot.
194+
* This should be an array of C strings which will
195+
* be processed later by the appropriate "in" functions.
196+
*/
197+
values= (char**)palloc(NCOLUMNS*sizeof(char*));
198+
for (i=0;i<NCOLUMNS;i++)
199+
{
200+
values[i]= (char*)palloc(NCHARS*sizeof(char));
201+
}
202+
i=0;
203+
snprintf(values[i++],NCHARS,"%lld",table_len);
204+
snprintf(values[i++],NCHARS,"%lld",tuple_count);
205+
snprintf(values[i++],NCHARS,"%lld",tuple_len);
206+
snprintf(values[i++],NCHARS,"%.2f",tuple_percent);
207+
snprintf(values[i++],NCHARS,"%lld",dead_tuple_count);
208+
snprintf(values[i++],NCHARS,"%lld",dead_tuple_len);
209+
snprintf(values[i++],NCHARS,"%.2f",dead_tuple_percent);
210+
snprintf(values[i++],NCHARS,"%lld",free_space);
211+
snprintf(values[i++],NCHARS,"%.2f",free_percent);
212+
213+
/* build a tuple */
214+
tuple=BuildTupleFromCStrings(attinmeta,values);
215+
216+
/* make the tuple into a datum */
217+
result=TupleGetDatum(slot,tuple);
218+
219+
/* Clean up */
220+
for (i=0;i<NCOLUMNS;i++)
221+
{
222+
pfree(values[i]);
223+
}
224+
pfree(values);
225+
226+
SRF_RETURN_NEXT(funcctx,result);
155227
}
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
DROP FUNCTION pgstattuple(text);
2-
CREATE FUNCTION pgstattuple(text) RETURNS float8
3-
AS 'MODULE_PATHNAME', 'pgstattuple'
4-
LANGUAGE 'c' WITH (isstrict);
1+
DROP VIEW pgstattuple_view CASCADE;
2+
CREATE VIEW pgstattuple_view AS
3+
SELECT
4+
0::BIGINT AS table_len,-- physical table length in bytes
5+
0::BIGINT AS tuple_count,-- number of live tuples
6+
0::BIGINT AS tuple_len,-- total tuples length in bytes
7+
0.0::FLOAT AS tuple_percent,-- live tuples in %
8+
0::BIGINT AS dead_tuple_count,-- number of dead tuples
9+
0::BIGINT AS dead_tuple_len,-- total dead tuples length in bytes
10+
0.0::FLOAT AS dead_tuple_percent,-- dead tuples in %
11+
0::BIGINT AS free_space,-- free space in bytes
12+
0.0::FLOAT AS free_percent;-- free space in %
13+
14+
CREATE OR REPLACE FUNCTION pgstattuple(text) RETURNS SETOF pgstattuple_view
15+
AS 'MODULE_PATHNAME', 'pgstattuple'
16+
LANGUAGE 'c' WITH (isstrict);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp