Skip to content

Commit

Permalink
[Backport 2.8][#7809][YSQL] Add session flag to enable non-transactio…
Browse files Browse the repository at this point in the history
…nal writes on COPY

Summary:
Allowing faster writes on copy command by using session variable "yb_force_non_transactional_writes".
The default value for this flag is false.
To enable the flag,
```
SET yb_force_non_transactional_writes=true
```
To disable the flag,
```
SET yb_force_non_transactional_writes=false
```
Note, the session variable must be applied on non-DDL operations as non-transactional writes on DDL operations is not supported.

Test Plan: Jenkins: rebase: 2.8

Reviewers: mihnea, smishra

Reviewed By: smishra

Subscribers: yql, smishra

Differential Revision: https://phabricator.dev.yugabyte.com/D14195
  • Loading branch information
emhna committed Dec 2, 2021
1 parent 435940b commit 8305278
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
35 changes: 34 additions & 1 deletion java/yb-pgsql/src/test/java/org/yb/pgsql/TestBatchCopyFrom.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class TestBatchCopyFrom extends BasePgSQLTest {
private static final String BATCH_TXN_SESSION_VARIABLE_NAME =
"yb_default_copy_from_rows_per_transaction";
private static final int BATCH_TXN_SESSION_VARIABLE_DEFAULT_ROWS = 1000;
private static final String NON_TXN_WRITE_SESSION_VARIABLE_NAME =
"yb_force_non_transactional_writes";

private String getAbsFilePath(String fileName) {
return TestUtils.getBaseTmpDir() + "/" + fileName;
Expand Down Expand Up @@ -440,7 +442,7 @@ public void tesStdinCopyInTransactionBlockWithProceedingTransaction() throws Exc
}

@Test
public void testSessionVariable() throws Exception {
public void testBatchTxnSessionVariable() throws Exception {
String absFilePath = getAbsFilePath("batch-copyfrom-sessionvar.txt");
String tableName = "batchSessionTable";
int totalValidLines = 7;
Expand Down Expand Up @@ -482,6 +484,37 @@ public void testSessionVariable() throws Exception {
}
}

@Test
public void testNonTxnWriteSessionVariable() throws Exception {
String absFilePath = getAbsFilePath("batch-copyfrom-nontxn-sessionvar.txt");
String tableName = "nontxnSessionVarTable";
int totalValidLines = 5;
int expectedCopiedLines = totalValidLines;

createFileInTmpDir(absFilePath, totalValidLines);

try (Statement statement = connection.createStatement()) {
// ensure non-txn session variable is off by default
assertOneRow(statement, "SHOW " + NON_TXN_WRITE_SESSION_VARIABLE_NAME, "off");
// set non-txn session variable
statement.execute("SET " + NON_TXN_WRITE_SESSION_VARIABLE_NAME + "=true");
statement.execute(String.format(
"CREATE TABLE %s (a Integer, b serial, c varchar, d int)", tableName));
statement.execute(String.format(
"COPY %s FROM \'%s\' WITH (FORMAT CSV, HEADER)", tableName, absFilePath));

// ensure no discrepency in result
assertOneRow(statement, "SELECT COUNT(*) FROM " + tableName, expectedCopiedLines);
// ensure DDL's, DML's execute without error
statement.execute("ALTER TABLE " + tableName + " ADD COLUMN e INT");
statement.execute("ALTER TABLE " + tableName + " DROP COLUMN e");
statement.execute("INSERT INTO " + tableName + " VALUES (100)");
statement.execute("TRUNCATE TABLE " + tableName);
statement.execute("DROP TABLE " + tableName);
statement.execute("CREATE TABLE " + tableName + " (a int primary key, b text)");
}
}

@Test
public void testSessionVariableWithRowsPerTransactionOption() throws Exception {
String absFilePath = getAbsFilePath("batch-copyfrom-sessionvar-with-query-option.txt");
Expand Down
5 changes: 4 additions & 1 deletion src/postgres/src/backend/executor/ybcModifyTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
#include "pg_yb_utils.h"
#include "access/yb_scan.h"

bool yb_force_non_transactional_writes = false;

/*
* Hack to ensure that the next CommandCounterIncrement() will call
* CommandEndInvalidationMessages(). The result of this call is not
Expand Down Expand Up @@ -417,11 +419,12 @@ Oid YBCExecuteInsertForDb(Oid dboid,
TupleDesc tupleDesc,
HeapTuple tuple)
{
bool non_transactional = !IsSystemRelation(rel) && yb_force_non_transactional_writes;
return YBCExecuteInsertInternal(dboid,
rel,
tupleDesc,
tuple,
false /* is_single_row_txn */);
non_transactional);
}

Oid YBCExecuteNonTxnInsert(Relation rel,
Expand Down
11 changes: 11 additions & 0 deletions src/postgres/src/backend/utils/misc/guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "commands/vacuum.h"
#include "commands/variable.h"
#include "commands/trigger.h"
#include "executor/ybcModifyTable.h"
#include "funcapi.h"
#include "jit/jit.h"
#include "libpq/auth.h"
Expand Down Expand Up @@ -2008,6 +2009,16 @@ static struct config_bool ConfigureNamesBool[] =
NULL, NULL, NULL
},

{
{"yb_force_non_transactional_writes", PGC_USERSET, CLIENT_CONN_STATEMENT,
gettext_noop("Sets the boolean flag to enable or disable non transaction writes."),
NULL
},
&yb_force_non_transactional_writes,
false,
NULL, NULL, NULL
},

/* End-of-list marker */
{
{NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL
Expand Down
7 changes: 7 additions & 0 deletions src/postgres/src/include/executor/ybcModifyTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
#include "nodes/execnodes.h"
#include "executor/tuptable.h"

/**
* YSQL guc variables that can be used to enable non transactional writes.
* e.g. 'SET yb_force_non_transactional_writes=true'
* See also the corresponding entries in guc.c.
*/
extern bool yb_force_non_transactional_writes;

//------------------------------------------------------------------------------
// YugaByte modify table API.

Expand Down

0 comments on commit 8305278

Please sign in to comment.