Created
October 30, 2013 16:03
-
-
Save vinnix/7235246 to your computer and use it in GitHub Desktop.
Exemplo de biblioteca em linguagem C, para utilizar dentro do PostgreSQL / plpgsql.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "postgres.h" | |
#include <sys/file.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
#include "catalog/pg_type.h" | |
#include "funcapi.h" | |
#include "miscadmin.h" | |
#include "postmaster/syslogger.h" | |
#include "storage/fd.h" | |
#include "utils/builtins.h" | |
#include "utils/datetime.h" | |
#include "fmgr.h" | |
#include "miscadmin.h" | |
#ifdef PG_MODULE_MAGIC | |
PG_MODULE_MAGIC; | |
#endif | |
typedef struct | |
{ | |
char *location; | |
DIR *dirdesc; | |
} directory_fctx; | |
/*----------------------- | |
* * some helper functions | |
* */ | |
/* Auxiliary Functions should go first */ | |
/* | |
* * check for superuser, bark if not. | |
* */ | |
static void | |
requireSuperuser(void) | |
{ | |
if (!superuser()) ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errmsg("only superuser may access this")))); | |
} | |
Datum bytea_size(PG_FUNCTION_ARGS); | |
Datum hello_world(PG_FUNCTION_ARGS); | |
Datum vnx_pg_logdir_ls(PG_FUNCTION_ARGS); | |
PG_FUNCTION_INFO_V1(bytea_size); | |
PG_FUNCTION_INFO_V1(hello_world); | |
PG_FUNCTION_INFO_V1(vnx_pg_logdir_ls); | |
Datum bytea_size(PG_FUNCTION_ARGS) | |
{ | |
bytea *data = PG_GETARG_BYTEA_P(0); | |
unsigned char *ptr = (unsigned char *) VARDATA(data); | |
int32 tcount = 0, i; | |
// count characters | |
for (i = VARSIZE(data) - VARHDRSZ; i; i--) { | |
if (!i%1000) CHECK_FOR_INTERRUPTS(); | |
unsigned char c = *ptr++; // get the char | |
tcount++; | |
} | |
PG_RETURN_INT32(tcount); | |
} | |
Datum hello_word(PG_FUNCTION_ARGS) | |
{ | |
} | |
Datum | |
vnx_pg_logdir_ls(PG_FUNCTION_ARGS) | |
{ | |
FuncCallContext *funcctx; | |
struct dirent *de; | |
directory_fctx *fctx; | |
if (!superuser()) | |
ereport(ERROR, | |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), | |
(errmsg("only superuser can list the log directory")))); | |
if (strcmp(Log_filename, "postgresql-%Y-%m-%d_%H%M%S.log") != 0) | |
ereport(ERROR, | |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), | |
(errmsg("the log_filename parameter must equal 'postgresql-%%Y-%%m-%%d_%%H%%M%%S.log'")))); | |
if (SRF_IS_FIRSTCALL()) | |
{ | |
MemoryContext oldcontext; | |
TupleDesc tupdesc; | |
funcctx = SRF_FIRSTCALL_INIT(); | |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); | |
fctx = palloc(sizeof(directory_fctx)); | |
tupdesc = CreateTemplateTupleDesc(2, false); | |
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "starttime", | |
TIMESTAMPOID, -1, 0); | |
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "filename", | |
TEXTOID, -1, 0); | |
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc); | |
fctx->location = pstrdup(Log_directory); | |
fctx->dirdesc = AllocateDir(fctx->location); | |
if (!fctx->dirdesc) | |
ereport(ERROR, | |
(errcode_for_file_access(), | |
errmsg("could not read directory \"%s\": %m", | |
fctx->location))); | |
funcctx->user_fctx = fctx; | |
MemoryContextSwitchTo(oldcontext); | |
} | |
funcctx = SRF_PERCALL_SETUP(); | |
fctx = (directory_fctx *) funcctx->user_fctx; | |
while ((de = ReadDir(fctx->dirdesc, fctx->location)) != NULL) | |
{ | |
char *values[2]; | |
HeapTuple tuple; | |
char timestampbuf[32]; | |
char *field[MAXDATEFIELDS]; | |
char lowstr[MAXDATELEN + 1]; | |
int dtype; | |
int nf, | |
ftype[MAXDATEFIELDS]; | |
fsec_t fsec; | |
int tz = 0; | |
struct pg_tm date; | |
/* | |
** Default format: postgresql-YYYY-MM-DD_HHMMSS.log | |
**/ | |
if (strlen(de->d_name) != 32 | |
|| strncmp(de->d_name, "postgresql-", 11) != 0 | |
|| de->d_name[21] != '_' | |
|| strcmp(de->d_name + 28, ".log") != 0) | |
continue; | |
/* extract timestamp portion of filename */ | |
strcpy(timestampbuf, de->d_name + 11); | |
timestampbuf[17] = '\0'; | |
/* parse and decode expected timestamp to verify it's OK format */ | |
if (ParseDateTime(timestampbuf, lowstr, MAXDATELEN, field, ftype, MAXDATEFIELDS, &nf)) | |
continue; | |
if (DecodeDateTime(field, ftype, nf, &dtype, &date, &fsec, &tz)) | |
continue; | |
/* Seems the timestamp is OK; prepare and return tuple */ | |
values[0] = timestampbuf; | |
values[1] = palloc(strlen(fctx->location) + strlen(de->d_name) + 2); | |
sprintf(values[1], "%s/%s", fctx->location, de->d_name); | |
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values); | |
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple)); | |
} | |
FreeDir(fctx->dirdesc); | |
SRF_RETURN_DONE(funcctx); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment