[PATCH] Bug fix in initdb output

From: Nitin Jadhav <nitinjadhavpostgres(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH] Bug fix in initdb output
Date: 2021-03-01 04:52:37
Message-ID: CAMm1aWaNDuaPYFYMAqDeJrZmPtNvLcJRS++CcZWY8LT6KcoBZw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I found a behaviour mismatch in initdb's output between Windows and Unix in
PostgreSQL. When we run initdb, it displays a command at the end of the
output which is used to run the server. It displays properly in Unix where
we can directly use the command to run the server. But in the case of
Windows, the command shown in initdb's output cannot be directly used to
run the server since Windows uses forward slashes in the path but the
command contains backward slashes.

Please refer to the sample output of initdb given below. The command
contains 2 paths.
1. pg_ctl path - This path is handled properly in Unix whereas in Windows,
this path contains backward slashes which is a Unix complaint and it wont
work in Windows.
2. data path - This path is handled properly in both Unix and Windows.

C:\Users\Administrator\Desktop\Nitin_PostgreSQL\postgresql_test\src\tools\msvc>install\bin\initdb.exe
> -D database\data
> The files belonging to this database system will be owned by user
> "Administrator".
> This user must also own the server process.
> The database cluster will be initialized with locale "English_United
> States.1252".
> The default database encoding has accordingly been set to "WIN1252".
> The default text search configuration will be set to "english".
> Data page checksums are disabled.
> creating directory database/data ... ok
> creating subdirectories ... ok
> selecting dynamic shared memory implementation ... windows
> selecting default max_connections ... 100
> selecting default shared_buffers ... 128MB
> selecting default time zone ... US/Pacific
> creating configuration files ... ok
> running bootstrap script ... ok
> performing post-bootstrap initialization ... ok
> syncing data to disk ... ok
> initdb: warning: enabling "trust" authentication for local connections
> You can change this by editing pg_hba.conf or using the option -A, or
> --auth-local and --auth-host, the next time you run initdb.
> Success. You can now start the database server using:
> install/bin/pg_ctl -D ^"database^\data^" -l logfile start

The problem in the code is canonicalize_path() function simplifies the path
and it also converts the native path to Unix style path while simplifying
the path. That is why the path shown in windows contains backslashes.
Please refer below piece of code for more information.

> /* Get directory specification used to start initdb ... */

strlcpy(pg_ctl_path, argv[0], sizeof(pg_ctl_path));
> canonicalize_path(pg_ctl_path);
> get_parent_directory(pg_ctl_path);
> /* ... and tag on pg_ctl instead */
> join_path_components(pg_ctl_path, pg_ctl_path, "pg_ctl");

In case of a data directory path, it uses the native path. Hence there is
no problem.

> /* path to pg_ctl, properly quoted */
> appendShellString(start_db_cmd, pg_ctl_path);
> /* add -D switch, with properly quoted data directory */
> appendPQExpBufferStr(start_db_cmd, " -D ");
> appendShellString(start_db_cmd, pgdata_native);

The solution to this issue is we need to convert the ' pg_ctl_path' to
native path before displaying. The issue is fixed in the patch attached.

Following is the sample output after fixing the issue.

Success. You can now start the database server using:
> ^"install^\bin^\pg^_ctl^" -D ^"database^\data^" -l logfile start

Now this path can be directly used to run the server in windows.

Please share your thoughts on this. If we go ahead with this change, then
we need to back-patch. I would be happy to create those patches.

Thanks and Regards,
Nitin Jadhav

Attachment Content-Type Size
0001-Fix_initdb_output.patch application/octet-stream 751 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2021-03-01 04:57:12 Re: Different compression methods for FPI
Previous Message Thomas Munro 2021-03-01 04:46:03 Re: Fix DROP TABLESPACE on Windows with ProcSignalBarrier?