Skip to content

Commit 8dbecd5

Browse files
committed
WIP 26122017
1 parent 54378ad commit 8dbecd5

File tree

1 file changed

+81
-24
lines changed

1 file changed

+81
-24
lines changed

src/backend/utils/adt/misc.c

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -362,32 +362,48 @@ pg_rotate_logfile(PG_FUNCTION_ARGS)
362362

363363
typedef struct
364364
{
365-
char *location;
366-
DIR *dirdesc;
367-
} ts_db_fctx;
365+
char *databaseOid;
366+
char *relationOid;
367+
} tbsp_record;
368+
369+
typedef struct
370+
{
371+
tbsp_record *records;
372+
int reccount;
373+
} tbsp_fctx;
374+
375+
int MyFNatoi(const char *numArray, int *value)
376+
{
377+
int n = 0;
378+
return sscanf(numArray, "%d%n", value, &n) > 0 /* integer was converted */
379+
&& numArray[n] == '\0'; /* all input got consumed */
380+
}
368381

369382
Datum
370383
pg_objs_per_tablespace(PG_FUNCTION_ARGS)
371384
{
372385
FuncCallContext *funcctx;
373-
ts_db_fctx *fctx;
374386
char *values[2];
387+
DIR *subdirdesc;
388+
DIR *dirdesc;
389+
char *location;
375390
HeapTuple tuple;
376391
struct dirent *direntry;
392+
struct dirent *subdirentry;
393+
tbsp_record *records = NULL;
394+
int maxrecords = 0;
395+
tbsp_fctx *fctx;
396+
int t;
377397

378398
if (SRF_IS_FIRSTCALL())
379399
{
380400

381401
MemoryContext oldcontext;
382402
TupleDesc tupdesc;
383403

384-
elog(LOG, "%s\n", "Inicio SRF_IS_FIRSTCALL");
385-
386404
funcctx = SRF_FIRSTCALL_INIT();
387405
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
388406

389-
fctx = palloc(sizeof(ts_db_fctx));
390-
391407
tupdesc = CreateTemplateTupleDesc(2, false);
392408
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "database",
393409
TEXTOID, -1, 0);
@@ -396,36 +412,77 @@ elog(LOG, "%s\n", "Inicio SRF_IS_FIRSTCALL");
396412

397413
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
398414

399-
fctx->location = psprintf("base");
400-
fctx->dirdesc = AllocateDir(fctx->location);
415+
location = psprintf("base");
416+
dirdesc = AllocateDir(location);
417+
while ((direntry = ReadDir(dirdesc, location)) != NULL)
418+
{
419+
char *subdir;
420+
421+
if (direntry->d_name[0] == '.')
422+
continue;
423+
424+
if (direntry->d_type == DT_DIR)
425+
{
426+
subdir = psprintf("base/%s", direntry->d_name);
427+
subdirdesc = AllocateDir(subdir);
428+
while ((subdirentry = ReadDir(subdirdesc, subdir)) != NULL)
429+
{
430+
if (subdirentry->d_name[0] == '.')
431+
continue;
432+
433+
if (!MyFNatoi(subdirentry->d_name, &t))
434+
continue;
435+
436+
if (maxrecords == 0)
437+
{
438+
records = (tbsp_record*) malloc(sizeof(tbsp_record));
439+
records[0].databaseOid = get_database_name(atoi(direntry->d_name));
440+
records[0].relationOid = strdup(subdirentry->d_name);
441+
maxrecords++;
442+
}
443+
else
444+
{
445+
records = (tbsp_record*) realloc(records, (maxrecords+1) * sizeof(tbsp_record));
446+
records[maxrecords].databaseOid = get_database_name(atoi(direntry->d_name));
447+
records[maxrecords].relationOid = strdup(subdirentry->d_name);
448+
maxrecords++;
449+
}
450+
}
451+
FreeDir(subdirdesc);
452+
}
453+
}
454+
FreeDir(dirdesc);
455+
456+
fctx = palloc(sizeof(tbsp_fctx));
457+
458+
fctx->records = records;
459+
fctx->reccount = maxrecords;
401460

402461
funcctx->user_fctx = fctx;
403462
MemoryContextSwitchTo(oldcontext);
404-
elog(LOG, "FIM SRF_IS_FIRSTCALL - %s\n", "pg_objs_per_tablespace");
405463
}
406464

407465
funcctx = SRF_PERCALL_SETUP();
408-
fctx = (ts_db_fctx *) funcctx->user_fctx;
409-
410-
if (!fctx->dirdesc) /* not a tablespace */
411-
SRF_RETURN_DONE(funcctx);
412-
413-
elog(LOG,"Uma Execucao : %s\n", "PG_OBJ_PER_TABLESPACE");
466+
fctx = (tbsp_fctx *) funcctx->user_fctx;
414467

415-
while ((direntry = ReadDir(fctx->dirdesc, fctx->location)) != NULL)
468+
if (funcctx->call_cntr < fctx->reccount)
416469
{
417-
values[0] = "base" ; //databaseOid;
418-
values[1] = direntry->d_name; //subdirentry->d_name;
470+
values[0] = fctx->records[funcctx->call_cntr].databaseOid;
471+
values[1] = fctx->records[funcctx->call_cntr].relationOid;
419472

420-
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
421-
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
473+
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
474+
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
422475
}
423-
424-
FreeDir(fctx->dirdesc);
425476
SRF_RETURN_DONE(funcctx);
426477
}
427478

428479
/* Function to find out which databases make use of a tablespace */
480+
typedef struct
481+
{
482+
char *location;
483+
DIR *dirdesc;
484+
} ts_db_fctx;
485+
429486
Datum
430487
pg_tablespace_databases(PG_FUNCTION_ARGS)
431488
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy