Re: [HACKERS] make pg_controldata accept -D dirname

2014-10-25 Thread Michael Paquier
On Sat, Oct 25, 2014 at 1:20 AM, Heikki Linnakangas
hlinnakan...@vmware.com wrote:
 Argh, looks like I forgot the actual code changes required.
 I just noticed that pg_controldata and pg_resetxlog don't check for extra
 arguments:
 $ pg_resetxlog data fds sdf sdf
 Transaction log reset
I think that it would be good to add as well a set of TAP tests for
all those things. So attached are two patches, one to complete the
tests of pg_controldata, and a second one to add TAP tests for
pg_resetxlog.
-- 
Michael
From b196d4159d7fce4f84222659a3b445c86cd2b5e8 Mon Sep 17 00:00:00 2001
From: Michael Paquier mich...@otacoo.com
Date: Sun, 26 Oct 2014 13:08:05 +0900
Subject: [PATCH 1/2] Add more TAP tests for pg_controldata

Those checks are related to extra arguments and the newly-introduced
option -D.
---
 src/bin/pg_controldata/t/001_pg_controldata.pl | 10 +-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/bin/pg_controldata/t/001_pg_controldata.pl b/src/bin/pg_controldata/t/001_pg_controldata.pl
index 35ad10a..f9cdcec 100644
--- a/src/bin/pg_controldata/t/001_pg_controldata.pl
+++ b/src/bin/pg_controldata/t/001_pg_controldata.pl
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 use TestLib;
-use Test::More tests = 6;
+use Test::More tests = 10;
 
 my $tempdir = TestLib::tempdir;
 
@@ -11,6 +11,14 @@ program_options_handling_ok('pg_controldata');
 command_fails(['pg_controldata'], 'pg_controldata without arguments fails');
 command_fails([ 'pg_controldata', 'nonexistent' ],
 	'pg_controldata with nonexistent directory fails');
+command_fails([ 'pg_controldata', 'arg1', 'arg2', 'arg3' ],
+	'pg_controldata with too many arguments fails');
+command_fails([ 'pg_controldata', '-D' ],
+	'pg_controldata -D with no directory defined fails');
+command_fails([ 'pg_controldata', '-D', 'arg1', 'arg2', 'arg3' ],
+	'pg_controldata -D with too many arguments fails');
 system_or_bail initdb -D '$tempdir'/data -A trust /dev/null;
 command_like([ 'pg_controldata', $tempdir/data ],
 	qr/checkpoint/, 'pg_controldata produces output');
+command_like([ 'pg_controldata', -D, $tempdir/data ],
+	qr/checkpoint/, 'pg_controldata produces output');
-- 
2.1.2

From 7d92cbfd0607fbf35902408e3a82bf2ca60bf71f Mon Sep 17 00:00:00 2001
From: Michael Paquier mpaqu...@otacoo.com
Date: Sat, 25 Oct 2014 08:14:37 +
Subject: [PATCH 2/2] Add TAP tests for pg_resetxlog

Those checks are related to extra arguments, the use of option -D with
a data directory as well as the generation of output for this utility.
---
 src/bin/pg_resetxlog/.gitignore|  1 +
 src/bin/pg_resetxlog/Makefile  |  6 ++
 src/bin/pg_resetxlog/t/001_pg_resetxlog.pl | 24 
 3 files changed, 31 insertions(+)
 create mode 100644 src/bin/pg_resetxlog/t/001_pg_resetxlog.pl

diff --git a/src/bin/pg_resetxlog/.gitignore b/src/bin/pg_resetxlog/.gitignore
index 6b84208..68d8890 100644
--- a/src/bin/pg_resetxlog/.gitignore
+++ b/src/bin/pg_resetxlog/.gitignore
@@ -1 +1,2 @@
 /pg_resetxlog
+/tmp_check/
diff --git a/src/bin/pg_resetxlog/Makefile b/src/bin/pg_resetxlog/Makefile
index 6610690..5ebf8f4 100644
--- a/src/bin/pg_resetxlog/Makefile
+++ b/src/bin/pg_resetxlog/Makefile
@@ -33,3 +33,9 @@ uninstall:
 
 clean distclean maintainer-clean:
 	rm -f pg_resetxlog$(X) $(OBJS)
+
+check: all
+	$(prove_check)
+
+installcheck:
+	$(prove_installcheck)
diff --git a/src/bin/pg_resetxlog/t/001_pg_resetxlog.pl b/src/bin/pg_resetxlog/t/001_pg_resetxlog.pl
new file mode 100644
index 000..4e6e0a7
--- /dev/null
+++ b/src/bin/pg_resetxlog/t/001_pg_resetxlog.pl
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+use TestLib;
+use Test::More tests = 10;
+
+my $tempdir = TestLib::tempdir;
+
+program_help_ok('pg_resetxlog');
+program_version_ok('pg_resetxlog');
+program_options_handling_ok('pg_resetxlog');
+command_fails(['pg_resetxlog'], 'pg_resetxlog without arguments fails');
+command_fails([ 'pg_resetxlog', 'nonexistent' ],
+	'pg_resetxlog with nonexistent directory fails');
+command_fails([ 'pg_resetxlog', 'arg1', 'arg2', 'arg3' ],
+	'pg_resetxlog with too many arguments fails');
+command_fails([ 'pg_resetxlog', '-D' ],
+	'pg_resetxlog -D with no directory defined fails');
+command_fails([ 'pg_resetxlog', '-D', 'arg1', 'arg2', 'arg3' ],
+	'pg_resetxlog -D with too many arguments fails');
+system_or_bail initdb -D '$tempdir'/data -A trust /dev/null;
+command_like([ 'pg_resetxlog', $tempdir/data ],
+	qr/Transaction/, 'pg_resetxlog produces output');
+command_like([ 'pg_resetxlog', -D, $tempdir/data ],
+	qr/Transaction/, 'pg_resetxlog produces output');
-- 
2.1.2


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-10-24 Thread Michael Paquier
On Thu, Sep 25, 2014 at 12:38 PM, Heikki Linnakangas 
hlinnakan...@vmware.com wrote:

 On 09/24/2014 05:48 PM, Abhijit Menon-Sen wrote:

 Updated patches attached.


 Thanks, applied some version of these.

This set of patches has been applied as b0d81ad but patch 0001 did not make
it in, so pg_controldata is not yet able to accept -D:
$ pg_controldata -D /foo/path
pg_controldata: could not open file -D/global/pg_control for reading: No
such file or directory
$ pg_controldata /foo/path
pg_controldata: could not open file /foo/path/global/pg_control for
reading: No such file or directory
-- 
Michael


Re: [HACKERS] make pg_controldata accept -D dirname

2014-10-24 Thread Heikki Linnakangas

On 10/24/2014 11:36 AM, Michael Paquier wrote:

On Thu, Sep 25, 2014 at 12:38 PM, Heikki Linnakangas 
hlinnakan...@vmware.com wrote:


On 09/24/2014 05:48 PM, Abhijit Menon-Sen wrote:


Updated patches attached.



Thanks, applied some version of these.


This set of patches has been applied as b0d81ad but patch 0001 did not make
it in, so pg_controldata is not yet able to accept -D:
$ pg_controldata -D /foo/path
pg_controldata: could not open file -D/global/pg_control for reading: No
such file or directory
$ pg_controldata /foo/path
pg_controldata: could not open file /foo/path/global/pg_control for
reading: No such file or directory


Argh, looks like I forgot the actual code changes required.

I just noticed that pg_controldata and pg_resetxlog don't check for 
extra arguments:


$ pg_resetxlog data fds sdf sdf
Transaction log reset

Fixed that too.

- Heikki



--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-25 Thread Heikki Linnakangas

On 09/24/2014 05:48 PM, Abhijit Menon-Sen wrote:

Updated patches attached.


Thanks, applied some version of these.

- Heikki



--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Magnus Hagander
On Wed, Sep 24, 2014 at 12:59 PM, Abhijit Menon-Sen a...@2ndquadrant.com 
wrote:
 Hi.

 I can never remember that pg_controldata takes only a dirname rather
 than -D dirname, and I gather I'm not the only one. Here's a tiny
 patch to make it work either way. As far as I can see, it doesn't
 break anything, not even if you have a data directory named -D.

I haven't looked at the code, but definitely +1 for the feature.
That's really quite annoying.

-- 
 Magnus Hagander
 Me: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Heikki Linnakangas

On 09/24/2014 01:59 PM, Abhijit Menon-Sen wrote:

Hi.

I can never remember that pg_controldata takes only a dirname rather
than -D dirname, and I gather I'm not the only one. Here's a tiny
patch to make it work either way. As far as I can see, it doesn't
break anything, not even if you have a data directory named -D.


Ah, I frequently run into that too; but with pg_resetxlog.

- Heikki



--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Abhijit Menon-Sen
At 2014-09-24 14:03:41 +0300, hlinnakan...@vmware.com wrote:

 Ah, I frequently run into that too; but with pg_resetxlog.

Well, that's no fun. Patch attached.

-- Abhijit
From 23fc4d90d0353e1c6d65ca5715fc0338199f01cf Mon Sep 17 00:00:00 2001
From: Abhijit Menon-Sen a...@2ndquadrant.com
Date: Wed, 24 Sep 2014 16:48:36 +0530
Subject: Make pg_resetxlog also accept -D datadir

---
 src/bin/pg_resetxlog/pg_resetxlog.c | 13 +
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/bin/pg_resetxlog/pg_resetxlog.c b/src/bin/pg_resetxlog/pg_resetxlog.c
index 302d005..3b43aff 100644
--- a/src/bin/pg_resetxlog/pg_resetxlog.c
+++ b/src/bin/pg_resetxlog/pg_resetxlog.c
@@ -89,7 +89,7 @@ main(int argc, char *argv[])
 	MultiXactId set_oldestmxid = 0;
 	char	   *endptr;
 	char	   *endptr2;
-	char	   *DataDir;
+	char	   *DataDir = NULL;
 	int			fd;
 
 	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN(pg_resetxlog));
@@ -111,10 +111,14 @@ main(int argc, char *argv[])
 	}
 
 
-	while ((c = getopt(argc, argv, fl:m:no:O:x:e:)) != -1)
+	while ((c = getopt(argc, argv, D:fl:m:no:O:x:e:)) != -1)
 	{
 		switch (c)
 		{
+			case 'D':
+DataDir = optarg;
+break;
+
 			case 'f':
 force = true;
 break;
@@ -233,7 +237,7 @@ main(int argc, char *argv[])
 		}
 	}
 
-	if (optind == argc)
+	if (DataDir == NULL  optind == argc)
 	{
 		fprintf(stderr, _(%s: no data directory specified\n), progname);
 		fprintf(stderr, _(Try \%s --help\ for more information.\n), progname);
@@ -257,7 +261,8 @@ main(int argc, char *argv[])
 	}
 #endif
 
-	DataDir = argv[optind];
+	if (DataDir == NULL)
+		DataDir = argv[optind];
 
 	if (chdir(DataDir)  0)
 	{
-- 
1.9.1


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Michael Paquier
On Wed, Sep 24, 2014 at 7:59 PM, Abhijit Menon-Sen a...@2ndquadrant.com wrote:
 I can never remember that pg_controldata takes only a dirname rather
 than -D dirname, and I gather I'm not the only one. Here's a tiny
 patch to make it work either way. As far as I can see, it doesn't
 break anything, not even if you have a data directory named -D.
+1. I forget it all the time as well...
-- 
Michael


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Rajeev rastogi

On 24 September 2014 17:15, Michael Paquier Wrote:
 
 On Wed, Sep 24, 2014 at 7:59 PM, Abhijit Menon-Sen a...@2ndquadrant.com
 wrote:
  I can never remember that pg_controldata takes only a dirname rather
  than -D dirname, and I gather I'm not the only one. Here's a tiny
  patch to make it work either way. As far as I can see, it doesn't
  break anything, not even if you have a data directory named -D.
 +1. I forget it all the time as well...

+1, always I get confused once pg_controldata does not work with -D.


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Alvaro Herrera
Magnus Hagander wrote:
 On Wed, Sep 24, 2014 at 12:59 PM, Abhijit Menon-Sen a...@2ndquadrant.com 
 wrote:
  Hi.
 
  I can never remember that pg_controldata takes only a dirname rather
  than -D dirname, and I gather I'm not the only one. Here's a tiny
  patch to make it work either way. As far as I can see, it doesn't
  break anything, not even if you have a data directory named -D.
 
 I haven't looked at the code, but definitely +1 for the feature.
 That's really quite annoying.

+1

-- 
Álvaro Herrerahttp://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training  Services


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Thom Brown
On 24 September 2014 12:04, Magnus Hagander mag...@hagander.net wrote:

 On Wed, Sep 24, 2014 at 12:59 PM, Abhijit Menon-Sen a...@2ndquadrant.com
 wrote:
  Hi.
 
  I can never remember that pg_controldata takes only a dirname rather
  than -D dirname, and I gather I'm not the only one. Here's a tiny
  patch to make it work either way. As far as I can see, it doesn't
  break anything, not even if you have a data directory named -D.

 I haven't looked at the code, but definitely +1 for the feature.
 That's really quite annoying.


And here I was thinking it was just me.

+1

Thom


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Heikki Linnakangas

On 09/24/2014 02:21 PM, Abhijit Menon-Sen wrote:

At 2014-09-24 14:03:41 +0300, hlinnakan...@vmware.com wrote:


Ah, I frequently run into that too; but with pg_resetxlog.


Well, that's no fun. Patch attached.


Thanks. Please update the docs and usage(), too.

- Heikki



--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Abhijit Menon-Sen
At 2014-09-24 16:02:29 +0300, hlinnakan...@vmware.com wrote:

 Thanks. Please update the docs and usage(), too.

I'm sorry, but I don't think it would be an improvement to make the docs
explain that it's valid to use either -D datadir or specify it without
an option. If both commands were changed to *require* -D datadir, that
would be worth documenting. But of course I didn't want to change any
behaviour for people who have a better memory than I do.

I think it's all right as a quick hack to remove an inconsistency that
has clearly annoyed many people, but not much more.

Would you be OK with my just sticking a [-D] before DATADIR in the
usage() message for both programs, with no further explanation? (But
to be honest, I don't think even that is necessary or desirable.)

-- Abhijit


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Abhijit Menon-Sen
To put it another way, I doubt any of the people who were surprised by
it looked at either the usage message or the docs. ;-)

-- Abhijit


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Tom Lane
Abhijit Menon-Sen a...@2ndquadrant.com writes:
 At 2014-09-24 16:02:29 +0300, hlinnakan...@vmware.com wrote:
 Thanks. Please update the docs and usage(), too.

 I'm sorry, but I don't think it would be an improvement to make the docs
 explain that it's valid to use either -D datadir or specify it without
 an option.

What's so hard about [ -D ] before the datadir argument?

regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Abhijit Menon-Sen
At 2014-09-24 09:25:12 -0400, t...@sss.pgh.pa.us wrote:

 What's so hard about [ -D ] before the datadir argument?

I'm sorry to have given you the impression that I objected to it because
it was hard. Since I proposed the same thing a few lines after what you
quoted, I guess I have to agree it's not hard.

I think it's pointless, because if you're going to look at the usage
message to begin with, why not do what it already says? But I don't
care enough to argue about it any further.

Patches attached.

-- Abhijit

P.S. Trivia: I can't find any other options in Postgres where the
argument is mandatory but the option is optional.
From 69d7386ab59ca9df74af5abe5a5c3cf5a93e64bb Mon Sep 17 00:00:00 2001
From: Abhijit Menon-Sen a...@2ndquadrant.com
Date: Wed, 24 Sep 2014 16:26:00 +0530
Subject: Make pg_controldata ignore a -D before DataDir

---
 src/bin/pg_controldata/pg_controldata.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c
index f815024..4386283 100644
--- a/src/bin/pg_controldata/pg_controldata.c
+++ b/src/bin/pg_controldata/pg_controldata.c
@@ -33,7 +33,7 @@ usage(const char *progname)
 {
 	printf(_(%s displays control information of a PostgreSQL database cluster.\n\n), progname);
 	printf(_(Usage:\n));
-	printf(_(  %s [OPTION] [DATADIR]\n), progname);
+	printf(_(  %s [OPTION] [[-D] DATADIR]\n), progname);
 	printf(_(\nOptions:\n));
 	printf(_(  -V, --version  output version information, then exit\n));
 	printf(_(  -?, --help show this help, then exit\n));
@@ -120,7 +120,11 @@ main(int argc, char *argv[])
 	}
 
 	if (argc  1)
+	{
 		DataDir = argv[1];
+		if (strcmp(DataDir, -D) == 0  argc  2)
+			DataDir = argv[2];
+	}
 	else
 		DataDir = getenv(PGDATA);
 	if (DataDir == NULL)
-- 
1.9.1

From 388b5009281184051398657449e649ec9585a242 Mon Sep 17 00:00:00 2001
From: Abhijit Menon-Sen a...@2ndquadrant.com
Date: Wed, 24 Sep 2014 16:48:36 +0530
Subject: Make pg_resetxlog also accept -D datadir

---
 src/bin/pg_resetxlog/pg_resetxlog.c | 15 ++-
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/bin/pg_resetxlog/pg_resetxlog.c b/src/bin/pg_resetxlog/pg_resetxlog.c
index 302d005..8ff5df0 100644
--- a/src/bin/pg_resetxlog/pg_resetxlog.c
+++ b/src/bin/pg_resetxlog/pg_resetxlog.c
@@ -89,7 +89,7 @@ main(int argc, char *argv[])
 	MultiXactId set_oldestmxid = 0;
 	char	   *endptr;
 	char	   *endptr2;
-	char	   *DataDir;
+	char	   *DataDir = NULL;
 	int			fd;
 
 	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN(pg_resetxlog));
@@ -111,10 +111,14 @@ main(int argc, char *argv[])
 	}
 
 
-	while ((c = getopt(argc, argv, fl:m:no:O:x:e:)) != -1)
+	while ((c = getopt(argc, argv, D:fl:m:no:O:x:e:)) != -1)
 	{
 		switch (c)
 		{
+			case 'D':
+DataDir = optarg;
+break;
+
 			case 'f':
 force = true;
 break;
@@ -233,7 +237,7 @@ main(int argc, char *argv[])
 		}
 	}
 
-	if (optind == argc)
+	if (DataDir == NULL  optind == argc)
 	{
 		fprintf(stderr, _(%s: no data directory specified\n), progname);
 		fprintf(stderr, _(Try \%s --help\ for more information.\n), progname);
@@ -257,7 +261,8 @@ main(int argc, char *argv[])
 	}
 #endif
 
-	DataDir = argv[optind];
+	if (DataDir == NULL)
+		DataDir = argv[optind];
 
 	if (chdir(DataDir)  0)
 	{
@@ -1077,7 +1082,7 @@ static void
 usage(void)
 {
 	printf(_(%s resets the PostgreSQL transaction log.\n\n), progname);
-	printf(_(Usage:\n  %s [OPTION]... DATADIR\n\n), progname);
+	printf(_(Usage:\n  %s [OPTION]... [-D] DATADIR\n\n), progname);
 	printf(_(Options:\n));
 	printf(_(  -e XIDEPOCH  set next transaction ID epoch\n));
 	printf(_(  -f   force update to be done\n));
-- 
1.9.1


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Heikki Linnakangas

On 09/24/2014 04:49 PM, Abhijit Menon-Sen wrote:

At 2014-09-24 09:25:12 -0400, t...@sss.pgh.pa.us wrote:


What's so hard about [ -D ] before the datadir argument?


I'm sorry to have given you the impression that I objected to it because
it was hard. Since I proposed the same thing a few lines after what you
quoted, I guess I have to agree it's not hard.

I think it's pointless, because if you're going to look at the usage
message to begin with, why not do what it already says? But I don't
care enough to argue about it any further.


There's also the reverse situation: you see that a script contains a 
line like pg_controldata -D foo. You're accustomed to doing just 
pg_controldata foo, and you wonder what the -D option does. So you 
look it up in the docs or pg_controldata --help.


- Heikki



--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Alvaro Herrera
Abhijit Menon-Sen wrote:

 P.S. Trivia: I can't find any other options in Postgres where the
 argument is mandatory but the option is optional.

psql behaves similarly with its -d and -U switches also; note --help:

Usage:
  psql [OPTION]... [DBNAME [USERNAME]]
...
  -d, --dbname=DBNAME  database name to connect to (default: alvherre)
...
  -U, --username=USERNAME  database user name (default: alvherre)


Both are optional and have defaults.  Datadir for pg_controldata is also
optional and --help contains this blurb:

If no data directory (DATADIR) is specified, the environment variable PGDATA
is used.

I think you could replace those lines with
  -Ddata directory blah (default: value of $PGDATA)


But psql --help doesn't use $PGUSER to expand the default value for -U;
I'm not clear if this means we shouldn't expand PGDATA in the help line
for pg_controldata, or need to fix psql to expand PGUSER in -U.

-- 
Álvaro Herrerahttp://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training  Services


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] make pg_controldata accept -D dirname

2014-09-24 Thread Abhijit Menon-Sen
Updated patches attached.

-- Abhijit
From b3bd465357f96ebf1953b3a98f25fb51bac5eb26 Mon Sep 17 00:00:00 2001
From: Abhijit Menon-Sen a...@2ndquadrant.com
Date: Wed, 24 Sep 2014 16:26:00 +0530
Subject: Make pg_controldata ignore a -D before DataDir

---
 doc/src/sgml/ref/pg_controldata.sgml|  5 +++--
 src/bin/pg_controldata/pg_controldata.c | 13 +++--
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/doc/src/sgml/ref/pg_controldata.sgml b/doc/src/sgml/ref/pg_controldata.sgml
index fbf40fc..b4be660 100644
--- a/doc/src/sgml/ref/pg_controldata.sgml
+++ b/doc/src/sgml/ref/pg_controldata.sgml
@@ -40,8 +40,9 @@ PostgreSQL documentation
   para
This utility can only be run by the user who initialized the cluster because
it requires read access to the data directory.
-   You can specify the data directory on the command line, or use
-   the environment variable envarPGDATA/.  This utility supports the options
+   You can specify the data directory on the command line, with or without
+   option-D/ or use the environment variable envarPGDATA/.
+   This utility supports the options
option-V/ and option--version/, which print the
applicationpg_controldata/application version and exit.  It also
supports options option-?/ and option--help/, which output the
diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c
index f815024..cfa6911 100644
--- a/src/bin/pg_controldata/pg_controldata.c
+++ b/src/bin/pg_controldata/pg_controldata.c
@@ -31,14 +31,19 @@
 static void
 usage(const char *progname)
 {
+	const char *pgdata;
+
+	pgdata = getenv(PGDATA);
+	if (!pgdata)
+		pgdata = ;
+
 	printf(_(%s displays control information of a PostgreSQL database cluster.\n\n), progname);
 	printf(_(Usage:\n));
 	printf(_(  %s [OPTION] [DATADIR]\n), progname);
 	printf(_(\nOptions:\n));
+	printf(_(  -D DATADIR data directory (default: \%s\)\n), pgdata);
 	printf(_(  -V, --version  output version information, then exit\n));
 	printf(_(  -?, --help show this help, then exit\n));
-	printf(_(\nIf no data directory (DATADIR) is specified, 
-			 the environment variable PGDATA\nis used.\n\n));
 	printf(_(Report bugs to pgsql-b...@postgresql.org.\n));
 }
 
@@ -120,7 +125,11 @@ main(int argc, char *argv[])
 	}
 
 	if (argc  1)
+	{
 		DataDir = argv[1];
+		if (strcmp(DataDir, -D) == 0  argc  2)
+			DataDir = argv[2];
+	}
 	else
 		DataDir = getenv(PGDATA);
 	if (DataDir == NULL)
-- 
1.9.1

From 51c651a24db4f3b977fa38c08177acc062a5fb56 Mon Sep 17 00:00:00 2001
From: Abhijit Menon-Sen a...@2ndquadrant.com
Date: Wed, 24 Sep 2014 16:48:36 +0530
Subject: Make pg_resetxlog also accept -D datadir

---
 doc/src/sgml/ref/pg_resetxlog.sgml  |  6 --
 src/bin/pg_resetxlog/pg_resetxlog.c | 16 +++-
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/doc/src/sgml/ref/pg_resetxlog.sgml b/doc/src/sgml/ref/pg_resetxlog.sgml
index 0b53bd6..7b2386a 100644
--- a/doc/src/sgml/ref/pg_resetxlog.sgml
+++ b/doc/src/sgml/ref/pg_resetxlog.sgml
@@ -22,6 +22,7 @@ PostgreSQL documentation
  refsynopsisdiv
   cmdsynopsis
commandpg_resetxlog/command
+   arg choice=optoption-D/option replaceable class=parameterdatadir/replaceable/arg
arg choice=optoption-f/option/arg
arg choice=optoption-n/option/arg
arg choice=optoption-o/option replaceable class=parameteroid/replaceable/arg
@@ -30,7 +31,7 @@ PostgreSQL documentation
arg choice=optoption-m/option replaceable class=parametermxid/replaceable,replaceable class=parametermxid/replaceable/arg
arg choice=optoption-O/option replaceable class=parametermxoff/replaceable/arg
arg choice=optoption-l/option replaceable class=parameterxlogfile/replaceable/arg
-   arg choice=plainreplaceabledatadir/replaceable/arg
+   arg choice=optreplaceabledatadir/replaceable/arg
   /cmdsynopsis
  /refsynopsisdiv
 
@@ -55,7 +56,8 @@ PostgreSQL documentation
   para
This utility can only be run by the user who installed the server, because
it requires read/write access to the data directory.
-   For safety reasons, you must specify the data directory on the command line.
+   For safety reasons, you must specify the data directory on the command line
+   (with or without option-D/).
commandpg_resetxlog/command does not use the environment variable
envarPGDATA/.
   /para
diff --git a/src/bin/pg_resetxlog/pg_resetxlog.c b/src/bin/pg_resetxlog/pg_resetxlog.c
index 302d005..9364e49 100644
--- a/src/bin/pg_resetxlog/pg_resetxlog.c
+++ b/src/bin/pg_resetxlog/pg_resetxlog.c
@@ -89,7 +89,7 @@ main(int argc, char *argv[])
 	MultiXactId set_oldestmxid = 0;
 	char	   *endptr;
 	char	   *endptr2;
-	char	   *DataDir;
+	char	   *DataDir = NULL;
 	int			fd;
 
 	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN(pg_resetxlog));
@@ -111,10 +111,14 @@ main(int argc, char *argv[])
 	}
 
 
-	while ((c = getopt(argc, argv, fl:m:no:O:x:e:)) != -1)
+	while ((c = getopt(argc, argv, D:fl:m:no:O:x:e:)) != -1)
 	{
 		switch (c)