Show remote database name in remote error messages.
authorMarko Kreen <markokr@gmail.com>
Mon, 12 Apr 2010 13:03:14 +0000 (16:03 +0300)
committerMarko Kreen <markokr@gmail.com>
Mon, 12 Apr 2010 14:39:09 +0000 (17:39 +0300)
expected/plproxy_encoding.out
expected/plproxy_test.out
sql/plproxy_init.sql
sql/plproxy_test.sql
src/cluster.c
src/execute.c
src/main.c
src/plproxy.h

index e52ac25dfcb7e3c5922f440ae84cc2459b081075..0d0ea12fe626ecb7f59ef3df6c404f4e2dfe994c 100644 (file)
@@ -108,7 +108,7 @@ select * from test_encoding2('クライアント側のデータ');
 (1 row)
 
 select * from test_encoding3('クライアント側のデータ');
-NOTICE:  public.test_encoding3(1): REMOTE NOTICE: got: クライアント側のデータ
+NOTICE:  public.test_encoding3(1): [test_enc_part] REMOTE NOTICE: got: クライアント側のデータ
  id |         コラム         
 ----+------------------------
   3 | クライアント側のデータ
@@ -215,7 +215,7 @@ select * from test_encoding2('クライアント側のデータ');
 (1 row)
 
 select * from test_encoding3('クライアント側のデータ');
-NOTICE:  public.test_encoding3(1): REMOTE NOTICE: got: クライアント側のデータ
+NOTICE:  public.test_encoding3(1): [test_enc_part] REMOTE NOTICE: got: クライアント側のデータ
  id |         コラム         
 ----+------------------------
   3 | クライアント側のデータ
index 5b5fb7a0a8f4b6030af3c69a52ca154523184b58..9500283243e105f112ed0d786c0883a2ea83ce3d 100644 (file)
@@ -379,7 +379,7 @@ as $$
     run on 0;
 $$ language plproxy;
 select * from test_error1();
-ERROR:  public.test_error1(0): REMOTE ERROR: column "line2err" does not exist
+ERROR:  public.test_error1(0): [test_part] REMOTE ERROR: column "line2err" does not exist
 LINE 1: select line2err
                ^
 QUERY:  select line2err
@@ -392,6 +392,30 @@ as $$
 $$ language plproxy;
 select * from test_error2();
 NOTICE:  PL/Proxy: dropping stale conn
-ERROR:  public.test_error2(0): REMOTE ERROR: column "err" does not exist
+ERROR:  public.test_error2(0): [test_part] REMOTE ERROR: column "err" does not exist
 LINE 1: select * from test_error2();
                ^
+create function test_error3() returns int4
+as $$
+    connect 'dbname=test_part';
+$$ language plproxy;
+select * from test_error3();
+ERROR:  public.test_error3(0): [test_part] REMOTE ERROR: function public.test_error3() does not exist
+LINE 1: select * from test_error3();
+                            ^
+HINT:  Remote hint: No function matches the given name and argument types. You might need to add explicit type casts.
+-- test invalid db
+create function test_bad_db() returns int4
+as $$
+    cluster 'badcluster';
+$$ language plproxy;
+select * from test_bad_db();
+ERROR:  PL/Proxy function public.test_bad_db(0): [nonex_db] PQconnectPoll: FATAL:  database "nonex_db" does not exist
+
+create function test_bad_db2() returns int4
+as $$
+    connect 'dbname=wrong_name_db';
+$$ language plproxy;
+select * from test_bad_db2();
+ERROR:  PL/Proxy function public.test_bad_db2(0): [wrong_name_db] PQconnectPoll: FATAL:  database "wrong_name_db" does not exist
+
index a6bd1f94959d908918b78a63aea3854e7e4a747b..0d7312342b1fae82fc7498236e71b97898e9241b 100644 (file)
@@ -16,6 +16,9 @@ begin
     if cluster_name = 'testcluster' then
         return 5;
     end if;
+    if cluster_name = 'badcluster' then
+        return 5;
+    end if;
     raise exception 'no such cluster: %', cluster_name;
 end; $$ language plpgsql;
 
@@ -27,6 +30,10 @@ begin
         return next 'host=127.0.0.1 dbname=test_part';
         return;
     end if;
+    if cluster_name = 'badcluster' then
+        return next 'host=127.0.0.1 dbname=nonex_db';
+        return;
+    end if;
     raise exception 'no such cluster: %', cluster_name;
 end; $$ language plpgsql;
 
index c3056c7439c33b862943ecc3c65a8801b31c1365..d2f725caacf8332857223c503c1b3983b442edec 100644 (file)
@@ -256,4 +256,23 @@ as $$
 $$ language plproxy;
 select * from test_error2();
 
+create function test_error3() returns int4
+as $$
+    connect 'dbname=test_part';
+$$ language plproxy;
+select * from test_error3();
+
+-- test invalid db
+create function test_bad_db() returns int4
+as $$
+    cluster 'badcluster';
+$$ language plproxy;
+select * from test_bad_db();
+
+create function test_bad_db2() returns int4
+as $$
+    connect 'dbname=wrong_name_db';
+$$ language plproxy;
+select * from test_bad_db2();
+
 
index 7a85f3657f1ee5129e7166e121c0b0aeadc628ee..4d43aed9e2f82dcd5ab18c91c44be9cd485db176 100644 (file)
@@ -207,6 +207,7 @@ add_connection(ProxyCluster *cluster, char *connstr, int part_num)
        {
                conn = &cluster->conn_list[cluster->conn_count++];
                conn->connstr = MemoryContextStrdup(cluster_mem, final->data);
+               conn->cluster = cluster;
        }
 
        cluster->part_map[part_num] = conn;
@@ -812,6 +813,7 @@ fake_cluster(ProxyFunction *func, const char *connect_str)
        cluster->part_map = palloc(sizeof(ProxyConnection *));
        cluster->conn_list = palloc0(sizeof(ProxyConnection));
        conn = &cluster->conn_list[0];
+       conn->cluster = cluster;
        cluster->part_map[0] = conn;
 
        conn->connstr = pstrdup(cluster->name);
index 8cb83e9a9ed571d36138ba645d60607521a681cd..4db9052eac46982049397bed5ff5629782d2f154 100644 (file)
@@ -48,8 +48,8 @@ static int geterrcode(void)
 static void
 conn_error(ProxyFunction *func, ProxyConnection *conn, const char *desc)
 {
-       plproxy_error(func, "%s: %s",
-                                 desc, PQerrorMessage(conn->db));
+       plproxy_error(func, "[%s] %s: %s",
+                                 PQdb(conn->db), desc, PQerrorMessage(conn->db));
 }
 
 /* Compare if major/minor match. Works on "MAJ.MIN.*" */
@@ -261,8 +261,9 @@ intr_loop:
 static void
 handle_notice(void *arg, const PGresult *res)
 {
-       ProxyCluster *cluster = arg;
-       plproxy_remote_error(cluster->cur_func, res, false);
+       ProxyConnection *conn = arg;
+       ProxyCluster *cluster = conn->cluster;
+       plproxy_remote_error(cluster->cur_func, conn, res, false);
 }
 
 /* check existing conn status or launch new conn */
@@ -310,7 +311,7 @@ prepare_conn(ProxyFunction *func, ProxyConnection *conn)
                conn_error(func, conn, "PQconnectStart");
 
        /* override default notice handler */
-       PQsetNoticeReceiver(conn->db, handle_notice, func->cur_cluster);
+       PQsetNoticeReceiver(conn->db, handle_notice, conn);
 }
 
 /*
@@ -353,7 +354,7 @@ another_result(ProxyFunction *func, ProxyConnection *conn)
                                PQclear(conn->res);
                        conn->res = res;
 
-                       plproxy_remote_error(func, res, true);
+                       plproxy_remote_error(func, conn, res, true);
                        break;
                default:
                        if (conn->res)
index 055b27c6e1e5ce11142f326e7fc62b71fe485205..a020c22a3f77f4d9310684a386a3eacdb3c9e087 100644 (file)
@@ -83,7 +83,7 @@ plproxy_error(ProxyFunction *func, const char *fmt,...)
  * Pass remote error/notice/warning through.
  */
 void
-plproxy_remote_error(ProxyFunction *func, const PGresult *res, bool iserr)
+plproxy_remote_error(ProxyFunction *func, ProxyConnection *conn, const PGresult *res, bool iserr)
 {
        const char *ss = PQresultErrorField(res, PG_DIAG_SQLSTATE);
        const char *sev = PQresultErrorField(res, PG_DIAG_SEVERITY);
@@ -105,7 +105,7 @@ plproxy_remote_error(ProxyFunction *func, const PGresult *res, bool iserr)
 
        ereport(elevel, (
                errcode(MAKE_SQLSTATE(ss[0], ss[1], ss[2], ss[3], ss[4])),
-               errmsg("%s(%d): REMOTE %s: %s", func->name, func->arg_count, sev, msg),
+               errmsg("%s(%d): [%s] REMOTE %s: %s", func->name, func->arg_count, PQdb(conn->db), sev, msg),
                det ? errdetail("Remote detail: %s", det) : 0,
                hint ? errhint("Remote hint: %s", hint) : 0,
                spos ? errposition(atoi(spos)) : 0,
index f6049cb809263faac0696b5b3285c579ebf22344..31e6583f3b365fb81aa6ec11d20a6018fb6902f5 100644 (file)
@@ -125,6 +125,7 @@ typedef struct ProxyConfig
 /* Single database connection */
 typedef struct
 {
+       struct ProxyCluster *cluster;
        const char *connstr;            /* Connection string for libpq */
 
        /* state */
@@ -330,7 +331,7 @@ typedef struct ProxyFunction
 /* main.c */
 Datum          plproxy_call_handler(PG_FUNCTION_ARGS);
 void           plproxy_error(ProxyFunction *func, const char *fmt,...);
-void           plproxy_remote_error(ProxyFunction *func, const PGresult *res, bool iserr);
+void           plproxy_remote_error(ProxyFunction *func, ProxyConnection *conn, const PGresult *res, bool iserr);
 
 /* function.c */
 void           plproxy_function_cache_init(void);