Skip to content

Commit

Permalink
[pgmoneta#5] SQL: CHECKPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
GuChad369 committed May 27, 2024
1 parent c5d39dd commit bca8074
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc/manual/user-03-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ CREATE EXTENSION pgmoneta_ext;
| Function | Description |
|-----------------------------|--------------------------------------------------------|
| `pgmoneta_ext_version()` | Return the version number of `pgmoneta_ext` as a Datum.|
| `pgmoneta_ext_switch_wal()` | A function for switching to a new WAL file. |
| `pgmoneta_ext_checkpoint()` | A function which forces a checkpoint. |
4 changes: 2 additions & 2 deletions sql/pgmoneta_ext--0.1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ CREATE FUNCTION pgmoneta_ext_version() RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

CREATE FUNCTION pgmoneta_ext_switch_wal() RETURNS text
CREATE FUNCTION pgmoneta_ext_checkpoint() RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
LANGUAGE C STRICT;
28 changes: 16 additions & 12 deletions src/pgmoneta_ext/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
/* PostgreSQL */
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "miscadmin.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "miscadmin.h"
#include "utils/builtins.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(pgmoneta_ext_version);
PG_FUNCTION_INFO_V1(pgmoneta_ext_switch_wal);
PG_FUNCTION_INFO_V1(pgmoneta_ext_checkpoint);

Datum
pgmoneta_ext_version(PG_FUNCTION_ARGS)
Expand All @@ -57,19 +57,23 @@ pgmoneta_ext_version(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(version);
}

#ifndef RequestCheckpoint
extern void RequestCheckpoint(int flags);
#endif

Datum
pgmoneta_ext_switch_wal(PG_FUNCTION_ARGS)
pgmoneta_ext_checkpoint(PG_FUNCTION_ARGS)
{
Datum wal_filename;
XLogRecPtr recptr;
char walfile[1024];
Datum check_point;
char cp[1024];

// Call the RequestXLogSwitch function with mark_unimportant as false
recptr = RequestXLogSwitch(false);
memset(&cp, 0, sizeof(cp));
snprintf(&cp[0], sizeof(cp), "%s", "CHECKPOINT");

snprintf(walfile, sizeof(walfile), "%X/%X", (uint32) (recptr >> 32), (uint32) recptr);
// Perform the checkpoint
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_WAIT | CHECKPOINT_FORCE);

wal_filename = CStringGetTextDatum(walfile);
check_point = CStringGetTextDatum(cp);

PG_RETURN_DATUM(wal_filename);
PG_RETURN_DATUM(check_point);
}

0 comments on commit bca8074

Please sign in to comment.