Skip to content

Commit

Permalink
Add --use-binary-copy, tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
VaibhaveS committed Aug 2, 2024
1 parent 789c955 commit b28d466
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/include/clone.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
--create-slot Create the replication slot
--origin Use this Postgres replication origin node name
--endpos Stop replaying changes when reaching this LSN
--use-copy-binary Use the COPY BINARY format for COPY operations
1 change: 1 addition & 0 deletions src/bin/pgcopydb/cli_clone_follow.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
" --create-slot Create the replication slot\n" \
" --origin Use this Postgres replication origin node name\n" \
" --endpos Stop replaying changes when reaching this LSN\n" \
" --use-copy-binary Use the COPY BINARY format for COPY operations\n" \

CommandLine clone_command =
make_command(
Expand Down
14 changes: 12 additions & 2 deletions src/bin/pgcopydb/cli_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ cli_copydb_getenv(CopyDBOptions *options)
{ PGCOPYDB_SKIP_CTID_SPLIT, ENV_TYPE_BOOL,
&(options->skipCtidSplit) },
{ PGCOPYDB_SKIP_TABLESPACES, ENV_TYPE_BOOL,
&(options->restoreOptions.noTableSpaces) }
&(options->restoreOptions.noTableSpaces) },
{ PGCOPYDB_USE_COPY_BINARY, ENV_TYPE_BOOL,
&(options->useCopyBinary) }
};

int parserCount = sizeof(parsers) / sizeof(parsers[0]);
Expand Down Expand Up @@ -616,6 +618,7 @@ cli_copy_db_getopts(int argc, char **argv)
{ "skip-db-properties", no_argument, NULL, 'g' },
{ "skip-split-by-ctid", no_argument, NULL, 'k' },
{ "no-tablespaces", no_argument, NULL, 'y' },
{ "use-copy-binary", no_argument, NULL, 'n' },
{ "filter", required_argument, NULL, 'F' },
{ "filters", required_argument, NULL, 'F' },
{ "requirements", required_argument, NULL, 'Q' },
Expand Down Expand Up @@ -651,7 +654,7 @@ cli_copy_db_getopts(int argc, char **argv)
}

const char *optstring =
"S:T:D:J:I:b:L:u:mcAPOXj:xBeMlUagkyF:F:Q:irRCN:fp:ws:o:tE:Vvdzqh";
"S:T:D:J:I:b:L:u:mcAPOXj:xBeMlUagkynF:F:Q:irRCN:fp:ws:o:tE:Vvdzqh";

while ((c = getopt_long(argc, argv,
optstring, long_options, &option_index)) != -1)
Expand Down Expand Up @@ -1084,6 +1087,13 @@ cli_copy_db_getopts(int argc, char **argv)
break;
}

case 'n':
{
options.useCopyBinary = true;
log_trace("--use-copy-binary");
break;
}

case '?':
default:
{
Expand Down
1 change: 1 addition & 0 deletions src/bin/pgcopydb/cli_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typedef struct CopyDBOptions
bool skipCtidSplit;
bool noRolesPasswords;
bool failFast;
bool useCopyBinary;

bool restart;
bool resume;
Expand Down
1 change: 1 addition & 0 deletions src/bin/pgcopydb/copydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ copydb_init_specs(CopyDataSpec *specs,
.skipCtidSplit = options->skipCtidSplit,
.noRolesPasswords = options->noRolesPasswords,
.failFast = options->failFast,
.useCopyBinary = options->useCopyBinary,

.restart = options->restart,
.resume = options->resume,
Expand Down
1 change: 1 addition & 0 deletions src/bin/pgcopydb/copydb.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ typedef struct CopyDataSpec
bool skipDBproperties;
bool skipCtidSplit;
bool noRolesPasswords;
bool useCopyBinary;

bool restart;
bool resume;
Expand Down
1 change: 1 addition & 0 deletions src/bin/pgcopydb/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#define PGCOPYDB_SKIP_TABLESPACES "PGCOPYDB_SKIP_TABLESPACES"
#define PGCOPYDB_SKIP_DB_PROPERTIES "PGCOPYDB_SKIP_DB_PROPERTIES"
#define PGCOPYDB_SKIP_CTID_SPLIT "PGCOPYDB_SKIP_CTID_SPLIT"
#define PGCOPYDB_USE_COPY_BINARY "PGCOPYDB_USE_COPY_BINARY"

/* default values for the command line options */
#define DEFAULT_TABLE_JOBS 4
Expand Down
20 changes: 17 additions & 3 deletions src/bin/pgcopydb/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -2926,7 +2926,7 @@ pg_copy_from_stdin(PGSQL *pgsql, const char *qname)
{
char sql[BUFSIZE] = { 0 };

sformat(sql, sizeof(sql), "COPY %s FROM stdin WITH (format binary)", qname);
sformat(sql, sizeof(sql), "COPY %s FROM stdin", qname);

char *endpoint =
pgsql->connectionType == PGSQL_CONN_SOURCE ? "SOURCE" : "TARGET";
Expand Down Expand Up @@ -3070,7 +3070,13 @@ pg_copy_send_query(PGSQL *pgsql, CopyArgs *args, ExecStatusType status)
args->srcAttrList,
args->srcQname);
}
appendPQExpBuffer(sql, "to stdout with (format binary);");

appendPQExpBuffer(sql, "to stdout");

if (args->useCopyBinary)
{
appendPQExpBuffer(sql, " with (format binary)");
}
}
else if (status == PGRES_COPY_IN)
{
Expand All @@ -3085,10 +3091,18 @@ pg_copy_send_query(PGSQL *pgsql, CopyArgs *args, ExecStatusType status)
appendPQExpBuffer(sql, "copy %s from stdin", args->dstQname);
}

if (args->freeze)
if (args->freeze && args->useCopyBinary)
{
appendPQExpBuffer(sql, " with (freeze, format binary)");
}
else if (args->freeze)
{
appendPQExpBuffer(sql, " with (freeze)");
}
else if (args->useCopyBinary)
{
appendPQExpBuffer(sql, " with (format binary)");
}
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/bin/pgcopydb/pgsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ typedef struct CopyArgs
char *logCommand;
bool truncate;
bool freeze;
bool useCopyBinary;
uint64_t bytesTransmitted;
} CopyArgs;

Expand Down
3 changes: 1 addition & 2 deletions src/bin/pgcopydb/table-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ copydb_table_create_lockfile(CopyDataSpec *specs,
args->truncate = false; /* default value, see below */
args->freeze = tableSpecs->sourceTable->partition.partCount <= 1;
args->bytesTransmitted = 0;
args->useCopyBinary = specs->useCopyBinary;

/*
* Check to see if we want to TRUNCATE the table and benefit from the COPY
Expand Down Expand Up @@ -1440,8 +1441,6 @@ copydb_prepare_copy_query(CopyTableDataSpec *tableSpecs, CopyArgs *args)
}
}

appendPQExpBuffer(srcWhereClause, " WITH (FORMAT BINARY)");

if (PQExpBufferBroken(srcWhereClause))
{
log_error("Failed to create where clause for %s: out of memory",
Expand Down
2 changes: 1 addition & 1 deletion tests/endpos-in-multi-wal-txn/copydb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sleep 1
pgcopydb stream setup

# pgcopydb clone uses the environment variables
pgcopydb clone
pgcopydb clone --use-binary-copy

# now that the copying is done, inject some SQL DML changes to the source
psql -d ${PGCOPYDB_SOURCE_PGURI} -f /usr/src/pgcopydb/multi-wal-txn.sql
Expand Down
2 changes: 1 addition & 1 deletion tests/follow-9.6/copydb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ psql -d ${PGCOPYDB_SOURCE_PGURI} -f /usr/src/pgcopydb/ddl.sql
find ${TMPDIR}

# pgcopydb copy db uses the environment variables
pgcopydb clone --follow --notice
pgcopydb clone --follow --notice --use-copy-binary

# cleanup
pgcopydb stream sentinel get
Expand Down
1 change: 1 addition & 0 deletions tests/pagila/copydb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pgcopydb clone --notice \
--skip-ext-comments \
--skip-db-properties \
--estimate-table-sizes \
--use-copy-binary \
--source ${PAGILA_SOURCE_PGURI} \
--target ${PAGILA_TARGET_PGURI}

Expand Down

0 comments on commit b28d466

Please sign in to comment.