mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-12 18:34:36 +08:00
Add pg_ls_tmpdir function
This lists the contents of a temporary directory associated to a given tablespace, useful to get information about on-disk consumption caused by temporary files used by a session query. By default, pg_default is scanned, and a tablespace can be specified as argument. This function is intended to be used by monitoring tools, and, unlike pg_ls_dir(), access to them can be granted to non-superusers so that those monitoring tools can observe the principle of least privilege. Access is also given by default to members of pg_monitor. Author: Nathan Bossart Reviewed-by: Laurenz Albe Discussion: https://postgr.es/m/92F458A2-6459-44B8-A7F2-2ADD3225046A@amazon.com
This commit is contained in:
parent
d73f4c74dd
commit
9cd92d1a33
@ -20355,6 +20355,20 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
|
|||||||
role and may be granted to other non-superuser roles.
|
role and may be granted to other non-superuser roles.
|
||||||
</entry>
|
</entry>
|
||||||
</row>
|
</row>
|
||||||
|
<row>
|
||||||
|
<entry>
|
||||||
|
<literal><function>pg_ls_tmpdir(<optional><parameter>tablespace</parameter> <type>oid</type></optional>)</function></literal>
|
||||||
|
</entry>
|
||||||
|
<entry><type>setof record</type></entry>
|
||||||
|
<entry>
|
||||||
|
List the name, size, and last modification time of files in the
|
||||||
|
temporary directory for <parameter>tablespace</parameter>. If
|
||||||
|
<parameter>tablespace</parameter> is not provided, the
|
||||||
|
<literal>pg_default</literal> tablespace is used. Access is granted
|
||||||
|
to members of the <literal>pg_monitor</literal> role and may be
|
||||||
|
granted to other non-superuser roles.
|
||||||
|
</entry>
|
||||||
|
</row>
|
||||||
<row>
|
<row>
|
||||||
<entry>
|
<entry>
|
||||||
<literal><function>pg_read_file(<parameter>filename</parameter> <type>text</type> [, <parameter>offset</parameter> <type>bigint</type>, <parameter>length</parameter> <type>bigint</type> [, <parameter>missing_ok</parameter> <type>boolean</type>] ])</function></literal>
|
<literal><function>pg_read_file(<parameter>filename</parameter> <type>text</type> [, <parameter>offset</parameter> <type>bigint</type>, <parameter>length</parameter> <type>bigint</type> [, <parameter>missing_ok</parameter> <type>boolean</type>] ])</function></literal>
|
||||||
@ -20428,6 +20442,19 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
|
|||||||
<command>GRANT</command>.
|
<command>GRANT</command>.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
<indexterm>
|
||||||
|
<primary>pg_ls_tmpdir</primary>
|
||||||
|
</indexterm>
|
||||||
|
<para>
|
||||||
|
<function>pg_ls_tmpdir</function> returns the name, size, and last modified
|
||||||
|
time (mtime) of each file in the temporary file directory for the specified
|
||||||
|
<parameter>tablespace</parameter>. If <parameter>tablespace</parameter> is
|
||||||
|
not provided, the <literal>pg_default</literal> tablespace is used. By
|
||||||
|
default only superusers and members of the <literal>pg_monitor</literal>
|
||||||
|
role can use this function. Access may be granted to others using
|
||||||
|
<command>GRANT</command>.
|
||||||
|
</para>
|
||||||
|
|
||||||
<indexterm>
|
<indexterm>
|
||||||
<primary>pg_read_file</primary>
|
<primary>pg_read_file</primary>
|
||||||
</indexterm>
|
</indexterm>
|
||||||
|
@ -1150,6 +1150,8 @@ REVOKE EXECUTE ON FUNCTION lo_export(oid, text) FROM public;
|
|||||||
|
|
||||||
REVOKE EXECUTE ON FUNCTION pg_ls_logdir() FROM public;
|
REVOKE EXECUTE ON FUNCTION pg_ls_logdir() FROM public;
|
||||||
REVOKE EXECUTE ON FUNCTION pg_ls_waldir() FROM public;
|
REVOKE EXECUTE ON FUNCTION pg_ls_waldir() FROM public;
|
||||||
|
REVOKE EXECUTE ON FUNCTION pg_ls_tmpdir() FROM public;
|
||||||
|
REVOKE EXECUTE ON FUNCTION pg_ls_tmpdir(oid) FROM public;
|
||||||
|
|
||||||
REVOKE EXECUTE ON FUNCTION pg_read_file(text) FROM public;
|
REVOKE EXECUTE ON FUNCTION pg_read_file(text) FROM public;
|
||||||
REVOKE EXECUTE ON FUNCTION pg_read_file(text,bigint,bigint) FROM public;
|
REVOKE EXECUTE ON FUNCTION pg_read_file(text,bigint,bigint) FROM public;
|
||||||
@ -1170,6 +1172,8 @@ REVOKE EXECUTE ON FUNCTION pg_ls_dir(text,boolean,boolean) FROM public;
|
|||||||
--
|
--
|
||||||
GRANT EXECUTE ON FUNCTION pg_ls_logdir() TO pg_monitor;
|
GRANT EXECUTE ON FUNCTION pg_ls_logdir() TO pg_monitor;
|
||||||
GRANT EXECUTE ON FUNCTION pg_ls_waldir() TO pg_monitor;
|
GRANT EXECUTE ON FUNCTION pg_ls_waldir() TO pg_monitor;
|
||||||
|
GRANT EXECUTE ON FUNCTION pg_ls_tmpdir() TO pg_monitor;
|
||||||
|
GRANT EXECUTE ON FUNCTION pg_ls_tmpdir(oid) TO pg_monitor;
|
||||||
|
|
||||||
GRANT pg_read_all_settings TO pg_monitor;
|
GRANT pg_read_all_settings TO pg_monitor;
|
||||||
GRANT pg_read_all_stats TO pg_monitor;
|
GRANT pg_read_all_stats TO pg_monitor;
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
#include "access/htup_details.h"
|
#include "access/htup_details.h"
|
||||||
#include "access/xlog_internal.h"
|
#include "access/xlog_internal.h"
|
||||||
#include "catalog/pg_authid.h"
|
#include "catalog/pg_authid.h"
|
||||||
|
#include "catalog/pg_tablespace_d.h"
|
||||||
#include "catalog/pg_type.h"
|
#include "catalog/pg_type.h"
|
||||||
#include "funcapi.h"
|
#include "funcapi.h"
|
||||||
#include "mb/pg_wchar.h"
|
#include "mb/pg_wchar.h"
|
||||||
@ -31,6 +32,7 @@
|
|||||||
#include "storage/fd.h"
|
#include "storage/fd.h"
|
||||||
#include "utils/builtins.h"
|
#include "utils/builtins.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
|
#include "utils/syscache.h"
|
||||||
#include "utils/timestamp.h"
|
#include "utils/timestamp.h"
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
@ -520,7 +522,7 @@ pg_ls_dir_1arg(PG_FUNCTION_ARGS)
|
|||||||
|
|
||||||
/* Generic function to return a directory listing of files */
|
/* Generic function to return a directory listing of files */
|
||||||
static Datum
|
static Datum
|
||||||
pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir)
|
pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir, bool missing_ok)
|
||||||
{
|
{
|
||||||
FuncCallContext *funcctx;
|
FuncCallContext *funcctx;
|
||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
@ -549,10 +551,18 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir)
|
|||||||
fctx->dirdesc = AllocateDir(fctx->location);
|
fctx->dirdesc = AllocateDir(fctx->location);
|
||||||
|
|
||||||
if (!fctx->dirdesc)
|
if (!fctx->dirdesc)
|
||||||
ereport(ERROR,
|
{
|
||||||
(errcode_for_file_access(),
|
if (missing_ok && errno == ENOENT)
|
||||||
errmsg("could not open directory \"%s\": %m",
|
{
|
||||||
fctx->location)));
|
MemoryContextSwitchTo(oldcontext);
|
||||||
|
SRF_RETURN_DONE(funcctx);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode_for_file_access(),
|
||||||
|
errmsg("could not open directory \"%s\": %m",
|
||||||
|
fctx->location)));
|
||||||
|
}
|
||||||
|
|
||||||
funcctx->user_fctx = fctx;
|
funcctx->user_fctx = fctx;
|
||||||
MemoryContextSwitchTo(oldcontext);
|
MemoryContextSwitchTo(oldcontext);
|
||||||
@ -601,12 +611,50 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir)
|
|||||||
Datum
|
Datum
|
||||||
pg_ls_logdir(PG_FUNCTION_ARGS)
|
pg_ls_logdir(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
return pg_ls_dir_files(fcinfo, Log_directory);
|
return pg_ls_dir_files(fcinfo, Log_directory, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function to return the list of files in the WAL directory */
|
/* Function to return the list of files in the WAL directory */
|
||||||
Datum
|
Datum
|
||||||
pg_ls_waldir(PG_FUNCTION_ARGS)
|
pg_ls_waldir(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
return pg_ls_dir_files(fcinfo, XLOGDIR);
|
return pg_ls_dir_files(fcinfo, XLOGDIR, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generic function to return the list of files in pgsql_tmp
|
||||||
|
*/
|
||||||
|
static Datum
|
||||||
|
pg_ls_tmpdir(FunctionCallInfo fcinfo, Oid tblspc)
|
||||||
|
{
|
||||||
|
char path[MAXPGPATH];
|
||||||
|
|
||||||
|
if (!SearchSysCacheExists1(TABLESPACEOID, ObjectIdGetDatum(tblspc)))
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
||||||
|
errmsg("tablespace with OID %u does not exist",
|
||||||
|
tblspc)));
|
||||||
|
|
||||||
|
TempTablespacePath(path, tblspc);
|
||||||
|
return pg_ls_dir_files(fcinfo, path, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Function to return the list of temporary files in the pg_default tablespace's
|
||||||
|
* pgsql_tmp directory
|
||||||
|
*/
|
||||||
|
Datum
|
||||||
|
pg_ls_tmpdir_noargs(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
return pg_ls_tmpdir(fcinfo, DEFAULTTABLESPACE_OID);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Function to return the list of temporary files in the specified tablespace's
|
||||||
|
* pgsql_tmp directory
|
||||||
|
*/
|
||||||
|
Datum
|
||||||
|
pg_ls_tmpdir_1arg(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
return pg_ls_tmpdir(fcinfo, PG_GETARG_OID(0));
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* yyyymmddN */
|
/* yyyymmddN */
|
||||||
#define CATALOG_VERSION_NO 201809301
|
#define CATALOG_VERSION_NO 201810051
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -10199,6 +10199,16 @@
|
|||||||
provolatile => 'v', prorettype => 'record', proargtypes => '',
|
provolatile => 'v', prorettype => 'record', proargtypes => '',
|
||||||
proallargtypes => '{text,int8,timestamptz}', proargmodes => '{o,o,o}',
|
proallargtypes => '{text,int8,timestamptz}', proargmodes => '{o,o,o}',
|
||||||
proargnames => '{name,size,modification}', prosrc => 'pg_ls_waldir' },
|
proargnames => '{name,size,modification}', prosrc => 'pg_ls_waldir' },
|
||||||
|
{ oid => '5029', descr => 'list files in the pgsql_tmp directory',
|
||||||
|
proname => 'pg_ls_tmpdir', procost => '10', prorows => '20', proretset => 't',
|
||||||
|
provolatile => 'v', prorettype => 'record', proargtypes => '',
|
||||||
|
proallargtypes => '{text,int8,timestamptz}', proargmodes => '{o,o,o}',
|
||||||
|
proargnames => '{name,size,modification}', prosrc => 'pg_ls_tmpdir_noargs' },
|
||||||
|
{ oid => '5030', descr => 'list files in the pgsql_tmp directory',
|
||||||
|
proname => 'pg_ls_tmpdir', procost => '10', prorows => '20', proretset => 't',
|
||||||
|
provolatile => 'v', prorettype => 'record', proargtypes => 'oid',
|
||||||
|
proallargtypes => '{oid,text,int8,timestamptz}', proargmodes => '{i,o,o,o}',
|
||||||
|
proargnames => '{tablespace,name,size,modification}', prosrc => 'pg_ls_tmpdir_1arg' },
|
||||||
|
|
||||||
# hash partitioning constraint function
|
# hash partitioning constraint function
|
||||||
{ oid => '5028', descr => 'hash partition CHECK constraint',
|
{ oid => '5028', descr => 'hash partition CHECK constraint',
|
||||||
|
Loading…
Reference in New Issue
Block a user