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 Jun 3, 2024
1 parent 5628ab1 commit 6694a33
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
24 changes: 23 additions & 1 deletion cmake/FindPsqlDevel.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@
# postgresql-server-devel Support
#

find_program(PG_CONFIG pg_config)

if (NOT PG_CONFIG)
message(FATAL_ERROR "pg_config executable not found. Ensure PostgreSQL is installed and pg_config is in your PATH.")
endif()

# Use pg_config to get the include and library directories
execute_process(COMMAND ${PG_CONFIG} --includedir
OUTPUT_VARIABLE PSQLDEVEL_INCLUDE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process(COMMAND ${PG_CONFIG} --libdir
OUTPUT_VARIABLE PSQLDEVEL_LIBRARY_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)

find_path(PSQLDEVEL_INCLUDE_DIR
NAMES libpq-fe.h
PATH_SUFFIXES postgresql
PATHS ${PSQLDEVEL_INCLUDE_DIR}
)

find_library(PSQLDEVEL_LIBRARY
NAMES pq
PATHS ${PSQLDEVEL_LIBRARY_DIR}
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PsqlDevel DEFAULT_MSG
PSQLDEVEL_INCLUDE_DIR PSQLDEVEL_LIBRARY)

if (PSQLDEVEL_INCLUDE_DIR AND PSQLDEVEL_LIBRARY)
set(PSQLDEVEL_FOUND TRUE)
else()
set(PSQLDEVEL_FOUND FALSE)
endif()

if (PSQLDEVEL_FOUND)
set(PSQLDEVEL_INCLUDE_DIRS ${PSQLDEVEL_INCLUDE_DIR})
set(PSQLDEVEL_LIBRARIES ${PSQLDEVEL_LIBRARY})
Expand Down
1 change: 1 addition & 0 deletions doc/manual/user-03-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ After you create the extension `pgmoneta_ext` using the `postgres` role, you can
|-----------------------------|-----------|--------------------------------------------------------|
| `pgmoneta_ext_version()` | false | Return the version number of `pgmoneta_ext` as a Datum.|
| `pgmoneta_ext_switch_wal()` | true | A function for switching to a new WAL file. |
| `pgmoneta_ext_checkpoint()` | true | A function which forces a checkpoint. |
7 changes: 7 additions & 0 deletions sql/pgmoneta_ext--0.1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ CREATE FUNCTION pgmoneta_ext_switch_wal(OUT success bool,
)
RETURNS record
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

CREATE FUNCTION pgmoneta_ext_checkpoint(OUT success bool,
OUT value text
)
RETURNS record
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
53 changes: 53 additions & 0 deletions src/pgmoneta_ext/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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 Down Expand Up @@ -114,5 +115,57 @@ pgmoneta_ext_switch_wal(PG_FUNCTION_ARGS)
tuple = heap_form_tuple(tupdesc, values, nulls);
result = HeapTupleGetDatum(tuple);

PG_RETURN_DATUM(result);
}

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

Datum
pgmoneta_ext_checkpoint(PG_FUNCTION_ARGS)
{
Datum values[2];
Datum result;
HeapTuple tuple;
Oid roleid;
TupleDesc tupdesc;
bool nulls[2];
int is_superuser;
char cp[1024];

memset(nulls, 0, sizeof(nulls));
memset(&cp, 0, sizeof(cp));

roleid = GetUserId();
is_superuser = pgmoneta_ext_check_privilege(roleid);

if (!is_superuser)
{
// Perform the checkpoint
RequestCheckpoint(CHECKPOINT_IMMEDIATE | CHECKPOINT_WAIT | CHECKPOINT_FORCE);

values[0] = BoolGetDatum(true);
snprintf(&cp[0], sizeof(cp), "%s", "CHECKPOINT");
values[1] = CStringGetTextDatum(cp);
}
else
{
ereport(LOG, errmsg_internal("pgmoneta_ext_checkpoint: Current role is not a superuser"));

values[0] = BoolGetDatum(false);
nulls[1] = true;
}

// Create a tuple descriptor for our result type
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
{
ereport(ERROR, errmsg_internal("pgmoneta_ext_checkpoint: Return type must be a row type"));
}

// Build the result tuple
tuple = heap_form_tuple(tupdesc, values, nulls);
result = HeapTupleGetDatum(tuple);

PG_RETURN_DATUM(result);
}

0 comments on commit 6694a33

Please sign in to comment.