postgres.git / summary / log / commit / refs

commit    52d2f887d21a3649ea573b31f3a1b9dd5b68ea85
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Thu May 15 01:14:09 2014 +0000
First released in: 9.4.0, 8.4.22, 9.0.18, 9.1.14, 9.2.9, 9.3.5

    In initdb, ensure stdout/stderr buffering behavior is what we expect.
    
    Since this program may print to either stdout or stderr, the relative
    ordering of its messages depends on the buffering behavior of those files.
    Force stdout to be line-buffered and stderr to be unbuffered, ensuring
    that the behavior will match standard Unix interactive behavior, even
    when stdout and stderr are rerouted to a file.
    
    Per complaint from Tomas Vondra.  The particular case he pointed out is
    new in HEAD, but issues of the same sort could arise in any branch with
    other error messages, so back-patch to all branches.
    
    I'm unsure whether we might not want to do this in other client programs
    as well.  For the moment, just fix initdb.


src/bin/initdb/initdb.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 7565b9b2ded..47d31373dd1 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -2510,6 +2510,15 @@ main(int argc, char *argv[]) "pg_stat_tmp" }; + /* + * Ensure that buffering behavior of stdout and stderr matches what it is + * in interactive usage (at least on most platforms). This prevents + * unexpected output ordering when, eg, output is redirected to a file. + * POSIX says we must do this before any other usage of these files. + */ + setvbuf(stdout, NULL, _IOLBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb")); [parent: e31d77c96f66]