Skip to content

Instantly share code, notes, and snippets.

@arthurgailes
Created June 7, 2023 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurgailes/59856dc9a4ec6700cfa48f78cd41c214 to your computer and use it in GitHub Desktop.
Save arthurgailes/59856dc9a4ec6700cfa48f78cd41c214 to your computer and use it in GitHub Desktop.
R PostgreSQL Helpers
# Get all indexes, then drop and add before and after writing to table
# Speeds up writes
get_index_table <- function(con, tablename, schemaname) {
index_df <- dbGetQuery(con, paste0(
"SELECT indexname, indexdef FROM pg_indexes
WHERE tablename = '", tablename, "' AND schemaname = '", schemaname, "'"))
index_df <- subset(index_df, !grepl("[pf]key", indexname))
index_df$indexname <- paste0(schemaname, ".", index_df$indexname)
return(index_df)
}
drop_indexes <- function(con, indexes) {
for(index in indexes) dbExecute(con, paste("DROP INDEX IF EXISTS ", index))
}
restore_indexes <- function(con, index_def_list) {
for(def in index_def_list) dbExecute(con, def)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment