mirror of https://github.com/postgres/postgres
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
527 lines
14 KiB
527 lines
14 KiB
|
11 years ago
|
/*-------------------------------------------------------------------------
|
||
|
|
*
|
||
|
5 years ago
|
* libpq_source.c
|
||
|
|
* Functions for fetching files from a remote server via libpq.
|
||
|
11 years ago
|
*
|
||
|
6 years ago
|
* Copyright (c) 2013-2020, PostgreSQL Global Development Group
|
||
|
11 years ago
|
*
|
||
|
|
*-------------------------------------------------------------------------
|
||
|
|
*/
|
||
|
|
#include "postgres_fe.h"
|
||
|
|
|
||
|
6 years ago
|
#include "catalog/pg_type_d.h"
|
||
|
5 years ago
|
#include "common/connect.h"
|
||
|
11 years ago
|
#include "datapagemap.h"
|
||
|
|
#include "file_ops.h"
|
||
|
|
#include "filemap.h"
|
||
|
6 years ago
|
#include "pg_rewind.h"
|
||
|
8 years ago
|
#include "port/pg_bswap.h"
|
||
|
5 years ago
|
#include "rewind_source.h"
|
||
|
11 years ago
|
|
||
|
|
/*
|
||
|
|
* Files are fetched max CHUNKSIZE bytes at a time.
|
||
|
|
*
|
||
|
|
* (This only applies to files that are copied in whole, or for truncated
|
||
|
|
* files where we copy the tail. Relation files, where we know the individual
|
||
|
|
* blocks that need to be fetched, are fetched in BLCKSZ chunks.)
|
||
|
|
*/
|
||
|
|
#define CHUNKSIZE 1000000
|
||
|
|
|
||
|
5 years ago
|
typedef struct
|
||
|
|
{
|
||
|
|
rewind_source common; /* common interface functions */
|
||
|
|
|
||
|
|
PGconn *conn;
|
||
|
|
bool copy_started;
|
||
|
|
} libpq_source;
|
||
|
|
|
||
|
|
static void init_libpq_conn(PGconn *conn);
|
||
|
|
static char *run_simple_query(PGconn *conn, const char *sql);
|
||
|
|
static void run_simple_command(PGconn *conn, const char *sql);
|
||
|
|
|
||
|
|
/* public interface functions */
|
||
|
|
static void libpq_traverse_files(rewind_source *source,
|
||
|
|
process_file_callback_t callback);
|
||
|
|
static void libpq_queue_fetch_range(rewind_source *source, const char *path,
|
||
|
|
off_t off, size_t len);
|
||
|
|
static void libpq_finish_fetch(rewind_source *source);
|
||
|
|
static char *libpq_fetch_file(rewind_source *source, const char *path,
|
||
|
|
size_t *filesize);
|
||
|
|
static XLogRecPtr libpq_get_current_wal_insert_lsn(rewind_source *source);
|
||
|
|
static void libpq_destroy(rewind_source *source);
|
||
|
11 years ago
|
|
||
|
5 years ago
|
/*
|
||
|
|
* Create a new libpq source.
|
||
|
|
*
|
||
|
|
* The caller has already established the connection, but should not try
|
||
|
|
* to use it while the source is active.
|
||
|
|
*/
|
||
|
|
rewind_source *
|
||
|
|
init_libpq_source(PGconn *conn)
|
||
|
11 years ago
|
{
|
||
|
5 years ago
|
libpq_source *src;
|
||
|
|
|
||
|
|
init_libpq_conn(conn);
|
||
|
|
|
||
|
|
src = pg_malloc0(sizeof(libpq_source));
|
||
|
|
|
||
|
|
src->common.traverse_files = libpq_traverse_files;
|
||
|
|
src->common.fetch_file = libpq_fetch_file;
|
||
|
|
src->common.queue_fetch_range = libpq_queue_fetch_range;
|
||
|
|
src->common.finish_fetch = libpq_finish_fetch;
|
||
|
|
src->common.get_current_wal_insert_lsn = libpq_get_current_wal_insert_lsn;
|
||
|
|
src->common.destroy = libpq_destroy;
|
||
|
11 years ago
|
|
||
|
5 years ago
|
src->conn = conn;
|
||
|
11 years ago
|
|
||
|
5 years ago
|
return &src->common;
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Initialize a libpq connection for use.
|
||
|
|
*/
|
||
|
|
static void
|
||
|
|
init_libpq_conn(PGconn *conn)
|
||
|
|
{
|
||
|
|
PGresult *res;
|
||
|
|
char *str;
|
||
|
11 years ago
|
|
||
|
6 years ago
|
/* disable all types of timeouts */
|
||
|
5 years ago
|
run_simple_command(conn, "SET statement_timeout = 0");
|
||
|
|
run_simple_command(conn, "SET lock_timeout = 0");
|
||
|
|
run_simple_command(conn, "SET idle_in_transaction_session_timeout = 0");
|
||
|
6 years ago
|
|
||
|
5 years ago
|
/* secure search_path */
|
||
|
8 years ago
|
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
|
||
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||
|
|
pg_fatal("could not clear search_path: %s",
|
||
|
|
PQresultErrorMessage(res));
|
||
|
|
PQclear(res);
|
||
|
|
|
||
|
11 years ago
|
/*
|
||
|
|
* Check that the server is not in hot standby mode. There is no
|
||
|
|
* fundamental reason that couldn't be made to work, but it doesn't
|
||
|
|
* currently because we use a temporary table. Better to check for it
|
||
|
|
* explicitly than error out, for a better error message.
|
||
|
|
*/
|
||
|
5 years ago
|
str = run_simple_query(conn, "SELECT pg_is_in_recovery()");
|
||
|
11 years ago
|
if (strcmp(str, "f") != 0)
|
||
|
7 years ago
|
pg_fatal("source server must not be in recovery mode");
|
||
|
11 years ago
|
pg_free(str);
|
||
|
|
|
||
|
|
/*
|
||
|
11 years ago
|
* Also check that full_page_writes is enabled. We can get torn pages if
|
||
|
11 years ago
|
* a page is modified while we read it with pg_read_binary_file(), and we
|
||
|
|
* rely on full page images to fix them.
|
||
|
|
*/
|
||
|
5 years ago
|
str = run_simple_query(conn, "SHOW full_page_writes");
|
||
|
11 years ago
|
if (strcmp(str, "on") != 0)
|
||
|
7 years ago
|
pg_fatal("full_page_writes must be enabled in the source server");
|
||
|
11 years ago
|
pg_free(str);
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
5 years ago
|
* Run a query that returns a single value.
|
||
|
|
*
|
||
|
11 years ago
|
* The result should be pg_free'd after use.
|
||
|
11 years ago
|
*/
|
||
|
|
static char *
|
||
|
5 years ago
|
run_simple_query(PGconn *conn, const char *sql)
|
||
|
11 years ago
|
{
|
||
|
|
PGresult *res;
|
||
|
|
char *result;
|
||
|
|
|
||
|
|
res = PQexec(conn, sql);
|
||
|
|
|
||
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||
|
6 years ago
|
pg_fatal("error running query (%s) on source server: %s",
|
||
|
11 years ago
|
sql, PQresultErrorMessage(res));
|
||
|
|
|
||
|
|
/* sanity check the result set */
|
||
|
|
if (PQnfields(res) != 1 || PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
|
||
|
7 years ago
|
pg_fatal("unexpected result set from query");
|
||
|
11 years ago
|
|
||
|
|
result = pg_strdup(PQgetvalue(res, 0, 0));
|
||
|
|
|
||
|
|
PQclear(res);
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
/*
|
||
|
5 years ago
|
* Run a command.
|
||
|
|
*
|
||
|
6 years ago
|
* In the event of a failure, exit immediately.
|
||
|
|
*/
|
||
|
|
static void
|
||
|
5 years ago
|
run_simple_command(PGconn *conn, const char *sql)
|
||
|
6 years ago
|
{
|
||
|
|
PGresult *res;
|
||
|
|
|
||
|
|
res = PQexec(conn, sql);
|
||
|
|
|
||
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||
|
|
pg_fatal("error running query (%s) in source server: %s",
|
||
|
|
sql, PQresultErrorMessage(res));
|
||
|
|
|
||
|
|
PQclear(res);
|
||
|
|
}
|
||
|
|
|
||
|
11 years ago
|
/*
|
||
|
5 years ago
|
* Call the pg_current_wal_insert_lsn() function in the remote system.
|
||
|
11 years ago
|
*/
|
||
|
5 years ago
|
static XLogRecPtr
|
||
|
|
libpq_get_current_wal_insert_lsn(rewind_source *source)
|
||
|
11 years ago
|
{
|
||
|
5 years ago
|
PGconn *conn = ((libpq_source *) source)->conn;
|
||
|
11 years ago
|
XLogRecPtr result;
|
||
|
|
uint32 hi;
|
||
|
|
uint32 lo;
|
||
|
|
char *val;
|
||
|
|
|
||
|
5 years ago
|
val = run_simple_query(conn, "SELECT pg_current_wal_insert_lsn()");
|
||
|
11 years ago
|
|
||
|
|
if (sscanf(val, "%X/%X", &hi, &lo) != 2)
|
||
|
7 years ago
|
pg_fatal("unrecognized result \"%s\" for current WAL insert location", val);
|
||
|
11 years ago
|
|
||
|
|
result = ((uint64) hi) << 32 | lo;
|
||
|
|
|
||
|
11 years ago
|
pg_free(val);
|
||
|
|
|
||
|
11 years ago
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Get a list of all files in the data directory.
|
||
|
|
*/
|
||
|
5 years ago
|
static void
|
||
|
|
libpq_traverse_files(rewind_source *source, process_file_callback_t callback)
|
||
|
11 years ago
|
{
|
||
|
5 years ago
|
PGconn *conn = ((libpq_source *) source)->conn;
|
||
|
11 years ago
|
PGresult *res;
|
||
|
|
const char *sql;
|
||
|
|
int i;
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Create a recursive directory listing of the whole data directory.
|
||
|
|
*
|
||
|
|
* The WITH RECURSIVE part does most of the work. The second part gets the
|
||
|
|
* targets of the symlinks in pg_tblspc directory.
|
||
|
|
*
|
||
|
|
* XXX: There is no backend function to get a symbolic link's target in
|
||
|
|
* general, so if the admin has put any custom symbolic links in the data
|
||
|
|
* directory, they won't be copied correctly.
|
||
|
|
*/
|
||
|
|
sql =
|
||
|
|
"WITH RECURSIVE files (path, filename, size, isdir) AS (\n"
|
||
|
|
" SELECT '' AS path, filename, size, isdir FROM\n"
|
||
|
11 years ago
|
" (SELECT pg_ls_dir('.', true, false) AS filename) AS fn,\n"
|
||
|
|
" pg_stat_file(fn.filename, true) AS this\n"
|
||
|
11 years ago
|
" UNION ALL\n"
|
||
|
|
" SELECT parent.path || parent.filename || '/' AS path,\n"
|
||
|
|
" fn, this.size, this.isdir\n"
|
||
|
|
" FROM files AS parent,\n"
|
||
|
11 years ago
|
" pg_ls_dir(parent.path || parent.filename, true, false) AS fn,\n"
|
||
|
|
" pg_stat_file(parent.path || parent.filename || '/' || fn, true) AS this\n"
|
||
|
11 years ago
|
" WHERE parent.isdir = 't'\n"
|
||
|
|
")\n"
|
||
|
|
"SELECT path || filename, size, isdir,\n"
|
||
|
|
" pg_tablespace_location(pg_tablespace.oid) AS link_target\n"
|
||
|
|
"FROM files\n"
|
||
|
|
"LEFT OUTER JOIN pg_tablespace ON files.path = 'pg_tblspc/'\n"
|
||
|
|
" AND oid::text = files.filename\n";
|
||
|
|
res = PQexec(conn, sql);
|
||
|
|
|
||
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||
|
11 years ago
|
pg_fatal("could not fetch file list: %s",
|
||
|
11 years ago
|
PQresultErrorMessage(res));
|
||
|
|
|
||
|
|
/* sanity check the result set */
|
||
|
|
if (PQnfields(res) != 4)
|
||
|
7 years ago
|
pg_fatal("unexpected result set while fetching file list");
|
||
|
11 years ago
|
|
||
|
|
/* Read result to local variables */
|
||
|
|
for (i = 0; i < PQntuples(res); i++)
|
||
|
|
{
|
||
|
6 years ago
|
char *path;
|
||
|
|
int64 filesize;
|
||
|
|
bool isdir;
|
||
|
|
char *link_target;
|
||
|
11 years ago
|
file_type_t type;
|
||
|
|
|
||
|
6 years ago
|
if (PQgetisnull(res, i, 1))
|
||
|
11 years ago
|
{
|
||
|
|
/*
|
||
|
|
* The file was removed from the server while the query was
|
||
|
|
* running. Ignore it.
|
||
|
|
*/
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
path = PQgetvalue(res, i, 0);
|
||
|
|
filesize = atol(PQgetvalue(res, i, 1));
|
||
|
|
isdir = (strcmp(PQgetvalue(res, i, 2), "t") == 0);
|
||
|
|
link_target = PQgetvalue(res, i, 3);
|
||
|
|
|
||
|
11 years ago
|
if (link_target[0])
|
||
|
|
type = FILE_TYPE_SYMLINK;
|
||
|
|
else if (isdir)
|
||
|
|
type = FILE_TYPE_DIRECTORY;
|
||
|
|
else
|
||
|
|
type = FILE_TYPE_REGULAR;
|
||
|
|
|
||
|
11 years ago
|
process_source_file(path, type, filesize, link_target);
|
||
|
11 years ago
|
}
|
||
|
11 years ago
|
PQclear(res);
|
||
|
11 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
/*
|
||
|
|
* Queue up a request to fetch a piece of a file from remote system.
|
||
|
11 years ago
|
*/
|
||
|
|
static void
|
||
|
5 years ago
|
libpq_queue_fetch_range(rewind_source *source, const char *path, off_t off,
|
||
|
|
size_t len)
|
||
|
11 years ago
|
{
|
||
|
5 years ago
|
libpq_source *src = (libpq_source *) source;
|
||
|
|
uint64 begin = off;
|
||
|
|
uint64 end = off + len;
|
||
|
|
|
||
|
|
/*
|
||
|
|
* On first call, create a temporary table, and start COPYing to it.
|
||
|
|
* We will load it with the list of blocks that we need to fetch.
|
||
|
|
*/
|
||
|
|
if (!src->copy_started)
|
||
|
|
{
|
||
|
|
PGresult *res;
|
||
|
|
|
||
|
|
run_simple_command(src->conn, "CREATE TEMPORARY TABLE fetchchunks(path text, begin int8, len int4)");
|
||
|
|
|
||
|
|
res = PQexec(src->conn, "COPY fetchchunks FROM STDIN");
|
||
|
|
if (PQresultStatus(res) != PGRES_COPY_IN)
|
||
|
|
pg_fatal("could not send file list: %s",
|
||
|
|
PQresultErrorMessage(res));
|
||
|
|
PQclear(res);
|
||
|
|
|
||
|
|
src->copy_started = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Write the file range to a temporary table in the server.
|
||
|
|
*
|
||
|
|
* The range is sent to the server as a COPY formatted line, to be inserted
|
||
|
|
* into the 'fetchchunks' temporary table. The libpq_finish_fetch() uses
|
||
|
|
* the temporary table to actually fetch the data.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/* Split the range into CHUNKSIZE chunks */
|
||
|
|
while (end - begin > 0)
|
||
|
|
{
|
||
|
|
char linebuf[MAXPGPATH + 23];
|
||
|
|
unsigned int len;
|
||
|
|
|
||
|
|
/* Fine as long as CHUNKSIZE is not bigger than UINT32_MAX */
|
||
|
|
if (end - begin > CHUNKSIZE)
|
||
|
|
len = CHUNKSIZE;
|
||
|
|
else
|
||
|
|
len = (unsigned int) (end - begin);
|
||
|
|
|
||
|
|
snprintf(linebuf, sizeof(linebuf), "%s\t" UINT64_FORMAT "\t%u\n", path, begin, len);
|
||
|
|
|
||
|
|
if (PQputCopyData(src->conn, linebuf, strlen(linebuf)) != 1)
|
||
|
|
pg_fatal("could not send COPY data: %s",
|
||
|
|
PQerrorMessage(src->conn));
|
||
|
|
|
||
|
|
begin += len;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Receive all the queued chunks and write them to the target data directory.
|
||
|
|
*/
|
||
|
|
static void
|
||
|
|
libpq_finish_fetch(rewind_source *source)
|
||
|
|
{
|
||
|
|
libpq_source *src = (libpq_source *) source;
|
||
|
11 years ago
|
PGresult *res;
|
||
|
5 years ago
|
const char *sql;
|
||
|
11 years ago
|
|
||
|
5 years ago
|
if (PQputCopyEnd(src->conn, NULL) != 1)
|
||
|
|
pg_fatal("could not send end-of-COPY: %s",
|
||
|
|
PQerrorMessage(src->conn));
|
||
|
|
|
||
|
|
while ((res = PQgetResult(src->conn)) != NULL)
|
||
|
|
{
|
||
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||
|
|
pg_fatal("unexpected result while sending file list: %s",
|
||
|
|
PQresultErrorMessage(res));
|
||
|
|
PQclear(res);
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* We've now copied the list of file ranges that we need to fetch to the
|
||
|
|
* temporary table. Now, actually fetch all of those ranges.
|
||
|
|
*/
|
||
|
|
sql =
|
||
|
|
"SELECT path, begin,\n"
|
||
|
|
" pg_read_binary_file(path, begin, len, true) AS chunk\n"
|
||
|
|
"FROM fetchchunks\n";
|
||
|
|
|
||
|
|
if (PQsendQueryParams(src->conn, sql, 0, NULL, NULL, NULL, NULL, 1) != 1)
|
||
|
|
pg_fatal("could not send query: %s", PQerrorMessage(src->conn));
|
||
|
11 years ago
|
|
||
|
7 years ago
|
pg_log_debug("getting file chunks");
|
||
|
11 years ago
|
|
||
|
5 years ago
|
if (PQsetSingleRowMode(src->conn) != 1)
|
||
|
7 years ago
|
pg_fatal("could not set libpq connection to single row mode");
|
||
|
11 years ago
|
|
||
|
5 years ago
|
/*----
|
||
|
|
* The result set is of format:
|
||
|
|
*
|
||
|
|
* path text -- path in the data directory, e.g "base/1/123"
|
||
|
|
* begin int8 -- offset within the file
|
||
|
|
* chunk bytea -- file content
|
||
|
|
*----
|
||
|
|
*/
|
||
|
|
while ((res = PQgetResult(src->conn)) != NULL)
|
||
|
11 years ago
|
{
|
||
|
|
char *filename;
|
||
|
|
int filenamelen;
|
||
|
9 years ago
|
int64 chunkoff;
|
||
|
11 years ago
|
int chunksize;
|
||
|
|
char *chunk;
|
||
|
|
|
||
|
|
switch (PQresultStatus(res))
|
||
|
|
{
|
||
|
|
case PGRES_SINGLE_TUPLE:
|
||
|
|
break;
|
||
|
|
|
||
|
|
case PGRES_TUPLES_OK:
|
||
|
11 years ago
|
PQclear(res);
|
||
|
11 years ago
|
continue; /* final zero-row result */
|
||
|
|
|
||
|
|
default:
|
||
|
10 years ago
|
pg_fatal("unexpected result while fetching remote files: %s",
|
||
|
11 years ago
|
PQresultErrorMessage(res));
|
||
|
|
}
|
||
|
|
|
||
|
|
/* sanity check the result set */
|
||
|
|
if (PQnfields(res) != 3 || PQntuples(res) != 1)
|
||
|
7 years ago
|
pg_fatal("unexpected result set size while fetching remote files");
|
||
|
11 years ago
|
|
||
|
9 years ago
|
if (PQftype(res, 0) != TEXTOID ||
|
||
|
9 years ago
|
PQftype(res, 1) != INT8OID ||
|
||
|
11 years ago
|
PQftype(res, 2) != BYTEAOID)
|
||
|
|
{
|
||
|
7 years ago
|
pg_fatal("unexpected data types in result set while fetching remote files: %u %u %u",
|
||
|
11 years ago
|
PQftype(res, 0), PQftype(res, 1), PQftype(res, 2));
|
||
|
|
}
|
||
|
|
|
||
|
|
if (PQfformat(res, 0) != 1 &&
|
||
|
|
PQfformat(res, 1) != 1 &&
|
||
|
|
PQfformat(res, 2) != 1)
|
||
|
|
{
|
||
|
7 years ago
|
pg_fatal("unexpected result format while fetching remote files");
|
||
|
11 years ago
|
}
|
||
|
|
|
||
|
|
if (PQgetisnull(res, 0, 0) ||
|
||
|
11 years ago
|
PQgetisnull(res, 0, 1))
|
||
|
11 years ago
|
{
|
||
|
7 years ago
|
pg_fatal("unexpected null values in result while fetching remote files");
|
||
|
11 years ago
|
}
|
||
|
|
|
||
|
9 years ago
|
if (PQgetlength(res, 0, 1) != sizeof(int64))
|
||
|
7 years ago
|
pg_fatal("unexpected result length while fetching remote files");
|
||
|
11 years ago
|
|
||
|
|
/* Read result set to local variables */
|
||
|
9 years ago
|
memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int64));
|
||
|
8 years ago
|
chunkoff = pg_ntoh64(chunkoff);
|
||
|
11 years ago
|
chunksize = PQgetlength(res, 0, 2);
|
||
|
|
|
||
|
|
filenamelen = PQgetlength(res, 0, 0);
|
||
|
|
filename = pg_malloc(filenamelen + 1);
|
||
|
|
memcpy(filename, PQgetvalue(res, 0, 0), filenamelen);
|
||
|
|
filename[filenamelen] = '\0';
|
||
|
|
|
||
|
|
chunk = PQgetvalue(res, 0, 2);
|
||
|
|
|
||
|
11 years ago
|
/*
|
||
|
8 years ago
|
* If a file has been deleted on the source, remove it on the target
|
||
|
|
* as well. Note that multiple unlink() calls may happen on the same
|
||
|
|
* file if multiple data chunks are associated with it, hence ignore
|
||
|
|
* unconditionally anything missing. If this file is not a relation
|
||
|
|
* data file, then it has been already truncated when creating the
|
||
|
|
* file chunk list at the previous execution of the filemap.
|
||
|
11 years ago
|
*/
|
||
|
|
if (PQgetisnull(res, 0, 2))
|
||
|
|
{
|
||
|
7 years ago
|
pg_log_debug("received null value for chunk for file \"%s\", file has been deleted",
|
||
|
7 years ago
|
filename);
|
||
|
8 years ago
|
remove_target_file(filename, true);
|
||
|
11 years ago
|
pg_free(filename);
|
||
|
|
PQclear(res);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
pg_log_debug("received chunk for file \"%s\", offset " INT64_FORMAT ", size %d",
|
||
|
|
filename, chunkoff, chunksize);
|
||
|
11 years ago
|
|
||
|
|
open_target_file(filename, false);
|
||
|
|
|
||
|
|
write_target_range(chunk, chunkoff, chunksize);
|
||
|
11 years ago
|
|
||
|
|
pg_free(filename);
|
||
|
|
|
||
|
|
PQclear(res);
|
||
|
11 years ago
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
5 years ago
|
* Fetch a single file as a malloc'd buffer.
|
||
|
11 years ago
|
*/
|
||
|
5 years ago
|
static char *
|
||
|
|
libpq_fetch_file(rewind_source *source, const char *path, size_t *filesize)
|
||
|
11 years ago
|
{
|
||
|
5 years ago
|
PGconn *conn = ((libpq_source *) source)->conn;
|
||
|
11 years ago
|
PGresult *res;
|
||
|
|
char *result;
|
||
|
|
int len;
|
||
|
|
const char *paramValues[1];
|
||
|
|
|
||
|
5 years ago
|
paramValues[0] = path;
|
||
|
11 years ago
|
res = PQexecParams(conn, "SELECT pg_read_binary_file($1)",
|
||
|
|
1, NULL, paramValues, NULL, NULL, 1);
|
||
|
|
|
||
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||
|
11 years ago
|
pg_fatal("could not fetch remote file \"%s\": %s",
|
||
|
5 years ago
|
path, PQresultErrorMessage(res));
|
||
|
11 years ago
|
|
||
|
|
/* sanity check the result set */
|
||
|
|
if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0))
|
||
|
7 years ago
|
pg_fatal("unexpected result set while fetching remote file \"%s\"",
|
||
|
5 years ago
|
path);
|
||
|
11 years ago
|
|
||
|
|
/* Read result to local variables */
|
||
|
|
len = PQgetlength(res, 0, 0);
|
||
|
|
result = pg_malloc(len + 1);
|
||
|
|
memcpy(result, PQgetvalue(res, 0, 0), len);
|
||
|
|
result[len] = '\0';
|
||
|
|
|
||
|
11 years ago
|
PQclear(res);
|
||
|
|
|
||
|
5 years ago
|
pg_log_debug("fetched file \"%s\", length %d", path, len);
|
||
|
11 years ago
|
|
||
|
|
if (filesize)
|
||
|
|
*filesize = len;
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
5 years ago
|
* Close a libpq source.
|
||
|
11 years ago
|
*/
|
||
|
|
static void
|
||
|
5 years ago
|
libpq_destroy(rewind_source *source)
|
||
|
11 years ago
|
{
|
||
|
5 years ago
|
pfree(source);
|
||
|
|
/* NOTE: we don't close the connection here, as it was not opened by us. */
|
||
|
11 years ago
|
}
|