Re: [BUGS] BUG #2129: dblink problem - Mailing list pgsql-patches

From Joe Conway
Subject Re: [BUGS] BUG #2129: dblink problem
Date
Msg-id 43B9E2AF.7040003@joeconway.com
Whole thread Raw
Responses Re: [BUGS] BUG #2129: dblink problem
List pgsql-patches
Akio Iwaasa wrote:
> The following bug has been logged online:
>
> Bug reference:      2129
> Logged by:          Akio Iwaasa

>
> "postgres" process terminated with "signal 11"
> because of my wrong SQL statement using "dblink".
>
> --- SQL statement(Select statement a function) ---
>  select into RET *
>   from dblink(''select C1,C2,C3 from TABLE01 where ... '') <<<<< 3 column
>    as LINK_TABLE01(LC1 varchar(5),LC2 varchar(5),
>                    LC3 varchar(5),LC4 varchar(5)) ;        <<<<< 4 column

The attached patch (against cvs HEAD) fixes the reported issue.

However, there is a remaining oddity with dblink_fetch(). Basically,
each time dblink_fetch() is called, the named cursor is advanced, even
though an error is thrown before returning any rows. Is there a simple
way to get the number of columns in the result, without actually
advancing the cursor?

If no one thinks the above is a problem, I'll commit the attached
against HEAD and stable branches back to 7.3.

Joe
Index: dblink.c
===================================================================
RCS file: /cvsroot/pgsql/contrib/dblink/dblink.c,v
retrieving revision 1.50
diff -c -r1.50 dblink.c
*** dblink.c    22 Nov 2005 18:17:04 -0000    1.50
--- dblink.c    3 Jan 2006 02:20:43 -0000
***************
*** 579,592 ****
          /* got results, keep track of them */
          funcctx->user_fctx = res;

-         /* fast track when no results */
-         if (funcctx->max_calls < 1)
-         {
-             if (res)
-                 PQclear(res);
-             SRF_RETURN_DONE(funcctx);
-         }
-
          /* get a tuple descriptor for our result type */
          switch (get_call_result_type(fcinfo, NULL, &tupdesc))
          {
--- 579,584 ----
***************
*** 609,614 ****
--- 601,621 ----
          /* make sure we have a persistent copy of the tupdesc */
          tupdesc = CreateTupleDescCopy(tupdesc);

+         /* check result and tuple descriptor have the same number of columns */
+         if (PQnfields(res) != tupdesc->natts)
+             ereport(ERROR,
+                     (errcode(ERRCODE_DATATYPE_MISMATCH),
+                 errmsg("remote query result rowtype does not match "
+                         "the specified FROM clause rowtype")));
+
+         /* fast track when no results */
+         if (funcctx->max_calls < 1)
+         {
+             if (res)
+                 PQclear(res);
+             SRF_RETURN_DONE(funcctx);
+         }
+
          /* store needed metadata for subsequent calls */
          attinmeta = TupleDescGetAttInMetadata(tupdesc);
          funcctx->attinmeta = attinmeta;
***************
*** 778,791 ****
          if (freeconn)
              PQfinish(conn);

-         /* fast track when no results */
-         if (funcctx->max_calls < 1)
-         {
-             if (res)
-                 PQclear(res);
-             SRF_RETURN_DONE(funcctx);
-         }
-
          if (!is_sql_cmd)
          {
              /* get a tuple descriptor for our result type */
--- 785,790 ----
***************
*** 811,816 ****
--- 810,830 ----
              tupdesc = CreateTupleDescCopy(tupdesc);
          }

+         /* check result and tuple descriptor have the same number of columns */
+         if (PQnfields(res) != tupdesc->natts)
+             ereport(ERROR,
+                     (errcode(ERRCODE_DATATYPE_MISMATCH),
+                 errmsg("remote query result rowtype does not match "
+                         "the specified FROM clause rowtype")));
+
+         /* fast track when no results */
+         if (funcctx->max_calls < 1)
+         {
+             if (res)
+                 PQclear(res);
+             SRF_RETURN_DONE(funcctx);
+         }
+
          /* store needed metadata for subsequent calls */
          attinmeta = TupleDescGetAttInMetadata(tupdesc);
          funcctx->attinmeta = attinmeta;

pgsql-patches by date:

Previous
From: Marko Kreen
Date:
Subject: pgcrypto: bug in gen_salt (md5/xdes)
Next
From: Tom Lane
Date:
Subject: ShmemIndexLock rewrite