digoal
2022-08-08
PostgreSQL , 读文件 , 文件不存在 , 错误
pg_read_[binary_]file (filename, missing_ok)
增加文件不存在的保护, 可避免错误.
+-- pg_read_file()
+select length(pg_read_file('postgresql.auto.conf')) > 30;
+ ?column?
+----------
+ t
+(1 row)
+
+select length(pg_read_file('postgresql.auto.conf', 1, 30));
+ length
+--------
+ 30
+(1 row)
+
+-- Test missing_ok
+select pg_read_file('does not exist'); -- error
+ERROR: could not open file "does not exist" for reading: No such file or directory
+select pg_read_file('does not exist', true) IS NULL; -- ok
+ ?column?
+----------
+ t
+(1 row)
Support pg_read_[binary_]file (filename, missing_ok).
author Tom Lane <tgl@sss.pgh.pa.us>
Fri, 29 Jul 2022 19:38:49 +0000 (15:38 -0400)
committer Tom Lane <tgl@sss.pgh.pa.us>
Fri, 29 Jul 2022 19:38:49 +0000 (15:38 -0400)
commit 283129e325b721a5a62227f20d7e3d149b379c73
tree a77377711f7fa536929419cead77d7dcc18f1485 tree
parent fd96d14d950f2b1d19b5cb3b8e5a7d4d2b3fa161 commit | diff
Support pg_read_[binary_]file (filename, missing_ok).
There wasn't an especially nice way to read all of a file while
passing missing_ok = true. Add an additional overloaded variant
to support that use-case.
While here, refactor the C code to avoid a rats-nest of PG_NARGS
checks, instead handling the argument collection in the outer
wrapper functions. It's a bit longer this way, but far more
straightforward.
(Upon looking at the code coverage report for genfile.c, I was
impelled to also add a test case for pg_stat_file() -- tgl)
Kyotaro Horiguchi
Discussion: https://postgr.es/m/20220607.160520.1984541900138970018.horikyota.ntt@gmail.com