INSERT SQL statement.
Before you begin
Before reading this page, do the following:- Create a CockroachDB Standard cluster or start a local cluster.
- Install a Driver or ORM Framework.
- Connect to the database.
Your application should use a retry loop to handle transaction errors that can occur under contention.
Insert rows
When inserting multiple rows, a single multi-row insert statement is faster than multiple single-row statements.cockroach sql reference docs.
- Build a Python App with CockroachDB (psycopg3)
- Build a Python App with CockroachDB and SQLAlchemy
- Build a Python App with CockroachDB and Django
- Build a Python App with CockroachDB and asyncpg
Bulk insert
If you need to get a lot of data into a CockroachDB cluster quickly, use theIMPORT statement instead of sending SQL INSERTs from application code. It will be much faster because it bypasses the SQL layer altogether and writes directly to the data store using low-level commands. For instructions, see the Migration Overview.
Limit the size of rows
To help you avoid failures arising from misbehaving applications that bloat the size of rows, you can specify the behavior when a row or individual column family larger than a specified size is written to the database. Use the cluster settingssql.guardrails.max_row_size_log to discover large rows and sql.guardrails.max_row_size_err to reject large rows.
When you write a row that exceeds sql.guardrails.max_row_size_log:
INSERT,UPSERT,UPDATE,CREATE TABLE AS,CREATE INDEX,ALTER TABLE,ALTER INDEX,IMPORT, orRESTOREstatements will log aLargeRowto theSQL_PERFchannel.SELECT,DELETE,TRUNCATE, andDROPare not affected.
sql.guardrails.max_row_size_err:
INSERT,UPSERT, andUPDATEstatements will fail with a code54000 (program_limit_exceeded)error.CREATE TABLE AS,CREATE INDEX,ALTER TABLE,ALTER INDEX,IMPORT, andRESTOREstatements will log aLargeRowInternalevent to theSQL_INTERNAL_PERFchannel.SELECT,DELETE,TRUNCATE, andDROPare not affected.
sql.guardrails.max_row_size_log in conjunction with SELECT pg_column_size() queries to detect and fix any existing large rows before lowering sql.guardrails.max_row_size_err.
See also
Reference information related to this task:- Migration Overview
IMPORT- Import performance
INSERTUPSERT- Transaction Contention
- Multi-row DML best practices
- Insert Multiple Rows

