#include "fmgr.h"

Go to the source code of this file.
Functions | |
| Datum | dblink_connect (PG_FUNCTION_ARGS) |
| Datum | dblink_disconnect (PG_FUNCTION_ARGS) |
| Datum | dblink_open (PG_FUNCTION_ARGS) |
| Datum | dblink_close (PG_FUNCTION_ARGS) |
| Datum | dblink_fetch (PG_FUNCTION_ARGS) |
| Datum | dblink_record (PG_FUNCTION_ARGS) |
| Datum | dblink_send_query (PG_FUNCTION_ARGS) |
| Datum | dblink_get_result (PG_FUNCTION_ARGS) |
| Datum | dblink_get_connections (PG_FUNCTION_ARGS) |
| Datum | dblink_is_busy (PG_FUNCTION_ARGS) |
| Datum | dblink_cancel_query (PG_FUNCTION_ARGS) |
| Datum | dblink_error_message (PG_FUNCTION_ARGS) |
| Datum | dblink_exec (PG_FUNCTION_ARGS) |
| Datum | dblink_get_pkey (PG_FUNCTION_ARGS) |
| Datum | dblink_build_sql_insert (PG_FUNCTION_ARGS) |
| Datum | dblink_build_sql_delete (PG_FUNCTION_ARGS) |
| Datum | dblink_build_sql_update (PG_FUNCTION_ARGS) |
| Datum | dblink_current_query (PG_FUNCTION_ARGS) |
| Datum | dblink_get_notify (PG_FUNCTION_ARGS) |
| Datum | dblink_fdw_validator (PG_FUNCTION_ARGS) |
| Datum dblink_build_sql_delete | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1698 of file dblink.c.
References AccessShareLock, ACL_SELECT, cstring_to_text(), ereport, errcode(), errmsg(), ERROR, get_rel_from_relname(), get_sql_delete(), get_text_array_contents(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_INT32, PG_GETARG_POINTER, PG_GETARG_TEXT_P, PG_RETURN_TEXT_P, relation_close(), and validate_pkattnums().
{
text *relname_text = PG_GETARG_TEXT_P(0);
int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
int32 pknumatts_arg = PG_GETARG_INT32(2);
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
Relation rel;
int *pkattnums;
int pknumatts;
char **tgt_pkattvals;
int tgt_nitems;
char *sql;
/*
* Open target relation.
*/
rel = get_rel_from_relname(relname_text, AccessShareLock, ACL_SELECT);
/*
* Process pkattnums argument.
*/
validate_pkattnums(rel, pkattnums_arg, pknumatts_arg,
&pkattnums, &pknumatts);
/*
* Target array is made up of key values that will be used to build the
* SQL string for use on the remote system.
*/
tgt_pkattvals = get_text_array_contents(tgt_pkattvals_arry, &tgt_nitems);
/*
* There should be one target array key value for each key attnum
*/
if (tgt_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("target key array length must match number of key " \
"attributes")));
/*
* Prep work is finally done. Go get the SQL string.
*/
sql = get_sql_delete(rel, pkattnums, pknumatts, tgt_pkattvals);
/*
* Now we can close the relation.
*/
relation_close(rel, AccessShareLock);
/*
* And send it
*/
PG_RETURN_TEXT_P(cstring_to_text(sql));
}
| Datum dblink_build_sql_insert | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1607 of file dblink.c.
References AccessShareLock, ACL_SELECT, cstring_to_text(), ereport, errcode(), errmsg(), ERROR, get_rel_from_relname(), get_sql_insert(), get_text_array_contents(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_INT32, PG_GETARG_POINTER, PG_GETARG_TEXT_P, PG_RETURN_TEXT_P, relation_close(), and validate_pkattnums().
{
text *relname_text = PG_GETARG_TEXT_P(0);
int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
int32 pknumatts_arg = PG_GETARG_INT32(2);
ArrayType *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
Relation rel;
int *pkattnums;
int pknumatts;
char **src_pkattvals;
char **tgt_pkattvals;
int src_nitems;
int tgt_nitems;
char *sql;
/*
* Open target relation.
*/
rel = get_rel_from_relname(relname_text, AccessShareLock, ACL_SELECT);
/*
* Process pkattnums argument.
*/
validate_pkattnums(rel, pkattnums_arg, pknumatts_arg,
&pkattnums, &pknumatts);
/*
* Source array is made up of key values that will be used to locate the
* tuple of interest from the local system.
*/
src_pkattvals = get_text_array_contents(src_pkattvals_arry, &src_nitems);
/*
* There should be one source array key value for each key attnum
*/
if (src_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("source key array length must match number of key " \
"attributes")));
/*
* Target array is made up of key values that will be used to build the
* SQL string for use on the remote system.
*/
tgt_pkattvals = get_text_array_contents(tgt_pkattvals_arry, &tgt_nitems);
/*
* There should be one target array key value for each key attnum
*/
if (tgt_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("target key array length must match number of key " \
"attributes")));
/*
* Prep work is finally done. Go get the SQL string.
*/
sql = get_sql_insert(rel, pkattnums, pknumatts, src_pkattvals, tgt_pkattvals);
/*
* Now we can close the relation.
*/
relation_close(rel, AccessShareLock);
/*
* And send it
*/
PG_RETURN_TEXT_P(cstring_to_text(sql));
}
| Datum dblink_build_sql_update | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1775 of file dblink.c.
References AccessShareLock, ACL_SELECT, cstring_to_text(), ereport, errcode(), errmsg(), ERROR, get_rel_from_relname(), get_sql_update(), get_text_array_contents(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_INT32, PG_GETARG_POINTER, PG_GETARG_TEXT_P, PG_RETURN_TEXT_P, relation_close(), and validate_pkattnums().
{
text *relname_text = PG_GETARG_TEXT_P(0);
int2vector *pkattnums_arg = (int2vector *) PG_GETARG_POINTER(1);
int32 pknumatts_arg = PG_GETARG_INT32(2);
ArrayType *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
Relation rel;
int *pkattnums;
int pknumatts;
char **src_pkattvals;
char **tgt_pkattvals;
int src_nitems;
int tgt_nitems;
char *sql;
/*
* Open target relation.
*/
rel = get_rel_from_relname(relname_text, AccessShareLock, ACL_SELECT);
/*
* Process pkattnums argument.
*/
validate_pkattnums(rel, pkattnums_arg, pknumatts_arg,
&pkattnums, &pknumatts);
/*
* Source array is made up of key values that will be used to locate the
* tuple of interest from the local system.
*/
src_pkattvals = get_text_array_contents(src_pkattvals_arry, &src_nitems);
/*
* There should be one source array key value for each key attnum
*/
if (src_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("source key array length must match number of key " \
"attributes")));
/*
* Target array is made up of key values that will be used to build the
* SQL string for use on the remote system.
*/
tgt_pkattvals = get_text_array_contents(tgt_pkattvals_arry, &tgt_nitems);
/*
* There should be one target array key value for each key attnum
*/
if (tgt_nitems != pknumatts)
ereport(ERROR,
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
errmsg("target key array length must match number of key " \
"attributes")));
/*
* Prep work is finally done. Go get the SQL string.
*/
sql = get_sql_update(rel, pkattnums, pknumatts, src_pkattvals, tgt_pkattvals);
/*
* Now we can close the relation.
*/
relation_close(rel, AccessShareLock);
/*
* And send it
*/
PG_RETURN_TEXT_P(cstring_to_text(sql));
}
| Datum dblink_cancel_query | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1315 of file dblink.c.
References conn, cstring_to_text(), PG_RETURN_TEXT_P, PQcancel(), PQfreeCancel(), and PQgetCancel().
{
int res = 0;
char *conname = NULL;
PGconn *conn = NULL;
remoteConn *rconn = NULL;
PGcancel *cancel;
char errbuf[256];
DBLINK_INIT;
DBLINK_GET_NAMED_CONN;
cancel = PQgetCancel(conn);
res = PQcancel(cancel, errbuf, 256);
PQfreeCancel(cancel);
if (res == 1)
PG_RETURN_TEXT_P(cstring_to_text("OK"));
else
PG_RETURN_TEXT_P(cstring_to_text(errbuf));
}
| Datum dblink_close | ( | PG_FUNCTION_ARGS | ) |
Definition at line 441 of file dblink.c.
References appendStringInfo(), BOOLOID, buf, remoteConn::conn, conn, cstring_to_text(), StringInfoData::data, dblink_res_error(), DBLINK_RES_INTERNALERROR, get_fn_expr_argtype(), getConnectionByName(), initStringInfo(), remoteConn::newXactForCursor, remoteConn::openCursorCount, PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_TEXT_P, PGRES_COMMAND_OK, PQclear(), PQexec(), PQresultStatus(), and text_to_cstring().
{
PGconn *conn = NULL;
PGresult *res = NULL;
char *curname = NULL;
char *conname = NULL;
StringInfoData buf;
char *msg;
remoteConn *rconn = NULL;
bool fail = true; /* default to backward compatible behavior */
DBLINK_INIT;
initStringInfo(&buf);
if (PG_NARGS() == 1)
{
/* text */
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
rconn = pconn;
}
else if (PG_NARGS() == 2)
{
/* might be text,text or text,bool */
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
{
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
fail = PG_GETARG_BOOL(1);
rconn = pconn;
}
else
{
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
rconn = getConnectionByName(conname);
}
}
if (PG_NARGS() == 3)
{
/* text,text,bool */
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
fail = PG_GETARG_BOOL(2);
rconn = getConnectionByName(conname);
}
if (!rconn || !rconn->conn)
DBLINK_CONN_NOT_AVAIL;
else
conn = rconn->conn;
appendStringInfo(&buf, "CLOSE %s", curname);
/* close the cursor */
res = PQexec(conn, buf.data);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
dblink_res_error(conname, res, "could not close cursor", fail);
PG_RETURN_TEXT_P(cstring_to_text("ERROR"));
}
PQclear(res);
/* if we started a transaction, decrement cursor count */
if (rconn->newXactForCursor)
{
(rconn->openCursorCount)--;
/* if count is zero, commit the transaction */
if (rconn->openCursorCount == 0)
{
rconn->newXactForCursor = FALSE;
res = PQexec(conn, "COMMIT");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
DBLINK_RES_INTERNALERROR("commit error");
PQclear(res);
}
}
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}
| Datum dblink_connect | ( | PG_FUNCTION_ARGS | ) |
Definition at line 243 of file dblink.c.
References remoteConn::conn, conn, CONNECTION_BAD, connstr, createNewConnection(), cstring_to_text(), dblink_connstr_check(), dblink_security_check(), ereport, errcode(), errdetail_internal(), errmsg(), ERROR, get_connect_string(), GetDatabaseEncodingName(), MemoryContextAlloc(), NULL, pfree(), PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_TEXT_P, PQconnectdb(), PQerrorMessage(), PQfinish(), PQsetClientEncoding(), PQstatus(), pstrdup(), text_to_cstring(), and TopMemoryContext.
{
char *conname_or_str = NULL;
char *connstr = NULL;
char *connname = NULL;
char *msg;
PGconn *conn = NULL;
remoteConn *rconn = NULL;
DBLINK_INIT;
if (PG_NARGS() == 2)
{
conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(1));
connname = text_to_cstring(PG_GETARG_TEXT_PP(0));
}
else if (PG_NARGS() == 1)
conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0));
if (connname)
rconn = (remoteConn *) MemoryContextAlloc(TopMemoryContext,
sizeof(remoteConn));
/* first check for valid foreign data server */
connstr = get_connect_string(conname_or_str);
if (connstr == NULL)
connstr = conname_or_str;
/* check password in connection string if not superuser */
dblink_connstr_check(connstr);
conn = PQconnectdb(connstr);
if (PQstatus(conn) == CONNECTION_BAD)
{
msg = pstrdup(PQerrorMessage(conn));
PQfinish(conn);
if (rconn)
pfree(rconn);
ereport(ERROR,
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
errmsg("could not establish connection"),
errdetail_internal("%s", msg)));
}
/* check password actually used if not superuser */
dblink_security_check(conn, rconn);
/* attempt to set client encoding to match server encoding */
PQsetClientEncoding(conn, GetDatabaseEncodingName());
if (connname)
{
rconn->conn = conn;
createNewConnection(connname, rconn);
}
else
pconn->conn = conn;
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}
| Datum dblink_current_query | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1856 of file dblink.c.
References current_query(), and PG_RETURN_DATUM.
{
/* This is now just an alias for the built-in function current_query() */
PG_RETURN_DATUM(current_query(fcinfo));
}
| Datum dblink_disconnect | ( | PG_FUNCTION_ARGS | ) |
Definition at line 310 of file dblink.c.
References remoteConn::conn, conn, cstring_to_text(), deleteConnection(), getConnectionByName(), pfree(), PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_TEXT_P, PQfinish(), and text_to_cstring().
{
char *conname = NULL;
remoteConn *rconn = NULL;
PGconn *conn = NULL;
DBLINK_INIT;
if (PG_NARGS() == 1)
{
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
rconn = getConnectionByName(conname);
if (rconn)
conn = rconn->conn;
}
else
conn = pconn->conn;
if (!conn)
DBLINK_CONN_NOT_AVAIL;
PQfinish(conn);
if (rconn)
{
deleteConnection(conname);
pfree(rconn);
}
else
pconn->conn = NULL;
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}
| Datum dblink_error_message | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1350 of file dblink.c.
References conn, cstring_to_text(), NULL, PG_RETURN_TEXT_P, and PQerrorMessage().
{
char *msg;
char *conname = NULL;
PGconn *conn = NULL;
remoteConn *rconn = NULL;
DBLINK_INIT;
DBLINK_GET_NAMED_CONN;
msg = PQerrorMessage(conn);
if (msg == NULL || msg[0] == '\0')
PG_RETURN_TEXT_P(cstring_to_text("OK"));
else
PG_RETURN_TEXT_P(cstring_to_text(msg));
}
| Datum dblink_exec | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1372 of file dblink.c.
References BOOLOID, remoteConn::conn, conn, connstr, cstring_to_text(), dblink_res_error(), elog, ereport, errcode(), errmsg(), ERROR, get_fn_expr_argtype(), PG_CATCH, PG_END_TRY, PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_NARGS, PG_RE_THROW, PG_RETURN_TEXT_P, PG_TRY, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear(), PQcmdStatus(), PQexec(), PQfinish(), PQresultStatus(), and text_to_cstring().
{
text *volatile sql_cmd_status = NULL;
PGconn *volatile conn = NULL;
volatile bool freeconn = false;
DBLINK_INIT;
PG_TRY();
{
char *msg;
PGresult *res = NULL;
char *connstr = NULL;
char *sql = NULL;
char *conname = NULL;
remoteConn *rconn = NULL;
bool fail = true; /* default to backward compatible behavior */
if (PG_NARGS() == 3)
{
/* must be text,text,bool */
DBLINK_GET_CONN;
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
fail = PG_GETARG_BOOL(2);
}
else if (PG_NARGS() == 2)
{
/* might be text,text or text,bool */
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
{
conn = pconn->conn;
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
fail = PG_GETARG_BOOL(1);
}
else
{
DBLINK_GET_CONN;
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
}
}
else if (PG_NARGS() == 1)
{
/* must be single text argument */
conn = pconn->conn;
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
}
else
/* shouldn't happen */
elog(ERROR, "wrong number of arguments");
if (!conn)
DBLINK_CONN_NOT_AVAIL;
res = PQexec(conn, sql);
if (!res ||
(PQresultStatus(res) != PGRES_COMMAND_OK &&
PQresultStatus(res) != PGRES_TUPLES_OK))
{
dblink_res_error(conname, res, "could not execute command", fail);
/*
* and save a copy of the command status string to return as our
* result tuple
*/
sql_cmd_status = cstring_to_text("ERROR");
}
else if (PQresultStatus(res) == PGRES_COMMAND_OK)
{
/*
* and save a copy of the command status string to return as our
* result tuple
*/
sql_cmd_status = cstring_to_text(PQcmdStatus(res));
PQclear(res);
}
else
{
PQclear(res);
ereport(ERROR,
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
errmsg("statement returning results not allowed")));
}
}
PG_CATCH();
{
/* if needed, close the connection to the database */
if (freeconn)
PQfinish(conn);
PG_RE_THROW();
}
PG_END_TRY();
/* if needed, close the connection to the database */
if (freeconn)
PQfinish(conn);
PG_RETURN_TEXT_P(sql_cmd_status);
}
| Datum dblink_fdw_validator | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1954 of file dblink.c.
References appendStringInfo(), buf, StringInfoData::data, DefElem::defname, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, initStringInfo(), is_valid_dblink_option(), _PQconninfoOption::keyword, StringInfoData::len, lfirst, PG_GETARG_DATUM, PG_GETARG_OID, PG_RETURN_VOID, PQconndefaults(), and untransformRelOptions().
{
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
Oid context = PG_GETARG_OID(1);
ListCell *cell;
static const PQconninfoOption *options = NULL;
/*
* Get list of valid libpq options.
*
* To avoid unnecessary work, we get the list once and use it throughout
* the lifetime of this backend process. We don't need to care about
* memory context issues, because PQconndefaults allocates with malloc.
*/
if (!options)
{
options = PQconndefaults();
if (!options) /* assume reason for failure is OOM */
ereport(ERROR,
(errcode(ERRCODE_FDW_OUT_OF_MEMORY),
errmsg("out of memory"),
errdetail("could not get libpq's default connection options")));
}
/* Validate each supplied option. */
foreach(cell, options_list)
{
DefElem *def = (DefElem *) lfirst(cell);
if (!is_valid_dblink_option(options, def->defname, context))
{
/*
* Unknown option, or invalid option for the context specified,
* so complain about it. Provide a hint with list of valid
* options for the context.
*/
StringInfoData buf;
const PQconninfoOption *opt;
initStringInfo(&buf);
for (opt = options; opt->keyword; opt++)
{
if (is_valid_dblink_option(options, opt->keyword, context))
appendStringInfo(&buf, "%s%s",
(buf.len > 0) ? ", " : "",
opt->keyword);
}
ereport(ERROR,
(errcode(ERRCODE_FDW_OPTION_NAME_NOT_FOUND),
errmsg("invalid option \"%s\"", def->defname),
errhint("Valid options in this context are: %s",
buf.data)));
}
}
PG_RETURN_VOID();
}
| Datum dblink_fetch | ( | PG_FUNCTION_ARGS | ) |
Definition at line 528 of file dblink.c.
References appendStringInfo(), BOOLOID, buf, remoteConn::conn, conn, StringInfoData::data, dblink_res_error(), ereport, errcode(), errmsg(), ERROR, get_fn_expr_argtype(), getConnectionByName(), initStringInfo(), materializeResult(), PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_NARGS, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear(), PQexec(), PQresultStatus(), prepTuplestoreResult(), and text_to_cstring().
{
PGresult *res = NULL;
char *conname = NULL;
remoteConn *rconn = NULL;
PGconn *conn = NULL;
StringInfoData buf;
char *curname = NULL;
int howmany = 0;
bool fail = true; /* default to backward compatible */
prepTuplestoreResult(fcinfo);
DBLINK_INIT;
if (PG_NARGS() == 4)
{
/* text,text,int,bool */
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
howmany = PG_GETARG_INT32(2);
fail = PG_GETARG_BOOL(3);
rconn = getConnectionByName(conname);
if (rconn)
conn = rconn->conn;
}
else if (PG_NARGS() == 3)
{
/* text,text,int or text,int,bool */
if (get_fn_expr_argtype(fcinfo->flinfo, 2) == BOOLOID)
{
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
howmany = PG_GETARG_INT32(1);
fail = PG_GETARG_BOOL(2);
conn = pconn->conn;
}
else
{
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
howmany = PG_GETARG_INT32(2);
rconn = getConnectionByName(conname);
if (rconn)
conn = rconn->conn;
}
}
else if (PG_NARGS() == 2)
{
/* text,int */
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
howmany = PG_GETARG_INT32(1);
conn = pconn->conn;
}
if (!conn)
DBLINK_CONN_NOT_AVAIL;
initStringInfo(&buf);
appendStringInfo(&buf, "FETCH %d FROM %s", howmany, curname);
/*
* Try to execute the query. Note that since libpq uses malloc, the
* PGresult will be long-lived even though we are still in a short-lived
* memory context.
*/
res = PQexec(conn, buf.data);
if (!res ||
(PQresultStatus(res) != PGRES_COMMAND_OK &&
PQresultStatus(res) != PGRES_TUPLES_OK))
{
dblink_res_error(conname, res, "could not fetch from cursor", fail);
return (Datum) 0;
}
else if (PQresultStatus(res) == PGRES_COMMAND_OK)
{
/* cursor does not exist - closed already or bad name */
PQclear(res);
ereport(ERROR,
(errcode(ERRCODE_INVALID_CURSOR_NAME),
errmsg("cursor \"%s\" does not exist", curname)));
}
materializeResult(fcinfo, conn, res);
return (Datum) 0;
}
| Datum dblink_get_connections | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1254 of file dblink.c.
References accumArrayResult(), CStringGetTextDatum, CurrentMemoryContext, hash_seq_init(), hash_seq_search(), makeArrayResult(), remoteConnHashEnt::name, NULL, PG_RETURN_ARRAYTYPE_P, PG_RETURN_NULL, and TEXTOID.
{
HASH_SEQ_STATUS status;
remoteConnHashEnt *hentry;
ArrayBuildState *astate = NULL;
if (remoteConnHash)
{
hash_seq_init(&status, remoteConnHash);
while ((hentry = (remoteConnHashEnt *) hash_seq_search(&status)) != NULL)
{
/* stash away current value */
astate = accumArrayResult(astate,
CStringGetTextDatum(hentry->name),
false, TEXTOID, CurrentMemoryContext);
}
}
if (astate)
PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate,
CurrentMemoryContext));
else
PG_RETURN_NULL();
}
| Datum dblink_get_notify | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1874 of file dblink.c.
References pgNotify::be_pid, remoteConn::conn, conn, CreateTemplateTupleDesc(), CStringGetTextDatum, DBLINK_GET_NAMED_CONN, DBLINK_NOTIFY_COLS, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, pgNotify::extra, Int32GetDatum, INT4OID, MemoryContextSwitchTo(), NULL, PG_NARGS, PQconsumeInput(), PQfreemem(), PQnotifies(), prepTuplestoreResult(), pgNotify::relname, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, TEXTOID, TupleDescInitEntry(), tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), values, and work_mem.
{
char *conname = NULL;
PGconn *conn = NULL;
remoteConn *rconn = NULL;
PGnotify *notify;
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
TupleDesc tupdesc;
Tuplestorestate *tupstore;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
prepTuplestoreResult(fcinfo);
DBLINK_INIT;
if (PG_NARGS() == 1)
DBLINK_GET_NAMED_CONN;
else
conn = pconn->conn;
/* create the tuplestore in per-query memory */
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);
tupdesc = CreateTemplateTupleDesc(DBLINK_NOTIFY_COLS, false);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "notify_name",
TEXTOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "be_pid",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "extra",
TEXTOID, -1, 0);
tupstore = tuplestore_begin_heap(true, false, work_mem);
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
MemoryContextSwitchTo(oldcontext);
PQconsumeInput(conn);
while ((notify = PQnotifies(conn)) != NULL)
{
Datum values[DBLINK_NOTIFY_COLS];
bool nulls[DBLINK_NOTIFY_COLS];
memset(values, 0, sizeof(values));
memset(nulls, 0, sizeof(nulls));
if (notify->relname != NULL)
values[0] = CStringGetTextDatum(notify->relname);
else
nulls[0] = true;
values[1] = Int32GetDatum(notify->be_pid);
if (notify->extra != NULL)
values[2] = CStringGetTextDatum(notify->extra);
else
nulls[2] = true;
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
PQfreemem(notify);
PQconsumeInput(conn);
}
/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);
return (Datum) 0;
}
| Datum dblink_get_pkey | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1480 of file dblink.c.
References AccessShareLock, ACL_SELECT, FuncCallContext::attinmeta, BuildTupleFromCStrings(), FuncCallContext::call_cntr, CreateTemplateTupleDesc(), get_pkey_attnames(), get_rel_from_relname(), HeapTupleGetDatum, INT4OID, FuncCallContext::max_calls, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, NULL, palloc(), PG_GETARG_TEXT_P, relation_close(), SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, TEXTOID, TupleDescGetAttInMetadata(), TupleDescInitEntry(), FuncCallContext::user_fctx, and values.
{
int16 numatts;
char **results;
FuncCallContext *funcctx;
int32 call_cntr;
int32 max_calls;
AttInMetadata *attinmeta;
MemoryContext oldcontext;
/* stuff done only on the first call of the function */
if (SRF_IS_FIRSTCALL())
{
Relation rel;
TupleDesc tupdesc;
/* create a function context for cross-call persistence */
funcctx = SRF_FIRSTCALL_INIT();
/*
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
/* open target relation */
rel = get_rel_from_relname(PG_GETARG_TEXT_P(0), AccessShareLock, ACL_SELECT);
/* get the array of attnums */
results = get_pkey_attnames(rel, &numatts);
relation_close(rel, AccessShareLock);
/*
* need a tuple descriptor representing one INT and one TEXT column
*/
tupdesc = CreateTemplateTupleDesc(2, false);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "position",
INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "colname",
TEXTOID, -1, 0);
/*
* Generate attribute metadata needed later to produce tuples from raw
* C strings
*/
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
if ((results != NULL) && (numatts > 0))
{
funcctx->max_calls = numatts;
/* got results, keep track of them */
funcctx->user_fctx = results;
}
else
{
/* fast track when no results */
MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
MemoryContextSwitchTo(oldcontext);
}
/* stuff done on every call of the function */
funcctx = SRF_PERCALL_SETUP();
/*
* initialize per-call variables
*/
call_cntr = funcctx->call_cntr;
max_calls = funcctx->max_calls;
results = (char **) funcctx->user_fctx;
attinmeta = funcctx->attinmeta;
if (call_cntr < max_calls) /* do when there is more left to send */
{
char **values;
HeapTuple tuple;
Datum result;
values = (char **) palloc(2 * sizeof(char *));
values[0] = (char *) palloc(12); /* sign, 10 digits, '\0' */
sprintf(values[0], "%d", call_cntr + 1);
values[1] = results[call_cntr];
/* build the tuple */
tuple = BuildTupleFromCStrings(attinmeta, values);
/* make the tuple into a datum */
result = HeapTupleGetDatum(tuple);
SRF_RETURN_NEXT(funcctx, result);
}
else
{
/* do when there is no more left */
SRF_RETURN_DONE(funcctx);
}
}
| Datum dblink_get_result | ( | PG_FUNCTION_ARGS | ) |
Definition at line 655 of file dblink.c.
References dblink_record_internal().
{
return dblink_record_internal(fcinfo, true);
}
| Datum dblink_is_busy | ( | PG_FUNCTION_ARGS | ) |
Definition at line 1289 of file dblink.c.
References conn, PG_RETURN_INT32, PQconsumeInput(), and PQisBusy().
{
char *conname = NULL;
PGconn *conn = NULL;
remoteConn *rconn = NULL;
DBLINK_INIT;
DBLINK_GET_NAMED_CONN;
PQconsumeInput(conn);
PG_RETURN_INT32(PQisBusy(conn));
}
| Datum dblink_open | ( | PG_FUNCTION_ARGS | ) |
Definition at line 348 of file dblink.c.
References appendStringInfo(), BOOLOID, buf, remoteConn::conn, conn, cstring_to_text(), StringInfoData::data, dblink_res_error(), DBLINK_RES_INTERNALERROR, get_fn_expr_argtype(), getConnectionByName(), initStringInfo(), remoteConn::newXactForCursor, remoteConn::openCursorCount, PG_GETARG_BOOL, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_TEXT_P, PGRES_COMMAND_OK, PQclear(), PQexec(), PQresultStatus(), PQTRANS_IDLE, PQtransactionStatus(), and text_to_cstring().
{
char *msg;
PGresult *res = NULL;
PGconn *conn = NULL;
char *curname = NULL;
char *sql = NULL;
char *conname = NULL;
StringInfoData buf;
remoteConn *rconn = NULL;
bool fail = true; /* default to backward compatible behavior */
DBLINK_INIT;
initStringInfo(&buf);
if (PG_NARGS() == 2)
{
/* text,text */
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
rconn = pconn;
}
else if (PG_NARGS() == 3)
{
/* might be text,text,text or text,text,bool */
if (get_fn_expr_argtype(fcinfo->flinfo, 2) == BOOLOID)
{
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
fail = PG_GETARG_BOOL(2);
rconn = pconn;
}
else
{
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(2));
rconn = getConnectionByName(conname);
}
}
else if (PG_NARGS() == 4)
{
/* text,text,text,bool */
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(2));
fail = PG_GETARG_BOOL(3);
rconn = getConnectionByName(conname);
}
if (!rconn || !rconn->conn)
DBLINK_CONN_NOT_AVAIL;
else
conn = rconn->conn;
/* If we are not in a transaction, start one */
if (PQtransactionStatus(conn) == PQTRANS_IDLE)
{
res = PQexec(conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
DBLINK_RES_INTERNALERROR("begin error");
PQclear(res);
rconn->newXactForCursor = TRUE;
/*
* Since transaction state was IDLE, we force cursor count to
* initially be 0. This is needed as a previous ABORT might have wiped
* out our transaction without maintaining the cursor count for us.
*/
rconn->openCursorCount = 0;
}
/* if we started a transaction, increment cursor count */
if (rconn->newXactForCursor)
(rconn->openCursorCount)++;
appendStringInfo(&buf, "DECLARE %s CURSOR FOR %s", curname, sql);
res = PQexec(conn, buf.data);
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
{
dblink_res_error(conname, res, "could not open cursor", fail);
PG_RETURN_TEXT_P(cstring_to_text("ERROR"));
}
PQclear(res);
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}
| Datum dblink_record | ( | PG_FUNCTION_ARGS | ) |
Definition at line 621 of file dblink.c.
References dblink_record_internal().
{
return dblink_record_internal(fcinfo, false);
}
| Datum dblink_send_query | ( | PG_FUNCTION_ARGS | ) |
Definition at line 628 of file dblink.c.
References conn, elog, ERROR, NOTICE, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_INT32, PQerrorMessage(), PQsendQuery(), and text_to_cstring().
{
char *conname = NULL;
PGconn *conn = NULL;
char *sql = NULL;
remoteConn *rconn = NULL;
int retval;
if (PG_NARGS() == 2)
{
DBLINK_GET_NAMED_CONN;
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
}
else
/* shouldn't happen */
elog(ERROR, "wrong number of arguments");
/* async query send */
retval = PQsendQuery(conn, sql);
if (retval != 1)
elog(NOTICE, "could not send query: %s", PQerrorMessage(conn));
PG_RETURN_INT32(retval);
}
1.7.1