Re: Enhancement to psql command, feedback.

From: John McKown <john(dot)archie(dot)mckown(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Ron <ronljohnsonjr(at)gmail(dot)com>, "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Enhancement to psql command, feedback.
Date: 2018-05-09 14:17:55
Message-ID: CAAJSdjjsYE0bOW3wa2AYUEPRS0+f_wMf64E0qpFTZNiAix1VSQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, May 9, 2018 at 8:56 AM, David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com> wrote:

> On Wed, May 9, 2018 at 6:44 AM, John McKown <john(dot)archie(dot)mckown(at)gmail(dot)com>
> wrote:
>
>> Again, this is just a discussion point. And I'm quite willing to admit
>> defeat if most people don't think that it is worth the effort.
>>
>
> ​-1, at least per the example. I would not want "-U postgres" inside the
> file. I tend to rely on service entries, not environment variables, and
> wouldn't want to hard-code them either. While psql has grown more
> flow-control capabilities recently it is, in most cases, a support language
> for me, not a main entry point. Shell scripts merge the per-instance
> run-time environment I need with the behavior the script provides - merging
> that I find I need more often than not and don't miss the added overhead in
> the few cases where it is unnecessary.
>
> David J.
>
>
​I agree. I wouldn't want the -U inside a "regular" shell script either. As
a minor example, consider the following _almost_ equivalent scripts.

$ cat psql-script.sh
#!/bin/sh
psql "$@" -f - <<EOT
select * from table;
EOF

$ cat psql-script.sql
#!/usr/bin/psql -f -
select * from table

$ chmod 755 psql-script.{sh,sql}
$ ./psql-script.sh -U postgres -d somedb -h remote-host.com
$ ./psql-script.sql -U postgres -d somedb -h remote-host.com

​​

​These are _almost_ equivalent.​ The first execution shown after the chmod
is effectively:

psql -U postgres -d somedb -h remote-host.com -f - <<EOT
select * from table;
EOT

​The second is effectively:

/usr/bin/psql -f ./psql-script.sql -U postgres -d somedb -h remote-host.com

The only difference is whether the -f is "at the front" or "at the end" of
the "generated" command which is actually sent to the exec() function. In
reality, from what the BASH maintainer has said, the first script is a bit
like:

file=$(mktemp) # generate a temporary file name
{ cat <<EOT
select * from table;
EOT
} >${file}
psql -U postgres -d somedb -h remote-host.com -f ${file}

It just that the HERE document doesn't actually create the ${file}
variable. I have NO idea how other shell implement HERE documents.

However, in the second case, the "magic" first line causes psql, at
present, to report an error and abort. This is why I'd like to modify how
the file referenced via the -f argument is processed. That is, the first
line of any file referenced & executed via the -f argument will be ignored
if and only if it starts with a shebang (#!). If the first line of the file
does not start with a shebang, it is processed normally as are all
subsequent lines.

If I get the energy & time, I'll give a look at the actual source. If it is
within my, admitted limited, ability to generate a patch to implement what
I'm thinking of, I'll post it over on the development forum.

--
We all have skeletons in our closet.
Mine are so old, they have osteoporosis.

Maranatha! <><
John McKown

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John McKown 2018-05-09 14:18:06 Re: Enhancement to psql command, feedback.
Previous Message David G. Johnston 2018-05-09 13:56:45 Re: Enhancement to psql command, feedback.