Re: [Proposal] Adding Log File Capability to pg_createsubscriber
Euler Taveira <euler@eulerto.com>
From: "Euler Taveira" <euler@eulerto.com>
To: "Gyan Sreejith" <gyan.sreejith@gmail.com>,
"vignesh C" <vignesh21@gmail.com>
Cc: "Amit Kapila" <amit.kapila16@gmail.com>,
"kuroda.hayato@fujitsu.com" <kuroda.hayato@fujitsu.com>,
"pgsql-hackers@lists.postgresql.org" <pgsql-hackers@lists.postgresql.org>,
"Peter Smith" <smithpb2250@gmail.com>
Date: 2026-03-05T14:48:57Z
Lists: pgsql-hackers
On Tue, Feb 24, 2026, at 8:55 PM, Gyan Sreejith wrote:
> I have made the changes you suggested and have attached the patch below.
>
[Avoid top-posting ...]
I took another look at this patch. Comments are below:
+/*
+ * Open a new logfile with proper permissions.
+ * From src/backend/postmaster/syslogger.c
+ */
+static FILE *
+logfile_open(const char *filename, const char *mode)
+{
Don't duplicate code. If you are reusing a function, my advice is to move it to
src/common. You can always use "ifdef FRONTEND" to use the appropriate log
message (elog/ereport vs pg_error, for example).
- * XXX this code was extracted from BootStrapXLOG().
+ * XXX this code was extracted from BootStrapXpg_log_info().
This is a typo.
+ case 'l':
+ {
+ char timestamp[128];
+ struct timeval tval;
+ time_t now;
+ struct tm tmbuf;
I would expect to have this code in a function. That's the pattern it is using.
+ strftime(timestamp, sizeof(timestamp), "%Y-%m-%d-%H-%M-%S", localtime_r(&now, &tmbuf));
+ /* append microseconds */
+ snprintf(timestamp + strlen(timestamp), sizeof(timestamp) - strlen(timestamp),
+ ".%06u", (unsigned int) (tval.tv_usec));
+ log_timestamp = pg_strdup(timestamp);
Do we really need microseconds? I would say milliseconds is sufficient. I
suggest to remove the dash (-); it is using a different style from existing
code (pg_upgrade).
+ opt.log_dir = pg_strdup(optarg);
+ canonicalize_path(opt.log_dir);
You didn't have a check for long path. It is not a good idea in general. See
MAXPGPATH examples. It would be a good idea if the path are restricted to
MAXPGPATH length.
+ if (errno == ENOENT)
+ {
+ if (mkdir(opt.log_dir, S_IRWXU) == 0)
+ pg_log_info("log directory created");
+ else if (errno == EACCES)
+ pg_fatal("permission denied trying to create log directory \"%s\": %m", opt.log_dir);
+ else
+ pg_fatal("could not create log directory \"%s\": %m", opt.log_dir);
+ }
+ else if (errno == EACCES)
+ pg_fatal("permission denied trying to access directory \"%s\": %m", opt.log_dir);
+ else
+ pg_fatal("could not access directory \"%s\": %m", opt.log_dir);
The "permission denied" is redundant here because it will be in %m. Instead, I
suggest that you use
could not create directory \"%s\": %m
The main advantage is that this sentence is already available. It avoids
translation effort.
+#undef pg_log_info
+#define pg_log_info(...) do{\
+ if (internal_log_file_fp != NULL) \
+ internal_log_file_write(__VA_ARGS__); \
+ else \
+ pg_log_generic(PG_LOG_INFO,PG_LOG_PRIMARY,__VA_ARGS__);\
+} while(0)
I don't like the fact that internal_log_file_fp is not declared before this
#define.
One of the arguments to have this feature was that pg_createsubscriber mixes the
server and tool messages. Couldn't we fix it adding "marks" on the output saying
the server log messages starts here and the server log messages ends here?
--
Euler Taveira
EDB https://www.enterprisedb.com/
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
pg_createsubscriber: Don't use MAXPGPATH
- 99b726ac4894 19 (unreleased) landed
-
pg_createsubscriber: Remove separate logfile_open() function
- f5528b90b411 19 (unreleased) landed
-
pg_createsubscriber: Use logging.c log file callback
- 847336ba53af 19 (unreleased) landed
-
Add log file support to logging.c
- 41237556f8c5 19 (unreleased) landed
-
pg_createsubscriber: Add -l/--logdir option to redirect output to files.
- 6b5b7eae3ae6 19 (unreleased) landed
-
pg_createsubscriber: Introduce module-specific logging functions.
- d6628a5ea0a5 19 (unreleased) landed
-
Fix another buglet in archive_waldump.c.
- 69c57466a752 19 (unreleased) cited