From 47914c620fc3dca51f909e5367f65d3a20d9a497 Mon Sep 17 00:00:00 2001 From: Daniel Possenriede Date: Wed, 31 Mar 2021 14:04:51 +0200 Subject: [PATCH] fix dbListTables() for Postgres < v10 --- R/tables.R | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/R/tables.R b/R/tables.R index f5937259..31ce185e 100644 --- a/R/tables.R +++ b/R/tables.R @@ -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]] })