seed
required when connecting to PostgreSQL in multisession
plan?
#672
-
I'm getting warning about unexpected random numbers, when connecting to PostgreSQL (14) inside a future (and disconnecting immediately), when library(future)
plan(multisession) # plan(sequential)
f <- future::future({
con <- DBI::dbConnect(
drv = RPostgres::Postgres(),
dbname = "postgres",
user = "postgres",
password = "<password>"
)
DBI::dbDisconnect(con)
})
value(f) I skimmed through Session info:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
My best guess is that RPostgres, or one of its package dependencies, generates a random number when loaded or during initialization of the database, e.g. generate a random TCP port, a random temporary variable, or similar. It's not super common, but I've seen a few packages doing this. Sometimes this only happens once per R process, meaning if you call the same code multiple times, it only happens the first time, which could explain why you don't see it when using the 'sequential' backend; the RNG has been used before the future is evaluated. To help narrow in on code that generates random numbers (i.e. uses the RNG) when running interactively, see https://www.jottr.org/2020/09/21/detect-when-the-random-number-generator-was-used/. That has helped me, via trial and error, to identify where the RNG is used. |
Beta Was this translation helpful? Give feedback.
My best guess is that RPostgres, or one of its package dependencies, generates a random number when loaded or during initialization of the database, e.g. generate a random TCP port, a random temporary variable, or similar. It's not super common, but I've seen a few packages doing this. Sometimes this only happens once per R process, meaning if you call the same code multiple times, it only happens the first time, which could explain why you don't see it when using the 'sequential' backend; the RNG has been used before the future is evaluated.
To help narrow in on code that generates random numbers (i.e. uses the RNG) when running interactively, see https://www.jottr.org/2020/09/21/detect-…