Help › Import, export and back up

Copy tables between connections

Copy tables or a result into another connection, of the same database or another kind, creating the tables or adding to them, with every change of type listed first.

Querlyn 0.1.0 is not released yet. This help describes it ahead of release.

Querlyn copies tables, or the rows of a result, from one connection into another: from PostgreSQL to SQLite, from MySQL to PostgreSQL, from Production into a local copy. For each table you choose whether the copy creates it on the other connection, adds its rows to a table that is there, or replaces that table's rows. Before anything runs, the plan shows each column's type on the other side and lists every value that may not arrive exactly.

Start a copy

The connection you copy from must be connected.

To copyDo this
A tableSelect it in the sidebar and choose File › Copy to Another Connection…, or right-click it and choose Copy to Another Connection….
Several tables, or a whole schemaSelect the schema in the sidebar and choose the same. Every table of the schema is chosen at the start.
The rows of a resultChoose Query › Copy Result to Table…, or right-click a cell of the result and choose Copy Result to Table….

The dialog has three steps: Choose, Plan and Copy. Next and Back move between the first two.

Step 1: what and where

  • Tables: tick the tables to copy. All and None tick or clear them all.
  • Rows (a result whose rows are not all loaded): Every row (runs the query again) copies everything the query returns; The rows loaded copies what the grid holds. Every row is offered only for a single statement that only reads; otherwise a line says why not.
  • Connection: where the copy goes. Any of your connections can be chosen, the one you copy from included (to copy into another schema or under another name). A connection that is not connected is connected when you choose it. A read-only connection is refused.
  • Database (on servers with several) and Schema: where the tables go on that connection. The connection's own schema is chosen at the start.

Step 2: the plan

The plan lists each table with where it goes, what the copy does with it and how many of its columns may change values. Select a table to see its plan below the list.

Target table is the name on the other connection. It starts as the same name, written the way the other database writes names (in capitals on Oracle and Snowflake, for example).

The copy is one of three:

ChoiceWhat happens
CreateCreates the table on the other connection, copies the rows, then adds its indexes and foreign keys. Chosen at the start when no table of that name is there.
AppendAdds the rows to the table that is there; its rows stay. Chosen at the start when a table of that name is there.
Replace RowsDeletes every row of the table that is there, then copies the rows. Querlyn asks before it runs (see Replacing rows).

When the copy creates the table

Each column is listed with its type here and its type there, and the CREATE TABLE statement is shown as it will run. A column whose values may not all arrive exactly is marked Lossy, with the reason: a time zone the other database cannot keep, a decimal wider than it holds, a type it does not have. Other differences are noted under the column, such as a UUID kept as text.

The new table keeps:

  • the primary key, NOT NULL, unique keys and indexes;
  • each column's default, where the other database has the same thing (the current time, a new UUID, a number or a text); a default it cannot write is listed and left out;
  • numbering (an identity, auto-increment or serial column), where the other database can number that column;
  • foreign keys to tables that are copied too or are there already. Others are listed and left out.

Comments are kept. Check constraints are kept between databases of the same kind only, since they are written in the source database's SQL. Triggers and permissions are not copied.

Create indexes and foreign keys after the rows (on at the start) adds indexes and foreign keys after all rows are in, which is faster. Clear it to leave them out: the table then has its primary key and unique keys only.

When the copy adds to a table

Each column of the table that is there is listed with the column it is filled from. Columns are matched by name, ignoring upper and lower case, spaces, hyphens, dots and underscores. Choose another column, or Default to let the database fill it. A column that needs a value (NOT NULL without a default) must get one, and a column the database computes cannot. Columns of the source that go nowhere are listed after "Not copied".

Copy N Tables is enabled when every table has a plan; until then, the table that needs something says what.

Copying into Production

When the connection the copy goes to is a Production connection, Querlyn asks first. The question is titled Copy into Production? and names each table, where it goes, and whether it is created, added to, or has its rows replaced.

Replacing rows

Replacing rows is destructive, so it is asked like a destructive statement (see Confirm risky statements): the question names each table whose rows are deleted and the statement that deletes them (TRUNCATE on PostgreSQL and ClickHouse, DELETE elsewhere). On Production this is a second question after the first. Where the rows are deleted with DELETE, the question reminds you that it also deletes the rows of other tables whose foreign keys say ON DELETE CASCADE.

On ClickHouse and Snowflake, where Querlyn copies without a transaction, the rows are deleted only once every row is staged, just before the staged rows are moved in. If that last step fails, the table is left empty; the plan, the question and the summary say so.

Nothing is deleted before you answer. Whether Querlyn asks follows Ask before running statements for the connection's risk level, as for statements you run.

While it runs

The dialog shows each table as it is copied: the rows copied, of about how many, the rate and the time left. Tables are copied one after another, tables that others refer to first.

ButtonWhat it does
PauseStops after the batch being written, until Resume. The table's transaction stays open, so its locks are held while paused. File › Pause Copy and File › Resume Copy do the same. Not offered when the copy goes to a Production connection: there only Cancel Copy, so a Production table is never left locked while nobody watches.
Cancel CopyStops the copy (File › Cancel Copy). The table being copied is rolled back; tables already copied stay.
HideKeeps it running. Its progress is then in the status bar; a click there, or File › Show Copy, brings the dialog back.

Quitting, or disconnecting either connection, while a copy runs asks first: Cancel Copy stops it, Keep Running does not.

One copy runs at a time. It reads and writes on sessions of its own, so your query tabs and their transactions are not touched. On the connection the copy goes to, the lock timeout of its risk level applies: a statement that waits longer for another session's lock fails its table instead of waiting. The statement console lists every statement the copy sends, its rows as a count.

The summary

When the copy ends, each table says how many rows were copied, how long it took and how they were written, or why it failed. Copy Summary copies the whole summary as text.

Each table is copied in a transaction of its own: a table that fails is left as it was, and the other tables are copied. When the database refuses a row, Querlyn finds that row where the database lets it and shows it, with the database's message: "Not copied: the target refused row 4,012", then the row's values. Values of a column marked sensitive show as ••••••••; see Screen-share mode.

Where a database cannot create a table inside a transaction (MySQL, MariaDB, Oracle Database, ClickHouse, Snowflake), a table the copy created is dropped again when its copy fails. When that drop fails too, the summary says so, and you drop it yourself.

How values are copied

Values are copied as they are, not as text: decimals keep every digit, times keep their fraction of a second, binary values their bytes, and JSON, UUIDs and arrays stay JSON, UUIDs and arrays where the other database has them. Where it does not, the plan says what they become:

ValueWhere the other database has no such type
A time with a time zoneMySQL, MariaDB, ClickHouse (and SQL Server, Snowflake for a time of day): written as the same instant in UTC; the offset is not kept. SQLite keeps the offset in the text it stores.
A UUIDText of 36 characters.
An arrayA JSON array.
JSONJSON text on SQL Server, Redshift, Snowflake, ClickHouse and Oracle Database before 21c.
A decimal without a precisionMySQL, MariaDB, SQL Server, DuckDB, Snowflake, Redshift and ClickHouse need one: the widest they have, with 10 digits after the point (30 on MySQL and MariaDB). Wider values are refused, and further digits after the point are rounded. Oracle Database keeps 38 significant digits. SQLite keeps it exactly as text.
A long text in a key or an indexMySQL and MariaDB limit it to 768 characters, SQL Server to 450, Oracle Database to 1,000. Longer values are refused.
A type of one database only (inet, geometry, an enum)Text. An enum's list of allowed values is not kept.
A booleantinyint(1) on MySQL and MariaDB, bit on SQL Server, NUMBER(1) on Oracle Database before 23ai.

A value that does not fit is refused by the database and fails its table; Querlyn never cuts a value to fit.

Numbering after an append

When rows are added with their own numbers to a column the database numbers itself, Querlyn moves the numbering on past the highest number, so the next row added gets a new one. On Amazon Redshift and Snowflake that is not possible; the plan says so.

Masked columns

The copy writes the real values. A column marked sensitive keeps its mark on the other connection, under the table's new name, so it is masked there too.

How rows are written

Rows are read and written in batches, so a table of any size is never held whole. Each database is written the fastest way Querlyn has:

DatabaseRows are written by
PostgreSQLCOPY … FROM STDIN
SQLiteOne prepared INSERT, run for each row
DuckDBIts appender
Oracle DatabaseArray INSERT
ClickHouseINSERT … FORMAT TabSeparated into a temporary table, then moved into the target in one step
SnowflakeINSERT with bound values into a temporary table, then moved into the target in one step
BigQueryINSERT statements of up to 500 rows each, with bound values
MySQL, MariaDB, SQL Server, Amazon RedshiftINSERT statements of many rows each

A table without transactions on MySQL or MariaDB (MyISAM, for example) cannot be added to: it could not be rolled back.

To copy rows into a file instead, see Export results to a file. To load a file, see Import data from a file.

Help for Querlyn 0.1.0. All help topics