Re: Enhancement to psql command, feedback.

Поиск
Список
Период
Сортировка
От John McKown
Тема Re: Enhancement to psql command, feedback.
Дата
Msg-id CAAJSdjjsYE0bOW3wa2AYUEPRS0+f_wMf64E0qpFTZNiAix1VSQ@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Enhancement to psql command, feedback.  ("David G. Johnston" <david.g.johnston@gmail.com>)
Ответы Re: Enhancement to psql command, feedback.  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-general
On Wed, May 9, 2018 at 8:56 AM, David G. Johnston <david.g.johnston@gmail.com> wrote:
On Wed, May 9, 2018 at 6:44 AM, John McKown <john.archie.mckown@gmail.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

В списке pgsql-general по дате отправления:

Предыдущее
От: "David G. Johnston"
Дата:
Сообщение: Re: Enhancement to psql command, feedback.
Следующее
От: John McKown
Дата:
Сообщение: Re: Enhancement to psql command, feedback.