Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 10b5f0b

Browse files
committed
Here's a small patch for dblink:
- fixed dblink invalid pointer causing corrupt elog message - fixed dblink_tok improper handling of null results - fixed examples in README.dblink Joe Conway
1 parent ec96f1d commit 10b5f0b

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

contrib/dblink/README.dblink

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@
2424
*
2525
*/
2626

27-
Version 0.2 (29 May, 2001):
27+
28+
Version 0.3 (14 June, 2001):
2829
Function to test returning data set from remote database
2930
Tested under Linux (Red Hat 6.2 and 7.0) and PostgreSQL 7.1 and 7.2devel
3031

3132
Release Notes:
3233

34+
Version 0.3
35+
- fixed dblink invalid pointer causing corrupt elog message
36+
- fixed dblink_tok improper handling of null results
37+
- fixed examples in README.dblink
38+
3339
Version 0.2
3440
- initial release
3541

@@ -124,11 +130,11 @@ to 'fake' a UNION, e.g.
124130
'hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
125131
,'select f1, f2 from mytable'
126132
) as dblink_p
127-
union
128-
select null,null where false
133+
union all
134+
select null where false
129135
) as t1
130136
where
131-
f1 = 'mycriteria';
137+
dblink_tok(t1.dblink_p,0) = 'mycriteria';
132138

133139
in order to work around an issue with the query optimizer. A more convenient way to approach
134140
this problem is to create a view:
@@ -143,8 +149,8 @@ this problem is to create a view:
143149
'hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
144150
,'select f1, f2 from mytable'
145151
) as dblink_p
146-
union
147-
select null,null where false
152+
union all
153+
select null where false
148154
) as t1;
149155

150156
Then you can simply write:

contrib/dblink/dblink.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ dblink(PG_FUNCTION_ARGS)
5757
conn = PQconnectdb(optstr);
5858
if (PQstatus(conn) == CONNECTION_BAD)
5959
{
60-
msg = PQerrorMessage(conn);
60+
msg = pstrdup(PQerrorMessage(conn));
6161
PQfinish(conn);
6262
elog(ERROR, "dblink: connection error: %s", msg);
6363
}
6464

6565
res = PQexec(conn, "BEGIN");
6666
if (PQresultStatus(res) != PGRES_COMMAND_OK)
6767
{
68-
msg = PQerrorMessage(conn);
68+
msg = pstrdup(PQerrorMessage(conn));
6969
PQclear(res);
7070
PQfinish(conn);
7171
elog(ERROR, "dblink: begin error: %s", msg);
@@ -84,7 +84,7 @@ dblink(PG_FUNCTION_ARGS)
8484
res = PQexec(conn, execstatement);
8585
if (!res || (PQresultStatus(res) != PGRES_COMMAND_OK && PQresultStatus(res) != PGRES_TUPLES_OK))
8686
{
87-
msg = PQerrorMessage(conn);
87+
msg = pstrdup(PQerrorMessage(conn));
8888
PQclear(res);
8989
PQfinish(conn);
9090
elog(ERROR, "dblink: sql error: %s", msg);
@@ -96,7 +96,7 @@ dblink(PG_FUNCTION_ARGS)
9696

9797
res = PQexec(conn, "FETCH ALL in mycursor");
9898
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
99-
msg = PQerrorMessage(conn);
99+
msg = pstrdup(PQerrorMessage(conn));
100100
PQclear(res);
101101
PQfinish(conn);
102102
elog(ERROR, "dblink: sql error: %s", msg);
@@ -230,20 +230,28 @@ dblink_tok(PG_FUNCTION_ARGS)
230230
elog(ERROR, "dblink: field number %d does not exist", fldnum);
231231
}
232232

233-
text_len = PQgetlength(results->res, results->tup_num, fldnum);
233+
if (PQgetisnull(results->res, results->tup_num, fldnum) == 1) {
234234

235-
result = (char *) palloc(text_len + 1);
235+
PG_RETURN_NULL();
236236

237-
if (result != NULL) {
238-
strcpy(result, PQgetvalue(results->res, results->tup_num, fldnum));
239-
strcat(result, "\0");
240237
} else {
241-
elog(ERROR, "dblink: insufficient memory" );
242-
}
243238

244-
result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(result)));
239+
text_len = PQgetlength(results->res, results->tup_num, fldnum);
240+
241+
result = (char *) palloc(text_len + 1);
245242

246-
PG_RETURN_TEXT_P(result_text);
243+
if (result != NULL) {
244+
strcpy(result, PQgetvalue(results->res, results->tup_num, fldnum));
245+
strcat(result, "\0");
246+
} else {
247+
elog(ERROR, "dblink: insufficient memory" );
248+
}
249+
250+
result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(result)));
251+
252+
PG_RETURN_TEXT_P(result_text);
253+
254+
}
247255
}
248256

249257

0 commit comments

Comments
 (0)