Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#22] Get LSN #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/manual/user-02-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ ALTER USER repl WITH NOSUPERUSER;
| `pgmoneta_ext_get_oids()` | Default | None | Return all OIDs on the current server.|
| `pgmoneta_ext_get_file()`| SUPERUSER | path/to/file | Return the bytes of the specified file that is passed in.|
| `pgmoneta_ext_get_files()` | SUPERUSER | path/to/dir | Return all file paths in the specified directory passed in.|
| `pgmoneta_ext_get_lsn()` | Default | None | Return the location of the last write of WAL.|
6 changes: 5 additions & 1 deletion sql/pgmoneta_ext--0.1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ CREATE FUNCTION pgmoneta_ext_get_file(file_path TEXT) RETURNS TEXT
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION pgmoneta_ext_get_files(file_path TEXT) RETURNS text[]
CREATE FUNCTION pgmoneta_ext_get_files(file_path TEXT) RETURNS text[]
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;

CREATE FUNCTION pgmoneta_ext_get_lsn() RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
14 changes: 14 additions & 0 deletions src/pgmoneta_ext/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ PG_FUNCTION_INFO_V1(pgmoneta_ext_get_oid);
PG_FUNCTION_INFO_V1(pgmoneta_ext_get_oids);
PG_FUNCTION_INFO_V1(pgmoneta_ext_get_file);
PG_FUNCTION_INFO_V1(pgmoneta_ext_get_files);
PG_FUNCTION_INFO_V1(pgmoneta_ext_get_lsn);

Datum
pgmoneta_ext_version(PG_FUNCTION_ARGS)
Expand Down Expand Up @@ -449,3 +450,16 @@ list_files(const char* name, ArrayBuildState* astate)
}
closedir(dir);
}

Datum
pgmoneta_ext_get_lsn(PG_FUNCTION_ARGS)
{
XLogRecPtr lsn;
char lsn_str[64];

lsn = GetXLogWriteRecPtr();

snprintf(lsn_str, sizeof(lsn_str), "%X/%X", (uint32)(lsn >> 32), (uint32)lsn);

PG_RETURN_TEXT_P(cstring_to_text(lsn_str));
}
11 changes: 11 additions & 0 deletions test/lib/pgmoneta_ext_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#define PGMONETA_EXT_GET_FILES_REGEX "^\\{((\\/[^,]+,?\\s*)*)\\}$"
#define PGMONETA_EXT_GET_OID_REGEX "^[1-9][0-9]*$"
#define PGMONETA_EXT_GET_OIDS_REGEX "^\\([1-9][0-9]*,[a-zA-Z0-9]+\\)$"
#define PGMONETA_EXT_GET_LSN_REGEX "^[A-Za-z0-9]+/[A-Za-z0-9]+$"

#define PGMONETA_EXT_GET_FILE_PATH "/pgsql/logfile"
#define PGMONETA_EXT_GET_FILES_PATH "/conf"
Expand Down Expand Up @@ -120,6 +121,15 @@ START_TEST(test_pgmoneta_ext_get_oids)
}
END_TEST

START_TEST(test_pgmoneta_ext_get_lsn)
{
char output[BUFFER_SIZE];
int result = execute_command("psql -h localhost -p 5432 -U repl -d postgres -t -c 'SELECT pgmoneta_ext_get_lsn();'", output, BUFFER_SIZE);
ck_assert_int_eq(result, 0);
ck_assert_msg(regex_match(output, PGMONETA_EXT_GET_LSN_REGEX) == 0, "Expected WAL switch result not found in output: %s", output);
}
END_TEST

Suite*
pgmoneta_ext_suite(void)
{
Expand All @@ -137,6 +147,7 @@ pgmoneta_ext_suite(void)
tcase_add_test(tc_core, test_pgmoneta_ext_get_files);
tcase_add_test(tc_core, test_pgmoneta_ext_get_oid);
tcase_add_test(tc_core, test_pgmoneta_ext_get_oids);
tcase_add_test(tc_core, test_pgmoneta_ext_get_lsn);
suite_add_tcase(s, tc_core);

return s;
Expand Down