-
Notifications
You must be signed in to change notification settings - Fork 3
/
pg_dropbuffers.c
88 lines (73 loc) · 1.84 KB
/
pg_dropbuffers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*-------------------------------------------------------------------------
*
* pg_dropbuffers.c
*
* This program is open source, licensed under the PostgreSQL license.
* For license terms, see the LICENSE file.
*
*-------------------------------------------------------------------------
*/
#include <sys/wait.h>
#include <unistd.h>
#include "postgres.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
PG_MODULE_MAGIC;
/*--- Functions --- */
void _PG_init(void);
void _PG_fini(void);
PG_FUNCTION_INFO_V1(pg_drop_current_db_buffers);
Datum pg_drop_current_db_buffers(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pg_drop_system_cache);
Datum pg_drop_system_cache(PG_FUNCTION_ARGS);
void
_PG_init(void)
{
/* nothing */
}
void
_PG_fini(void)
{
/* nothing */
}
Datum
pg_drop_current_db_buffers(PG_FUNCTION_ARGS)
{
FlushDatabaseBuffers(MyDatabaseId);
/* buffers can still be dirty here */
DropDatabaseBuffers(MyDatabaseId);
elog(LOG, "dropped shared buffers");
PG_RETURN_VOID();
}
Datum
pg_drop_system_cache(PG_FUNCTION_ARGS)
{
int exit_code;
int pid = fork();
if(pid == 0)
{
sync();
execl("/usr/bin/sudo", "/usr/bin/sudo", "/sbin/sysctl", "vm.drop_caches=3", NULL);
/* never executed; just to silence the compiler */
}
else
{
waitpid(pid, &exit_code, 0);
if(exit_code == 0)
{
elog(LOG, "dropped system cache");
}
else
{
ereport(
ERROR,
(
errcode(ERRCODE_SYSTEM_ERROR),
errmsg("dropping system cache failed with return code %d", exit_code),
errhint("Make sure the user running postgresql is allowed to run the command `/usr/bin/sudo /sbin/sysctl vm.drop_caches=3`")
)
);
}
}
PG_RETURN_VOID();
}