From: Joe Conway Date: Tue, 3 Jan 2006 23:48:42 +0000 (+0000) Subject: When the remote query result has a different number of columns X-Git-Tag: REL7_3_13~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4e0f51f13df2354d6c248e1874b0c16644623d30;p=postgresql When the remote query result has a different number of columns than the local query specifies (e.g. in the FROM clause), throw an ERROR (instead of crashing). Fix for bug #2129 reported by Akio Iwaasa. --- diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c index a7f2aead50..4d52f8d106 100644 --- a/contrib/dblink/dblink.c +++ b/contrib/dblink/dblink.c @@ -315,10 +315,6 @@ dblink_fetch(PG_FUNCTION_ARGS) /* got results, keep track of them */ funcctx->user_fctx = res; - /* fast track when no results */ - if (funcctx->max_calls < 1) - SRF_RETURN_DONE(funcctx); - /* check typtype to see if we have a predetermined return type */ functypeid = get_func_rettype(funcid); functyptype = get_typtype(functypeid); @@ -342,6 +338,20 @@ dblink_fetch(PG_FUNCTION_ARGS) /* store needed metadata for subsequent calls */ slot = TupleDescGetSlot(tupdesc); funcctx->slot = slot; + + /* check result and tuple descriptor have the same number of columns */ + if (PQnfields(res) != tupdesc->natts) + elog(ERROR, "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); + } + attinmeta = TupleDescGetAttInMetadata(tupdesc); funcctx->attinmeta = attinmeta; @@ -500,10 +510,6 @@ dblink_record(PG_FUNCTION_ARGS) PQfinish(conn); } - /* fast track when no results */ - if (funcctx->max_calls < 1) - SRF_RETURN_DONE(funcctx); - /* check typtype to see if we have a predetermined return type */ functypeid = get_func_rettype(funcid); functyptype = get_typtype(functypeid); @@ -530,6 +536,20 @@ dblink_record(PG_FUNCTION_ARGS) /* store needed metadata for subsequent calls */ slot = TupleDescGetSlot(tupdesc); funcctx->slot = slot; + + /* check result and tuple descriptor have the same number of columns */ + if (PQnfields(res) != tupdesc->natts) + elog(ERROR, "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); + } + attinmeta = TupleDescGetAttInMetadata(tupdesc); funcctx->attinmeta = attinmeta; diff --git a/contrib/dblink/doc/cursor b/contrib/dblink/doc/cursor index 3bc6bdb2fe..0b9235b427 100644 --- a/contrib/dblink/doc/cursor +++ b/contrib/dblink/doc/cursor @@ -69,6 +69,13 @@ Outputs Returns setof record +Note + + On a mismatch between the number of return fields as specified in the FROM + clause, and the actual number of fields returned by the remote cursor, an + ERROR will be thrown. In this event, the remote cursor is still advanced + by as many rows as it would have been if the ERROR had not occurred. + Example usage test=# select dblink_connect('dbname=template1');