@@ -362,32 +362,48 @@ pg_rotate_logfile(PG_FUNCTION_ARGS)
362
362
363
363
typedef struct
364
364
{
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
+ }
368
381
369
382
Datum
370
383
pg_objs_per_tablespace (PG_FUNCTION_ARGS )
371
384
{
372
385
FuncCallContext * funcctx ;
373
- ts_db_fctx * fctx ;
374
386
char * values [2 ];
387
+ DIR * subdirdesc ;
388
+ DIR * dirdesc ;
389
+ char * location ;
375
390
HeapTuple tuple ;
376
391
struct dirent * direntry ;
392
+ struct dirent * subdirentry ;
393
+ tbsp_record * records = NULL ;
394
+ int maxrecords = 0 ;
395
+ tbsp_fctx * fctx ;
396
+ int t ;
377
397
378
398
if (SRF_IS_FIRSTCALL ())
379
399
{
380
400
381
401
MemoryContext oldcontext ;
382
402
TupleDesc tupdesc ;
383
403
384
- elog (LOG , "%s\n" , "Inicio SRF_IS_FIRSTCALL" );
385
-
386
404
funcctx = SRF_FIRSTCALL_INIT ();
387
405
oldcontext = MemoryContextSwitchTo (funcctx -> multi_call_memory_ctx );
388
406
389
- fctx = palloc (sizeof (ts_db_fctx ));
390
-
391
407
tupdesc = CreateTemplateTupleDesc (2 , false);
392
408
TupleDescInitEntry (tupdesc , (AttrNumber ) 1 , "database" ,
393
409
TEXTOID , -1 , 0 );
@@ -396,36 +412,77 @@ elog(LOG, "%s\n", "Inicio SRF_IS_FIRSTCALL");
396
412
397
413
funcctx -> attinmeta = TupleDescGetAttInMetadata (tupdesc );
398
414
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 ;
401
460
402
461
funcctx -> user_fctx = fctx ;
403
462
MemoryContextSwitchTo (oldcontext );
404
- elog (LOG , "FIM SRF_IS_FIRSTCALL - %s\n" , "pg_objs_per_tablespace" );
405
463
}
406
464
407
465
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 ;
414
467
415
- while (( direntry = ReadDir ( fctx -> dirdesc , fctx -> location )) != NULL )
468
+ if ( funcctx -> call_cntr < fctx -> reccount )
416
469
{
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 ;
419
472
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 ));
422
475
}
423
-
424
- FreeDir (fctx -> dirdesc );
425
476
SRF_RETURN_DONE (funcctx );
426
477
}
427
478
428
479
/* 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
+
429
486
Datum
430
487
pg_tablespace_databases (PG_FUNCTION_ARGS )
431
488
{
0 commit comments