diff options
author | Andres Freund <andres@anarazel.de> | 2025-02-19 19:35:09 -0500 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2025-02-19 19:35:09 -0500 |
commit | d38bab5edd60dbe6309512b6c8daea37ce579b70 (patch) | |
tree | 62b5dd8ef05658e2952cc21f8253f667c02956aa | |
parent | 9b1cb58c5f2127ddc682778b486739059ea2479d (diff) | |
download | postgresql-d38bab5edd60dbe6309512b6c8daea37ce579b70.tar.gz postgresql-d38bab5edd60dbe6309512b6c8daea37ce579b70.zip |
pgbench: Increase RLIMIT_NOFILE if necessary
pgbench already had code to check if the soft rlimit is too low for the
specified number of connections. If too low, it errored out, telling the user
to increase the limit.
However, we can do better: If the hard limit allows, increase the soft limit
to be sufficiently for the number of connections.
It is common for the soft limit to be considerably lower than the hard limit,
due to the danger of soft limits > 1024 breaking programs that use the
select(2), as explained in [1].
[1]: https://0pointer.net/blog/file-descriptor-limits.html
Author: Jelte Fennema-Nio <postgres@jeltef.nl>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAGECzQQh6VSy3KG4pN1d%3Dh9J%3DD1rStFCMR%2Bt7yh_Kwj-g87aLQ%40mail.gmail.com
-rw-r--r-- | src/bin/pgbench/pgbench.c | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 5e1fcf59c61..fdc957fa34d 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -6815,13 +6815,26 @@ main(int argc, char **argv) #ifdef HAVE_GETRLIMIT if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) pg_fatal("getrlimit failed: %m"); - if (rlim.rlim_cur < nclients + 3) + + if (rlim.rlim_max < nclients + 3) { pg_log_error("need at least %d open files, but system limit is %ld", - nclients + 3, (long) rlim.rlim_cur); + nclients + 3, (long) rlim.rlim_max); pg_log_error_hint("Reduce number of clients, or use limit/ulimit to increase the system limit."); exit(1); } + + if (rlim.rlim_cur < nclients + 3) + { + rlim.rlim_cur = nclients + 3; + if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) + { + pg_log_error("need at least %d open files, but couldn't raise the limit: %m", + nclients + 3); + pg_log_error_hint("Reduce number of clients, or use limit/ulimit to increase the system limit."); + exit(1); + } + } #endif /* HAVE_GETRLIMIT */ break; case 'C': |