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

Commit676bb9a

Browse files
committed
dbsize modification to support tablespaces
Gavin Sherry
1 parenta583675 commit676bb9a

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

‎contrib/dbsize/dbsize.c

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66

77
#include"access/heapam.h"
88
#include"catalog/catalog.h"
9+
#include"catalog/catname.h"
910
#include"catalog/namespace.h"
11+
#include"catalog/pg_tablespace.h"
1012
#include"commands/dbcommands.h"
1113
#include"fmgr.h"
1214
#include"storage/fd.h"
1315
#include"utils/builtins.h"
1416

1517

18+
staticint64
19+
get_tablespace_size(Oiddbid,Oidspcid,boolbaddirOK);
20+
1621
staticchar*
1722
psnprintf(size_tlen,constchar*fmt,...)
1823
{
@@ -44,25 +49,68 @@ database_size(PG_FUNCTION_ARGS)
4449
Namedbname=PG_GETARG_NAME(0);
4550

4651
Oiddbid;
47-
char*dbpath;
48-
DIR*dirdesc;
49-
structdirent*direntry;
5052
int64totalsize;
53+
#ifdefSYMLINK
54+
Relationdbrel;
55+
HeapScanDescscan;
56+
HeapTupletuple;
57+
#endif
5158

5259
dbid=get_database_oid(NameStr(*dbname));
5360
if (!OidIsValid(dbid))
5461
ereport(ERROR,
5562
(errcode(ERRCODE_UNDEFINED_DATABASE),
5663
errmsg("database \"%s\" does not exist",NameStr(*dbname))));
5764

58-
dbpath=GetDatabasePath(dbid);
65+
#ifdefSYMLINK
66+
67+
dbrel=heap_openr(TableSpaceRelationName,AccessShareLock);
68+
scan=heap_beginscan(dbrel,SnapshotNow,0, (ScanKey)NULL);
69+
70+
totalsize=0;
71+
72+
while((tuple=heap_getnext(scan,ForwardScanDirection)))
73+
{
74+
Oidspcid=HeapTupleGetOid(tuple);
75+
if(spcid!=GLOBALTABLESPACE_OID)
76+
totalsize+=get_tablespace_size(dbid,spcid, true);
77+
}
78+
heap_endscan(scan);
79+
heap_close(dbrel,AccessShareLock);
80+
#else
81+
/* Same as always */
82+
totalsize=get_tablespace_size(dbid,DEFAULTTABLESPACE_OID, false);
83+
#endif
84+
85+
/*
86+
* We need to keep in mind that we may not be called from the database
87+
* whose size we're reporting so, we need to look in every tablespace
88+
* to see if our database has data in there
89+
*/
90+
91+
PG_RETURN_INT64(totalsize);
92+
}
93+
94+
staticint64
95+
get_tablespace_size(Oiddbid,Oidspcid,boolbaddirOK)
96+
{
97+
char*dbpath;
98+
DIR*dirdesc;
99+
structdirent*direntry;
100+
int64totalsize;
101+
102+
dbpath=GetDatabasePath(dbid,spcid);
59103

60104
dirdesc=AllocateDir(dbpath);
61105
if (!dirdesc)
62-
ereport(ERROR,
106+
{
107+
if(baddirOK)
108+
return0;
109+
else
110+
ereport(ERROR,
63111
(errcode_for_file_access(),
64112
errmsg("could not open directory \"%s\": %m",dbpath)));
65-
113+
}
66114
totalsize=0;
67115
for (;;)
68116
{
@@ -87,18 +135,14 @@ database_size(PG_FUNCTION_ARGS)
87135
ereport(ERROR,
88136
(errcode_for_file_access(),
89137
errmsg("could not stat \"%s\": %m",fullname)));
90-
91138
totalsize+=statbuf.st_size;
92139
pfree(fullname);
93140
}
94141

95142
FreeDir(dirdesc);
96-
97-
PG_RETURN_INT64(totalsize);
143+
return (totalsize);
98144
}
99145

100-
101-
102146
/*
103147
* SQL function: relation_size(text) returns bigint
104148
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp