Search Postgresql Archives

Re: Enhancement to psql command, feedback.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, May 9, 2018 at 8:56 AM, David G. Johnston <david.g.johnston@xxxxxxxxx> wrote:
On Wed, May 9, 2018 at 6:44 AM, John McKown <john.archie.mckown@xxxxxxxxx> 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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux