R/pg_size.R

Defines functions pg_tbl_size pg_db_size

Documented in pg_db_size pg_tbl_size

#' Gets database size
#'
#' @param conn Connection
#' @param dbname Database name. If not provided, it returns the current
#'      database size.
#'
#' @return Named string with database size
#' @export
#'
#' @examples
#' \dontrun{
#' pg_db_size(conn,"postgres")
#' }
pg_db_size <- function(conn,dbname = NULL){

 if (is.null(dbname)){

   dbname <- DBI::dbGetInfo(conn)$dbname

 }

 q <- glue::glue_sql("SELECT pg_size_pretty( pg_database_size({dbname}))",.con = conn)

      DBI::dbGetQuery(conn,q) %>%
       .[[1]] %>%
        stats::setNames(dbname)


}


#' Gets table's size
#'
#' @param conn Connection
#' @param tbl String with table's name
#'
#' @return Named string with table's size
#' @export
#'
#' @examples
#' \dontrun{
#' pg_tbl_size(conn,"data")
#' }
pg_tbl_size <- function(conn, tbl = NULL){


  q <- glue::glue_sql("SELECT pg_size_pretty(pg_total_relation_size({tbl}))",.con = conn)

  DBI::dbGetQuery(conn,q) %>%
    .[[1]] %>%
    stats::setNames(tbl)

}
jjesusfilho/rpsql documentation built on Aug. 9, 2021, 7:33 p.m.