Обсуждение: pgsql pg_connect

Поиск
Список
Период
Сортировка

pgsql pg_connect

От
Jurgen Defurne
Дата:
Hello, people on the mailing list,

This is a rather technical question, but the people of pgsql ask that certain questions be first posted here.
While busy learning the postgreSQL system and Tcl/Tk, I noticed something that to me (a fulltime applications programmer) seems a flaw in libpgtcl.
When using pg_connect to a non-existing database, one doesn't have the means to control the extent of the error. If you try the following script :

load libpgtcl.so
set connection [pg_connect notanexistingdatabase]
it is not at all possible to recover the running program from this error. I have tried this using catch, but to no avail. This is the result :

Connection to database failed
FATAL 1:  Database notadatabase does not exist in pg_database
    while executing
"pg_connect notadatabase"
    (file "connect.tcl" line 10)

Or, this is an error in Tcl, or this is not good enough thought out in libpgtcl.

Why do I say this about libpgtcl ?

As I mentioned, I am a full-time applications programmer. When writing software for customers, you do NOT want to let loose parts of the OS which they don't know. You want to implement a graceful recovery of this error. In this case, I would like a way that my program keeps running, so that it is possible to send a notification to the user that the system isn't available and that he should try another time.
You could of course say, make sure that you have an existing database.
Well, this isn't always possible, and the backend may be down.
The kind of error that you have in this case is a resource allocation error. It should always be possible for the programmer to recover from this, or at least provide a meaningful message and a way to get out, without leaving the user on his own devices.
Once you have a result from the connection, the programmer should be able to test this result and act accordingly. If he doesn't test it, and he tries to execute a query, and he gets strange results, then it is his fault and responsibility. But this is what testing is for.

With regards to everybody,

Jurgen Defurne
Flanders
Belgium

Re: [GENERAL] pgsql pg_connect

От
"Ross J. Reedstrom"
Дата:
Jurgen -
In general, libpgtcl questions are best addressed to the 'interfaces' list.
The PgAccess program that comes with postgresql is a tcl program, and it
handles the problem with a catch. Here's the proc that does it (rewrapped
for email):

proc {open_database} {} {
    global dbc host pport dbname username password newusername\
      newpassword sdbname newdbname newhost newpport pref pgsql
    cursor_clock
    if {$newusername!=""} {
    set connres [catch {set newdbc [pg_connect -conninfo\
      "host=$newhost port=$newpport dbname=$newdbname\
      user=$newusername password=$newpassword"]} msg]
    } {
    set connres [catch {set newdbc [pg_connect $newdbname\
      -host $newhost -port $newpport]} msg]
    }
    if {$connres} {
    cursor_normal
    show_error "Error trying to connect to database \"$newdbname\"\
      on host $newhost\n\nPostgreSQL error message: $msg"
    return $msg
    } {
    catch {pg_disconnect $dbc}
    set dbc $newdbc
    set host $newhost
    set pport $newpport
    set dbname $newdbname

...other stuff deleted...

    }
}



On Thu, Oct 28, 1999 at 01:33:54PM +0200, Jurgen Defurne wrote:
> Hello, people on the mailing list,
>
> load libpgtcl.so
>
> set connection [pg_connect notanexistingdatabase]
>
> it is not at all possible to recover the running program from this
> error. I have tried this using catch, but to no avail. This is the
> result :
>
> Connection to database failed
> FATAL 1:  Database notadatabase does not exist in pg_database
>     while executing
> "pg_connect notadatabase"
>     (file "connect.tcl" line 10)
>
> Or, this is an error in Tcl, or this is not good enough thought out in
> libpgtcl.
>
> Why do I say this about libpgtcl ?

Ross
--
Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St.,  Houston, TX 77005