Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: encountered pq: command is too large for CREATE TABLE AS and INSERT INTO ... SELECT operations #25828

Closed
drewdeally opened this issue May 22, 2018 · 9 comments
Assignees
Labels
A-sql-execution Relating to SQL execution. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)

Comments

@drewdeally
Copy link

Created 2 column table with 2.7M rows and tried to CTAS from primary table to new table and encountered the following issue.



root@:26257/test> select count(1) from accounts;
+---------+
|  count  |
+---------+
| 2790683 |
+---------+
(1 row)


root@:26257/test> insert into accounts2 select * from accounts;
pq: command is too large: 116025971 bytes (max: 67108864)
root@:26257/test> drop table accounts2;
DROP TABLE

Time: 30.228115ms

root@:26257/test> create table accounts2 as select * from accounts;
pq: command is too large: 345539008 bytes (max: 67108864)
root@:26257/test> select * from crdb_internal.node_statement_statistics;
+---------+------------------+-------+--------------------------------------


@jordanlewis jordanlewis added A-sql-execution Relating to SQL execution. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) labels May 22, 2018
@jordanlewis
Copy link
Member

The root cause of this issue is the limit on transaction size. INSERT INTO ... SELECT is naturally affected by this, because it has to preserve transactionality.

CREATE TABLE AS, on the other hand, does not have to be transactional. We should be able to support large CREATE TABLE AS statements by breaking them up into multiple transactions that use a consistent transaction timestamp, I think.

@jordanlewis
Copy link
Member

cc @tschottdorf @danhhz - perhaps there's a fancier way to do this with SSTable rewriting?

@bdarnell
Copy link
Contributor

CREATE TABLE AS does need to be transactional - the resulting table should come into existence all at once from the perspective of any outside observer.

Note that the transaction size limit has been significantly raised in 2.0, but CREATE TABLE AS runs into the (much smaller) command size limit. A transactional CREATE TABLE AS that splits its work into multiple KV-level commands would be able to handle larger tables (although it would still be slower than using the export/import machinery).

@benesch benesch changed the title sql: encountered pq: command is too large for CTAS and IAS operations sql: encountered pq: command is too large for CREATE TABLE AS and INSERT INTO ... SELECT operations Jul 24, 2018
@benesch
Copy link
Contributor

benesch commented Jul 24, 2018

Adjusted the title to make this easier to search for.

@drewdeally
Copy link
Author

So Please confirm CTAS or CTAS with 1=2 and IAS would be the same issue.. I am sure of it - just want to confirm.

tim-o added a commit to cockroachdb/docs that referenced this issue Aug 14, 2018
Docs currently suggest this is fixed, but per cockroachdb/cockroach#25828 it still applies to INSERT INTO... SELECT FROM and CREATE TABLE AS SELECT statements.
tim-o added a commit to cockroachdb/docs that referenced this issue Aug 14, 2018
@adityamaru27
Copy link
Contributor

@awoods187 @jordanlewis @rolandcrosby
After discussion with @dt, we considered breaking the change into two stages:

  1. Make CTAS a job.
  2. Use bulk ingestion to execute the writes much faster.

The migration to a job would imply that the table created by CTAS would not be usable within the same txn, as jobs are only processed following a commit. Just wanted to run this by the Execution team before any dev work is started.

adityamaru27 added a commit to adityamaru27/cockroach that referenced this issue Jun 6, 2019
CTAS was initially planned and executed in the same user transaction.
As a first step in improving its performance and scalability
(issue cockroachdb#25828) we needed to split the responsibility of creating a new
TableDescriptor, and executing the AS query.

While the user txn continues to create and store the new desc, the
execution has been made async by moving the logic to the
SchemaChanger. This requires the SchemaChanger to be able to
bootstrap its state from the desc it reads, thereby requiring the
addition of these fields.

Release note: None
adityamaru27 added a commit to adityamaru27/cockroach that referenced this issue Jun 20, 2019
CTAS was initially planned and executed in the same user transaction.
As a first step in improving its performance and scalability
(issue cockroachdb#25828) we needed to split the responsibility of creating a new
TableDescriptor, and executing the AS query.

While the user txn continues to create and store the new desc, the
execution has been made async by moving the logic to the
SchemaChanger. This requires the SchemaChanger to be able to
bootstrap its state from the desc it reads, thereby requiring the
addition of these fields.

Release note: None
adityamaru27 added a commit to adityamaru27/cockroach that referenced this issue Jun 24, 2019
CTAS was initially planned and executed in the same user transaction.
As a first step in improving its performance and scalability
(issue cockroachdb#25828) we needed to split the responsibility of creating a new
TableDescriptor, and executing the AS query.

While the user txn continues to create and store the new desc, the
execution has been made async by moving the logic to the
SchemaChanger. This requires the SchemaChanger to be able to
bootstrap its state from the desc it reads, thereby requiring the
addition of these fields.

Release note: None
craig bot pushed a commit that referenced this issue Jun 25, 2019
38061: pkg: Split CTAS execution between user txn and SchemaChanger. r=adityamaru27 a=adityamaru27

As a first step in improving the performance and scalability of the `CTAS` command,
(issue #25828) we needed to split the responsibility of creating a new
`TableDescriptor`, and executing the `AS` query to backfill the new table.

While the user txn continues to create and store the new desc, the
execution of the `AS` query has been made async by moving the logic to the
SchemaChanger.

Co-authored-by: Aditya Maru <[email protected]>
@adityamaru27
Copy link
Contributor

adityamaru27 commented Jul 10, 2019

With #38374 now merged, I ran a benchmark on a 4 node default configuration cluster to compare CTAS performance in the 19.1.2 release, and on master.
The previous implementation would error out with pq: command is too large for greater than 300k rows, thus I used a bank table with 300k rows (100MiB) in my testing.

Average over 5 runs:
v19.1.2 - 7.88s
master - 1.02s
So we observe an 87% speedup in smaller tables.

I then tested the new implementation with source tables of varying sizes all the way up to a 25 GiB, 50 million row table which it completed in 18m14s.

@jordanlewis
Copy link
Member

This is awesome @adityamaru27! I am super psyched about it.

@tbg
Copy link
Member

tbg commented Jul 10, 2019

Very nice work @adityamaru27.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-execution Relating to SQL execution. C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)
Projects
None yet
Development

No branches or pull requests

6 participants