Skip to content

Commit

Permalink
fix dbListTables() for Postgres < v10
Browse files Browse the repository at this point in the history
  • Loading branch information
dpprdan committed Mar 31, 2021
1 parent 1da38e3 commit 47914c6
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,36 @@ setMethod("dbReadTable", c("PqConnection", "character"),
#' @export
#' @rdname postgres-tables
setMethod("dbListTables", "PqConnection", function(conn, ...) {
major_server_version <- dbGetInfo(conn)$db.version %/% 10000

query <- paste0(
# pg_class docs: https://www.postgresql.org/docs/current/catalog-pg-class.html
"SELECT cl.relname AS name FROM pg_class AS cl ",
"JOIN pg_namespace AS n ON cl.relnamespace = n.oid ",
"WHERE (n.nspname = ANY (current_schemas(true))) ",
"AND (n.nspname <> 'pg_catalog') ",
"AND (cl.relkind IN ('r', 'p', 'f', 'v', 'm')) ",
"AND NOT cl.relispartition ",
"AND (n.nspname <> 'pg_catalog') "
)

if (major_server_version >= 10) {
# relkind = 'p' and relispartition only supported from v10 onwards
query <- paste0(
query,
# r = ordinary table, v = view, m = materialized view, f = foreign table, p = partitioned table
"AND (cl.relkind IN ('r', 'v', 'm', 'f', 'p')) ",
"AND NOT cl.relispartition "
)
} else {
query <- paste0(
query,
"AND (cl.relkind IN ('r', 'v', 'm', 'f')) "
)
}

query <- paste0(
query,
"ORDER BY name"
)

dbGetQuery(conn, query)[[1]]
})

Expand Down

0 comments on commit 47914c6

Please sign in to comment.