Efficiently load data across multiple shards using COPY and batch INSERT
SPQR supports bulk data loading operations that automatically distribute data across shards according to your sharding setup. This page covers two main approaches: COPY FROM for file-based loading and batch INSERT for multi-value statements.
Be careful with bulk operations when not using two-phase commit. There is no concept of snapshot isolation across shards in SPQR. This means:
A SELECT that runs on multiple shards at the same time may see partial results
If an error occurs on one shard, the transaction is rolled back only on that shard
The COPY command allows for efficient bulk copying of data from files or stdin into a sharded table. For more information on the PostgreSQL COPY command, see the PostgreSQL documentation.From stdin:
COPY xx (i, j) FROM STDIN WITH DELIMITER '|';1|12|23|3...\.NOTICE: send query to shard(s) : shard1,shard2,shard3,shard4
From file using psql:
dbname=> \copy "spqr-test" (uid, data) from 'data_test.csv' WITH DELIMITER ';';NOTICE: send query to shard(s) : shard1,shard2,shard3,shard4
Batch INSERT rewriting only works with simple protocol. Parameterized queries using extended protocol (prepared statements with placeholders) are not supported for cross-shard batch inserts.
When engine V2 is enabled, SPQR supports batch INSERT statements that target multiple shards. The router automatically analyzes each row in the VALUES clause, determines the target shard based on the sharding key, and rewrites the query into separate INSERT statements per shard.
-- Insert multiple rows that target different shardsINSERT INTO users (id, name) VALUES (1, 'Alice'), -- routes to sh1 (100, 'Bob'), -- routes to sh2 (2, 'Charlie') -- routes to sh1/* __spqr__engine_v2: true */;-- NOTICE: send query to shard(s) : sh1,sh2
INSERT INTO users (id, name) VALUES (100, 'Bob'); → sh2
For atomic batch inserts across shards, combine engine V2 with two-phase commit:
SET __spqr__engine_v2 TO on;SET __spqr__commit_strategy TO '2pc';BEGIN;INSERT INTO users (id, name) VALUES (1, 'Alice'), (100, 'Bob');COMMIT; -- Uses 2PC for atomicity across shards
{"docsConfig":{"$schema":"https://mintlify.com/docs.json","theme":"mint","name":"SPQR","colors":{"primary":"#5382ff","light":"#e2ebff","dark":"#5382ff"},"favicon":"https://mintcdn.com/pg-shardingspqr/55SknXFsB6PObVk2/logo/spqr_icon-main.svg?fit=max\u0026auto=format\u0026n=55SknXFsB6PObVk2\u0026q=85\u0026s=d6c32807f9b0a07418733c3d548908d3","logo":{"light":"https://mintcdn.com/pg-shardingspqr/55SknXFsB6PObVk2/logo/spqr_icon-main.svg?fit=max\u0026auto=format\u0026n=55SknXFsB6PObVk2\u0026q=85\u0026s=d6c32807f9b0a07418733c3d548908d3","dark":"https://mintcdn.com/pg-shardingspqr/55SknXFsB6PObVk2/logo/spqr_icon-main.svg?fit=max\u0026auto=format\u0026n=55SknXFsB6PObVk2\u0026q=85\u0026s=d6c32807f9b0a07418733c3d548908d3","href":"https://pg-sharding.tech/"},"appearance":{"default":"light","strict":null},"navbar":{"primary":{"type":"github","href":"https://github.com/pg-sharding/spqr"}},"styling":{"latex":false},"navigation":{"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"pg-shardingspqr","hasAudioNarration":false,"actualSubdomain":"pg-shardingspqr","customDomains":["docs.pg-sharding.tech"],"gitSource":{"type":"github","owner":"pg-sharding","repo":"spqr","deployBranch":"master","contentDirectory":"docs","isPrivate":false,"mountPath":"","label":""},"feedback":{"thumbs":true,"edits":true},"entitlements":{},"buildId":"6af21cef42832bf137e17b28","clientVersion":"0.0.4094","searchFilterCounts":[{"language":"default","tag":"Documentation","count":102},{"language":"default","tag":"Sharding","count":19},{"language":"default","tag":"Features","count":16},{"language":"default","tag":"Configuration Guide","count":6},{"language":"default","tag":"Deployment Guide","count":5},{"language":"default","tag":"Cluster Components","count":4},{"language":"default","tag":"Balancing","count":3},{"language":"default","tag":"Admin Console","count":3},{"language":"default","tag":"Query routing","count":1}]},"assistantConfig":{},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[],"googleFontsInlined":false},"customCss":[],"page":{"pagePath":"sharding/bulk","slug":"/sharding/bulk","pageMetadata":{"title":"Bulk Operations","description":"Efficiently load data across multiple shards using COPY and batch INSERT","href":"/sharding/bulk"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Efficiently load data across multiple shards using COPY and batch INSERT\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,li:`li`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Heading,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsxs(_components.p,{children:[`SPQR supports bulk data loading operations that automatically distribute data across shards according to your sharding setup. This page covers two main approaches: `,_jsx(_components.code,{children:`COPY FROM`}),` for file-based loading and batch `,_jsx(_components.code,{children:`INSERT`}),` for multi-value statements.`]}),`\n`,_jsxs(Warning,{children:[_jsx(_components.p,{children:`Be careful with bulk operations when not using two-phase commit. There is no concept of snapshot isolation across shards in SPQR. This means:`}),_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[`A `,_jsx(_components.code,{children:`SELECT`}),` that runs on multiple shards at the same time may see partial results`]}),`\n`,_jsx(_components.li,{children:`If an error occurs on one shard, the transaction is rolled back only on that shard`}),`\n`,_jsxs(_components.li,{children:[`Use `,_jsx(_components.a,{href:`/sharding/distributed_transactions#2-two-phase-commit-2pc`,children:`two-phase commit`}),` for atomic operations across shards`]}),`\n`]})]}),`\n`,_jsx(Heading,{level:`2`,id:`copy-from`,children:`COPY FROM`}),`\n`,_jsxs(_components.p,{children:[`The `,_jsx(_components.code,{children:`COPY`}),` command allows for efficient bulk copying of data from files or stdin into a sharded table. For more information on the PostgreSQL `,_jsx(_components.code,{children:`COPY`}),` command, see the `,_jsx(_components.a,{href:`https://www.postgresql.org/docs/current/sql-copy.html`,children:`PostgreSQL documentation`}),`.`]}),`\n`,_jsx(_components.p,{children:_jsx(_components.strong,{children:`From stdin:`})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`7`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`7`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`COPY`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` xx (i, j) `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`FROM`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` STDIN `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`WITH`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` DELIMITER `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`'|'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`|`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`|`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`3`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`|`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`3`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`...`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`\\\\.`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`NOTICE: `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`send`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` query `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`to`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` shard(s) : shard1,shard2,shard3,shard4`})]}),`\n`]})})}),`\n`,_jsx(_components.p,{children:_jsx(_components.strong,{children:`From file using psql:`})}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`shellscript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`shellscript`,children:_jsxs(_components.code,{language:`shellscript`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`dbname`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=>`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D7BA7D`},children:` \\\\c`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`opy`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"spqr-test\"`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:` (uid,`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` data`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) from `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`'data_test.csv'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` WITH DELIMITER `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`';'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:`NOTICE:`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` send`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` query`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` to`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` shard`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#DCDCAA`},children:`s`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`:`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` shard1,shard2,shard3,shard4`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`batch-insert`,children:`Batch INSERT`}),`\n`,_jsx(Warning,{children:_jsxs(_components.p,{children:[`Batch INSERT rewriting only works with `,_jsx(_components.strong,{children:`simple protocol`}),`. Parameterized queries using extended protocol (prepared statements with placeholders) are not supported for cross-shard batch inserts.`]})}),`\n`,_jsxs(_components.p,{children:[`When `,_jsx(_components.a,{href:`/routing/hints#spqr-engine-v2`,children:`engine V2`}),` is enabled, SPQR supports batch INSERT statements that target multiple shards. The router automatically analyzes each row in the VALUES clause, determines the target shard based on the sharding key, and rewrites the query into separate INSERT statements per shard.`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`7`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`7`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- Insert multiple rows that target different shards`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INSERT INTO`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` users (id, `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`name`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`VALUES`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`'Alice'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`), `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- routes to sh1`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`100`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`'Bob'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`), `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- routes to sh2`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`'Charlie'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- routes to sh1`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`/* __spqr__engine_v2: true */`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- NOTICE: send query to shard(s) : sh1,sh2`})}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`SPQR rewrites this into:`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.code,{children:`INSERT INTO users (id, name) VALUES (1, 'Alice'), (2, 'Charlie');`}),` → sh1`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.code,{children:`INSERT INTO users (id, name) VALUES (100, 'Bob');`}),` → sh2`]}),`\n`]}),`\n`,_jsx(_components.p,{children:`For atomic batch inserts across shards, combine engine V2 with two-phase commit:`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`6`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`6`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`SET`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` __spqr__engine_v2 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`TO`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` on`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`SET`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` __spqr__commit_strategy `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`TO`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` '2pc'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`BEGIN`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INSERT INTO`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` users (id, `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`name`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`VALUES`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`'Alice'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`), (`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`100`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`'Bob'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`COMMIT`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`; `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- Uses 2PC for atomicity across shards`})]}),`\n`]})})})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Bulk Operations","description":"Efficiently load data across multiple shards using COPY and batch INSERT","href":"/sharding/bulk"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"COPY FROM","slug":"copy-from","depth":2,"children":[]},{"title":"Batch INSERT","slug":"batch-insert","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"pg-shardingspqr","siteName":"SPQR","title":"Bulk Operations - SPQR","navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-09-03T00:25:57.226Z","originalFileLocation":"sharding/bulk.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"light\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Efficiently load data across multiple shards using COPY and batch INSERT\">\u003cmeta data-mint-head name=\"application-name\" content=\"SPQR\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Bulk Operations - SPQR\">\u003cmeta data-mint-head property=\"og:description\" content=\"Efficiently load data across multiple shards using COPY and batch INSERT\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"SPQR\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://docs.pg-sharding.tech/sharding/bulk\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://pg-shardingspqr.mintlify.site/_mintlify/api/og/v2?division=Sharding\u0026amp;title=Bulk+Operations\u0026amp;description=Efficiently+load+data+across+multiple+shards+using+COPY+and+batch+INSERT\u0026amp;theme=adb02f870ad3d5f731d83243\u0026amp;signature=8cff3d428d1cf9d6ac0b3458164770404d7c376d9993d6604ab6c0d17dd77373\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Bulk Operations - SPQR\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Efficiently load data across multiple shards using COPY and batch INSERT\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://pg-shardingspqr.mintlify.site/_mintlify/api/og/v2?division=Sharding\u0026amp;title=Bulk+Operations\u0026amp;description=Efficiently+load+data+across+multiple+shards+using+COPY+and+batch+INSERT\u0026amp;theme=adb02f870ad3d5f731d83243\u0026amp;signature=8cff3d428d1cf9d6ac0b3458164770404d7c376d9993d6604ab6c0d17dd77373\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"SPQR\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#5382ff\">\u003clink data-mint-head rel=\"canonical\" href=\"https://docs.pg-sharding.tech/sharding/bulk\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/sharding/bulk.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://docs.pg-sharding.tech/#organization\",\"name\":\"SPQR\",\"url\":\"https://docs.pg-sharding.tech\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/pg-shardingspqr/55SknXFsB6PObVk2/logo/spqr_icon-main.svg?fit=max\u0026auto=format\u0026n=55SknXFsB6PObVk2\u0026q=85\u0026s=d6c32807f9b0a07418733c3d548908d3\"}},{\"@type\":\"WebSite\",\"@id\":\"https://docs.pg-sharding.tech#website\",\"name\":\"SPQR\",\"url\":\"https://docs.pg-sharding.tech\",\"publisher\":{\"@id\":\"https://docs.pg-sharding.tech/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://docs.pg-sharding.tech/sharding/bulk#webpage\",\"url\":\"https://docs.pg-sharding.tech/sharding/bulk\",\"name\":\"Bulk Operations\",\"description\":\"Efficiently load data across multiple shards using COPY and batch INSERT\",\"dateModified\":\"2026-09-03T00:25:57.226Z\",\"isPartOf\":{\"@id\":\"https://docs.pg-sharding.tech#website\"},\"breadcrumb\":{\"@id\":\"https://docs.pg-sharding.tech/sharding/bulk#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://docs.pg-sharding.tech/sharding/bulk#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Documentation\",\"item\":\"https://docs.pg-sharding.tech/welcome/index\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sharding\",\"item\":\"https://docs.pg-sharding.tech/welcome/get_started\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bulk Operations\",\"item\":\"https://docs.pg-sharding.tech/sharding/bulk\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://docs.pg-sharding.tech/sharding/bulk#article\",\"headline\":\"Bulk Operations\",\"name\":\"Bulk Operations\",\"description\":\"Efficiently load data across multiple shards using COPY and batch INSERT\",\"url\":\"https://docs.pg-sharding.tech/sharding/bulk\",\"mainEntityOfPage\":{\"@id\":\"https://docs.pg-sharding.tech/sharding/bulk#webpage\"},\"dateModified\":\"2026-09-03T00:25:57.226Z\",\"publisher\":{\"@id\":\"https://docs.pg-sharding.tech/#organization\"},\"isPartOf\":{\"@id\":\"https://docs.pg-sharding.tech#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"SPQR\",\"creator\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","siteName":"SPQR","nav":{"global":null,"anchors":[{"anchor":"Documentation","icon":"book-open-cover","groups":[{"group":"Documentation","pages":[{"title":"Welcome to SPQR","href":"/welcome"},{"title":"Get Started","href":"/welcome/get_started"},{"group":"Features","pages":[{"title":"Sharding","href":"/features/sharding"},{"title":"Default shard","href":"/features/default_shard"},{"title":"Reference tables","href":"/features/reference_tables"},{"title":"Distributed Transactions","href":"/features/distributed_transactions"},{"title":"Transaction and session pooling","href":"/features/pooling"},{"title":"Fault Tolerance","href":"/features/fault_tolerance"},{"title":"Data Balancing","href":"/features/data_balancing"},{"title":"Cross-shard queries","href":"/features/cross_shard_queries"},{"title":"Multiple servers and failover","href":"/features/multiple_servers_and_failover"},{"title":"Works over PostgreSQL protocol","href":"/features/protocol"},{"title":"Dedicated read-only mode","href":"/features/read_only"},{"title":"Performance","href":"/features/performance"},{"title":"Various authentication types","href":"/features/authentication"},{"title":"Live configuration reloading","href":"/features/config_reloading"},{"title":"Statistics","href":"/features/statistics"},{"title":"Metrics","href":"/features/metrics"}]},{"group":"Deployment Guide","pages":[{"title":"Overview","href":"/deployment/overview"},{"title":"Router with Init SQL","href":"/deployment/bare_router"},{"title":"Router + Coordinator","href":"/deployment/router_coordinator"},{"title":"SPQR Infra Mode","href":"/deployment/spqrinfra"},{"title":"Prevent direct writes to shards","href":"/deployment/spqrguard"}]},{"group":"Configuration Guide","pages":[{"title":"Router","href":"/configuration/router"},{"title":"Virtual functions","href":"/configuration/virtual_function"},{"title":"Coordinator","href":"/configuration/coordinator"},{"title":"Balancer","href":"/configuration/balancer"},{"title":"Auth settings","href":"/configuration/auth"},{"title":"Roles settings","href":"/configuration/roles"}]},{"group":"Sharding","pages":[{"title":"Get Started","href":"/welcome/get_started"},{"title":"Direct key sharding","href":"/sharding/ranged"},{"title":"Hash-based sharding","href":"/sharding/hashed"},{"title":"Sharded Tables","href":"/sharding/sharded_tables"},{"title":"Reference Tables","href":"/sharding/reference_tables"},{"title":"Composite Sharding Keys","href":"/sharding/composite_keys"},{"title":"Distributed Transactions","href":"/sharding/distributed_transactions"},{"group":"Cluster Components","pages":[{"title":"Overview","href":"/sharding/cluster_components/overview"},{"title":"Shard","href":"/sharding/cluster_components/shard"},{"title":"Router","href":"/sharding/cluster_components/router"},{"title":"Coordinator","href":"/sharding/cluster_components/coordinator"}]},{"group":"Balancing","pages":[{"title":"Overview","href":"/sharding/balancing/overview"},{"title":"Details","href":"/sharding/balancing/details"},{"title":"Manual shard rebalancing","href":"/sharding/manual_rebalancing"}]},{"group":"Admin Console","pages":[{"title":"How to connect","href":"/sharding/console/how_to_connect"},{"title":"Concepts","href":"/sharding/console/concepts"},{"title":"SQL commands","description":"Create distributions, key ranges and tables and see cluster info","href":"/sharding/console/sql_commands"}]},{"title":"Bulk Operations","href":"/sharding/bulk"},{"title":"Default shard","description":null,"href":"/sharding/default_shard"}]},{"group":"Query routing","pages":[{"title":"Routing hints","href":"/routing/hints"}]},{"title":"Design Goals","href":"/welcome/design"},{"title":"Talks","href":"/welcome/talks"},{"title":"FAQ","href":"/welcome/faq"}]}]},{"anchor":"Community","href":"https://t.me/+jMGhyjwicpI3ZWQy","icon":"telegram"},{"anchor":"Releases","href":"https://github.com/pg-sharding/spqr/releases","icon":"clock"}]},"basePath":"","title":"Bulk Operations - SPQR","buildId":"dpl_42LwxxU1kGN114H4XzwGYXa9ZDah","deploymentId":"dpl_42LwxxU1kGN114H4XzwGYXa9ZDah","dynamics":[],"chunks":[]}