2011-10-26 20:13:33 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2017-07-07 14:08:55 +02:00
|
|
|
* streamutil.c - utility functions for pg_basebackup, pg_receivewal and
|
|
|
|
* pg_recvlogical
|
2011-10-26 20:13:33 +02:00
|
|
|
*
|
|
|
|
* Author: Magnus Hagander <magnus@hagander.net>
|
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2011-10-26 20:13:33 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/bin/pg_basebackup/streamutil.c
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2012-12-13 13:59:13 +01:00
|
|
|
#include "postgres_fe.h"
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2014-03-18 17:19:57 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/* for ntohl/htonl */
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
/* local includes */
|
|
|
|
#include "receivelog.h"
|
|
|
|
#include "streamutil.h"
|
|
|
|
|
2014-10-01 17:22:21 +02:00
|
|
|
#include "pqexpbuffer.h"
|
2014-03-18 17:19:57 +01:00
|
|
|
#include "common/fe_memutils.h"
|
|
|
|
#include "datatype/timestamp.h"
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2015-07-12 22:06:27 +02:00
|
|
|
#define ERRCODE_DUPLICATE_OBJECT "42710"
|
|
|
|
|
2011-10-26 20:13:33 +02:00
|
|
|
const char *progname;
|
2013-02-25 13:48:27 +01:00
|
|
|
char *connection_string = NULL;
|
2011-10-26 20:13:33 +02:00
|
|
|
char *dbhost = NULL;
|
|
|
|
char *dbuser = NULL;
|
|
|
|
char *dbport = NULL;
|
2014-03-18 17:19:57 +01:00
|
|
|
char *dbname = NULL;
|
2011-10-26 20:13:33 +02:00
|
|
|
int dbgetpassword = 0; /* 0=auto, -1=never, 1=always */
|
Simplify correct use of simple_prompt().
The previous API for this function had it returning a malloc'd string.
That meant that callers had to check for NULL return, which few of them
were doing, and it also meant that callers had to remember to free()
the string later, which required extra logic in most cases.
Instead, make simple_prompt() write into a buffer supplied by the caller.
Anywhere that the maximum required input length is reasonably small,
which is almost all of the callers, we can just use a local or static
array as the buffer instead of dealing with malloc/free.
A fair number of callers used "pointer == NULL" as a proxy for "haven't
requested the password yet". Maintaining the same behavior requires
adding a separate boolean flag for that, which adds back some of the
complexity we save by removing free()s. Nonetheless, this nets out
at a small reduction in overall code size, and considerably less code
than we would have had if we'd added the missing NULL-return checks
everywhere they were needed.
In passing, clean up the API comment for simple_prompt() and get rid
of a very-unnecessary malloc/free in its Windows code path.
This is nominally a bug fix, but it does not seem worth back-patching,
because the actual risk of an OOM failure in any of these places seems
pretty tiny, and all of them are client-side not server-side anyway.
This patch is by me, but it owes a great deal to Michael Paquier
who identified the problem and drafted a patch for fixing it the
other way.
Discussion: <CAB7nPqRu07Ot6iht9i9KRfYLpDaF2ZuUv5y_+72uP23ZAGysRg@mail.gmail.com>
2016-08-30 23:02:02 +02:00
|
|
|
static bool have_password = false;
|
|
|
|
static char password[100];
|
2011-10-26 20:13:33 +02:00
|
|
|
PGconn *conn = NULL;
|
|
|
|
|
2012-05-27 11:05:24 +02:00
|
|
|
/*
|
|
|
|
* Connect to the server. Returns a valid PGconn pointer if connected,
|
|
|
|
* or NULL on non-permanent error. On permanent error, the function will
|
|
|
|
* call exit(1) directly.
|
|
|
|
*/
|
2011-10-26 20:13:33 +02:00
|
|
|
PGconn *
|
|
|
|
GetConnection(void)
|
|
|
|
{
|
|
|
|
PGconn *tmpconn;
|
2013-02-25 13:48:27 +01:00
|
|
|
int argcount = 7; /* dbname, replication, fallback_app_name,
|
|
|
|
* host, user, port, password */
|
2011-10-26 20:13:33 +02:00
|
|
|
int i;
|
|
|
|
const char **keywords;
|
|
|
|
const char **values;
|
2012-05-22 16:02:47 +02:00
|
|
|
const char *tmpparam;
|
2013-11-15 23:27:41 +01:00
|
|
|
bool need_password;
|
2013-02-25 13:48:27 +01:00
|
|
|
PQconninfoOption *conn_opts = NULL;
|
|
|
|
PQconninfoOption *conn_opt;
|
|
|
|
char *err_msg = NULL;
|
|
|
|
|
2016-08-08 16:07:46 +02:00
|
|
|
/* pg_recvlogical uses dbname only; others use connection_string only. */
|
|
|
|
Assert(dbname == NULL || connection_string == NULL);
|
|
|
|
|
2013-02-25 13:48:27 +01:00
|
|
|
/*
|
|
|
|
* Merge the connection info inputs given in form of connection string,
|
2013-05-29 22:58:43 +02:00
|
|
|
* options and default values (dbname=replication, replication=true, etc.)
|
2016-08-08 16:07:46 +02:00
|
|
|
* Explicitly discard any dbname value in the connection string;
|
|
|
|
* otherwise, PQconnectdbParams() would interpret that value as being
|
|
|
|
* itself a connection string.
|
2013-02-25 13:48:27 +01:00
|
|
|
*/
|
|
|
|
i = 0;
|
|
|
|
if (connection_string)
|
|
|
|
{
|
|
|
|
conn_opts = PQconninfoParse(connection_string, &err_msg);
|
|
|
|
if (conn_opts == NULL)
|
|
|
|
{
|
2013-07-16 02:04:14 +02:00
|
|
|
fprintf(stderr, "%s: %s", progname, err_msg);
|
2013-07-14 21:31:23 +02:00
|
|
|
exit(1);
|
2013-02-25 13:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
|
|
|
|
{
|
2016-08-08 16:07:46 +02:00
|
|
|
if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
|
|
|
|
strcmp(conn_opt->keyword, "dbname") != 0)
|
2013-02-25 13:48:27 +01:00
|
|
|
argcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
|
|
|
|
values = pg_malloc0((argcount + 1) * sizeof(*values));
|
|
|
|
|
|
|
|
for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
|
|
|
|
{
|
2016-08-08 16:07:46 +02:00
|
|
|
if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
|
|
|
|
strcmp(conn_opt->keyword, "dbname") != 0)
|
2013-02-25 13:48:27 +01:00
|
|
|
{
|
|
|
|
keywords[i] = conn_opt->keyword;
|
|
|
|
values[i] = conn_opt->val;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
|
|
|
|
values = pg_malloc0((argcount + 1) * sizeof(*values));
|
|
|
|
}
|
|
|
|
|
|
|
|
keywords[i] = "dbname";
|
2014-03-18 17:19:57 +01:00
|
|
|
values[i] = dbname == NULL ? "replication" : dbname;
|
2013-02-25 13:48:27 +01:00
|
|
|
i++;
|
|
|
|
keywords[i] = "replication";
|
2014-03-18 17:19:57 +01:00
|
|
|
values[i] = dbname == NULL ? "true" : "database";
|
2013-02-25 13:48:27 +01:00
|
|
|
i++;
|
|
|
|
keywords[i] = "fallback_application_name";
|
|
|
|
values[i] = progname;
|
|
|
|
i++;
|
2011-10-26 20:13:33 +02:00
|
|
|
|
|
|
|
if (dbhost)
|
|
|
|
{
|
|
|
|
keywords[i] = "host";
|
|
|
|
values[i] = dbhost;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (dbuser)
|
|
|
|
{
|
|
|
|
keywords[i] = "user";
|
|
|
|
values[i] = dbuser;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (dbport)
|
|
|
|
{
|
|
|
|
keywords[i] = "port";
|
|
|
|
values[i] = dbport;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:27:41 +01:00
|
|
|
/* If -W was given, force prompt for password, but only the first time */
|
Simplify correct use of simple_prompt().
The previous API for this function had it returning a malloc'd string.
That meant that callers had to check for NULL return, which few of them
were doing, and it also meant that callers had to remember to free()
the string later, which required extra logic in most cases.
Instead, make simple_prompt() write into a buffer supplied by the caller.
Anywhere that the maximum required input length is reasonably small,
which is almost all of the callers, we can just use a local or static
array as the buffer instead of dealing with malloc/free.
A fair number of callers used "pointer == NULL" as a proxy for "haven't
requested the password yet". Maintaining the same behavior requires
adding a separate boolean flag for that, which adds back some of the
complexity we save by removing free()s. Nonetheless, this nets out
at a small reduction in overall code size, and considerably less code
than we would have had if we'd added the missing NULL-return checks
everywhere they were needed.
In passing, clean up the API comment for simple_prompt() and get rid
of a very-unnecessary malloc/free in its Windows code path.
This is nominally a bug fix, but it does not seem worth back-patching,
because the actual risk of an OOM failure in any of these places seems
pretty tiny, and all of them are client-side not server-side anyway.
This patch is by me, but it owes a great deal to Michael Paquier
who identified the problem and drafted a patch for fixing it the
other way.
Discussion: <CAB7nPqRu07Ot6iht9i9KRfYLpDaF2ZuUv5y_+72uP23ZAGysRg@mail.gmail.com>
2016-08-30 23:02:02 +02:00
|
|
|
need_password = (dbgetpassword == 1 && !have_password);
|
2013-11-15 23:27:41 +01:00
|
|
|
|
2013-11-22 03:46:43 +01:00
|
|
|
do
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2013-11-15 23:27:41 +01:00
|
|
|
/* Get a new password if appropriate */
|
|
|
|
if (need_password)
|
|
|
|
{
|
Simplify correct use of simple_prompt().
The previous API for this function had it returning a malloc'd string.
That meant that callers had to check for NULL return, which few of them
were doing, and it also meant that callers had to remember to free()
the string later, which required extra logic in most cases.
Instead, make simple_prompt() write into a buffer supplied by the caller.
Anywhere that the maximum required input length is reasonably small,
which is almost all of the callers, we can just use a local or static
array as the buffer instead of dealing with malloc/free.
A fair number of callers used "pointer == NULL" as a proxy for "haven't
requested the password yet". Maintaining the same behavior requires
adding a separate boolean flag for that, which adds back some of the
complexity we save by removing free()s. Nonetheless, this nets out
at a small reduction in overall code size, and considerably less code
than we would have had if we'd added the missing NULL-return checks
everywhere they were needed.
In passing, clean up the API comment for simple_prompt() and get rid
of a very-unnecessary malloc/free in its Windows code path.
This is nominally a bug fix, but it does not seem worth back-patching,
because the actual risk of an OOM failure in any of these places seems
pretty tiny, and all of them are client-side not server-side anyway.
This patch is by me, but it owes a great deal to Michael Paquier
who identified the problem and drafted a patch for fixing it the
other way.
Discussion: <CAB7nPqRu07Ot6iht9i9KRfYLpDaF2ZuUv5y_+72uP23ZAGysRg@mail.gmail.com>
2016-08-30 23:02:02 +02:00
|
|
|
simple_prompt("Password: ", password, sizeof(password), false);
|
|
|
|
have_password = true;
|
2013-11-15 23:27:41 +01:00
|
|
|
need_password = false;
|
|
|
|
}
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2013-11-15 23:27:41 +01:00
|
|
|
/* Use (or reuse, on a subsequent connection) password if we have it */
|
Simplify correct use of simple_prompt().
The previous API for this function had it returning a malloc'd string.
That meant that callers had to check for NULL return, which few of them
were doing, and it also meant that callers had to remember to free()
the string later, which required extra logic in most cases.
Instead, make simple_prompt() write into a buffer supplied by the caller.
Anywhere that the maximum required input length is reasonably small,
which is almost all of the callers, we can just use a local or static
array as the buffer instead of dealing with malloc/free.
A fair number of callers used "pointer == NULL" as a proxy for "haven't
requested the password yet". Maintaining the same behavior requires
adding a separate boolean flag for that, which adds back some of the
complexity we save by removing free()s. Nonetheless, this nets out
at a small reduction in overall code size, and considerably less code
than we would have had if we'd added the missing NULL-return checks
everywhere they were needed.
In passing, clean up the API comment for simple_prompt() and get rid
of a very-unnecessary malloc/free in its Windows code path.
This is nominally a bug fix, but it does not seem worth back-patching,
because the actual risk of an OOM failure in any of these places seems
pretty tiny, and all of them are client-side not server-side anyway.
This patch is by me, but it owes a great deal to Michael Paquier
who identified the problem and drafted a patch for fixing it the
other way.
Discussion: <CAB7nPqRu07Ot6iht9i9KRfYLpDaF2ZuUv5y_+72uP23ZAGysRg@mail.gmail.com>
2016-08-30 23:02:02 +02:00
|
|
|
if (have_password)
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2013-02-25 13:48:27 +01:00
|
|
|
keywords[i] = "password";
|
Simplify correct use of simple_prompt().
The previous API for this function had it returning a malloc'd string.
That meant that callers had to check for NULL return, which few of them
were doing, and it also meant that callers had to remember to free()
the string later, which required extra logic in most cases.
Instead, make simple_prompt() write into a buffer supplied by the caller.
Anywhere that the maximum required input length is reasonably small,
which is almost all of the callers, we can just use a local or static
array as the buffer instead of dealing with malloc/free.
A fair number of callers used "pointer == NULL" as a proxy for "haven't
requested the password yet". Maintaining the same behavior requires
adding a separate boolean flag for that, which adds back some of the
complexity we save by removing free()s. Nonetheless, this nets out
at a small reduction in overall code size, and considerably less code
than we would have had if we'd added the missing NULL-return checks
everywhere they were needed.
In passing, clean up the API comment for simple_prompt() and get rid
of a very-unnecessary malloc/free in its Windows code path.
This is nominally a bug fix, but it does not seem worth back-patching,
because the actual risk of an OOM failure in any of these places seems
pretty tiny, and all of them are client-side not server-side anyway.
This patch is by me, but it owes a great deal to Michael Paquier
who identified the problem and drafted a patch for fixing it the
other way.
Discussion: <CAB7nPqRu07Ot6iht9i9KRfYLpDaF2ZuUv5y_+72uP23ZAGysRg@mail.gmail.com>
2016-08-30 23:02:02 +02:00
|
|
|
values[i] = password;
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
2013-11-15 23:27:41 +01:00
|
|
|
else
|
2011-10-26 20:13:33 +02:00
|
|
|
{
|
2013-11-15 23:27:41 +01:00
|
|
|
keywords[i] = NULL;
|
|
|
|
values[i] = NULL;
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpconn = PQconnectdbParams(keywords, values, true);
|
|
|
|
|
2012-07-12 13:31:19 +02:00
|
|
|
/*
|
|
|
|
* If there is too little memory even to allocate the PGconn object
|
|
|
|
* and PQconnectdbParams returns NULL, we call exit(1) directly.
|
|
|
|
*/
|
|
|
|
if (!tmpconn)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not connect to server\n"),
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:27:41 +01:00
|
|
|
/* If we need a password and -w wasn't given, loop back and get one */
|
2011-10-26 20:13:33 +02:00
|
|
|
if (PQstatus(tmpconn) == CONNECTION_BAD &&
|
|
|
|
PQconnectionNeedsPassword(tmpconn) &&
|
|
|
|
dbgetpassword != -1)
|
|
|
|
{
|
2012-07-12 13:31:19 +02:00
|
|
|
PQfinish(tmpconn);
|
2013-11-15 23:27:41 +01:00
|
|
|
need_password = true;
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
2013-11-15 23:27:41 +01:00
|
|
|
}
|
2013-11-22 03:46:43 +01:00
|
|
|
while (need_password);
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2013-11-15 23:27:41 +01:00
|
|
|
if (PQstatus(tmpconn) != CONNECTION_OK)
|
|
|
|
{
|
2015-06-23 02:39:41 +02:00
|
|
|
fprintf(stderr, _("%s: could not connect to server: %s"),
|
2013-11-15 23:27:41 +01:00
|
|
|
progname, PQerrorMessage(tmpconn));
|
|
|
|
PQfinish(tmpconn);
|
2011-10-26 20:13:33 +02:00
|
|
|
free(values);
|
|
|
|
free(keywords);
|
2013-02-25 13:48:27 +01:00
|
|
|
if (conn_opts)
|
|
|
|
PQconninfoFree(conn_opts);
|
2013-11-15 23:27:41 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-26 20:13:33 +02:00
|
|
|
|
2013-11-15 23:27:41 +01:00
|
|
|
/* Connection ok! */
|
|
|
|
free(values);
|
|
|
|
free(keywords);
|
|
|
|
if (conn_opts)
|
|
|
|
PQconninfoFree(conn_opts);
|
|
|
|
|
|
|
|
/*
|
2017-02-23 20:04:43 +01:00
|
|
|
* Ensure we have the same value of integer_datetimes (now always "on") as
|
|
|
|
* the server we are connecting to.
|
2013-11-15 23:27:41 +01:00
|
|
|
*/
|
|
|
|
tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
|
|
|
|
if (!tmpparam)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
_("%s: could not determine server setting for integer_datetimes\n"),
|
2013-11-15 23:27:41 +01:00
|
|
|
progname);
|
|
|
|
PQfinish(tmpconn);
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-05-22 16:02:47 +02:00
|
|
|
|
2013-11-15 23:27:41 +01:00
|
|
|
if (strcmp(tmpparam, "on") != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
_("%s: integer_datetimes compile flag does not match server\n"),
|
2013-11-15 23:27:41 +01:00
|
|
|
progname);
|
|
|
|
PQfinish(tmpconn);
|
|
|
|
exit(1);
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
2013-11-15 23:27:41 +01:00
|
|
|
|
|
|
|
return tmpconn;
|
2011-10-26 20:13:33 +02:00
|
|
|
}
|
2014-03-18 17:19:57 +01:00
|
|
|
|
2014-10-01 17:22:21 +02:00
|
|
|
/*
|
|
|
|
* Run IDENTIFY_SYSTEM through a given connection and give back to caller
|
|
|
|
* some result information if requested:
|
|
|
|
* - System identifier
|
2016-01-05 21:25:12 +01:00
|
|
|
* - Current timeline ID
|
|
|
|
* - Start LSN position
|
|
|
|
* - Database name (NULL in servers prior to 9.4)
|
2014-10-01 17:22:21 +02:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli,
|
|
|
|
XLogRecPtr *startpos, char **db_name)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
2015-05-24 03:35:49 +02:00
|
|
|
uint32 hi,
|
|
|
|
lo;
|
2014-10-01 17:22:21 +02:00
|
|
|
|
|
|
|
/* Check connection existence */
|
|
|
|
Assert(conn != NULL);
|
|
|
|
|
|
|
|
res = PQexec(conn, "IDENTIFY_SYSTEM");
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
|
|
|
|
progname, "IDENTIFY_SYSTEM", PQerrorMessage(conn));
|
2014-10-06 17:18:13 +02:00
|
|
|
|
|
|
|
PQclear(res);
|
2014-10-01 17:22:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (PQntuples(res) != 1 || PQnfields(res) < 3)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
|
|
|
|
progname, PQntuples(res), PQnfields(res), 1, 3);
|
2014-10-06 17:18:13 +02:00
|
|
|
|
|
|
|
PQclear(res);
|
2014-10-01 17:22:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get system identifier */
|
|
|
|
if (sysid != NULL)
|
|
|
|
*sysid = pg_strdup(PQgetvalue(res, 0, 0));
|
|
|
|
|
|
|
|
/* Get timeline ID to start streaming from */
|
|
|
|
if (starttli != NULL)
|
|
|
|
*starttli = atoi(PQgetvalue(res, 0, 1));
|
|
|
|
|
|
|
|
/* Get LSN start position if necessary */
|
|
|
|
if (startpos != NULL)
|
|
|
|
{
|
|
|
|
if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
_("%s: could not parse write-ahead log location \"%s\"\n"),
|
2014-10-01 17:22:21 +02:00
|
|
|
progname, PQgetvalue(res, 0, 2));
|
2014-10-06 17:18:13 +02:00
|
|
|
|
|
|
|
PQclear(res);
|
2014-10-01 17:22:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*startpos = ((uint64) hi) << 32 | lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get database name, only available in 9.4 and newer versions */
|
2015-05-24 03:35:49 +02:00
|
|
|
if (db_name != NULL)
|
2014-10-01 17:22:21 +02:00
|
|
|
{
|
2016-01-05 21:25:12 +01:00
|
|
|
*db_name = NULL;
|
|
|
|
if (PQserverVersion(conn) >= 90400)
|
|
|
|
{
|
|
|
|
if (PQnfields(res) < 4)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"),
|
|
|
|
progname, PQntuples(res), PQnfields(res), 1, 4);
|
2014-10-01 17:22:21 +02:00
|
|
|
|
2016-01-05 21:25:12 +01:00
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!PQgetisnull(res, 0, 3))
|
|
|
|
*db_name = pg_strdup(PQgetvalue(res, 0, 3));
|
|
|
|
}
|
2014-10-01 17:22:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a replication slot for the given connection. This function
|
2017-03-19 17:06:44 +01:00
|
|
|
* returns true in case of success.
|
2014-10-01 17:22:21 +02:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
CreateReplicationSlot(PGconn *conn, const char *slot_name, const char *plugin,
|
2015-07-12 22:06:27 +02:00
|
|
|
bool is_physical, bool slot_exists_ok)
|
2014-10-01 17:22:21 +02:00
|
|
|
{
|
|
|
|
PQExpBuffer query;
|
|
|
|
PGresult *res;
|
|
|
|
|
|
|
|
query = createPQExpBuffer();
|
|
|
|
|
|
|
|
Assert((is_physical && plugin == NULL) ||
|
|
|
|
(!is_physical && plugin != NULL));
|
|
|
|
Assert(slot_name != NULL);
|
|
|
|
|
|
|
|
/* Build query */
|
|
|
|
if (is_physical)
|
|
|
|
appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\" PHYSICAL",
|
|
|
|
slot_name);
|
|
|
|
else
|
2017-03-14 22:13:56 +01:00
|
|
|
{
|
2014-10-01 17:22:21 +02:00
|
|
|
appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\" LOGICAL \"%s\"",
|
|
|
|
slot_name, plugin);
|
2017-03-14 22:13:56 +01:00
|
|
|
if (PQserverVersion(conn) >= 100000)
|
|
|
|
/* pg_recvlogical doesn't use an exported snapshot, so suppress */
|
|
|
|
appendPQExpBuffer(query, " NOEXPORT_SNAPSHOT");
|
|
|
|
}
|
2014-10-01 17:22:21 +02:00
|
|
|
|
|
|
|
res = PQexec(conn, query->data);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
|
|
{
|
2015-07-12 22:06:27 +02:00
|
|
|
const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
|
2014-10-06 17:18:13 +02:00
|
|
|
|
2015-08-12 17:35:50 +02:00
|
|
|
if (slot_exists_ok &&
|
|
|
|
sqlstate &&
|
|
|
|
strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
|
2015-07-12 22:06:27 +02:00
|
|
|
{
|
|
|
|
destroyPQExpBuffer(query);
|
|
|
|
PQclear(res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
|
|
|
|
progname, query->data, PQerrorMessage(conn));
|
|
|
|
|
|
|
|
destroyPQExpBuffer(query);
|
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-01 17:22:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PQntuples(res) != 1 || PQnfields(res) != 4)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields\n"),
|
|
|
|
progname, slot_name,
|
|
|
|
PQntuples(res), PQnfields(res), 1, 4);
|
2014-10-06 17:18:13 +02:00
|
|
|
|
|
|
|
destroyPQExpBuffer(query);
|
|
|
|
PQclear(res);
|
2014-10-01 17:22:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-06 17:18:13 +02:00
|
|
|
destroyPQExpBuffer(query);
|
2014-10-01 17:22:21 +02:00
|
|
|
PQclear(res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop a replication slot for the given connection. This function
|
|
|
|
* returns true in case of success.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
DropReplicationSlot(PGconn *conn, const char *slot_name)
|
|
|
|
{
|
|
|
|
PQExpBuffer query;
|
|
|
|
PGresult *res;
|
|
|
|
|
|
|
|
Assert(slot_name != NULL);
|
|
|
|
|
|
|
|
query = createPQExpBuffer();
|
|
|
|
|
|
|
|
/* Build query */
|
|
|
|
appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
|
|
|
|
slot_name);
|
|
|
|
res = PQexec(conn, query->data);
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("%s: could not send replication command \"%s\": %s"),
|
|
|
|
progname, query->data, PQerrorMessage(conn));
|
2014-10-06 17:18:13 +02:00
|
|
|
|
|
|
|
destroyPQExpBuffer(query);
|
|
|
|
PQclear(res);
|
2014-10-01 17:22:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PQntuples(res) != 0 || PQnfields(res) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields\n"),
|
|
|
|
progname, slot_name,
|
|
|
|
PQntuples(res), PQnfields(res), 0, 0);
|
2014-10-06 17:18:13 +02:00
|
|
|
|
|
|
|
destroyPQExpBuffer(query);
|
|
|
|
PQclear(res);
|
2014-10-01 17:22:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-12 22:25:51 +02:00
|
|
|
destroyPQExpBuffer(query);
|
2014-10-01 17:22:21 +02:00
|
|
|
PQclear(res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-18 17:19:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Frontend version of GetCurrentTimestamp(), since we are not linked with
|
2017-02-23 21:57:08 +01:00
|
|
|
* backend code.
|
2014-03-18 17:19:57 +01:00
|
|
|
*/
|
2017-02-23 21:57:08 +01:00
|
|
|
TimestampTz
|
2014-03-18 17:19:57 +01:00
|
|
|
feGetCurrentTimestamp(void)
|
|
|
|
{
|
2017-02-23 21:57:08 +01:00
|
|
|
TimestampTz result;
|
2014-03-18 17:19:57 +01:00
|
|
|
struct timeval tp;
|
|
|
|
|
|
|
|
gettimeofday(&tp, NULL);
|
|
|
|
|
2017-02-23 21:57:08 +01:00
|
|
|
result = (TimestampTz) tp.tv_sec -
|
2014-03-18 17:19:57 +01:00
|
|
|
((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
|
|
|
|
result = (result * USECS_PER_SEC) + tp.tv_usec;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Frontend version of TimestampDifference(), since we are not linked with
|
|
|
|
* backend code.
|
|
|
|
*/
|
|
|
|
void
|
2017-02-23 21:57:08 +01:00
|
|
|
feTimestampDifference(TimestampTz start_time, TimestampTz stop_time,
|
2014-05-06 18:12:18 +02:00
|
|
|
long *secs, int *microsecs)
|
2014-03-18 17:19:57 +01:00
|
|
|
{
|
2017-02-23 21:57:08 +01:00
|
|
|
TimestampTz diff = stop_time - start_time;
|
2014-03-18 17:19:57 +01:00
|
|
|
|
|
|
|
if (diff <= 0)
|
|
|
|
{
|
|
|
|
*secs = 0;
|
|
|
|
*microsecs = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*secs = (long) (diff / USECS_PER_SEC);
|
|
|
|
*microsecs = (int) (diff % USECS_PER_SEC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Frontend version of TimestampDifferenceExceeds(), since we are not
|
|
|
|
* linked with backend code.
|
|
|
|
*/
|
|
|
|
bool
|
2017-02-23 21:57:08 +01:00
|
|
|
feTimestampDifferenceExceeds(TimestampTz start_time,
|
|
|
|
TimestampTz stop_time,
|
2014-05-06 18:12:18 +02:00
|
|
|
int msec)
|
2014-03-18 17:19:57 +01:00
|
|
|
{
|
2017-02-23 21:57:08 +01:00
|
|
|
TimestampTz diff = stop_time - start_time;
|
2014-03-18 17:19:57 +01:00
|
|
|
|
|
|
|
return (diff >= msec * INT64CONST(1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts an int64 to network byte order.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
fe_sendint64(int64 i, char *buf)
|
|
|
|
{
|
|
|
|
uint32 n32;
|
|
|
|
|
|
|
|
/* High order half first, since we're doing MSB-first */
|
|
|
|
n32 = (uint32) (i >> 32);
|
|
|
|
n32 = htonl(n32);
|
|
|
|
memcpy(&buf[0], &n32, 4);
|
|
|
|
|
|
|
|
/* Now the low order half */
|
|
|
|
n32 = (uint32) i;
|
|
|
|
n32 = htonl(n32);
|
|
|
|
memcpy(&buf[4], &n32, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts an int64 from network byte order to native format.
|
|
|
|
*/
|
|
|
|
int64
|
|
|
|
fe_recvint64(char *buf)
|
|
|
|
{
|
|
|
|
int64 result;
|
|
|
|
uint32 h32;
|
|
|
|
uint32 l32;
|
|
|
|
|
|
|
|
memcpy(&h32, buf, 4);
|
|
|
|
memcpy(&l32, buf + 4, 4);
|
|
|
|
h32 = ntohl(h32);
|
|
|
|
l32 = ntohl(l32);
|
|
|
|
|
|
|
|
result = h32;
|
|
|
|
result <<= 32;
|
|
|
|
result |= l32;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|