Created
June 7, 2023 13:02
-
-
Save arthurgailes/59856dc9a4ec6700cfa48f78cd41c214 to your computer and use it in GitHub Desktop.
R PostgreSQL Helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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