diff --git a/src/current/_data/redirects.yml b/src/current/_data/redirects.yml index b98d2e398da..abbc661cc46 100644 --- a/src/current/_data/redirects.yml +++ b/src/current/_data/redirects.yml @@ -81,7 +81,7 @@ - destination: admin-ui-overview.md sources: ['explore-the-admin-ui.md'] - versions: ['v1.1', 'v2.0', 'v2.1', 'v19.1', 'v19.2', 'v20.1'] + versions: ['v1.1', 'v2.0', 'v2.1', 'v19.1', 'v20.1'] - destination: architecture/distribution-layer.md#range-merges sources: ['range-merges.md'] @@ -89,11 +89,11 @@ - destination: architecture/overview.md sources: ['architecture/index.md'] - versions: ['v1.1', 'v2.0', 'v2.1', 'v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v1.1', 'v2.0', 'v2.1', 'v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: authorization.md sources: ['create-and-manage-users.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: authorization.md sources: ['roles.md'] @@ -103,7 +103,7 @@ sources: - backup-data.md - restore-data.md - versions: ['v2.1', 'v19.1', 'v19.2', 'v20.1'] + versions: ['v2.1', 'v19.1', 'v20.1'] - destination: cdc-queries.md sources: ['cdc-transformations.md'] @@ -119,59 +119,59 @@ - destination: cockroach-cert.md sources: ['create-security-certificates.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-debug-ballast.md sources: ['debug-ballast.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-debug-encryption-active-key.md sources: ['debug-encryption-active-key.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-debug-merge-logs.md sources: ['debug-merge-logs.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-debug-zip.md sources: ['debug-zip.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-dump.md sources: ['sql-dump.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-gen.md sources: ['generate-cockroachdb-resources.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-init.md sources: ['initialize-a-cluster.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-node.md sources: ['view-node-details.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-quit.md sources: ['stop-a-node.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-sql.md sources: ['use-the-built-in-sql-client.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1', 'v21.2', 'v21.2', 'v22.2', 'v23.1', 'v23.2'] + versions: ['v20.1', 'v20.2', 'v21.1', 'v21.2', 'v21.2', 'v22.2', 'v23.1', 'v23.2'] - destination: cockroach-sqlfmt.md sources: ['use-the-query-formatter.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-start.md sources: ['start-a-node.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroach-version.md sources: ['view-version-details.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: cockroachdb-feature-availability.md sources: @@ -205,13 +205,13 @@ - destination: demo-low-latency-multi-region-deployment.md sources: ['demo-geo-partitioning.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: demo-replication-and-rebalancing.md sources: - demo-automatic-rebalancing.md - demo-data-replication.md - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: v23.2/disaster-recovery-planning.md sources: ['v23.2/disaster-recovery.md'] @@ -275,7 +275,7 @@ - destination: manual-deployment.md sources: ['cloud-deployment.md'] - versions: ['v1.1', 'v2.0', 'v2.1', 'v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v1.1', 'v2.0', 'v2.1', 'v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: plan-your-cluster-serverless.md sources: @@ -307,7 +307,7 @@ - destination: molt/migration-overview.md sources: ['import-data.md'] - versions: ['v2.1', 'v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v2.1', 'v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: molt/migration-overview.md sources: @@ -330,11 +330,11 @@ - destination: orchestrate-cockroachdb-with-kubernetes-multi-cluster.md sources: ['orchetrate-cockroachdb-with-kubernetes-multi-region.md'] - versions: ['v2.0', 'v2.1', 'v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v2.0', 'v2.1', 'v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: performance-benchmarking-with-tpc-c-1k-warehouses.md sources: ['performance-benchmarking-with-tpc-c.md'] - versions: ['v19.2', 'v20.1'] + versions: ['v20.1'] - destination: performance-benchmarking-with-tpcc-large.md sources: ['performance-benchmarking-with-tpc-c-100k-warehouses.md'] @@ -355,7 +355,7 @@ - destination: postgresql-compatibility.md sources: ['porting-postgres.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: regional-tables.md sources: @@ -369,15 +369,15 @@ - destination: scalar-expressions.md sources: ['sql-expressions.md'] - versions: ['v2.0', 'v2.1', 'v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v2.0', 'v2.1', 'v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: select-clause.md sources: ['select.md'] - versions: ['v2.0', 'v2.1', 'v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v2.0', 'v2.1', 'v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: selection-queries.md sources: ['selection-clauses.md'] - versions: ['v2.0', 'v2.1', 'v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v2.0', 'v2.1', 'v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: serverless-faqs.md sources: ['free-faqs.md'] @@ -395,7 +395,7 @@ - show-create-sequence.md - show-create-table.md - show-create-view.md - versions: ['v2.1', 'v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v2.1', 'v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: show-experimental-ranges.md sources: ['show-testing-ranges.md'] @@ -403,7 +403,7 @@ - destination: show-ranges.md sources: ['show-experimental-ranges.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: show-statements.md sources: ['show-queries.md'] @@ -429,7 +429,7 @@ - destination: start-a-local-cluster-in-docker-mac.md sources: ['start-a-local-cluster-in-docker.md'] - versions: ['v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v20.1', 'v20.2', 'v21.1'] - destination: stream-data-out-of-cockroachdb-using-changefeeds.md sources: ['change-data-capture.md'] @@ -453,7 +453,7 @@ - destination: topology-patterns.md sources: ['cluster-topology-patterns.md'] - versions: ['v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: ui-cdc-dashboard.md sources: ['admin-ui-cdc-dashboard.md'] @@ -766,7 +766,7 @@ - simplified-deployment.md - sql.md - strong-consistency.md - versions: ['v19.1', 'v19.2', 'v20.1', 'v20.2', 'v21.1'] + versions: ['v19.1', 'v20.1', 'v20.2', 'v21.1'] - destination: https://docs.peewee-orm.com/en/latest/peewee/playhouse.html sources: [':version/build-a-python-app-with-cockroachdb-peewee.md'] diff --git a/src/current/_data/releases.yml b/src/current/_data/releases.yml index 427a26fb0dd..b1b6a0eba79 100644 --- a/src/current/_data/releases.yml +++ b/src/current/_data/releases.yml @@ -1458,16 +1458,6 @@ previous_release: v19.1.0 -- release_name: v19.2.0-alpha.20190606 - major_version: v19.2 - release_date: '2019-06-06' - release_type: Testing - go_version: go1.12.12 - sha: 53fada85741a2a096c240b2e8a2347b523c4a463 - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - - release_name: v19.1.2 major_version: v19.1 @@ -1481,17 +1471,6 @@ previous_release: v19.1.1 -- release_name: v19.2.0-alpha.20190701 - major_version: v19.2 - release_date: '2019-07-01' - release_type: Testing - go_version: go1.12.12 - sha: 2c865eeb3e3b244468ffc509a62778bd1f46740f - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - previous_release: v19.2.0-alpha.20190606 - - release_name: v2.1.8 major_version: v2.1 @@ -1517,17 +1496,6 @@ previous_release: v19.1.2 -- release_name: v19.2.0-alpha.20190805 - major_version: v19.2 - release_date: '2019-08-05' - release_type: Testing - go_version: go1.12.12 - sha: 5bd37e8eb58ca66b9293c234bc572411057fec3a - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - previous_release: v19.2.0-alpha.20190701 - - release_name: v19.1.4 major_version: v19.1 @@ -1565,90 +1533,12 @@ previous_release: v19.1.4 -- release_name: v19.2.0-beta.20190930 - major_version: v19.2 - release_date: '2019-09-30' - release_type: Testing - go_version: go1.12.12 - sha: 250f4c36de2b88eff443cf9be9cd5d2759312c88 - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - previous_release: v19.2.0-alpha.20190805 - -- release_name: v19.2.0-beta.20191014 - major_version: v19.2 - release_date: '2019-10-14' - release_type: Testing - go_version: go1.12.12 - sha: 27ea46b456d6b3dabac2c0f6c5c120ed316fffce - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - previous_release: v19.2.0-beta.20190930 - -- release_name: v19.2.0-rc.1 - major_version: v19.2 - release_date: '2019-10-21' - release_type: Testing - go_version: go1.12.12 - sha: 239513342a2d23f683bbc1d386f87ff59cc78d10 - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - previous_release: v19.2.0-beta.20191014 - -- release_name: v19.2.0-rc.2 - major_version: v19.2 - release_date: '2019-10-28' - release_type: Testing - go_version: go1.12.12 - sha: 918d925bcb7d3b42e3f201d2e0f534a9d0d26684 - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - previous_release: v19.2.0-rc.1 - -- release_name: v19.2.0-rc.3 - major_version: v19.2 - release_date: '2019-11-04' - release_type: Testing - go_version: go1.12.12 - sha: 62801ce77d9055c00b0e30010f5998ea2cd86686 - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - previous_release: v19.2.0-rc.2 - -- release_name: v19.2.0-rc.4 - major_version: v19.2 - release_date: '2019-11-07' - release_type: Testing - go_version: go1.12.12 - sha: 2535f0363b5d9b1e466ca3144fe5bcf7ff17e63c - docker: - docker_image: cockroachdb/cockroach-unstable - source: true - previous_release: v19.2.0-rc.3 - -- release_name: v19.2.0 - major_version: v19.2 - release_date: '2019-11-12' - release_type: Production - go_version: go1.12.12 - sha: 338deb20a6e34750635d6be5385498d5871ff68c - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.0-rc.4 - - release_name: v20.1.0-alpha.20191118 major_version: v20.1 @@ -1660,18 +1550,6 @@ docker: docker_image: cockroachdb/cockroach-unstable source: true -- release_name: v19.2.1 - major_version: v19.2 - release_date: '2019-11-25' - release_type: Production - go_version: go1.12.12 - sha: 53eef0857d14cc3af720e136ddaff4eeab026fd0 - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.0 - - release_name: v2.1.10 major_version: v2.1 @@ -1697,18 +1575,6 @@ previous_release: v19.1.5 -- release_name: v19.2.2 - major_version: v19.2 - release_date: '2019-12-16' - release_type: Production - go_version: go1.12.12 - sha: 3cbd05602d4aeaebbccea18d66ad0fdf8db482a5 - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.1 - - release_name: v20.1.0-alpha20191216 major_version: v20.1 @@ -1756,18 +1622,6 @@ docker_image: cockroachdb/cockroach-unstable source: true previous_release: v20.1.0-alpha20191216 -- release_name: v19.2.3 - major_version: v19.2 - release_date: '2020-02-03' - release_type: Production - go_version: go1.12.12 - sha: 2353f82b598b216a594ed7e6fc2eca66fe9d75e7 - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.2 - - release_name: v19.1.8 major_version: v19.1 @@ -1781,18 +1635,6 @@ previous_release: v19.1.7 -- release_name: v19.2.4 - major_version: v19.2 - release_date: '2020-02-11' - release_type: Production - go_version: go1.12.12 - sha: eb883734bcdafd85d98eb0e49126749bc2cc1284 - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.3 - - release_name: v20.1.0-beta.1 major_version: v20.1 @@ -1816,18 +1658,6 @@ docker_image: cockroachdb/cockroach-unstable source: true previous_release: v20.1.0-beta.1 -- release_name: v19.2.5 - major_version: v19.2 - release_date: '2020-03-23' - release_type: Production - go_version: go1.12.12 - sha: 4f36d0c62435596ca103454e113ebe8e55f005de - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.4 - - release_name: v20.1.0-beta.3 major_version: v20.1 @@ -1851,18 +1681,6 @@ docker_image: cockroachdb/cockroach-unstable source: true previous_release: v20.1.0-beta.3 -- release_name: v19.2.6 - major_version: v19.2 - release_date: '2020-04-13' - release_type: Production - go_version: go1.12.12 - sha: ee759892738f7f203ff95ec7627b90d7c47b4350 - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.5 - - release_name: v20.1.0-rc.1 major_version: v20.1 @@ -1909,18 +1727,6 @@ docker_image: cockroachdb/cockroach source: true previous_release: v20.1.0-rc.2 -- release_name: v19.2.7 - major_version: v19.2 - release_date: '2020-05-20' - release_type: Production - go_version: go1.12.12 - sha: 2e19ff0576ff21e243f00f2e2acdaeea57aee6f3 - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.6 - - release_name: v20.1.1 major_version: v20.1 @@ -1967,18 +1773,6 @@ previous_release: v19.1.9 -- release_name: v19.2.8 - major_version: v19.2 - release_date: '2020-06-29' - release_type: Production - go_version: go1.12.12 - sha: 04216787bfef5c0a577d93b16c9e91fd44637ecf - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.7 - - release_name: v20.1.3 major_version: v20.1 @@ -1991,18 +1785,6 @@ docker_image: cockroachdb/cockroach source: true previous_release: v20.1.2 -- release_name: v19.2.9 - major_version: v19.2 - release_date: '2020-07-06' - release_type: Production - go_version: go1.12.12 - sha: 5930d185b895e7deae41833af8fcce49babd23a1 - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.8 - - release_name: v19.1.11 major_version: v19.1 @@ -2040,18 +1822,6 @@ docker_image: cockroachdb/cockroach source: true previous_release: v20.1.3 -- release_name: v19.2.10 - major_version: v19.2 - release_date: '2020-08-24' - release_type: Production - go_version: go1.12.12 - sha: 5a1eb98cf9e0139f58b138f2743a3e0f55408b2f - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.9 - - release_name: v20.2.0-alpha.3 major_version: v20.2 @@ -2131,18 +1901,6 @@ docker_image: cockroachdb/cockroach-unstable source: true previous_release: v20.2.0-beta.3 -- release_name: v19.2.11 - major_version: v19.2 - release_date: '2020-10-12' - release_type: Production - go_version: go1.12.12 - sha: e450c34dbf33745d889f41e1b1ea65197c1506fc - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.10 - - release_name: v20.1.7 major_version: v20.1 @@ -2286,18 +2044,6 @@ docker_image: cockroachdb/cockroach source: true previous_release: v20.1.9 -- release_name: v19.2.12 - major_version: v19.2 - release_date: '2021-01-19' - release_type: Production - go_version: go1.12.12 - sha: eaae94a719759b74ed9f7bb150ee7e4bdd7c6ff1 - windows: true - docker: - docker_image: cockroachdb/cockroach - source: true - previous_release: v19.2.11 - - release_name: v20.2.4 major_version: v20.2 diff --git a/src/current/_data/versions.csv b/src/current/_data/versions.csv index f66caa56fbd..4f03234569a 100644 --- a/src/current/_data/versions.csv +++ b/src/current/_data/versions.csv @@ -1,21 +1,21 @@ -major_version,release_date,maint_supp_exp_date,asst_supp_exp_date,last_ga_patch,initial_lts_patch,initial_lts_release_date,lts_maint_supp_exp_date,lts_asst_supp_exp_date,previous_version,crdb_branch_name,binary_removal_date -v1.0,2017-05-10,2018-05-10,2018-11-10,N/A,N/A,N/A,N/A,N/A,N/A,release-1.0,2024-01-01 -v1.1,2017-10-12,2018-10-12,2019-04-12,N/A,N/A,N/A,N/A,N/A,v1.0,release-1.1,2024-01-01 -v2.0,2018-04-04,2019-04-04,2019-10-04,N/A,N/A,N/A,N/A,N/A,v1.1,release-2.0,2024-01-01 -v2.1,2018-10-30,2019-10-30,2020-04-30,N/A,N/A,N/A,N/A,N/A,v2.0,release-2.1,2024-01-01 -v19.1,2019-04-30,2020-04-30,2020-10-30,N/A,N/A,N/A,N/A,N/A,v2.1,release-19.1,2024-01-01 -v19.2,2019-11-12,2020-11-12,2021-05-12,N/A,N/A,N/A,N/A,N/A,v19.1,release-19.2,2024-01-01 -v20.1,2020-05-12,2021-05-12,2021-11-12,N/A,N/A,N/A,N/A,N/A,v19.2,release-20.1,2025-05-12 -v20.2,2020-11-10,2021-11-10,2022-05-10,N/A,N/A,N/A,N/A,N/A,v20.1,release-20.2,2025-05-12 -v21.1,2021-05-18,2022-05-18,2022-11-18,N/A,N/A,N/A,N/A,N/A,v20.2,release-21.1,2025-05-18 -v21.2,2021-11-16,2022-11-16,2023-05-16,N/A,N/A,N/A,N/A,N/A,v21.1,release-21.2,2025-11-16 -v22.1,2022-05-24,2023-05-24,2023-11-24,N/A,N/A,N/A,N/A,N/A,v21.2,release-22.1,2026-05-24 -v22.2,2022-12-05,2023-12-05,2024-06-05,N/A,N/A,N/A,N/A,N/A,v22.1,release-22.2,2026-12-05 -v23.1,2023-05-15,2024-05-15,2024-11-15,23.1.11,23.1.12,2023-11-13,2024-11-13,2025-11-13,v22.2,release-23.1,2027-05-15 -v23.2,2024-02-05,2025-02-05,2025-08-05,23.2.6,23.2.7,2024-07-08,2025-07-08,2026-07-08,v23.1,release-23.2,2028-02-05 -v24.1,2024-05-20,2025-05-20,2025-11-20,24.1.5,24.1.6,2024-10-21,2025-10-21,2026-10-21,v23.2,release-24.1,2028-05-20 -v24.2,2024-08-12,2025-02-12,N/A,N/A,N/A,N/A,N/A,N/A,v24.1,release-24.2,2028-08-12 -v24.3,2024-11-18,2025-11-18,2026-05-18,24.3.11,24.3.12,2025-05-05,2026-05-05,2027-05-05,v24.2,release-24.3,2028-11-18 -v25.1,2025-02-18,2025-08-18,N/A,N/A,N/A,N/A,N/A,N/A,v24.3,release-25.1,2029-02-18 -v25.2,2025-05-09,2026-05-12,2026-11-12,N/A,N/A,N/A,N/A,N/A,v25.1,release-25.2,2029-05-09 -v25.3,2025-08-04,2026-02-04,N/A,N/A,N/A,N/A,N/A,N/A,v25.2,release-25.3,2029-08-04 +major_version,release_date,maint_supp_exp_date,asst_supp_exp_date,last_ga_patch,initial_lts_patch,initial_lts_release_date,lts_maint_supp_exp_date,lts_asst_supp_exp_date,previous_version,crdb_branch_name,binary_removal_date +v1.0,2017-05-10,2018-05-10,2018-11-10,N/A,N/A,N/A,N/A,N/A,N/A,release-1.0,2024-01-01 +v1.1,2017-10-12,2018-10-12,2019-04-12,N/A,N/A,N/A,N/A,N/A,v1.0,release-1.1,2024-01-01 +v2.0,2018-04-04,2019-04-04,2019-10-04,N/A,N/A,N/A,N/A,N/A,v1.1,release-2.0,2024-01-01 +v2.1,2018-10-30,2019-10-30,2020-04-30,N/A,N/A,N/A,N/A,N/A,v2.0,release-2.1,2024-01-01 +v19.1,2019-04-30,2020-04-30,2020-10-30,N/A,N/A,N/A,N/A,N/A,v2.1,release-19.1,2024-01-01 +v20.1,2020-05-12,2021-05-12,2021-11-12,N/A,N/A,N/A,N/A,N/A,v19.2,release-20.1,2025-05-12 +v20.2,2020-11-10,2021-11-10,2022-05-10,N/A,N/A,N/A,N/A,N/A,v20.1,release-20.2,2025-05-12 +v21.1,2021-05-18,2022-05-18,2022-11-18,N/A,N/A,N/A,N/A,N/A,v20.2,release-21.1,2025-05-18 +v21.2,2021-11-16,2022-11-16,2023-05-16,N/A,N/A,N/A,N/A,N/A,v21.1,release-21.2,2025-11-16 +v22.1,2022-05-24,2023-05-24,2023-11-24,N/A,N/A,N/A,N/A,N/A,v21.2,release-22.1,2026-05-24 +v22.2,2022-12-05,2023-12-05,2024-06-05,N/A,N/A,N/A,N/A,N/A,v22.1,release-22.2,2026-12-05 +v23.1,2023-05-15,2024-05-15,2024-11-15,23.1.11,23.1.12,2023-11-13,2024-11-13,2025-11-13,v22.2,release-23.1,2027-05-15 +v23.2,2024-02-05,2025-02-05,2025-08-05,23.2.6,23.2.7,2024-07-08,2025-07-08,2026-07-08,v23.1,release-23.2,2028-02-05 +v24.1,2024-05-20,2025-05-20,2025-11-20,24.1.5,24.1.6,2024-10-21,2025-10-21,2026-10-21,v23.2,release-24.1,2028-05-20 +v24.2,2024-08-12,2025-02-12,N/A,N/A,N/A,N/A,N/A,N/A,v24.1,release-24.2,2028-08-12 +v24.3,2024-11-18,2025-11-18,2026-05-18,24.3.11,24.3.12,2025-05-05,2026-05-05,2027-05-05,v24.2,release-24.3,2028-11-18 +v25.1,2025-02-18,2025-08-18,N/A,N/A,N/A,N/A,N/A,N/A,v24.3,release-25.1,2029-02-18 +v25.2,2025-05-09,2026-05-12,2026-11-12,N/A,N/A,N/A,N/A,N/A,v25.1,release-25.2,2029-05-09 +v25.3,2025-08-04,2026-02-04,N/A,N/A,N/A,N/A,N/A,N/A,v25.2,release-25.3,2029-08-04 + diff --git a/src/current/_includes/releases/whats-new-intro.md b/src/current/_includes/releases/whats-new-intro.md index 706bde7ba5a..74355c546ea 100644 --- a/src/current/_includes/releases/whats-new-intro.md +++ b/src/current/_includes/releases/whats-new-intro.md @@ -102,7 +102,7 @@ On this page, you can read about changes and find downloads for all production a {% comment %}Only show these bullet points if the version has been released{% endcomment %} {% if released == true %} {% comment %}v1.0 has no #v1-0-0 anchor, and before GA other releases also do not.{% endcomment %} -- For key feature enhancements in {{ page.major_version }} and other upgrade considerations, refer to the notes for {% if include.major_version.release_date != 'N/A' and page.major_version != 'v1.0' %}[{{ page.major_version }}.0](#{{ page.major_version | replace: '.', '-' }}-0){% else %}{{ page.major_version }} on this page{% endif %}. +- For key feature enhancements in {{ page.major_version }} and other upgrade considerations, refer to the notes for {% if include.major_version.release_date != 'N/A' and page.major_version != 'v1.0' and page.major_version != 'v19.2' %}[{{ page.major_version }}.0](#{{ page.major_version | replace: '.', '-' }}-0){% else %}{{ page.major_version }} on this page{% endif %}. {% endif %} {% endif %}{% comment %}End GA-only content{% endcomment %} - For details about release types, naming, and licensing, refer to the [Releases]({% link releases/index.md %}) page. diff --git a/src/current/releases/cloud.md b/src/current/releases/cloud.md index d51b9b85dde..548bf19ea57 100644 --- a/src/current/releases/cloud.md +++ b/src/current/releases/cloud.md @@ -1257,7 +1257,7 @@ CockroachDB {{ site.data.products.cloud }} now requires a user to have a Cockroa

General changes

- CockroachDB {{ site.data.products.cloud }} pricing is now available on the [pricing page](https://www.cockroachlabs.com/pricing/). -- CockroachDB {{ site.data.products.cloud }} clusters running CockroachDB v19.2 have been upgraded to [v19.2.4]({% link releases/v19.2.md %}#v19-2-4). All new clusters will now be created with CockroachDB v19.2.4. +- CockroachDB {{ site.data.products.cloud }} clusters running CockroachDB v19.2 have been upgraded to v19.2.4. All new clusters will now be created with CockroachDB v19.2.4. - CockroachDB {{ site.data.products.cloud }} now offers two options for per-node hardware configuration instead of three options. The hardware configuration [pricing]({% link cockroachcloud/create-your-cluster.md %}#step-2-select-the-cloud-provider) has been updated accordingly. - Added a **Sign up** link to the [CockroachDB {{ site.data.products.cloud }} **Log In** page](https://cockroachlabs.cloud/). - While [creating a new cluster]({% link cockroachcloud/create-your-cluster.md %}), you can now type in the number of nodes you want in the cluster instead of having to click the `+` sign repeatedly. diff --git a/src/current/v19.1/cockroachdb-in-comparison.md b/src/current/v19.1/cockroachdb-in-comparison.md index abe3d29214d..d519261094a 100644 --- a/src/current/v19.1/cockroachdb-in-comparison.md +++ b/src/current/v19.1/cockroachdb-in-comparison.md @@ -40,7 +40,7 @@ This page shows you how the key features of CockroachDB stack up against other d Database horizontal scale - tooltip icon + tooltip icon @@ -62,7 +62,7 @@ This page shows you how the key features of CockroachDB stack up against other d Database load balancing (internal) - tooltip icon + tooltip icon @@ -82,7 +82,7 @@ This page shows you how the key features of CockroachDB stack up against other d Failover - tooltip icon + tooltip icon @@ -104,7 +104,7 @@ This page shows you how the key features of CockroachDB stack up against other d Automated repair and RPO(Recovery Point Objective) - tooltip icon + tooltip icon @@ -126,7 +126,7 @@ This page shows you how the key features of CockroachDB stack up against other d Distributed reads - tooltip icon + tooltip icon @@ -144,7 +144,7 @@ This page shows you how the key features of CockroachDB stack up against other d Distributed transactions - tooltip icon + tooltip icon @@ -164,7 +164,7 @@ This page shows you how the key features of CockroachDB stack up against other d Database isolation levels - tooltip icon + tooltip icon @@ -186,7 +186,7 @@ This page shows you how the key features of CockroachDB stack up against other d Potential data issues (default) - tooltip icon + tooltip icon @@ -210,7 +210,7 @@ This page shows you how the key features of CockroachDB stack up against other d SQL - tooltip icon + tooltip icon @@ -230,7 +230,7 @@ This page shows you how the key features of CockroachDB stack up against other d Database schema change - tooltip icon + tooltip icon @@ -250,7 +250,7 @@ This page shows you how the key features of CockroachDB stack up against other d Cost based optimization - tooltip icon + tooltip icon @@ -272,7 +272,7 @@ This page shows you how the key features of CockroachDB stack up against other d Data Geo-partitioning - tooltip icon + tooltip icon @@ -294,7 +294,7 @@ This page shows you how the key features of CockroachDB stack up against other d Upgrade method - tooltip icon + tooltip icon @@ -312,7 +312,7 @@ This page shows you how the key features of CockroachDB stack up against other d Multi-region - tooltip icon + tooltip icon @@ -332,7 +332,7 @@ This page shows you how the key features of CockroachDB stack up against other d Multi-cloud - tooltip icon + tooltip icon diff --git a/src/current/v19.2/404.md b/src/current/v19.2/404.md deleted file mode 100644 index 13a69ddde5c..00000000000 --- a/src/current/v19.2/404.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -title: Page Not Found -description: "Page not found." -sitemap: false -search: exclude -related_pages: none -toc: false ---- - - -{%comment%} - - -{%endcomment%} \ No newline at end of file diff --git a/src/current/v19.2/add-column.md b/src/current/v19.2/add-column.md deleted file mode 100644 index 5584ce60376..00000000000 --- a/src/current/v19.2/add-column.md +++ /dev/null @@ -1,152 +0,0 @@ ---- -title: ADD COLUMN -summary: Use the ADD COLUMN statement to add columns to tables. -toc: true ---- - -The `ADD COLUMN` [statement](sql-statements.html) is part of `ALTER TABLE` and adds columns to tables. - -{% include {{ page.version.version }}/sql/combine-alter-table-commands.md %} - -## Synopsis - -
-{% include {{ page.version.version }}/sql/diagrams/add_column.html %} -
- -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the table. - -## Parameters - - Parameter | Description ------------|------------- - `table_name` | The name of the table to which you want to add the column. - `column_name` | The name of the column you want to add. The column name must follow these [identifier rules](keywords-and-identifiers.html#identifiers) and must be unique within the table but can have the same name as indexes or constraints. - `typename` | The [data type](data-types.html) of the new column. - `col_qualification` | An optional list of column definitions, which may include [column-level constraints](constraints.html), [collation](collate.html), or [column family assignments](column-families.html).

If the column family is not specified, the column will be added to the first column family. For more information about how column families are assigned, see [Column Families](column-families.html#assign-column-families-when-adding-columns).

Note that it is not possible to add a column with the [foreign key](foreign-key.html) constraint. As a workaround, you can add the column without the constraint, then use [`CREATE INDEX`](create-index.html) to index the column, and then use [`ADD CONSTRAINT`](add-constraint.html) to add the foreign key constraint to the column. - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Examples - -### Add a single column - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE accounts ADD COLUMN names STRING; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM accounts; -~~~ - -~~~ -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -| column_name | data_type | is_nullable | column_default | generation_expression | indices | -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -| id | INT | false | NULL | | {"primary"} | -| balance | DECIMAL | true | NULL | | {} | -| names | STRING | true | NULL | | {} | -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -(3 rows) -~~~ - -### Add multiple columns - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE accounts ADD COLUMN location STRING, ADD COLUMN amount DECIMAL; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM accounts; -~~~ - -~~~ -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -| column_name | data_type | is_nullable | column_default | generation_expression | indices | -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -| id | INT | false | NULL | | {"primary"} | -| balance | DECIMAL | true | NULL | | {} | -| names | STRING | true | NULL | | {} | -| location | STRING | true | NULL | | {} | -| amount | DECIMAL | true | NULL | | {} | -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -(5 rows) -~~~ - -### Add a column with a `NOT NULL` constraint and a `DEFAULT` value - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE accounts ADD COLUMN interest DECIMAL NOT NULL DEFAULT (DECIMAL '1.3'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM accounts; -~~~ -~~~ -+-------------+-----------+-------------+------------------------+-----------------------+-------------+ -| column_name | data_type | is_nullable | column_default | generation_expression | indices | -+-------------+-----------+-------------+------------------------+-----------------------+-------------+ -| id | INT | false | NULL | | {"primary"} | -| balance | DECIMAL | true | NULL | | {} | -| names | STRING | true | NULL | | {} | -| location | STRING | true | NULL | | {} | -| amount | DECIMAL | true | NULL | | {} | -| interest | DECIMAL | false | 1.3:::DECIMAL::DECIMAL | | {} | -+-------------+-----------+-------------+------------------------+-----------------------+-------------+ -(6 rows) -~~~ - -### Add a column with `NOT NULL` and `UNIQUE` constraints - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE accounts ADD COLUMN cust_number DECIMAL UNIQUE NOT NULL; -~~~ - -### Add a column with collation - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE accounts ADD COLUMN more_names STRING COLLATE en; -~~~ - -### Add a column and assign it to a column family - -#### Add a column and assign it to a new column family - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE accounts ADD COLUMN location1 STRING CREATE FAMILY new_family; -~~~ - -#### Add a column and assign it to an existing column family - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE accounts ADD COLUMN location2 STRING FAMILY existing_family; -~~~ - -#### Add a column and create a new column family if column family does not exist - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE accounts ADD COLUMN new_name STRING CREATE IF NOT EXISTS FAMILY f1; -~~~ - -## See also - -- [`ALTER TABLE`](alter-table.html) -- [Column-level Constraints](constraints.html) -- [Collation](collate.html) -- [Column Families](column-families.html) -- [`SHOW JOBS`](show-jobs.html) diff --git a/src/current/v19.2/add-constraint.md b/src/current/v19.2/add-constraint.md deleted file mode 100644 index a0b34ebee84..00000000000 --- a/src/current/v19.2/add-constraint.md +++ /dev/null @@ -1,172 +0,0 @@ ---- -title: ADD CONSTRAINT -summary: Use the ADD CONSTRAINT statement to add constraints to columns. -toc: true ---- - -The `ADD CONSTRAINT` [statement](sql-statements.html) is part of `ALTER TABLE` and can add the following [constraints](constraints.html) to columns: - -- [`UNIQUE`](#add-the-unique-constraint) -- [`CHECK`](#add-the-check-constraint) -- [Foreign key](#add-the-foreign-key-constraint-with-cascade) - -{{site.data.alerts.callout_info}} -The [`PRIMARY KEY`](primary-key.html) can only be applied through [`CREATE TABLE`](create-table.html). The [`DEFAULT`](default-value.html) and [`NOT NULL`](not-null.html) constraints are managed through [`ALTER COLUMN`](alter-column.html). -{{site.data.alerts.end}} - -{% include {{ page.version.version }}/sql/combine-alter-table-commands.md %} - -## Synopsis - -
-{% include {{ page.version.version }}/sql/diagrams/add_constraint.html %} -
- -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the table. - -## Parameters - - Parameter | Description ------------|------------- - `table_name` | The name of the table containing the column you want to constrain. - `constraint_name` | The name of the constraint, which must be unique to its table and follow these [identifier rules](keywords-and-identifiers.html#identifiers). - `constraint_elem` | The [`CHECK`](check.html), [foreign key](foreign-key.html), [`UNIQUE`](unique.html) constraint you want to add.

Adding/changing a `DEFAULT` constraint is done through [`ALTER COLUMN`](alter-column.html).

Adding/changing the table's `PRIMARY KEY` is not supported through `ALTER TABLE`; it can only be specified during [table creation](create-table.html). - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Examples - -### Add the `UNIQUE` constraint - -Adding the [`UNIQUE` constraint](unique.html) requires that all of a column's values be distinct from one another (except for *NULL* values). - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE orders ADD CONSTRAINT id_customer_unique UNIQUE (id, customer); -~~~ - -### Add the `CHECK` constraint - -Adding the [`CHECK` constraint](check.html) requires that all of a column's values evaluate to `TRUE` for a Boolean expression. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE orders ADD CONSTRAINT check_id_non_zero CHECK (id > 0); -~~~ - -Check constraints can be added to columns that were created earlier in the transaction. For example: - -{% include copy-clipboard.html %} -~~~ sql -> BEGIN; -> ALTER TABLE customers ADD COLUMN gdpr_status STRING; -> ALTER TABLE customers ADD CONSTRAINT check_gdpr_status CHECK (gdpr_status IN ('yes', 'no', 'unknown')); -> COMMIT; -~~~ - -~~~ -BEGIN -ALTER TABLE -ALTER TABLE -COMMIT -~~~ - -{{site.data.alerts.callout_info}} -The entire transaction will be rolled back, including any new columns that were added, in the following cases: - -- If an existing column is found containing values that violate the new constraint. -- If a new column has a default value or is a [computed column](computed-columns.html) that would have contained values that violate the new constraint. -{{site.data.alerts.end}} - -### Add the foreign key constraint with `CASCADE` - -To add a foreign key constraint, use the steps shown below. - -Given two tables, `customers` and `orders`: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE customers; -~~~ - -~~~ - table_name | create_statement -------------+---------------------------------------------------- - customers | CREATE TABLE customers ( + - | id INT8 NOT NULL, + - | name STRING NOT NULL, + - | address STRING NULL, + - | CONSTRAINT "primary" PRIMARY KEY (id ASC),+ - | FAMILY "primary" (id, name, address) + - | ) -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE orders; -~~~ - -~~~ - table_name | create_statement -------------+---------------------------------------------------------------------------------------------------------------- - orders | CREATE TABLE orders ( + - | id INT8 NOT NULL, + - | customer_id INT8 NULL, + - | status STRING NOT NULL, + - | CONSTRAINT "primary" PRIMARY KEY (id ASC), + - | FAMILY "primary" (id, customer_id, status), + - | CONSTRAINT check_status CHECK (status IN ('open':::STRING, 'complete':::STRING, 'cancelled':::STRING))+ - | ) -(1 row) -~~~ - -You can include a [foreign key action](foreign-key.html#foreign-key-actions) to specify what happens when a foreign key is updated or deleted. - -Using `ON DELETE CASCADE` will ensure that when the referenced row is deleted, all dependent objects are also deleted. - -{{site.data.alerts.callout_danger}} -`CASCADE` does not list the objects it drops or updates, so it should be used with caution. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE orders ADD CONSTRAINT customer_fk FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE CASCADE; -~~~ - -An index on the referencing columns is automatically created for you when you add a foreign key constraint to an empty table, if an appropriate index does not already exist. You can see it using [`SHOW INDEXES`](show-index.html): - -{% include copy-clipboard.html %} -~~~ sql -> SHOW INDEXES FROM orders; -~~~ - -~~~ - table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit -------------+-------------------------------+------------+--------------+-------------+-----------+---------+---------- - orders | primary | f | 1 | id | ASC | f | f - orders | orders_auto_index_customer_fk | t | 1 | customer_id | ASC | f | f - orders | orders_auto_index_customer_fk | t | 2 | id | ASC | f | t -(3 rows) -~~~ - -{{site.data.alerts.callout_info}} -Adding a foreign key for a non-empty table without an appropriate index will fail, since foreign key columns must be indexed. For more information about the requirements for creating foreign keys, see [Rules for creating foreign keys](foreign-key.html#rules-for-creating-foreign-keys). -{{site.data.alerts.end}} - -## See also - -- [Constraints](constraints.html) -- [Foreign Key Constraint](foreign-key.html) -- [`SHOW CONSTRAINTS`](show-constraints.html) -- [`RENAME CONSTRAINT`](rename-constraint.html) -- [`DROP CONSTRAINT`](drop-constraint.html) -- [`VALIDATE CONSTRAINT`](validate-constraint.html) -- [`ALTER COLUMN`](alter-column.html) -- [`CREATE TABLE`](create-table.html) -- [`ALTER TABLE`](alter-table.html) -- [`SHOW JOBS`](show-jobs.html) diff --git a/src/current/v19.2/admin-ui-access-and-navigate.md b/src/current/v19.2/admin-ui-access-and-navigate.md deleted file mode 100644 index a94bb495499..00000000000 --- a/src/current/v19.2/admin-ui-access-and-navigate.md +++ /dev/null @@ -1,124 +0,0 @@ ---- -title: Use the CockroachDB Admin UI -summary: Learn how to access and navigate the Admin UI. -toc: true ---- - -The built-in Admin UI helps you monitor and troubleshoot CockroachDB by providing information about the cluster's health, configuration, and operations. - -## Access the Admin UI - -For insecure clusters, anyone can access and view the Admin UI. For secure clusters, only authorized users can [access and view the Admin UI](#accessing-the-admin-ui-for-a-secure-cluster). In addition, certain areas of the Admin UI can only be [accessed by `admin` users](admin-ui-overview.html#admin-ui-access). - -You can access the Admin UI from any node in the cluster. - -The Admin UI is reachable at the IP address/hostname and port set via the `--http-addr` flag when [starting each node](cockroach-start.html), for example, `http://
:` for an insecure cluster or `https://
:` for a secure cluster. - -If `--http-addr` is not specified when starting a node, the Admin UI is reachable at the IP address/hostname set via the `--listen-addr` flag and port `8080`. - -For additional guidance on accessing the Admin UI in the context of cluster deployment, see [Start a Local Cluster](start-a-local-cluster.html) and [Manual Deployment](manual-deployment.html). - -### Accessing the Admin UI for a secure cluster - -Note that on secure clusters, certain areas of the Admin UI can only be accessed by `admin` users. For details on providing access to users, see [this page](admin-ui-overview.html#admin-ui-access). - -On [accessing the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui), your browser will consider the CockroachDB-created certificate invalid, so you’ll need to click through a warning message to get to the UI. For secure clusters, you can avoid getting the warning message by using a certificate issued by a public CA. For more information, refer to [Use a UI certificate and key to access the Admin UI](create-security-certificates-custom-ca.html#accessing-the-admin-ui-for-a-secure-cluster). - -For each user who should have access to the Admin UI for a secure cluster, [create a user with a password](create-user.html). On accessing the Admin UI, the users will see a Login screen, where they will need to enter their usernames and passwords. - -{{site.data.alerts.callout_info}} -This login information is stored in a system table that is replicated like other data in the cluster. If a majority of the nodes with the replicas of the system table data go down, users will be locked out of the Admin UI. -{{site.data.alerts.end}} - -To log out of the Admin UI, click the **Log Out** link at the bottom of the left-hand navigation bar. - -## Navigate the Admin UI - -The left-hand navigation bar allows you to navigate to the [Cluster Overview page](admin-ui-access-and-navigate.html), [cluster metrics dashboards](admin-ui-overview.html), the [Databases page](admin-ui-databases-page.html), the [Statements page](admin-ui-statements-page.html), the [Jobs page](admin-ui-jobs-page.html), and the [Advanced Debugging page](admin-ui-debug-pages.html). - -The main panel display changes for each page: - -Page | Main Panel Component ------------|------------ -Cluster Overview |
  • [Cluster Overview panel](admin-ui-cluster-overview-page.html)
  • [Node List](admin-ui-cluster-overview-page.html#node-list)
  • [Enterprise users](enterprise-licensing.html) can enable and switch to the [Node Map](admin-ui-cluster-overview-page.html#node-map-enterprise) view.
-Cluster Metrics |
  • [Time Series graphs](admin-ui-access-and-navigate.html#cluster-metrics)
  • [Summary Panel](admin-ui-access-and-navigate.html#summary-panel)
  • [Events List](admin-ui-access-and-navigate.html#events-panel)
-Databases | Information about the tables and grants in your [databases](admin-ui-databases-page.html). -Statements | Information about the SQL [statements](admin-ui-statements-page.html) running in the cluster. -Jobs | Information about all currently active schema changes and backup/restore [jobs](admin-ui-jobs-page.html). -Advanced Debugging | Advanced monitoring and troubleshooting [reports](admin-ui-debug-pages.html). These pages are experimental. If you find an issue, let us know through [these channels](https://www.cockroachlabs.com/community/). - -### Cluster Metrics - -The **Cluster Metrics** dashboards display the time series graphs that are useful to visualize and monitor data trends. To access the time series graphs, click **Metrics** on the left. - -You can hover over each graph to see actual point-in-time values. - -CockroachDB Admin UI - -{{site.data.alerts.callout_info}} -By default, CockroachDB stores time series metrics for the last 30 days, but you can reduce the interval for timeseries storage. Alternatively, if you are exclusively using a third-party tool such as [Prometheus](monitor-cockroachdb-with-prometheus.html) for time series monitoring, you can disable time series storage entirely. For more details, see this [FAQ](operational-faqs.html#can-i-reduce-or-disable-the-storage-of-timeseries-data). -{{site.data.alerts.end}} - -#### Change time range - -You can change the time range by clicking on the time window. -CockroachDB Admin UI - -{{site.data.alerts.callout_info}}The Admin UI shows time in UTC, even if you set a different time zone for your cluster. {{site.data.alerts.end}} - -#### View metrics for a single node - -By default, the time series panel displays the metrics for the entire cluster. To view the metrics for an individual node, select the node from the **Graph** drop-down list. -CockroachDB Admin UI - -### Summary panel - -The **Cluster Metrics** dashboards display the **Summary** panel of key metrics. To view the **Summary** panel, click **Metrics** on the left. - -CockroachDB Admin UI Summary Panel - -The **Summary** panel provides the following metrics: - -Metric | Description ---------|---- -Total Nodes | The total number of nodes in the cluster. Decommissioned nodes are not included in the Total Nodes count.

You can further drill down into the nodes details by clicking on [**View nodes list**](admin-ui-cluster-overview-page.html#node-list). -Dead Nodes | The number of [dead nodes](admin-ui-cluster-overview-page.html#dead-nodes) in the cluster. -Capacity Used | The storage capacity used as a percentage of total storage capacity allocated across all nodes. -Unavailable Ranges | The number of unavailable ranges in the cluster. A non-zero number indicates an unstable cluster. -Queries per second | The total number of `SELECT`, `UPDATE`, `INSERT`, and `DELETE` queries executed per second across the cluster. -P50 Latency | The 50th percentile of service latency. Service latency is calculated as the time between when the cluster receives a query and finishes executing the query. This time does not include returning results to the client. -P99 Latency | The 99th percentile of service latency. - -{{site.data.alerts.callout_info}} -{% include {{ page.version.version }}/misc/available-capacity-metric.md %} -{{site.data.alerts.end}} - -### Events panel - -The **Cluster Metrics** dashboards display the **Events** panel that lists the 10 most recent events logged for the all nodes across the cluster. To view the **Events** panel, click **Metrics** on the left-hand navigation bar. To see the list of all events, click **View all events** in the **Events** panel. - -CockroachDB Admin UI Events - -The following types of events are listed: - -- Database created -- Database dropped -- Table created -- Table dropped -- Table altered -- Index created -- Index dropped -- View created -- View dropped -- Schema change reversed -- Schema change finished -- Node joined -- Node decommissioned -- Node restarted -- Cluster setting changed - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-cdc-dashboard.md b/src/current/v19.2/admin-ui-cdc-dashboard.md deleted file mode 100644 index f54dd270da0..00000000000 --- a/src/current/v19.2/admin-ui-cdc-dashboard.md +++ /dev/null @@ -1,73 +0,0 @@ ---- -title: Changefeeds Dashboard -summary: The Changefeeds dashboard lets you monitor the changefeeds created across your cluster. -toc: true ---- - -The **Changefeeds** dashboard in the CockroachDB Admin UI lets you monitor the [changefeeds](change-data-capture.html) created across your cluster. To view this dashboard, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui), click **Metrics** on the left-hand navigation bar, and then select **Dashboard** > **Changefeeds**. - - -The **Changefeeds** dashboard displays the following time series graphs: - -## Max Changefeed Latency - -CockroachDB Admin UI Max Changefeed Latency graph - -- In the node view, the graph shows the maximum latency for resolved timestamps of any running changefeed for the node. - -- In the cluster view, the graph shows the maximum latency for resolved timestamps of any running changefeed across all nodes. - -{{site.data.alerts.callout_info}} -The maximum latency for resolved timestamps is distinct from and slower than the commit-to-emit latency for individual change messages. For more information about resolved timestamps, see [Ordering guarantees](change-data-capture.html#ordering-guarantees). -{{site.data.alerts.end}} - -## Sink Byte Traffic - -CockroachDB Admin UI Sink Byte Traffic graph - -- In the node view, the graph shows the number of bytes emitted by CockroachDB into the sink across all changefeeds for the selected node. - -- In the cluster view, the graph shows the number of bytes emitted by CockroachDB into the sink across all changefeeds and across all nodes in the cluster. - -On hovering over the graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -**Emitted Bytes** | The number of bytes emitted by CockroachDB into the sink for all changefeeds. - -## Sink Counts - -CockroachDB Admin UI Sink Counts graph - -- In the node view, the graph shows the number of messages that CockroachDB sent to the sink as well as the number of flushes that the sink performed for all changefeeds. - -- In the cluster view, the graph shows the number of messages that CockroachDB sent to the sink as well as the number of flushes that the sink performed for all changefeeds across the cluster. - -On hovering over the graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -**Messages** | The number of messages that CockroachDB sent to the sink for all changefeeds. -**Flushes** | The the number of flushes that the sink performed for all changefeeds. - -## Sink Timings - -CockroachDB Admin UI Sink Timings graph - -- In the node view, the graph shows the time in milliseconds per second required by CockroachDB to send messages to the sink as well as the time CockroachDB spent waiting for the sink to flush the messages for all changefeeds. - -- In the cluster view, the graph shows the time in milliseconds per second required by CockroachDB to send messages to the sink as the time CockroachDB spent waiting for the sink to flush the messages for all changefeeds across the cluster. - -On hovering over the graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -**Message Emit Time** | The time in milliseconds per second required by CockroachDB to send messages to the sink for all changefeeds. -**Flush Time** | The time in milliseconds per second that CockroachDB spent waiting for the sink to flush the messages for all changefeeds. - -## See also - -- [Change Data Capture](change-data-capture.html) -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-cluster-overview-page.md b/src/current/v19.2/admin-ui-cluster-overview-page.md deleted file mode 100644 index 2112cfaa8ee..00000000000 --- a/src/current/v19.2/admin-ui-cluster-overview-page.md +++ /dev/null @@ -1,90 +0,0 @@ ---- -title: Cluster Overview Page -toc: true ---- - -The **Cluster Overview** page of the Admin UI provides details of the cluster nodes and their liveness status, replication status, uptime, and key hardware metrics. [Enterprise users](enterprise-licensing.html) can enable and switch to the [Node Map](admin-ui-cluster-overview-page.html#node-map-enterprise) view. - -## Cluster Overview Panel - -CockroachDB Admin UI - -The **Cluster Overview** panel provides the following metrics: - -Metric | Description ---------|---- -Capacity Usage |
  • Used capacity: The storage capacity used by CockroachDB (represented as a percentage of total storage capacity allocated across all nodes).
  • Usable capacity: The space available for CockroachDB data storage (i.e., the storage capacity of the machine excluding the capacity used by the Cockroach binary, operating system, and other system files).
-Node Status |
  • The number of [live nodes](#live-nodes) in the cluster.
  • The number of suspect nodes in the cluster. A node is considered suspect if its liveness status is unavailable or the node is in the process of decommissioning.
  • The number of [dead nodes](#dead-nodes) in the cluster.
  • -Replication Status |
    • The total number of [ranges](architecture/overview.html#glossary) in the cluster.
    • The number of [under-replicated ranges](admin-ui-replication-dashboard.html#review-of-cockroachdb-terminology) in the cluster. A non-zero number indicates an unstable cluster.
    • The number of [unavailable ranges](admin-ui-replication-dashboard.html#review-of-cockroachdb-terminology) in the cluster. A non-zero number indicates an unstable cluster.
    • - -## Node List - -The **Node List** is the default view on the **Overview** page. -CockroachDB Admin UI - -### Live Nodes -Live nodes are nodes that are online and responding. They are marked with a green dot. If a node is removed or dies, the dot turns yellow to indicate that it is not responding. If the node remains unresponsive for a certain amount of time (5 minutes by default), the node turns red and is moved to the [**Dead Nodes**](#dead-nodes) section, indicating that it is no longer expected to come back. - -The following details are shown for each live node: - -Column | Description --------|------------ -ID | The ID of the node. -Address | The address of the node. You can click on the address to view further details about the node. -Uptime | How long the node has been running. -Replicas | The number of replicas on the node. -CPUs | The number of CPU cores on the machine. -Capacity Usage | The storage capacity used by CockroachDB as a percentage of the total usable capacity on the node. The value is represented numerically and as a bar graph. -Mem Usage | The memory used by CockroachDB as a percentage of the total memory on the node. The value is represented numerically and as a bar graph. -Version | The build tag of the CockroachDB version installed on the node. -Logs | Click **Logs** to see detailed logs for the node. [Requires `admin` privileges](admin-ui-overview.html#admin-ui-access) on secure clusters. - -### Dead Nodes - -Nodes are considered dead once they have not responded for a certain amount of time (5 minutes by default). At this point, the automated repair process starts, wherein CockroachDB automatically rebalances replicas from the dead node, using the unaffected replicas as sources. See [Stop a Node](cockroach-quit.html#how-it-works) for more information. - -The following details are shown for each dead node: - -Column | Description --------|------------ -ID | The ID of the node. -Address | The address of the node. You can click on the address to view further details about the node. -Down Since | How long the node has been down. - -### Decommissioned Nodes - -Nodes that have been decommissioned for removal from the cluster are listed in the **Decommissioned Nodes** table. - -When you initiate the [decommissioning process](remove-nodes.html#how-it-works) on a node, CockroachDB transfers all range replicas and range leases off the node so that it can be safely shut down. - -## Node Map (Enterprise) - -The **Node Map** is an [enterprise-only](enterprise-licensing.html) feature that gives you a visual representation of the geographical configuration of your cluster. - -CockroachDB Admin UI Summary Panel - -The Node Map consists of the following components: - -### Region component - -CockroachDB Admin UI Summary Panel - -{{site.data.alerts.callout_info}} -For multi-core systems, the user CPU percent can be greater than 100%. Full utilization of one core is considered as 100% CPU usage. If you have n cores, then the user CPU percent can range from 0% (indicating an idle system) to (n*100)% (indicating full utilization). -{{site.data.alerts.end}} - -### Node component - -CockroachDB Admin UI Summary Panel - -{{site.data.alerts.callout_info}} -For multi-core systems, the user CPU percent can be greater than 100%. Full utilization of one core is considered as 100% CPU usage. If you have n cores, then the user CPU percent can range from 0% (indicating an idle system) to (n*100)% (indicating full utilization). -{{site.data.alerts.end}} - -For guidance on enabling and using the node map, see [Enable Node Map](enable-node-map.html). - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-custom-chart-debug-page.md b/src/current/v19.2/admin-ui-custom-chart-debug-page.md deleted file mode 100644 index 751a9a4ee67..00000000000 --- a/src/current/v19.2/admin-ui-custom-chart-debug-page.md +++ /dev/null @@ -1,59 +0,0 @@ ---- -title: Custom Chart Debug Page -toc: true ---- - -The **Custom Chart** debug page in the Admin UI can be used to create one or multiple custom charts showing any combination of over [200 available metrics](#available-metrics). - -The definition of the customized dashboard is encoded in the URL. To share the dashboard with someone, send them the URL. Like any other URL, it can be bookmarked, sit in a pinned tab in your browser, etc. - - -## Accessing the **Custom Chart** page - -To access the **Custom Chart** debug page, [access the Admin UI](admin-ui-access-and-navigate.html), and either: - -- Open http://localhost:8080/#/debug/chart in your browser (replacing `localhost` and `8080` with your node's host and port). - -- Click the gear icon on the left to access the **Advanced Debugging Page**. In the **Reports** section, click **Custom TimeSeries Chart**. - -## Using the **Custom Chart** page - -CockroachDB Admin UI - -On the **Custom Chart** page, you can set the time span for all charts, add new custom charts, and customize each chart: - -- To set the time span for the page, use the dropdown menu above the charts and select the desired time span. - -- To add a chart, click **Add Chart** and customize the new chart. - -- To customize each chart, use the **Units** dropdown menu to set the units to display. Then use the table below the chart to select the metrics being queried, and how they'll be combined and displayed. Options include: -{% include {{page.version.version}}/admin-ui-custom-chart-debug-page-00.html %} - -## Examples - -### Query user and system CPU usage - -CockroachDB Admin UI - -To compare system vs. userspace CPU usage, select the following values under **Metric Name**: - -- `sys.cpu.sys.percent` -- `sys.cpu.user.percent` - -The Y-axis label is the **Count**. A count of 1 represents 100% utilization. The **Aggregator** of **Sum** can show the count to be above 1, which would mean CPU utilization is greater than 100%. - -Checking **Per Node** displays statistics for each node, which could show whether an individual node's CPU usage was higher or lower than the average. - -## Available metrics - -{{site.data.alerts.callout_info}} -This list is taken directly from the source code and is subject to change. Some of the metrics listed below are already visible in other areas of the [Admin UI](admin-ui-overview.html). -{{site.data.alerts.end}} - -{% include {{page.version.version}}/metric-names.md %} - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-databases-page.md b/src/current/v19.2/admin-ui-databases-page.md deleted file mode 100644 index b706ead0835..00000000000 --- a/src/current/v19.2/admin-ui-databases-page.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -title: Database Page -toc: true ---- - -{{site.data.alerts.callout_info}} -On a secure cluster, this area of the Admin UI can only be accessed by an `admin` user. See [Admin UI access](admin-ui-overview.html#admin-ui-access). -{{site.data.alerts.end}} - -The **Databases** page of the Admin UI provides details of the databases configured, the tables in each database, and the grants assigned to each user. To view these details, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) and then click **Databases** on the left-hand navigation bar. - - -## Tables view - -The **Tables** view shows details of the system table as well as the tables in your databases. To view these details, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) and then select **Databases** from the left-hand navigation bar. - -CockroachDB Admin UI Database Tables View - -The following details are displayed for each table: - -Metric | Description ---------|---- -Table Name | The name of the table. -Size | Approximate total disk size of the table across all replicas. -Ranges | The number of ranges in the table. -\# of Columns | The number of columns in the table. -\# of Indices | The number of indices for the table. - -## Grants view - -The **Grants** view shows the [privileges](authorization.html#assign-privileges) granted to users for each database. To view these details, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) and then select **Databases** from the left-hand navigation bar, select **Databases** from the left-hand navigation bar, and then select **Grants** from the **View** menu. - -For more details about grants and privileges, see [Grants](grant.html). - -CockroachDB Admin UI Database Grants View - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-debug-pages.md b/src/current/v19.2/admin-ui-debug-pages.md deleted file mode 100644 index 72a19461140..00000000000 --- a/src/current/v19.2/admin-ui-debug-pages.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -title: Advanced Debugging Page -toc: true ---- - -The **Advanced Debugging** page of the Admin UI provides links to advanced monitoring and troubleshooting reports and cluster configuration details. To view the **Advanced Debugging** page, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) and then click the gear icon on the left-hand navigation bar. - -{{site.data.alerts.callout_info}} -These pages are experimental and undocumented. If you find an issue, let us know through [these channels](https://www.cockroachlabs.com/community/). - {{site.data.alerts.end}} - -## License and node information - -On the right-side of the page, the following information is displayed: - -- CockroachDB license type: Helps determine if you have access to Enterprise features. -- Current node ID: Helps identify the current node when viewing the Admin UI through a load balancer. - -## Reports and Configuration - -The following debug reports and configuration views are useful for monitoring and troubleshooting CockroachDB: - -Report | Description | Access level ---------|-----|-------- -[Custom Time Series Chart](admin-ui-custom-chart-debug-page.html) | Create a custom chart of time series data. | All users -Problem Ranges | View ranges in your cluster that are unavailable, underreplicated, slow, or have other problems. | [`admin` users only on secure clusters](admin-ui-overview.html#admin-ui-access) -Network Latency | Check latencies between all nodes in your cluster. | All users -Data Distribution and Zone Configs | View the distribution of table data across nodes and verify zone configuration. | [`admin` users only on secure clusters](admin-ui-overview.html#admin-ui-access) -Cluster Settings | View cluster settings and their configured values. | All users can view data according to their privileges -Localities | Check node localities for your cluster. | [`admin` users only on secure clusters](admin-ui-overview.html#admin-ui-access) - -## Even More Advanced Debugging - -The **Even More Advanced Debugging** section of the page lists additional reports that are largely internal and intended for use by CockroachDB developers. You can ignore this section while monitoring and troubleshooting CockroachDB. Alternatively, if you want to learn how to use these pages, feel free to contact us through [these channels](https://www.cockroachlabs.com/community/). - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-hardware-dashboard.md b/src/current/v19.2/admin-ui-hardware-dashboard.md deleted file mode 100644 index 6aa0a526cc2..00000000000 --- a/src/current/v19.2/admin-ui-hardware-dashboard.md +++ /dev/null @@ -1,115 +0,0 @@ ---- -title: Hardware Dashboard -summary: The Hardware dashboard lets you monitor CPU usage, disk throughput, network traffic, storage capacity, and memory. -toc: true ---- - -The **Hardware** dashboard lets you monitor CPU usage, disk throughput, network traffic, storage capacity, and memory. To view this dashboard, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui), click **Metrics** on the left, and then select **Dashboard** > **Hardware**. - -The **Hardware** dashboard displays the following time series graphs: - -## CPU Percent - -CockroachDB Admin UI CPU Percent graph - -{{site.data.alerts.callout_info}} -This graph shows the CPU consumption by the CockroachDB process only and is useful as long as there are no other processes consuming significant CPU on the node. In case you have other processes running on the node, use a separate monitoring tool to measure the total CPU consumption across all processes. -{{site.data.alerts.end}} - -- In the node view, the graph shows the percentage of CPU in use by the CockroachDB process for the selected node. - -- In the cluster view, the graph shows the percentage of CPU in use by the CockroachDB process across all nodes. - -{{site.data.alerts.callout_info}} -For multi-core systems, the percentage of CPU usage is calculated by normalizing the CPU usage across all cores, whereby 100% utilization indicates that all cores are fully utilized. -{{site.data.alerts.end}} - -## Memory Usage - -CockroachDB Admin UI Memory Usage graph - -{{site.data.alerts.callout_info}} -This graph shows the memory consumption by the CockroachDB process only and is useful as long as there are no other processes consuming significant memory on the node. In case you have other processes running on the node, use a separate monitoring tool to measure the total memory consumption across all processes. -{{site.data.alerts.end}} - -- In the node view, the graph shows the memory in use by CockroachDB for the selected node. - -- In the cluster view, the graph shows the memory in use by CockroachDB across all nodes in the cluster. - -## Disk Read Bytes - -CockroachDB Admin UI Disk Read Bytes graph - -- In the node view, the graph shows the 10-second average of the number of bytes read per second by all processes, including CockroachDB, for the selected node. - -- In the cluster view, the graph shows the 10-second average of the number of bytes read per second by all processes, including CockroachDB, across all nodes. - -## Disk Write Bytes - -CockroachDB Admin UI Disk Write Bytes graph - -- In the node view, the graph shows the 10-second average of the number of bytes written per second by all processes, including CockroachDB, for the node. - -- In the cluster view, the graph shows the 10-second average of the number of bytes written per second by all processes, including CockroachDB, across all nodes. - -## Disk Read Ops - -CockroachDB Admin UI Disk Read Ops graph - -- In the node view, the graph shows the 10-second average of the number of disk read ops per second for all processes, including CockroachDB, for the selected node. - -- In the cluster view, the graph shows the 10-second average of the number of disk read ops per second for all processes, including CockroachDB, across all nodes. - -## Disk Write Ops - -CockroachDB Admin UI Disk Write Ops graph - -- In the node view, the graph shows the 10-second average of the number of disk write ops per second for all processes, including CockroachDB, for the node. - -- In the cluster view, the graph shows the 10-second average of the number of disk write ops per second for all processes, including CockroachDB, across all nodes. - -## Disk IOPS in Progress - -CockroachDB Admin UI Disk IOPS in Progress graph - -- In the node view, the graph shows the number of disk reads and writes in queue for all processes, including CockroachDB, for the selected node. - -- In the cluster view, the graph shows the number of disk reads and writes in queue for all processes, including CockroachDB, across all nodes in the cluster. - -{{site.data.alerts.callout_info}} -For Mac OS, this graph is not populated and shows zero disk IOPS in progress. This is a [known limitation](https://github.com/cockroachdb/cockroach/issues/27927) that may be lifted in the future. -{{site.data.alerts.end}} - -## Available Disk Capacity - -CockroachDB Admin UI Disk Capacity graph - -- In the node view, the graph shows the available storage capacity for the selected node. - -- In the cluster view, the graph shows the available storage capacity across all nodes in the cluster. - -{{site.data.alerts.callout_info}} -{% include {{ page.version.version }}/misc/available-capacity-metric.md %} -{{site.data.alerts.end}} - -## Network Bytes Received - -CockroachDB Admin UI Network Bytes Received graph - -- In the node view, the graph shows the 10-second average of the number of network bytes received per second for all processes, including CockroachDB, for the node. - -- In the cluster view, the graph shows the 10-second average of the number of network bytes received for all processes, including CockroachDB, per second across all nodes. - -## Network Bytes Sent - -CockroachDB Admin UI Network Bytes Sent graph - -- In the node view, the graph shows the 10-second average of the number of network bytes sent per second by all processes, including CockroachDB, for the node. - -- In the cluster view, the graph shows the 10-second average of the number of network bytes sent per second by all processes, including CockroachDB, across all nodes. - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-jobs-page.md b/src/current/v19.2/admin-ui-jobs-page.md deleted file mode 100644 index 96d2f3b7b57..00000000000 --- a/src/current/v19.2/admin-ui-jobs-page.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -title: Jobs Page -toc: true ---- - -{{site.data.alerts.callout_info}} -On a secure cluster, this area of the Admin UI can only be accessed by an `admin` user. See [Admin UI access](admin-ui-overview.html#admin-ui-access). -{{site.data.alerts.end}} - -The **Jobs** page of the Admin UI provides details about the backup/restore jobs, schema changes, [user-created table statistics](create-statistics.html) and [automatic table statistics](cost-based-optimizer.html#table-statistics) jobs, and changefeeds performed across all nodes in the cluster. To view these details, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) and then click **Jobs** on the left-hand navigation bar. - - -## Job details - -The **Jobs** table displays the ID, description, user, creation time, and status of each backup and restore job, schema changes, user-created table statistics and automatic table statistics jobs, and changefeeds performed across all nodes in the cluster. To view the job's full description, click the drop-down arrow in the first column. - -CockroachDB Admin UI Jobs Page - -For changefeeds, the table displays a [high-water timestamp that advances as the changefeed progresses](change-data-capture.html#monitor-a-changefeed). This is a guarantee that all changes before or at the timestamp have been emitted. Hover over the high-water timestamp to view the [system time](as-of-system-time.html). - -The automatic table statistics jobs are not displayed even when the **TYPE** drop-down is set to **All**. To view the automatic statistics creation jobs, filter the results to **Automatic-Statistics Creation** as described in the [Filtering results](#filtering-results) section. - -## Filtering results - -You can filter the results based on the status of the jobs or the type of jobs (backups, restores, schema changes, changefeeds, user-created table statistics, and automatic table statistics). You can also choose to view either the latest 50 jobs or all the jobs across all nodes. - -Filter By | Description -----------|------------ -Job Status | From the **Status** menu, select the required status filter. -Job Type | From the **Type** menu, select **Backups**, **Restores**, **Imports**, **Schema Changes**, **Changefeed**, **Statistics Creation**, or **Auto-Statistics Creation**. -Jobs Shown | From the **Show** menu, select **First 50** or **All**. - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-overview-dashboard.md b/src/current/v19.2/admin-ui-overview-dashboard.md deleted file mode 100644 index 9bd758fd814..00000000000 --- a/src/current/v19.2/admin-ui-overview-dashboard.md +++ /dev/null @@ -1,72 +0,0 @@ ---- -title: Overview Dashboard -summary: The Overview dashboard lets you monitor important SQL performance, replication, and storage metrics. -toc: true ---- - -The **Overview** dashboard lets you monitor important SQL performance, replication, and storage metrics. To view this dashboard, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) and click **Metrics** on the left-hand navigation bar. The **Overview** dashboard is displayed by default. - - -The **Overview** dashboard displays the following time series graphs: - -## SQL Queries - -CockroachDB Admin UI SQL Queries graph - -- In the node view, the graph shows the 10-second average of the number of `SELECT`/`INSERT`/`UPDATE`/`DELETE` queries per second issued by SQL clients on the node. - -- In the cluster view, the graph shows the sum of the per-node averages, that is, an aggregate estimation of the current query load over the cluster, assuming the last 10 seconds of activity per node are representative of this load. - -## Service Latency: SQL, 99th percentile - -CockroachDB Admin UI Service Latency graph - -Service latency is calculated as the time between when the cluster receives a query and finishes executing the query. This time does not include returning results to the client. - -- In the node view, the graph shows the 99th [percentile](https://en.wikipedia.org/wiki/Percentile#The_normal_distribution_and_percentiles) of service latency for the node. - -- In the cluster view, the graph shows the 99th [percentile](https://en.wikipedia.org/wiki/Percentile#The_normal_distribution_and_percentiles) of service latency across all nodes in the cluster. - -## Replicas per Node - -CockroachDB Admin UI Replicas per node graph - -Ranges are subsets of your data, which are replicated to ensure survivability. Ranges are replicated to a configurable number of CockroachDB nodes. - -- In the node view, the graph shows the number of range replicas on the selected node. - -- In the cluster view, the graph shows the number of range replicas on each node in the cluster. - -For details about how to control the number and location of replicas, see [Configure Replication Zones](configure-replication-zones.html). - -{{site.data.alerts.callout_info}} -The timeseries data used to power the graphs in the Admin UI is stored within the cluster and accumulates for 30 days before it starts getting truncated. As a result, for the first 30 days or so of a cluster's life, you will see a steady increase in disk usage and the number of ranges even if you aren't writing data to the cluster yourself. For more details, see this [FAQ](operational-faqs.html#why-is-disk-usage-increasing-despite-lack-of-writes). -{{site.data.alerts.end}} - -## Capacity - -CockroachDB Admin UI Capacity graph - -You can monitor the **Capacity** graph to determine when additional storage is needed. - -- In the node view, the graph shows the maximum allocated capacity, available storage capacity, and capacity used by CockroachDB for the selected node. - -- In the cluster view, the graph shows the maximum allocated capacity, available storage capacity, and capacity used by CockroachDB across all nodes in the cluster. - -On hovering over the graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -**Capacity** | The maximum storage capacity allocated to CockroachDB. You can configure the maximum storage capacity for a given node using the `--store` flag. For more information, see [Start a Node](cockroach-start.html#store). -**Available** | The free storage capacity available to CockroachDB. -**Used** | Disk space used by the data in the CockroachDB store. Note that this value is less than (**Capacity** - **Available**) because **Capacity** and **Available** metrics consider the entire disk and all applications on the disk, including CockroachDB, whereas **Used** metric tracks only the store's disk usage. - -{{site.data.alerts.callout_info}} -{% include {{ page.version.version }}/misc/available-capacity-metric.md %} -{{site.data.alerts.end}} - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-overview.md b/src/current/v19.2/admin-ui-overview.md deleted file mode 100644 index d0b3e52dcec..00000000000 --- a/src/current/v19.2/admin-ui-overview.md +++ /dev/null @@ -1,60 +0,0 @@ ---- -title: Admin UI Overview -summary: Use the Admin UI to monitor and optimize cluster performance. -toc: true -key: explore-the-admin-ui.html ---- - -The CockroachDB Admin UI provides details about your cluster and database configuration, and helps you optimize cluster performance. - -## Admin UI areas - -Area | Description ---------|---- -[Node Map](enable-node-map.html) | View and monitor the metrics and geographical configuration of your cluster. -[Cluster Health](admin-ui-access-and-navigate.html#summary-panel) | View essential metrics about the cluster's health, such as the number of live, dead, and suspect nodes, the number of unavailable ranges, and the queries per second and service latency across the cluster. -[Overview Metrics](admin-ui-overview-dashboard.html) | View important SQL performance, replication, and storage metrics. -[Hardware Metrics](admin-ui-hardware-dashboard.html) | View metrics about CPU usage, disk throughput, network traffic, storage capacity, and memory. -[Runtime Metrics](admin-ui-runtime-dashboard.html) | View metrics about node count, CPU time, and memory usage. -[SQL Performance](admin-ui-sql-dashboard.html) | View metrics about SQL connections, byte traffic, queries, transactions, and service latency. -[Storage Utilization](admin-ui-storage-dashboard.html) | View metrics about storage capacity and file descriptors. -[Replication Details](admin-ui-replication-dashboard.html) | View metrics about how data is replicated across the cluster, such as range status, replicas per store, and replica quiescence. -[Changefeed Details](admin-ui-cdc-dashboard.html) | View metrics about the [changefeeds](change-data-capture.html) created across your cluster. -[Nodes Details](admin-ui-access-and-navigate.html#summary-panel) | View details of live, dead, and decommissioned nodes. -[Events](admin-ui-access-and-navigate.html#events-panel) | View a list of recent cluster events. -[Database Details](admin-ui-databases-page.html) | View details about the system and user databases in the cluster. -[Statements Details](admin-ui-statements-page.html) | Identify frequently executed or high latency [SQL statements](sql-statements.html) -[Jobs Details](admin-ui-jobs-page.html) | View details of the jobs running in the cluster. -[Advanced Debugging Pages](admin-ui-debug-pages.html) | View advanced monitoring and troubleshooting reports. These include details about data distribution, the state of specific queues, and slow query metrics. These details are largely intended for use by CockroachDB developers. - -## Admin UI access - -On insecure clusters, all areas of the Admin UI are accessible to all users. - -On secure clusters, certain areas of the Admin UI can only be accessed by [`admin` users](authorization.html#admin-role). These areas display information from privileged HTTP endpoints that operate with `admin` privilege. - -For security reasons, non-admin users access only the data over which they have privileges (e.g., their tables and list of sessions), and data that does not require privileges (e.g., cluster health, node status, metrics). - -{{site.data.alerts.callout_info}} -The default `root` user is a member of the `admin` role, but on CockroachDB clusters prior to v20.1, the Admin UI cannot be accessed by `root`. To access the secure Admin UI areas, [grant a user membership to the `admin` role](grant-roles.html): - -GRANT admin TO \; -{{site.data.alerts.end}} - -Secure area | Privileged information ------|----- -[Node Map](enable-node-map.html) | Database and table names -[Database Details](admin-ui-databases-page.html) | Stored table data -[Statements Details](admin-ui-statements-page.html) | SQL statements -[Jobs Details](admin-ui-jobs-page.html) | SQL statements and operational details -[Advanced Debugging Pages](admin-ui-debug-pages.html) (some reports) | Stored table data, operational details, internal IP addresses, names, credentials, application data (depending on report) - -{{site.data.alerts.callout_info}} -By default, the Admin UI shares anonymous usage details with Cockroach Labs. For information about the details shared and how to opt-out of reporting, see [Diagnostics Reporting](diagnostics-reporting.html). -{{site.data.alerts.end}} - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-replication-dashboard.md b/src/current/v19.2/admin-ui-replication-dashboard.md deleted file mode 100644 index 46e78093151..00000000000 --- a/src/current/v19.2/admin-ui-replication-dashboard.md +++ /dev/null @@ -1,99 +0,0 @@ ---- -title: Replication Dashboard -summary: The Replication dashboard lets you monitor the replication metrics for your cluster. -toc: true ---- - -The **Replication** dashboard in the CockroachDB Admin UI enables you to monitor the replication metrics for your cluster. To view this dashboard, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui), click **Metrics** on the left-hand navigation bar, and then select **Dashboard** > **Replication**. - - -## Review of CockroachDB terminology - -- **Range**: CockroachDB stores all user data and almost all system data in a giant sorted map of key-value pairs. This keyspace is divided into "ranges", contiguous chunks of the keyspace, so that every key can always be found in a single range. -- **Range Replica:** CockroachDB replicates each range (3 times by default) and stores each replica on a different node. -- **Range Lease:** For each range, one of the replicas holds the "range lease". This replica, referred to as the "leaseholder", is the one that receives and coordinates all read and write requests for the range. -- **Under-replicated Ranges:** When a cluster is first initialized, the few default starting ranges will only have a single replica, but as soon as other nodes are available, they will replicate to them until they've reached their desired replication factor, the default being 3. If a range does not have enough replicas, the range is said to be "under-replicated". -- **Unavailable Ranges:** If a majority of a range's replicas are on nodes that are unavailable, then the entire range is unavailable and will be unable to process queries. - -For more details, see [Scalable SQL Made Easy: How CockroachDB Automates Operations](https://www.cockroachlabs.com/blog/automated-rebalance-and-repair/) - -## Replication dashboard - -The **Replication** dashboard displays the following time series graphs: - -### Ranges - -CockroachDB Admin UI Replicas per Store - -The **Ranges** graph shows you various details about the status of ranges. - -- In the node view, the graph shows details about ranges on the node. - -- In the cluster view, the graph shows details about ranges across all nodes in the cluster. - -On hovering over the graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -Ranges | The number of ranges. -Leaders | The number of ranges with leaders. If the number does not match the number of ranges for a long time, troubleshoot your cluster. -Lease Holders | The number of ranges that have leases. -Leaders w/o Leases | The number of Raft leaders without leases. If the number if non-zero for a long time, troubleshoot your cluster. -Unavailable | The number of unavailable ranges. If the number if non-zero for a long time, troubleshoot your cluster. -Under-replicated | The number of under-replicated ranges. - -### Replicas Per Store - -CockroachDB Admin UI Replicas per Store - -- In the node view, the graph shows the number of range replicas on the store. - -- In the cluster view, the graph shows the number of range replicas on each store. - -You can [Configure replication zones](configure-replication-zones.html) to set the number and location of replicas. You can monitor the configuration changes using the Admin UI, as described in [Fault tolerance and recovery](demo-fault-tolerance-and-recovery.html). - -### Replica Quiescence - -CockroachDB Admin UI Replica Quiescence - -- In the node view, the graph shows the number of replicas on the node. - -- In the cluster view, the graph shows the number of replicas across all nodes. - -On hovering over the graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -Replicas | The number of replicas. -Quiescent | The number of replicas that haven't been accessed for a while. - -### Snapshots - -CockroachDB Admin UI Replica Snapshots - -Usually the nodes in a [Raft group](architecture/replication-layer.html#raft) stay synchronized by following along the log message by message. However, if a node is far enough behind the log (e.g., if it was offline or is a new node getting up to speed), rather than send all the individual messages that changed the range, the cluster can send it a snapshot of the range and it can start following along from there. Commonly this is done preemptively, when the cluster can predict that a node will need to catch up, but occasionally the Raft protocol itself will request the snapshot. - -Metric | Description --------|------------ -Generated | The number of snapshots created per second. -Applied (Raft-initiated) | The number of snapshots applied to nodes per second that were initiated within Raft. -Applied (Learner) | New in v19.2: The number of snapshots applied to nodes per second that were anticipated ahead of time (e.g., because a node was about to be added to a Raft group). This metric replaces the `Applied (Preemptive)` metric in 19.2 and onwards. -Applied (Preemptive) | The number of snapshots applied to nodes per second that were anticipated ahead of time (e.g., because a node was about to be added to a Raft group). This metric was used in pre-v19.2 releases and will be removed in future releases. -Reserved | The number of slots reserved per second for incoming snapshots that will be sent to a node. - -### Other graphs - -The **Replication** dashboard shows other time series graphs that are important for CockroachDB developers: - -- Leaseholders per Store -- Average Queries per Store -- Logical Bytes per Store -- Range Operations - -For monitoring CockroachDB, it is sufficient to use the [**Ranges**](#ranges), [**Replicas per Store**](#replicas-per-store), and [**Replica Quiescence**](#replica-quiescence) graphs. - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-runtime-dashboard.md b/src/current/v19.2/admin-ui-runtime-dashboard.md deleted file mode 100644 index 8dcfa60ff24..00000000000 --- a/src/current/v19.2/admin-ui-runtime-dashboard.md +++ /dev/null @@ -1,75 +0,0 @@ ---- -title: Runtime Dashboard -toc: true ---- - -The **Runtime** dashboard in the CockroachDB Admin UI lets you monitor runtime metrics for you cluster, such as node count, memory usage, and CPU time. To view this dashboard, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui), click **Metrics** on the left-hand navigation bar, and then select **Dashboard** > **Runtime**. - - -The **Runtime** dashboard displays the following time series graphs: - -## Live Node Count - -CockroachDB Admin UI Node Count - -In the node view as well as the cluster view, the graph shows the number of live nodes in the cluster. - -A dip in the graph indicates decommissioned nodes, dead nodes, or nodes that are not responding. To troubleshoot the dip in the graph, refer to the [Summary panel](admin-ui-access-and-navigate.html#summary-panel). - -## Memory Usage - -CockroachDB Admin UI Memory Usage - -- In the node view, the graph shows the memory in use for the selected node. - -- In the cluster view, the graph shows the memory in use across all nodes in the cluster. - -On hovering over the graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -RSS | Total memory in use by CockroachDB. -Go Allocated | Memory allocated by the Go layer. -Go Total | Total memory managed by the Go layer. -CGo Allocated | Memory allocated by the C layer. -CGo Total | Total memory managed by the C layer. - -{{site.data.alerts.callout_info}}If Go Total or CGO Total fluctuates or grows steadily over time, contact us.{{site.data.alerts.end}} - -## CPU Time - -CockroachDB Admin UI CPU Time - - -- In the node view, the graph shows the [CPU time](https://en.wikipedia.org/wiki/CPU_time) used by CockroachDB user and system-level operations for the selected node. -- In the cluster view, the graph shows the [CPU time](https://en.wikipedia.org/wiki/CPU_time) used by CockroachDB user and system-level operations across all nodes in the cluster. - -On hovering over the CPU Time graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -User CPU Time | Total CPU seconds per second used by the CockroachDB process across all nodes. -Sys CPU Time | Total CPU seconds per second used for CockroachDB system-level operations across all nodes. - -## Clock Offset - -CockroachDB Admin UI Clock Offset - -- In the node view, the graph shows the mean clock offset of the node against the rest of the cluster. -- In the cluster view, the graph shows the mean clock offset of each node against the rest of the cluster. - -## Other graphs - -The **Runtime** dashboard shows other time series graphs that are important for CockroachDB developers: - -- Goroutine Count -- GC Runs -- GC Pause Time - -For monitoring CockroachDB, it is sufficient to use the [**Live Node Count**](#live-node-count), [**Memory Usage**](#memory-usage), [**CPU Time**](#cpu-time), and [**Clock Offset**](#clock-offset) graphs. - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-sql-dashboard.md b/src/current/v19.2/admin-ui-sql-dashboard.md deleted file mode 100644 index e576c3052ed..00000000000 --- a/src/current/v19.2/admin-ui-sql-dashboard.md +++ /dev/null @@ -1,86 +0,0 @@ ---- -title: SQL Dashboard -summary: The SQL dashboard lets you monitor the performance of your SQL queries. -toc: true ---- - -The **SQL** dashboard in the CockroachDB Admin UI lets you monitor the performance of your SQL queries. To view this dashboard, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui), click **Metrics** on the left-hand navigation bar, and then select **Dashboard** > **SQL**. - - -The **SQL** dashboard displays the following time series graphs: - -## SQL Connections - -CockroachDB Admin UI SQL Connections - -- In the node view, the graph shows the number of connections currently open between the client and the selected node. - -- In the cluster view, the graph shows the total number of SQL client connections to all nodes combined. - -## SQL Byte Traffic - -CockroachDB Admin UI SQL Byte Traffic - -The **SQL Byte Traffic** graph helps you correlate SQL query count to byte traffic, especially in bulk data inserts or analytic queries that return data in bulk. - -- In the node view, the graph shows the current byte throughput (bytes/second) between all the currently connected SQL clients and the node. - -- In the cluster view, the graph shows the aggregate client throughput across all nodes. - -## SQL Queries - -CockroachDB Admin UI SQL Queries - -- In the node view, the graph shows the 10-second average of the number of `SELECT`/`INSERT`/`UPDATE`/`DELETE` queries per second issued by SQL clients on the node. - -- In the cluster view, the graph shows the sum of the per-node averages, that is, an aggregate estimation of the current query load over the cluster, assuming the last 10 seconds of activity per node are representative of this load. - -## SQL Query Errors - -CockroachDB Admin UI SQL Query Errors - -- In the node view, the graph shows the 10-second average of the number of SQL statements issued to the node that returned a [planning](architecture/sql-layer.html#sql-parser-planner-executor), [runtime](architecture/sql-layer.html#sql-parser-planner-executor), or [retry error](transactions.html#error-handling). - -- In the cluster view, the graph shows the 10-second average of the number of SQL statements that returned a [planning](architecture/sql-layer.html#sql-parser-planner-executor), [runtime](architecture/sql-layer.html#sql-parser-planner-executor), or [retry error](transactions.html#error-handling) across all nodes. - -## Service Latency: SQL, 99th percentile - -CockroachDB Admin UI Service Latency - -Service latency is calculated as the time between when the cluster receives a query and finishes executing the query. This time does not include returning results to the client. - -- In the node view, the graph displays the 99th [percentile](https://en.wikipedia.org/wiki/Percentile#The_normal_distribution_and_percentiles) of service latency for the selected node. - -- In the cluster view, the graph displays the 99th [percentile](https://en.wikipedia.org/wiki/Percentile#The_normal_distribution_and_percentiles) of service latency for each node in the cluster. - -## Transactions - -CockroachDB Admin UI Transactions - -- In the node view, the graph shows the 10-second average of the number of opened, committed, aborted, and rolled back [transactions](transactions.html) per second issued by SQL clients on the node. - -- In the cluster view, the graph shows the sum of the per-node averages, that is, an aggregate estimation of the current [transactions](transactions.html) load over the cluster, assuming the last 10 seconds of activity per node are representative of this load. - -If the graph shows excessive aborts or rollbacks, it might indicate issues with the SQL queries. In that case, re-examine queries to lower contention. - -Additionally, per-application average transaction times are displayed for each node, at the 90th and 99th percentiles. - -CockroachDB Admin UI Transaction Latencies - -## Other graphs - -The **SQL** dashboard shows other time series graphs that are important for CockroachDB developers: - -- KV Execution Latency -- Active Distributed SQL Queries -- Active Flows for Distributed SQL Queries -- Service Latency: DistSQL -- Schema Changes - -For monitoring CockroachDB, it is sufficient to use the [**SQL Connections**](#sql-connections), [**SQL Byte Traffic**](#sql-byte-traffic), [**SQL Queries**](#sql-queries), [**Service Latency**](#service-latency-sql-99th-percentile), and [**Transactions**](#transactions) graphs. - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-statements-page.md b/src/current/v19.2/admin-ui-statements-page.md deleted file mode 100644 index fbb6e3d34d9..00000000000 --- a/src/current/v19.2/admin-ui-statements-page.md +++ /dev/null @@ -1,141 +0,0 @@ ---- -title: Statements Page -toc: true ---- - -{{site.data.alerts.callout_info}} -On a secure cluster, this area of the Admin UI can only be accessed by an `admin` user. See [Admin UI access](admin-ui-overview.html#admin-ui-access). -{{site.data.alerts.end}} - -The **Statements** page helps you identify frequently executed or high latency [SQL statements](sql-statements.html). The **Statements** page also allows you to view the details of an individual SQL statement by clicking on the statement to view the **Statement Details** page. - -To view the **Statements** page, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) and then click **Statements** on the left. - -CockroachDB Admin UI Statements Page - -## Limitation - -The **Statements** page displays the details of the SQL statements executed within a specified time interval. At the end of the interval, the display is wiped clean, and you'll not see any statements on the page until the next set of statements is executed. By default, the time interval is set to one hour; however, you can customize the interval using the [`diagnostics.reporting.interval`](cluster-settings.html#settings) cluster setting. - -## Filtering by application - -If you have multiple applications running on the cluster, the **Statements** page shows the statements from all of the applications by default. To view the statements pertaining to a particular application, select the [application name](connection-parameters.html#additional-connection-parameters) from the **App** dropdown menu. If you haven't set the application name in the connection string, it appears as `unset` in the dropdown menu. - -## Understanding the Statements page - -### SQL statement fingerprint - -The **Statements** page displays the details of SQL statement fingerprints instead of individual SQL statements. - -A statement fingerprint is a grouping of similar SQL statements in their abstracted form by replacing the literal values with underscores (`_`). Grouping similar SQL statements as fingerprints helps you quickly identify frequently executed SQL statements and their latencies. - -A statement fingerprint is generated when two or more statements are the same after any literal values in them (e.g.,numbers and strings) are replaced with underscores. For example, the following statements have the same once their numbers have been replaced with underscores: - -- `INSERT INTO new_order(product_id, customer_id, transaction_id) VALUES (380, 11, 11098)` -- `INSERT INTO new_order(product_id, customer_id, transaction_id) VALUES (192, 891, 20)` -- `INSERT INTO new_order(product_id, customer_id, transaction_id) VALUES (784, 452, 78)` - -Thus, they can have the same fingerprint: - -`INSERT INTO new_order(product_id, customer_id, no_w_id) VALUES (_, _, _)` - -The following statements are different enough to not have the same fingerprint: - -- `INSERT INTO orders(product_id, customer_id, transaction_id) VALUES (380, 11, 11098)` -- `INSERT INTO new_order(product_id, customer_id, transaction_id) VALUES (380, 11, 11098)` -- `INSERT INTO new_order(product_id, customer_id, transaction_id) VALUES ($1, 11, 11098)` -- `INSERT INTO new_order(product_id, customer_id, transaction_id) VALUES ($1, $2, 11098)` -- `INSERT INTO new_order(product_id, customer_id, transaction_id) VALUES ($1, $2, $3)` - -### Parameters - -The **Statements** page displays the time, execution count, number of [retries](transactions.html#transaction-retries), number of rows affected, and latency for each statement fingerprint. By default, the statement fingerprints are sorted by time; however, you can sort the table by execution count, retries, rows affected, and latency. - -The following details are provided for each statement fingerprint: - -Parameter | Description ------|------------ -Statement | The SQL statement or the fingerprint of similar SQL statements.

      To view additional details of a statement fingerprint, click on the statement fingerprint in the **Statement** column to see the [**Statement Details** page](#statement-details-page). -TXN Type | New in v19.2: The type of transaction (implicit or explicit). Explicit transactions refer to the statements that are wrapped by [`BEGIN`](begin-transaction.html) and [`COMMIT`](commit-transaction.html) statements by the client. For statements not in explicit transactions, CockroachDB wraps each statement in individual implicit transactions. Explicit transactions employ [transactional pipelining](architecture/transaction-layer.html#transaction-pipelining) and therefore report latencies that do not account for replication. -Time | The cumulative time taken to execute the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation). -Execution Count | The total number of times the SQL statement (or multiple statements having the same fingerprint) is executed within the last hour or the [specified time interval](#limitation).

      The execution count is displayed in numerical value as well as in the form of a horizontal bar. The bar is color-coded to indicate the ratio of runtime success (indicated by blue) to runtime failure (indicated by red) of the execution count for the fingerprint. The bar also helps you compare the execution count across all SQL fingerprints in the table.

      You can sort the table by count. -Retries | The cumulative number of retries to execute the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation). -Rows Affected | The average number of rows returned while executing the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation).

      The number of rows returned are represented in two ways: The numerical value shows the number of rows returned, while the horizontal bar is color-coded (blue indicates the mean value and yellow indicates one standard deviation of the mean value of the number of rows returned). The bar helps you compare the mean rows across all SQL fingerprints in the table.

      You can sort the table by rows returned. -Latency | The average service latency of the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation).

      The latency is represented in two ways: The numerical value shows the mean latency, while the horizontal bar is color-coded (blue indicates the mean value and yellow indicates one standard deviation of the mean value of latency). The bar also helps you compare the mean latencies across all SQL fingerprints in the table.

      You can sort the table by latency. - -## Statement Details page - -The **Statement Details** page displays the logical plan as well as the details of the time, execution count, retries, rows returned, and latency by phase and by gateway node for the selected statement fingerprint. - -CockroachDB Admin UI Statements Page - -### Logical plan - -The **Logical Plan** section displays CockroachDB's query plan for an [explainable statement](sql-grammar.html#preparable_stmt). You can then use this information to optimize the query. For more information about logical plans, see [`EXPLAIN`](explain.html). - -By default, the logical plan for each fingerprint is sampled every 5 minutes. You can use the `sql.metrics.statement_details.plan_collection.period` [cluster setting](cluster-settings.html) to change this time interval. For example, to change the interval to 2 minutes, run the following [`SET CLUSTER SETTING`](set-cluster-setting.html) command: - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING sql.metrics.statement_details.plan_collection.period = '2m0s'; -~~~ - -### Latency by Phase - -The **Latency by Phase** table provides the mean value and one standard deviation of the mean value of the overall service latency as well as latency for each execution phase (parse, plan, run) for the SQL statement (or multiple statements having the same fingerprint). The table provides the service latency details in numerical values as well as color-coded bar graphs: blue indicates the mean value and yellow indicates one standard deviation of the mean value of latency. - -### Statistics by Gateway Node - -The **Statistics by Gateway Node** table provides a breakdown of the number of statements of the selected fingerprint per gateway node. For each gateway node, the table also provides the following details: - -Parameter | Description ------|------------ -Node | The ID of the gateway node. -Time | The cumulative time taken to execute the statement within the last hour or the [specified time interval](#limitation). -Execution Count | The total number of times the SQL statement (or multiple statements having the same fingerprint) is executed. -Retries | The cumulative number of retries to execute the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation). -Rows Affected | The average number of rows returned while executing the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation).

      The number of rows returned are represented in two ways: The numerical value shows the number of rows returned, while the horizontal bar is color-coded (blue indicates the mean value and yellow indicates one standard deviation of the mean value of the number of rows returned). The bar helps you compare the mean rows across all SQL fingerprints in the table.

      You can sort the table by rows returned. -Latency | The average service latency of the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation).

      The latency is represented in two ways: The numerical value shows the mean latency, while the horizontal bar is color-coded (blue indicates the mean value and yellow indicates one standard deviation of the mean value). The bar also helps you compare the mean latencies across all SQL fingerprints in the table.

      You can sort the table by latency. - -### Execution Count - -The **Execution Count** table provides information about the following parameters in numerical values as well as bar graphs: - -Parameter | Description ------|------------ -First Attempts | The cumulative number of first attempts to execute the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation). -Retries | The cumulative number of retries to execute the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation). -Max Retries | The highest number of retries for a single SQL statement with this fingerprint within the last hour or the [specified time interval](#limitation).

      For example, if three statements having the same fingerprint had to be retried 0, 1, and 5 times, then the Max Retries value for the fingerprint is 5. -Total | The total number of executions of statements with this fingerprint. It is calculated as the sum of first attempts and cumulative retries. - -### Row Count - -The **Row Count** table provides the mean value and one standard deviation of the mean value of cumulative count of rows returned by the SQL statement (or multiple statements having the same fingerprint). The table provides the service latency details in numerical values as well as a bar graph. - -### Statistics - -The statistics box on the right-hand side of the **Statements Details** page provides the following details for the statement fingerprint: - -Parameter | Description ------|------------ -Total time | The cumulative time taken to execute the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation). -Execution count | The total number of times the SQL statement (or multiple statements having the same fingerprint) is executed within the last hour or the [specified time interval](#limitation). -Executed without retry | The percentage of successful executions of the SQL statement (or multiple statements having the same fingerprint) on the first attempt within the last hour or the [specified time interval](#limitation). -Mean service latency | The average service latency of the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation). -Mean number of rows | The average number of rows returned while executing the SQL statement (or multiple statements having the same fingerprint) within the last hour or the [specified time interval](#limitation). - -The table below the statistics box provides the following details: - -Parameter | Description ------|------------ -App | Name of the application specified by the [`application_name`](show-vars.html#supported-variables) session setting. The **Statements Details** page shows the details for this application. -Transaction Type | New in v19.2: The type of transaction (implicit or explicit). Explicit transactions refer to the statements that are wrapped by `BEGIN` and `COMMIT` statements by the client. For statements not in explicit transactions, CockroachDB wraps each statement in individual implicit transactions. Explicit transactions employ [transactional pipelining](architecture/transaction-layer.html#transaction-pipelining) and therefore report latencies that do not account for replication. -Distributed execution? | Indicates whether the statement execution was distributed. -Used cost-based optimizer? | Indicates whether the statement (or multiple statements having the same fingerprint) were executed using the [cost-based optimizer](cost-based-optimizer.html). -Failed? | Indicate if the statement (or multiple statements having the same fingerprint) were executed successfully. - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/admin-ui-storage-dashboard.md b/src/current/v19.2/admin-ui-storage-dashboard.md deleted file mode 100644 index 4ddff48f469..00000000000 --- a/src/current/v19.2/admin-ui-storage-dashboard.md +++ /dev/null @@ -1,69 +0,0 @@ ---- -title: Storage Dashboard -summary: The Storage dashboard lets you monitor the storage utilization for your cluster. -toc: true ---- - -The **Storage** dashboard in the CockroachDB Admin UI lets you monitor the storage utilization for your cluster. To view this dashboard, [access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui), click **Metrics** on the left-hand navigation bar, and then select **Dashboard** > **Storage**. - - -The **Storage** dashboard displays the following time series graphs: - -## Capacity - -CockroachDB Admin UI Capacity graph - -You can monitor the **Capacity** graph to determine when additional storage is needed. - -- In the node view, the graph shows the maximum allocated capacity, available storage capacity, and capacity used by CockroachDB for the selected node. - -- In the cluster view, the graph shows the maximum allocated capacity, available storage capacity, and capacity used by CockroachDB across all nodes in the cluster. - -On hovering over the graph, the values for the following metrics are displayed: - -Metric | Description ---------|---- -**Capacity** | The maximum storage capacity allocated to CockroachDB. You can configure the maximum storage capacity for a given node using the `--store` flag. For more information, see [Start a Node](cockroach-start.html#store). -**Available** | The free storage capacity available to CockroachDB. -**Used** | Disk space used by the data in the CockroachDB store. Note that this value is less than (**Capacity** - **Available**) because **Capacity** and **Available** metrics consider the entire disk and all applications on the disk, including CockroachDB, whereas **Used** metric tracks only the store's disk usage. - -{{site.data.alerts.callout_info}} -{% include {{ page.version.version }}/misc/available-capacity-metric.md %} -{{site.data.alerts.end}} - -## File Descriptors - -CockroachDB Admin UI File Descriptors - -- In the node view, the graph shows the number of open file descriptors for that node, compared with the file descriptor limit. - -- In the cluster view, the graph shows the number of open file descriptors across all nodes, compared with the file descriptor limit. - -If the Open count is almost equal to the Limit count, increase [File Descriptors](recommended-production-settings.html#file-descriptors-limit). - -{{site.data.alerts.callout_info}} -If you are running multiple nodes on a single machine (not recommended), the actual number of open file descriptors are considered open on each node. Thus the limit count value displayed on the Admin UI is the actual value of open file descriptors multiplied by the number of nodes, compared with the file descriptor limit. -{{site.data.alerts.end}} - -For Windows systems, you can ignore the File Descriptors graph because the concept of file descriptors is not applicable to Windows. - -## Other graphs - -The **Storage** dashboard shows other time series graphs that are important for CockroachDB developers: - -- Live Bytes -- Log Commit Latency -- Command Commit Latency -- Read Amplification -- SSTables -- Compactions/Flushes -- Time Series Writes -- Time Series Bytes Written - -For monitoring CockroachDB, it is sufficient to use the [**Capacity**](#capacity) and [**File Descriptors**](#file-descriptors) graphs. - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) -- [Raw Status Endpoints](monitoring-and-alerting.html#raw-status-endpoints) diff --git a/src/current/v19.2/advanced-client-side-transaction-retries.md b/src/current/v19.2/advanced-client-side-transaction-retries.md deleted file mode 100644 index 6657a4c0385..00000000000 --- a/src/current/v19.2/advanced-client-side-transaction-retries.md +++ /dev/null @@ -1,69 +0,0 @@ ---- -title: Advanced Client-side Transaction Retries -summary: Advanced client-side transaction retry features for library authors -toc: true ---- - -This page has instructions for authors of [database drivers and ORMs](install-client-drivers.html) who would like to implement client-side retries in their database driver or ORM for maximum efficiency and ease of use by application developers. - -{{site.data.alerts.callout_info}} -If you are an application developer who needs to implement an application-level retry loop, see the [Client-side intervention example](transactions.html#client-side-intervention-example). -{{site.data.alerts.end}} - -## Overview - -To improve the performance of transactions that fail due to contention, CockroachDB includes a set of statements (listed below) that let you retry those transactions. Retrying transactions using these statements has the following benefits: - -1. When you use savepoints, you "hold your place in line" between attempts. Without savepoints, you're starting from scratch every time. -2. Transactions increase their priority each time they're retried, increasing the likelihood they will succeed. This has a lesser effect than #1. - -## How transaction retries work - -A retryable transaction goes through the process described below, which maps to the following SQL statements: - -{% include copy-clipboard.html %} -~~~ sql -> BEGIN; -- #1 -> SAVEPOINT cockroach_restart; -- #2 --- ... various transaction statements ... -- #3 -> RELEASE SAVEPOINT cockroach_restart; -- #5 (Or #4, ROLLBACK, in case of retry error) -> COMMIT; -~~~ - -1. The transaction starts with the [`BEGIN`](begin-transaction.html) statement. - -2. The [`SAVEPOINT`](savepoint.html) statement declares the intention to retry the transaction in the case of contention errors. Note that CockroachDB's savepoint implementation does not support all savepoint functionality, such as nested transactions. It must be executed after [`BEGIN`](begin-transaction.html) but before the first statement that manipulates a database. - -3. The statements in the transaction are executed. - -4. If a statement returns a retry error (identified via the `40001` error code or `"retry transaction"` string at the start of the error message), you can issue the [`ROLLBACK TO SAVEPOINT`](rollback-transaction.html) statement to restart the transaction and increase the transaction's priority. Alternately, the original [`SAVEPOINT`](savepoint.html) statement can be reissued to restart the transaction. - - You must now issue the statements in the transaction again. - - In cases where you do not want the application to retry the transaction, you can simply issue [`ROLLBACK`](rollback-transaction.html) at this point. Any other statements will be rejected by the server, as is generally the case after an error has been encountered and the transaction has not been closed. - -5. Once the transaction executes all statements without encountering contention errors, execute [`RELEASE SAVEPOINT`](release-savepoint.html) to commit the changes. If this succeeds, all changes made by the transaction become visible to subsequent transactions and are guaranteed to be durable if a crash occurs. - - In some cases, the [`RELEASE SAVEPOINT`](release-savepoint.html) statement itself can fail with a retry error, mainly because transactions in CockroachDB only realize that they need to be restarted when they attempt to commit. If this happens, the retry error is handled as described in step 4. - -## Customizing the savepoint name - -{% include {{ page.version.version }}/misc/customizing-the-savepoint-name.md %} - -## Examples - -For examples showing how to use [`SAVEPOINT`](savepoint.html) and the other statements described on this page to implement library support for a programming language, see the following: - -- [Build a Java app with CockroachDB](build-a-java-app-with-cockroachdb.html), in particular the logic in the `runSQL` method. -- The source code of the [sqlalchemy-cockroachdb](https://github.com/cockroachdb/sqlalchemy-cockroachdb) adapter for SQLAlchemy. - -## See also - -- [Transactions](transactions.html) -- [`BEGIN`](begin-transaction.html) -- [`COMMIT`](commit-transaction.html) -- [`ROLLBACK`](rollback-transaction.html) -- [`SAVEPOINT`](savepoint.html) -- [`RELEASE SAVEPOINT`](release-savepoint.html) -- [`SHOW`](show-vars.html) -- [CockroachDB Architecture: Transaction Layer](architecture/transaction-layer.html) diff --git a/src/current/v19.2/alter-column.md b/src/current/v19.2/alter-column.md deleted file mode 100644 index 549cdf59f30..00000000000 --- a/src/current/v19.2/alter-column.md +++ /dev/null @@ -1,124 +0,0 @@ ---- -title: ALTER COLUMN -summary: Use the ALTER COLUMN statement to set, change, or drop a column's DEFAULT constraint or to drop the NOT NULL constraint. -toc: true ---- - -The `ALTER COLUMN` [statement](sql-statements.html) is part of `ALTER TABLE` and can be used to: - -- Set, change, or drop a column's [`DEFAULT` constraint](default-value.html) -- Set or drop a column's [`NOT NULL` constraint](not-null.html) -- Increase the precision of the column's [data type](data-types.html) - -{{site.data.alerts.callout_info}} -To manage other constraints, see [`ADD CONSTRAINT`](add-constraint.html) and [`DROP CONSTRAINT`](drop-constraint.html). -{{site.data.alerts.end}} - -{% include {{ page.version.version }}/sql/combine-alter-table-commands.md %} - -## Synopsis - -
      -{% include {{ page.version.version }}/sql/diagrams/alter_column.html %} -
      - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the table. - -## Parameters - -| Parameter | Description | -|-----------|-------------| -| `table_name` | The name of the table with the column you want to modify. | -| `column_name` | The name of the column you want to modify. | -| `SET DEFAULT a_expr` | The new [Default Value](default-value.html) you want to use. | -| `typename` | The new, altered type you want to use.
      In CockroachDB versions < v20.2, support for altering column types is limited to increasing the precision of the current column type. For details, see [Altering column types](#altering-column-types).| - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Altering column types - -In CockroachDB versions < v20.2, support for altering column types is limited to increasing the precision of the current type of a column. You cannot convert the column type to another data type, or decrease the precision of the column type. Changing the column type from its current type to the same type and precision will result in a no-op, with no error. - -You can use `ALTER COLUMN TYPE` if the following conditions are met: - -- The on-disk representation of the column remains unchanged. For example, you cannot change the column data type from `STRING` to an `INT`. -- The existing data remains valid. For example, you can change the column data type from `STRING[10]` to `STRING[20]`, but not to `STRING [5]` since that will invalidate the existing data. - -The following are equivalent in CockroachDB: - -- `ALTER TABLE ... ALTER ... TYPE` -- `ALTER TABLE ... ALTER COLUMN TYPE` -- `ALTER TABLE ... ALTER COLUMN SET DATA TYPE` - -For an example of `ALTER COLUMN TYPE`, see [Increase a column type's precision](#increase-a-column-types-precision). - -## Examples - -### Set or change a `DEFAULT` value - -Setting the [`DEFAULT` value constraint](default-value.html) inserts the value when data's written to the table without explicitly defining the value for the column. If the column already has a `DEFAULT` value set, you can use this statement to change it. - -The below example inserts the Boolean value `true` whenever you inserted data to the `subscriptions` table without defining a value for the `newsletter` column. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE subscriptions ALTER COLUMN newsletter SET DEFAULT true; -~~~ - -### Remove `DEFAULT` constraint - -If the column has a defined [`DEFAULT` value](default-value.html), you can remove the constraint, which means the column will no longer insert a value by default if one is not explicitly defined for the column. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE subscriptions ALTER COLUMN newsletter DROP DEFAULT; -~~~ - -### Set `NOT NULL` constraint - -New in v19.2: Setting the [`NOT NULL` constraint](not-null.html) specifies that the column cannot contain `NULL` values. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE subscriptions ALTER COLUMN newsletter SET NOT NULL; -~~~ - -### Remove `NOT NULL` constraint - -If the column has the [`NOT NULL` constraint](not-null.html) applied to it, you can remove the constraint, which means the column becomes optional and can have *NULL* values written into it. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE subscriptions ALTER COLUMN newsletter DROP NOT NULL; -~~~ - -### Convert a computed column into a regular column - -{% include {{ page.version.version }}/computed-columns/convert-computed-column.md %} - -### Increase a column type's precision - -The [TPC-C](performance-benchmarking-with-tpc-c-1k-warehouses.html) database contains a `customer` table with a column `c_credit_lim` of type [`DECIMAL(10,2)`](decimal.html). Suppose you want to increase the precision of the column's data type to `DECIMAL (12,2)`: - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE customer ALTER c_credit_lim type DECIMAL (12,2); -~~~ - -~~~ -ALTER TABLE - -Time: 80.814044ms -~~~ - -## See also - -- [Constraints](constraints.html) -- [`ADD CONSTRAINT`](add-constraint.html) -- [`DROP CONSTRAINT`](drop-constraint.html) -- [`ALTER TABLE`](alter-table.html) -- [`SHOW JOBS`](show-jobs.html) diff --git a/src/current/v19.2/alter-database.md b/src/current/v19.2/alter-database.md deleted file mode 100644 index 5492fea57a2..00000000000 --- a/src/current/v19.2/alter-database.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -title: ALTER DATABASE -summary: Use the ALTER DATABASE statement to change an existing database. -toc: false ---- - -The `ALTER DATABASE` [statement](sql-statements.html) applies a schema change to a database. For information on using `ALTER DATABASE`, see the pages for its relevant [subcommands](#subcommands). - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Subcommands - -Subcommand | Description ------------|------------ -[`CONFIGURE ZONE`](configure-zone.html) | [Configure replication zones](configure-replication-zones.html) for a database. -[`RENAME`](rename-database.html) | Change the name of a database. - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} diff --git a/src/current/v19.2/alter-index.md b/src/current/v19.2/alter-index.md deleted file mode 100644 index 36c9bd50b1f..00000000000 --- a/src/current/v19.2/alter-index.md +++ /dev/null @@ -1,23 +0,0 @@ ---- -title: ALTER INDEX -summary: Use the ALTER INDEX statement to change an existing index. -toc: true ---- - -The `ALTER INDEX` [statement](sql-statements.html) applies a schema change to an index. For information on using `ALTER INDEX`, see the pages for its relevant [subcommands](#subcommands). - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Subcommands - -Subcommand | Description ------------|------------ -[`CONFIGURE ZONE`](configure-zone.html) | [Configure replication zones](configure-replication-zones.html) for an index. -[`PARTITION BY`](partition-by.html) | Partition, re-partition, or un-partition an index. ([Enterprise-only](enterprise-licensing.html)). -[`RENAME`](rename-index.html) | Change the name of an index. -[`SPLIT AT`](split-at.html) | Force a range split at the specified row in the index. -[`UNSPLIT AT`](unsplit-at.html) | New in v19.2: Remove a range split enforcement at a specified row in the index. - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} diff --git a/src/current/v19.2/alter-partition.md b/src/current/v19.2/alter-partition.md deleted file mode 100644 index 01e59232a62..00000000000 --- a/src/current/v19.2/alter-partition.md +++ /dev/null @@ -1,11 +0,0 @@ ---- -title: ALTER PARTITION -summary: Use the ALTER PARTITION statement to configure the replication zone for a partition. -toc: true ---- - -The `ALTER PARTITION` [statement](sql-statements.html) is used to configure replication zones for partitions. See the [`CONFIGURE ZONE`](configure-zone.html) subcommand for more details. - -{{site.data.alerts.callout_info}} -[Partitioning](partitioning.html) is an [enterprise-only](enterprise-licensing.html) feature. -{{site.data.alerts.end}} diff --git a/src/current/v19.2/alter-range.md b/src/current/v19.2/alter-range.md deleted file mode 100644 index 664d8b7f1d0..00000000000 --- a/src/current/v19.2/alter-range.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -title: ALTER RANGE -summary: Use the ALTER RANGE statement to configure the replication zone for a system range. -toc: true ---- - -The `ALTER RANGE` [statement](sql-statements.html) is used to configure replication zones for system ranges. See the [`CONFIGURE ZONE`](configure-zone.html) subcommand for more details. diff --git a/src/current/v19.2/alter-sequence.md b/src/current/v19.2/alter-sequence.md deleted file mode 100644 index c09c49d2087..00000000000 --- a/src/current/v19.2/alter-sequence.md +++ /dev/null @@ -1,118 +0,0 @@ ---- -title: ALTER SEQUENCE -summary: Use the ALTER SEQUENCE statement to change the name, increment values, and other settings of a sequence. -toc: true ---- - -The `ALTER SEQUENCE` [statement](sql-statements.html) [changes the name](rename-sequence.html), increment values, and other settings of a sequence. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the parent database. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/alter_sequence_options.html %}
      - -## Parameters - - - - Parameter | Description ------------|------------ -`IF EXISTS` | Modify the sequence only if it exists; if it does not exist, do not return an error. -`sequence_name` | The name of the sequence you want to modify. -`INCREMENT` | The new value by which the sequence is incremented. A negative number creates a descending sequence. A positive number creates an ascending sequence. -`MINVALUE` | The new minimum value of the sequence.

      Default: `1` -`MAXVALUE` | The new maximum value of the sequence.

      Default: `9223372036854775807` -`START` | The value the sequence starts at if you `RESTART` or if the sequence hits the `MAXVALUE` and `CYCLE` is set.

      `RESTART` and `CYCLE` are not implemented yet. -`CYCLE` | The sequence will wrap around when the sequence value hits the maximum or minimum value. If `NO CYCLE` is set, the sequence will not wrap. - -## Examples - -### Change the increment value of a sequence - -In this example, we're going to change the increment value of a sequence from its current state (i.e., `1`) to `2`. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER SEQUENCE customer_seq INCREMENT 2; -~~~ - -Next, we'll add another record to the table and check that the new record adheres to the new sequence. - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO customer_list (customer, address) VALUES ('Marie', '333 Ocean Ave'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customer_list; -~~~ -~~~ -+----+----------+--------------------+ -| id | customer | address | -+----+----------+--------------------+ -| 1 | Lauren | 123 Main Street | -| 2 | Jesse | 456 Broad Ave | -| 3 | Amruta | 9876 Green Parkway | -| 5 | Marie | 333 Ocean Ave | -+----+----------+--------------------+ -~~~ - -### Set the next value of a sequence - -In this example, we're going to change the next value of the example sequence (`customer_seq`). Currently, the next value will be `7` (i.e., `5` + `INCREMENT 2`). We will change the next value to `20`. - -{{site.data.alerts.callout_info}}You cannot set a value outside the MAXVALUE or MINVALUE of the sequence. {{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> SELECT setval('customer_seq', 20, false); -~~~ -~~~ -+--------+ -| setval | -+--------+ -| 20 | -+--------+ -~~~ - -{{site.data.alerts.callout_info}} -The `setval('seq_name', value, is_called)` function in CockroachDB SQL mimics the `setval()` function in PostgreSQL, but it does not store the `is_called` flag. Instead, it sets the value to `val - increment` for `false` or `val` for `true`. -{{site.data.alerts.end}} - -Let's add another record to the table to check that the new record adheres to the new next value. - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO customer_list (customer, address) VALUES ('Lola', '333 Schermerhorn'); -~~~ -~~~ -+----+----------+--------------------+ -| id | customer | address | -+----+----------+--------------------+ -| 1 | Lauren | 123 Main Street | -| 2 | Jesse | 456 Broad Ave | -| 3 | Amruta | 9876 Green Parkway | -| 5 | Marie | 333 Ocean Ave | -| 20 | Lola | 333 Schermerhorn | -+----+----------+--------------------+ -~~~ - -## See also - -- [`RENAME SEQUENCE`](rename-sequence.html) -- [`CREATE SEQUENCE`](create-sequence.html) -- [`DROP SEQUENCE`](drop-sequence.html) -- [`SHOW SEQUENCES`](show-sequences.html) -- [Functions and Operators](functions-and-operators.html) -- [Other SQL Statements](sql-statements.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/alter-table.md b/src/current/v19.2/alter-table.md deleted file mode 100644 index 50156eea34b..00000000000 --- a/src/current/v19.2/alter-table.md +++ /dev/null @@ -1,36 +0,0 @@ ---- -title: ALTER TABLE -summary: Use the ALTER TABLE statement to change the schema of a table. -toc: true ---- - -The `ALTER TABLE` [statement](sql-statements.html) applies a schema change to a table. For information on using `ALTER TABLE`, see the pages for its relevant [subcommands](#subcommands). - -{% include {{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Subcommands - -{{site.data.alerts.callout_success}} -Some subcommands can be used in combination in a single `ALTER TABLE` statement. For example, you can [atomically rename a column and add a new column with the old name of the existing column](rename-column.html#add-and-rename-columns-atomically). -{{site.data.alerts.end}} - -Subcommand | Description | Can combine with other subcommands? ------------|-------------|------------------------------------ -[`ADD COLUMN`](add-column.html) | Add columns to tables. | Yes -[`ADD CONSTRAINT`](add-constraint.html) | Add constraints to columns. | Yes -[`ALTER COLUMN`](alter-column.html) | Change or drop a column's [`DEFAULT` constraint](default-value.html) or [`NOT NULL` constraint](not-null.html). | Yes -[`CONFIGURE ZONE`](configure-zone.html) | [Configure replication zones](configure-replication-zones.html) for a table. | No -[`DROP COLUMN`](drop-column.html) | Remove columns from tables. | Yes -[`DROP CONSTRAINT`](drop-constraint.html) | Remove constraints from columns. | Yes -[`EXPERIMENTAL_AUDIT`](experimental-audit.html) | Enable per-table audit logs, for security purposes. | Yes -[`PARTITION BY`](partition-by.html) | Partition, re-partition, or un-partition a table ([Enterprise-only](enterprise-licensing.html)). | Yes -[`RENAME COLUMN`](rename-column.html) | Change the names of columns. | Yes -[`RENAME CONSTRAINT`](rename-constraint.html) | Change constraints columns. | Yes -[`RENAME TO`](rename-table.html) | Change the names of tables. | No -[`SPLIT AT`](split-at.html) | Force a range split at the specified row in the table. | No -[`UNSPLIT AT`](unsplit-at.html) | New in v19.2: Remove a range split enforcement at a specified row in the table. | No -[`VALIDATE CONSTRAINT`](validate-constraint.html) | Check whether values in a column match a [constraint](constraints.html) on the column. | Yes - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} diff --git a/src/current/v19.2/alter-user.md b/src/current/v19.2/alter-user.md deleted file mode 100644 index 7bc7b232dc8..00000000000 --- a/src/current/v19.2/alter-user.md +++ /dev/null @@ -1,78 +0,0 @@ ---- -title: ALTER USER -summary: The ALTER USER statement can be used to add or change a user's password. -toc: true ---- - -The `ALTER USER` [statement](sql-statements.html) can be used to add or change a [user's](create-user.html) password. - -## Considerations - -- Password creation and alteration is supported only in secure clusters for non-`root` users. - -## Required privileges - -The user must have the `INSERT` and `UPDATE` [privileges](authorization.html#assign-privileges) on the `system.users` table. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/alter_user_password.html %}
      - -## Parameters - - - -Parameter | Description -----------|------------- -`name` | The name of the user whose password you want to create or add. -`password` | Let the user [authenticate their access to a secure cluster](authentication.html#client-authentication) using this new password. Passwords should be entered as [string literal](sql-constants.html#string-literals). For compatibility with PostgreSQL, a password can also be entered as an [identifier](#change-password-using-an-identifier), although this is discouraged. - -## Examples - -### Change password using a string literal - -{% include copy-clipboard.html %} -~~~ sql -> ALTER USER carl WITH PASSWORD 'ilov3beefjerky'; -~~~ -~~~ -ALTER USER 1 -~~~ - -### Change password using an identifier - -The following statement changes the password to `ilov3beefjerky`, as above: - -{% include copy-clipboard.html %} -~~~ sql -> ALTER USER carl WITH PASSWORD ilov3beefjerky; -~~~ - -This is equivalent to the example in the previous section because the password contains only lowercase characters. - -In contrast, the following statement changes the password to `thereisnotomorrow`, even though the password in the syntax contains capitals, because identifiers are normalized automatically: - -{% include copy-clipboard.html %} -~~~ sql -> ALTER USER carl WITH PASSWORD ThereIsNoTomorrow; -~~~ - -To preserve case in a password specified using identifier syntax, use double quotes: - -{% include copy-clipboard.html %} -~~~ sql -> ALTER USER carl WITH PASSWORD "ThereIsNoTomorrow"; -~~~ - -## See also - -- [`DROP USER`](drop-user.html) -- [`SHOW USERS`](show-users.html) -- [`GRANT `](grant.html) -- [`SHOW GRANTS`](show-grants.html) -- [Create Security Certificates](cockroach-cert.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/alter-view.md b/src/current/v19.2/alter-view.md deleted file mode 100644 index 48fee782202..00000000000 --- a/src/current/v19.2/alter-view.md +++ /dev/null @@ -1,81 +0,0 @@ ---- -title: ALTER VIEW -summary: The ALTER VIEW statement changes the name of a view. -toc: true ---- - -The `ALTER VIEW` [statement](sql-statements.html) changes the name of a [view](views.html). - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -{{site.data.alerts.callout_info}} -It is not currently possible to change the `SELECT` statement executed by a view. Instead, you must drop the existing view and create a new view. Also, it is not currently possible to rename a view that other views depend on, but this ability may be added in the future (see [this issue](https://github.com/cockroachdb/cockroach/issues/10083)). -{{site.data.alerts.end}} - -## Required privileges - -The user must have the `DROP` [privilege](authorization.html#assign-privileges) on the view and the `CREATE` privilege on the parent database. - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/alter_view.html %} -
      - -## Parameters - -Parameter | Description -----------|------------ -`IF EXISTS` | Rename the view only if a view of `view_name` exists; if one does not exist, do not return an error. -`view_name` | The name of the view to rename. To find view names, use:

      `SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';` -`name` | The new [`name`](sql-grammar.html#name) for the view, which must be unique to its database and follow these [identifier rules](keywords-and-identifiers.html#identifiers). - -## Example - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.tables WHERE table_type = 'VIEW'; -~~~ - -~~~ -+---------------+-------------------+--------------------+------------+---------+ -| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | VERSION | -+---------------+-------------------+--------------------+------------+---------+ -| def | bank | user_accounts | VIEW | 2 | -| def | bank | user_emails | VIEW | 1 | -+---------------+-------------------+--------------------+------------+---------+ -(2 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> ALTER VIEW bank.user_emails RENAME TO bank.user_email_addresses; -~~~ - -{% include copy-clipboard.html %} -~~~ -> RENAME VIEW -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.tables WHERE table_type = 'VIEW'; -~~~ - -~~~ -+---------------+-------------------+----------------------+------------+---------+ -| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | VERSION | -+---------------+-------------------+----------------------+------------+---------+ -| def | bank | user_accounts | VIEW | 2 | -| def | bank | user_email_addresses | VIEW | 3 | -+---------------+-------------------+----------------------+------------+---------+ -(2 rows) -~~~ - -## See also - -- [Views](views.html) -- [`CREATE VIEW`](create-view.html) -- [`SHOW CREATE`](show-create.html) -- [`DROP VIEW`](drop-view.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/architecture/distribution-layer.md b/src/current/v19.2/architecture/distribution-layer.md deleted file mode 100644 index c51bb63c13c..00000000000 --- a/src/current/v19.2/architecture/distribution-layer.md +++ /dev/null @@ -1,203 +0,0 @@ ---- -title: Distribution Layer -summary: The distribution layer of CockroachDB's architecture provides a unified view of your cluster's data. -toc: true ---- - -The distribution layer of CockroachDB's architecture provides a unified view of your cluster's data. - -{{site.data.alerts.callout_info}} -If you haven't already, we recommend reading the [Architecture Overview](overview.html). -{{site.data.alerts.end}} - -## Overview - -To make all data in your cluster accessible from any node, CockroachDB stores data in a monolithic sorted map of key-value pairs. This key-space describes all of the data in your cluster, as well as its location, and is divided into what we call "ranges", contiguous chunks of the key-space, so that every key can always be found in a single range. - -CockroachDB implements a sorted map to enable: - - - **Simple lookups**: Because we identify which nodes are responsible for certain portions of the data, queries are able to quickly locate where to find the data they want. - - **Efficient scans**: By defining the order of data, it's easy to find data within a particular range during a scan. - -### Monolithic sorted map structure - -The monolithic sorted map is comprised of two fundamental elements: - -- System data, which include **meta ranges** that describe the locations of data in your cluster (among many other cluster-wide and local data elements) -- User data, which store your cluster's **table data** - -#### Meta ranges - -The locations of all ranges in your cluster are stored in a two-level index at the beginning of your key-space, known as meta ranges, where the first level (`meta1`) addresses the second, and the second (`meta2`) addresses data in the cluster. - -This two-level index plus user data can be visualized as a tree, with the root at `meta1`, the second level at `meta2`, and the leaves of the tree made up of the ranges that hold user data. - -![range-lookup.png](../../images/{{page.version.version}}/range-lookup.png "Meta ranges plus user data tree diagram") - -Importantly, every node has information on where to locate the `meta1` range (known as its range descriptor, detailed below), and the range is never split. - -This meta range structure lets us address up to 4EiB of user data by default: we can address 2^(18 + 18) = 2^36 ranges; each range addresses 2^26 B, and altogether we address 2^(36+26) B = 2^62 B = 4EiB. However, with larger range sizes, it's possible to expand this capacity even further. - -Meta ranges are treated mostly like normal ranges and are accessed and replicated just like other elements of your cluster's KV data. - -Each node caches values of the `meta2` range it has accessed before, which optimizes access of that data in the future. Whenever a node discovers that its `meta2` cache is invalid for a specific key, the cache is updated by performing a regular read on the `meta2` range. - -#### Table data - -After the node's meta ranges is the KV data your cluster stores. - -Each table and its secondary indexes initially map to a single range, where each key-value pair in the range represents a single row in the table (also called the primary index because the table is sorted by the primary key) or a single row in a secondary index. As soon as a range reaches 64 MiB in size, it splits into two ranges. This process continues as a table and its indexes continue growing. Once a table is split across multiple ranges, it's likely that the table and secondary indexes will be stored in separate ranges. However, a range can still contain data for both the table and a secondary index. - -The default 64MiB range size represents a sweet spot for us between a size that's small enough to move quickly between nodes, but large enough to store a meaningfully contiguous set of data whose keys are more likely to be accessed together. These ranges are then shuffled around your cluster to ensure survivability. - -These table ranges are replicated (in the aptly named replication layer), and have the addresses of each replica stored in the `meta2` range. - -### Using the monolithic sorted map - -As described in the [meta ranges section](#meta-ranges), the locations of all the ranges in a cluster are stored in a two-level index: - -- The first level (`meta1`) addresses the second level. -- The second level (`meta2`) addresses user data. - -This can also be visualized as a tree, with the root at `meta1`, the second level at `meta2`, and the leaves of the tree made up of the ranges that hold user data. - -When a node receives a request, it looks up the location of the range(s) that include the keys in the request in a bottom-up fashion, starting with the leaves of this tree. This process works as follows: - -1. For each key, the node looks up the location of the range containing the specified key in the second level of range metadata (`meta2`). That information is cached for performance; if the range's location is found in the cache, it is returned immediately. - -2. If the range's location is not found in the cache, the node looks up the location of the range where the actual value of `meta2` resides. This information is also cached; if the location of the `meta2` range is found in the cache, the node sends an RPC to the `meta2` range to get the location of the keys the request wants to operate on, and returns that information. - -3. Finally, if the location of the `meta2` range is not found in the cache, the node looks up the location of the range where the actual value of the first level of range metadata (`meta1`) resides. This lookup always succeeds because the location of `meta1` is distributed among all the nodes in the cluster using a gossip protocol. The node then uses the information from `meta1` to look up the location of `meta2`, and from `meta2` it looks up the locations of the ranges that include the keys in the request. - -Note that the process described above is recursive; every time a lookup is performed, it either (1) gets a location from the cache, or (2) performs another lookup on the value in the next level "up" in the tree. Because the range metadata is cached, a lookup can usually be performed without having to send an RPC to another node. - -Now that the node has the location of the range where the key from the request resides, it sends the KV operations from the request along to the range (using the [`DistSender`](#distsender) machinery) in a [`BatchRequest`](#batchrequest). - -### Interactions with other layers - -In relationship to other layers in CockroachDB, the distribution layer: - -- Receives requests from the transaction layer on the same node. -- Identifies which nodes should receive the request, and then sends the request to the proper node's replication layer. - -## Technical details and components - -### gRPC - -gRPC is the software nodes use to communicate with one another. Because the distribution layer is the first layer to communicate with other nodes, CockroachDB implements gRPC here. - -gRPC requires inputs and outputs to be formatted as protocol buffers (protobufs). To leverage gRPC, CockroachDB implements a protocol-buffer-based API defined in `api.proto`. - -For more information about gRPC, see the [official gRPC documentation](http://www.grpc.io/docs/guides/). - -### BatchRequest - -All KV operation requests are bundled into a [protobuf](https://en.wikipedia.org/wiki/Protocol_Buffers), known as a `BatchRequest`. The destination of this batch is identified in the `BatchRequest` header, as well as a pointer to the request's transaction record. (On the other side, when a node is replying to a `BatchRequest`, it uses a protobuf––`BatchResponse`.) - -This `BatchRequest` is also what's used to send requests between nodes using gRPC, which accepts and sends protocol buffers. - -### DistSender - -The gateway/coordinating node's `DistSender` receives `BatchRequest`s from its own `TxnCoordSender`. `DistSender` is then responsible for breaking up `BatchRequests` and routing a new set of `BatchRequests` to the nodes it identifies contain the data using the system [meta ranges](#meta-ranges). For a description of the process by which this lookup from a key to the node holding the key's range is performed, see [Using the monolithic sorted map](#using-the-monolithic-sorted-map). - -It sends the `BatchRequest`s to the replicas of a range, ordered in expectation of request latency. The leaseholder is tried first, if the request needs it. Requests received by a non-leaseholder may fail with an error pointing at the replica's last known leaseholder. These requests are retried transparently with the updated lease by the gateway node and never reach the client. - -As nodes begin replying to these commands, `DistSender` also aggregates the results in preparation for returning them to the client. - -### Meta range KV structure - -Like all other data in your cluster, meta ranges are structured as KV pairs. Both meta ranges have a similar structure: - -~~~ -metaX/successorKey -> [list of nodes containing data] -~~~ - -Element | Description ---------|------------------------ -`metaX` | The level of meta range. Here we use a simplified `meta1` or `meta2`, but these are actually represented in `cockroach` as `\x02` and `\x03` respectively. -`successorKey` | The first key *greater* than the key you're scanning for. This makes CockroachDB's scans efficient; it simply scans the keys until it finds a value greater than the key it's looking for, and that is where it finds the relevant data.

      The `successorKey` for the end of a keyspace is identified as `maxKey`. - -Here's an example: - -~~~ -meta2/M -> node1:26257, node2:26257, node3:26257 -~~~ - -In this case, the replica on `node1` is the leaseholder, and nodes 2 and 3 also contain replicas. - -#### Example - -Let's imagine we have an alphabetically sorted column, which we use for lookups. Here are what the meta ranges would approximately look like: - -1. `meta1` contains the address for the nodes containing the `meta2` replicas. - - ~~~ - # Points to meta2 range for keys [A-M) - meta1/M -> node1:26257, node2:26257, node3:26257 - - # Points to meta2 range for keys [M-Z] - meta1/maxKey -> node4:26257, node5:26257, node6:26257 - ~~~ - -2. `meta2` contains addresses for the nodes containing the replicas of each range in the cluster: - - ~~~ - # Contains [A-G) - meta2/G -> node1:26257, node2:26257, node3:26257 - - # Contains [G-M) - meta2/M -> node1:26257, node2:26257, node3:26257 - - #Contains [M-Z) - meta2/Z -> node4:26257, node5:26257, node6:26257 - - #Contains [Z-maxKey) - meta2/maxKey-> node4:26257, node5:26257, node6:26257 - ~~~ - -### Table data KV structure - -Key-value data, which represents the data in your tables using the following structure: - -~~~ -/// -> -~~~ - -The table itself is stored with an `index_id` of 1 for its `PRIMARY KEY` columns, with the rest of the columns in the table considered as stored/covered columns. - -### Range descriptors - -Each range in CockroachDB contains metadata, known as a range descriptor. A range descriptor is comprised of the following: - -- A sequential RangeID -- The keyspace (i.e., the set of keys) the range contains; for example, the first and last `` in the table data KV structure above. This determines the `meta2` range's keys. -- The addresses of nodes containing replicas of the range. This determines the `meta2` range's key's values. - -Because range descriptors comprise the key-value data of the `meta2` range, each node's `meta2` cache also stores range descriptors. - -Range descriptors are updated whenever there are: - -- Membership changes to a range's Raft group (discussed in more detail in the [Replication Layer](replication-layer.html#membership-changes-rebalance-repair)) -- Range splits - -All of these updates to the range descriptor occur locally on the range, and then propagate to the `meta2` range. - -### Range splits - -By default, CockroachDB attempts to keep ranges/replicas at 64MiB. Once a range reaches that limit we split it into two 32 MiB ranges (composed of contiguous key spaces). - -During this range split, the node creates a new Raft group containing all of the same members as the range that was split. The fact that there are now two ranges also means that there is a transaction that updates `meta2` with the new keyspace boundaries, as well as the addresses of the nodes using the range descriptor. - -## Technical interactions with other layers - -### Distribution and transaction layer - -The Distribution layer's `DistSender` receives `BatchRequests` from its own node's `TxnCoordSender`, housed in the Transaction layer. - -### Distribution and replication layer - -The Distribution layer routes `BatchRequests` to nodes containing ranges of data, which is ultimately routed to the Raft group leader or leaseholder, which are handled in the replication layer. - -## What's next? - -Learn how CockroachDB copies data and ensures consistency in the [replication layer](replication-layer.html). diff --git a/src/current/v19.2/architecture/life-of-a-distributed-transaction.md b/src/current/v19.2/architecture/life-of-a-distributed-transaction.md deleted file mode 100644 index b5974471fbc..00000000000 --- a/src/current/v19.2/architecture/life-of-a-distributed-transaction.md +++ /dev/null @@ -1,188 +0,0 @@ ---- -title: Life of a Distributed Transaction -summary: Learn how a query moves through the layers of CockroachDB's architecture. -toc: true ---- - -Because CockroachDB is a distributed transactional database, the path queries take is dramatically different from many other database architectures. To help familiarize you with CockroachDB's internals, this guide covers what that path actually is. - -If you've already read the [CockroachDB architecture documentation](overview.html), this guide serves as another way to conceptualize how the database works. This time, instead of focusing on the layers of CockroachDB's architecture, we're going to focus on the linear path that a query takes through the system (and then back out again). - -To get the most out of this guide, we recommend beginning with the architecture documentation's [overview](overview.html) and progressing through all of the following sections. This guide provides brief descriptions of each component's function and links to other documentation where appropriate, but assumes the reader has a basic understanding of the architecture in the first place. - -## Overview - -This guide is organized by the physical actors in the system, and then broken down into the components of each actor in the sequence in which they're involved. - -Here's a brief overview of the physical actors, in the sequence with which they're involved in executing a query: - -1. [**SQL Client**](#sql-client-postgres-wire-protocol) sends a query to your cluster. -1. [**Load Balancing**](#load-balancing-routing) routes the request to CockroachDB nodes in your cluster, which will act as a gateway. -1. [**Gateway**](#gateway) is a CockroachDB node that processes the SQL request and responds to the client. -1. [**Leaseholder**](#leaseholder-node) is a CockroachDB node responsible for serving reads and coordinating writes of a specific range of keys in your query. -1. [**Raft leader**](#raft-leader) is a CockroachDB node responsible for maintaining consensus among your CockroachDB replicas. - -Once the transaction completes, queries traverse these actors in approximately reverse order. We say "approximately" because there might be many leaseholders and Raft leaders involved in a single query, and there is little-to-no interaction with the load balancer during the response. - -## SQL Client/Postgres Wire Protocol - -To begin, a SQL client (e.g., an app) performs some kind of business logic against your CockroachDB cluster, such as inserting a new customer record. - -This request is sent over a connection to your CockroachDB cluster that's established using a PostgreSQL driver. - -## Load Balancing & Routing - -Modern architectures require distributing your cluster across machines to improve throughput, latency, and uptime. This means queries are routed through load balancers, which choose the best CockroachDB node to connect to. Because all CockroachDB nodes have perfectly symmetrical access to data, this means your load balancer can connect your client to any node in the cluster and access any data while still guaranteeing strong consistency. - -Your architecture might also have additional layers of routing to enforce regulatory compliance, such as ensuring GDPR compliance. - -Once your router and load balancer determine the best node to connect to, your client's connection is established to the gateway node. - -## Gateway - -The gateway node handles the connection with the client, both receiving and responding to the request. - -### SQL parsing & planning - -The gateway node first [parses](sql-layer.html#sql-parser-planner-executor) the client's SQL statement to ensure it's valid according to the CockroachDB dialect of SQL, and uses that information to [generate a logical SQL plan](sql-layer.html#logical-planning). - -Given that CockroachDB is a distributed database, though, it's also important to take a cluster's topology into account, so the logical plan is then converted into a physical plan—this means sometimes pushing operations onto the physical machines that contain the data. - -### SQL executor - -While CockroachDB presents a SQL interface to clients, the actual database is built on top of a key-value store. To mediate this, the physical plan generated at the end of SQL parsing is passed to the SQL executor, which executes the plan by performing key-value operations through the `TxnCoordSender`. For example, the SQL executor converts `INSERT `statements into `Put()` operations. - -### TxnCoordSender - -The `TxnCoordSender` provides an API to perform key-value operations on your database. - -On its back end, the `TxnCoordSender` performs a large amount of the accounting and tracking for a transaction, including: - -- Accounts for all keys involved in a transaction. This is used, among other ways, to manage the transaction's state. -- Packages all key-value operations into a `BatchRequest`, which are forwarded on to the node's `DistSender`. - -### DistSender - -The gateway node's `DistSender` receives `BatchRequests` from the `TxnCoordSender`. It dismantles the initial `BatchRequest` by taking each operation and finding which physical machine should receive the request for the range—known as the range's leaseholder. The address of the range's current leaseholder is readily available in both local caches, as well as in the [cluster's `meta` ranges](distribution-layer.html#meta-range-kv-structure). - -These dismantled `BatchRequests` are reassembled into new `BatchRequests` containing the address of the range's leaseholder. - -All write operations also propagate the leaseholder's address back to the `TxnCoordSender`, so it can track and clean up write operations as necessary. - -The `DistSender` sends out the first `BatchRequest` for each range in parallel. As soon as it receives a provisional acknowledgment from the leaseholder node’s evaluator (details below), it sends out the next `BatchRequest` for that range. - -The `DistSender` then waits to receive acknowledgments for all of its write operations, as well as values for all of its read operations. However, this wait isn't necessarily blocking, and the `DistSender` may still perform operations with ongoing transactions. - -## Leaseholder node - -The gateway node's `DistSender` tries to send its `BatchRequests` to the replica identified as the range's [leaseholder](replication-layer.html#leases), which is a single replica that serves all reads for a range, as well as coordinates all writes. Leaseholders play a crucial role in CockroachDB's architecture, so it's a good topic to make sure you're familiar with. - -### Request response - -Because the leaseholder replica can shift between nodes, all nodes must be able to return a request for any key, returning a response indicating one of these scenarios: - -##### No Longer Leaseholder - -If a node is no longer the leaseholder, but still contains a replica of the range, it denies the request but includes the last known address for the leaseholder of that range. - -Upon receipt of this response, the `DistSender` will update the header of the `BatchRequest` with the new address, and then resend the `BatchRequest` to the newly identified leaseholder. - -##### No Longer Has/Never Had Range - -If a node doesn't have a replica for the requested range, it denies the request without providing any further information. - -In this case, the `DistSender` must look up the current leaseholder using the [cluster's `meta` ranges](distribution-layer.html#meta-range-kv-structure). - -##### Success - -Once the node that contains the leaseholder of the range receives the `BatchRequest`, it begins processing it, and progresses onto checking the timestamp cache. - -### Timestamp cache - -The timestamp cache tracks the highest timestamp (i.e., most recent) for any read operation that a given range has served. - -Each write operation in a `BatchRequest` checks its own timestamp versus the timestamp cache to ensure that the write operation has a higher timestamp; this guarantees that history is never rewritten and you can trust that reads always served the most recent data. It's one of the crucial mechanisms CockroachDB uses to ensure serializability. If a write operation fails this check, it must be restarted at a timestamp higher than the timestamp cache's value. - -### Latch manager - -Operations in the `BatchRequest` are serialized through the leaseholder's latch manager. - -This works by giving each write operation a latch on a row. Any reads or writes that come in after the latch has been granted on the row must wait for the write to complete, at which point the latch is released and the subsequent operations can continue. - -### Batch Evaluation - -The batch evaluator ensures that write operations are valid. Our architecture makes this fairly trivial. First, the evaluator can simply check the leaseholder's data to ensure the write is valid; because it has coordinated all writes to the range, it must have the most up-to-date versions of the range's data. Secondly, because of the latch manager, each write operation is guaranteed to uncontested access to the range (i.e., there is no contention with other write operations). - -If the write operation is valid according to the evaluator, the leaseholder sends a provisional acknowledgment to the gateway node's `DistSender`; this lets the `DistSender` begin to send its subsequent `BatchRequests` for this range. - -Importantly, this feature is entirely built for transactional optimization (known as [transaction pipelining](transaction-layer.html#transaction-pipelining)). There are no issues if an operation passes the evaluator but doesn't end up committing. - -### Reads from RocksDB - -All operations (including writes) begin by reading from the local instance of RocksDB to check for write intents for the operation's key. We talk much more about [write intents in the transaction layer of the CockroachDB architecture](transaction-layer.html#write-intents), which is worth reading, but a simplified explanation is that these are provisional, uncommitted writes that express that some other concurrent transaction plans to write a value to the key. - -What we detail below is a simplified version of the CockroachDB transaction model. For more detail, check out [the transaction architecture documentation](transaction-layer.html). - -#### Resolving Write Intents - -If an operation encounters a write intent for a key, it attempts to "resolve" the write intent by checking the state of the write intent's transaction. If the write intent's transaction record is... - -- `COMMITTED`, this operation converts the write intent to a regular key-value pair, and then proceeds as if it had read that value instead of a write intent. -- `ABORTED`, this operation discards the write intent and reads the next-most-recent value from RocksDB. -- `PENDING`, the new transaction attempts to "push" the write intent's transaction by moving that transaction's timestamp forward (i.e., ahead of this transaction's timestamp); however, this only succeeds if the write intent's transaction has become inactive. - - If the push succeeds, the operation continues. - - If this push fails (which is the majority of the time), this transaction goes into the [`TxnWaitQueue`](transaction-layer.html#txnwaitqueue) on this node. The incoming transaction can only continue once the blocking transaction completes (i.e., commits or aborts). -- `MISSING`, the resolver consults the write intent's timestamp. - - If it was created within the transaction liveness threshold, it treats the transaction record as exhibiting the `PENDING` behavior, with the addition of tracking the push in the range's timestamp cache, which will inform the transaction that its timestamp was pushed once the transaction record gets created. - - If the write intent is older than the transaction liveness threshold, the resolution exhibits the `ABORTED` behavior. - - Note that transaction records might be missing because we've avoided writing the record until the transaction commits. For more information, see [Transaction Layer: Transaction records](transaction-layer.html#transaction-records). - -Check out our architecture documentation for more information about [CockroachDB's transactional model](transaction-layer.html). - -#### Read Operations - - -If the read doesn't encounter a write intent and the key-value operation is meant to serve a read, it can simply use the value it read from the leaseholder's instance of RocksDB. This works because the leaseholder had to be part of the Raft consensus group for any writes to complete, meaning it must have the most up-to-date version of the range's data. - -The leaseholder aggregates all read responses into a `BatchResponse` that will get returned to the gateway node's `DistSender`. - -As we mentioned before, each read operation also updates the timestamp cache. - -### Write Operations - -After guaranteeing that there are no existing write intents for the keys, `BatchRequest`'s key-value operations are converted to [Raft operations](replication-layer.html#raft) and have their values converted into write intents. - -The leaseholder then proposes these Raft operations to the Raft group leader. The leaseholder and the Raft leader are almost always the same node, but there are situations where the roles might drift to different nodes. However, when the two roles are not collocated on the same physical machine, CockroachDB will attempt to relocate them on the same node at the next opportunity. - -## Raft Leader - -CockroachDB leverages Raft as its consensus protocol. If you aren't familiar with it, we recommend checking out the details about [how CockroachDB leverages Raft](replication-layer.html#raft), as well as [learning more about how the protocol works at large](http://thesecretlivesofdata.com/raft/). - -In terms of executing transactions, the Raft leader receives proposed Raft commands from the leaseholder. Each Raft command is a write that is used to represent an atomic state change of the underlying key-value pairs stored in RocksDB. - -### Consensus - -For each command the Raft leader receives, it proposes a vote to the other members of the Raft group. - -Once the command achieves consensus (i.e., a majority of nodes including itself acknowledge the Raft command), it is committed to the Raft leader’s Raft log and written to RocksDB. At the same time, the Raft leader also sends a command to all other nodes to include the command in their Raft logs. - -Once the leader commits the Raft log entry, it’s considered committed. At this point the value is considered written, and if another operation comes in and performs a read on RocksDB for this key, they’ll encounter this value. - -Note that this write operation creates a write intent; these writes will not be fully committed until the gateway node sets the transaction record's status to `COMMITTED`. - -## On the way back up - -Now that we have followed an operation all the way down from the SQL client to RocksDB, we can pretty quickly cover what happens on the way back up (i.e., when generating a response to the client). - -1. Once the leaseholder applies a write to its Raft log, - it sends a commit acknowledgment to the gateway node's `DistSender`, which was waiting for this signal (having already received the provisional acknowledgment from the leaseholder's evaluator). -1. The gateway node's `DistSender` aggregates commit acknowledgments from all of the write operations in the `BatchRequest`, as well as any values from read operations that should be returned to the client. -1. Once all operations have successfully completed (i.e., reads have returned values and write intents have been committed), the `DistSender` tries to record the transaction's success in the transaction record (which provides a durable mechanism of tracking the transaction's state), which can cause a few situations to arise: - - It checks the timestamp cache of the range where the first write occurred to see if its timestamp got pushed forward. If it did, the transaction performs a [read refresh](transaction-layer.html#read-refreshing) to see if any values it needed have been changed. If the read refresh is successful, the transaction can commit at the pushed timestamp. If the read refresh fails, the transaction must be restarted. - - If the transaction is in an `ABORTED` state, the `DistSender` sends a response indicating as much, which ends up back at the SQL interface. - - Upon passing these checks the transaction record is either written for the first time with the `COMMITTED` state, or if it was in a `PENDING` state, it is moved to `COMMITTED`. Only at this point is the transaction considered committed. -1. The `DistSender` propagates any values that should be returned to the client (e.g., reads or the number of affected rows) to the `TxnCoordSender`, which in turn responds to the SQL interface with the value. - The `TxnCoordSender` also begins asynchronous intent cleanup by sending a request to the `DistSender` to convert all write intents it created for the transaction to fully committed values. However, this process is largely an optimization; if any operation encounters a write intent, it checks the write intent's transaction record. If the transaction record is `COMMITTED`, the operation can perform the same cleanup and convert the write intent to a fully committed value. -1. The SQL interface then responds to the client, and is now prepared to continue accepting new connections. diff --git a/src/current/v19.2/architecture/overview.md b/src/current/v19.2/architecture/overview.md deleted file mode 100644 index 8f3de93db1e..00000000000 --- a/src/current/v19.2/architecture/overview.md +++ /dev/null @@ -1,87 +0,0 @@ ---- -title: Architecture Overview -summary: Learn about the inner-workings of the CockroachDB architecture. -toc: true -key: cockroachdb-architecture.html ---- - -CockroachDB was designed to create the source-available database our developers would want to use: one that is both scalable and consistent. Developers often have questions about how we've achieved this, and this guide sets out to detail the inner-workings of the `cockroach` process as a means of explanation. - -However, you definitely do not need to understand the underlying architecture to use CockroachDB. These pages give serious users and database enthusiasts a high-level framework to explain what's happening under the hood. - -## Using this guide - -This guide is broken out into pages detailing each layer of CockroachDB. It's recommended to read through the layers sequentially, starting with this overview and then proceeding to the SQL layer. - -If you're looking for a high-level understanding of CockroachDB, you can simply read the **Overview** section of each layer. For more technical detail––for example, if you're interested in [contributing to the project](https://wiki.crdb.io/wiki/spaces/CRDB/pages/73204033/Contributing+to+CockroachDB)––you should read the **Components** sections as well. - -{{site.data.alerts.callout_info}}This guide details how CockroachDB is built, but does not explain how you should architect an application using CockroachDB. For help with your own application's architecture using CockroachDB, check out our user documentation.{{site.data.alerts.end}} - -## Goals of CockroachDB - -CockroachDB was designed in service of the following goals: - -- Make life easier for humans. This means being low-touch and highly automated for operators and simple to reason about for developers. -- Offer industry-leading consistency, even on massively scaled deployments. This means enabling distributed transactions, as well as removing the pain of eventual consistency issues and stale reads. -- Create an always-on database that accepts reads and writes on all nodes without generating conflicts. -- Allow flexible deployment in any environment, without tying you to any platform or vendor. -- Support familiar tools for working with relational data (i.e., SQL). - -With the confluence of these features, we hope that CockroachDB lets teams easily build global, scalable, resilient cloud services. - -## Glossary - -### Terms - -It's helpful to understand a few terms before reading our architecture documentation. - -{% include {{ page.version.version }}/misc/basic-terms.md %} - -### Concepts - -CockroachDB heavily relies on the following concepts, so being familiar with them will help you understand what our architecture achieves. - -Term | Definition ------|----------- -**Consistency** | CockroachDB uses "consistency" in both the sense of [ACID semantics](https://en.wikipedia.org/wiki/Consistency_(database_systems)) and the [CAP theorem](https://en.wikipedia.org/wiki/CAP_theorem), albeit less formally than either definition. What we try to express with this term is that your data should be anomaly-free. -**Consensus** | When a range receives a write, a quorum of nodes containing replicas of the range acknowledge the write. This means your data is safely stored and a majority of nodes agree on the database's current state, even if some of the nodes are offline.

      When a write *doesn't* achieve consensus, forward progress halts to maintain consistency within the cluster. -**Replication** | Replication involves creating and distributing copies of data, as well as ensuring copies remain consistent. However, there are multiple types of replication: namely, synchronous and asynchronous.

      Synchronous replication requires all writes to propagate to a quorum of copies of the data before being considered committed. To ensure consistency with your data, this is the kind of replication CockroachDB uses.

      Asynchronous replication only requires a single node to receive the write to be considered committed; it's propagated to each copy of the data after the fact. This is more or less equivalent to "eventual consistency", which was popularized by NoSQL databases. This method of replication is likely to cause anomalies and loss of data. -**Transactions** | A set of operations performed on your database that satisfy the requirements of [ACID semantics](https://en.wikipedia.org/wiki/Database_transaction). This is a crucial component for a consistent system to ensure developers can trust the data in their database. -**Multi-Active Availability** | Our consensus-based notion of high availability that lets each node in the cluster handle reads and writes for a subset of the stored data (on a per-range basis). This is in contrast to active-passive replication, in which the active node receives 100% of request traffic, as well as active-active replication, in which all nodes accept requests but typically cannot guarantee that reads are both up-to-date and fast. - -## Overview - -CockroachDB starts running on machines with two commands: - -- `cockroach start` with a `--join` flag for all of the initial nodes in the cluster, so the process knows all of the other machines it can communicate with -- `cockroach init` to perform a one-time initialization of the cluster - -Once the `cockroach` process is running, developers interact with CockroachDB through a SQL API, which we've modeled after PostgreSQL. Thanks to the symmetrical behavior of all nodes, you can send SQL requests to any of them; this makes CockroachDB really easy to integrate with load balancers. - -After receiving SQL RPCs, nodes convert them into operations that work with our distributed key-value store. As these RPCs start filling your cluster with data, CockroachDB algorithmically starts distributing your data among your nodes, breaking the data up into 64MiB chunks that we call ranges. Each range is replicated to at least 3 nodes to ensure survivability. This way, if nodes go down, you still have copies of the data which can be used for reads and writes, as well as replicating the data to other nodes. - -If a node receives a read or write request it cannot directly serve, it simply finds the node that can handle the request, and communicates with it. This way you do not need to know where your data lives, CockroachDB tracks it for you, and enables symmetric behavior for each node. - -Any changes made to the data in a range rely on a consensus algorithm to ensure a majority of its replicas agree to commit the change, ensuring industry-leading isolation guarantees and providing your application consistent reads, regardless of which node you communicate with. - -Ultimately, data is written to and read from disk using an efficient storage engine, which is able to keep track of the data's timestamp. This has the benefit of letting us support the SQL standard `AS OF SYSTEM TIME` clause, letting you find historical data for a period of time. - -However, while that high-level overview gives you a notion of what CockroachDB does, looking at how the `cockroach` process operates on each of these nodes will give you much greater understanding of our architecture. - -### Layers - -At the highest level, CockroachDB converts clients' SQL statements into key-value (KV) data, which is distributed among nodes and written to disk. Our architecture is the process by which we accomplish that, which is manifested as a number of layers that interact with those directly above and below it as relatively opaque services. - -The following pages describe the function each layer performs, but mostly ignore the details of other layers. This description is true to the experience of the layers themselves, which generally treat the other layers as black-box APIs. There are interactions that occur between layers which *are not* clearly articulated and require an understanding of each layer's function to understand the entire process. - -Layer | Order | Purpose -------|------------|-------- -[SQL](sql-layer.html) | 1 | Translate client SQL queries to KV operations. -[Transactional](transaction-layer.html) | 2 | Allow atomic changes to multiple KV entries. -[Distribution](distribution-layer.html) | 3 | Present replicated KV ranges as a single entity. -[Replication](replication-layer.html) | 4 | Consistently and synchronously replicate KV ranges across many nodes. This layer also enables consistent reads via leases. -[Storage](storage-layer.html) | 5 | Write and read KV data on disk. - -## What's next? - -Begin understanding our architecture by learning how CockroachDB works with applications in the [SQL layer](sql-layer.html). diff --git a/src/current/v19.2/architecture/reads-and-writes-overview.md b/src/current/v19.2/architecture/reads-and-writes-overview.md deleted file mode 100644 index 97e22720469..00000000000 --- a/src/current/v19.2/architecture/reads-and-writes-overview.md +++ /dev/null @@ -1,67 +0,0 @@ ---- -title: Reads and Writes in CockroachDB -summary: Learn how reads and writes are affected by the replicated and distributed nature of data in CockroachDB. -toc: true ---- - -This page explains how reads and writes are affected by the replicated and distributed nature of data in CockroachDB. It starts by summarizing some important [CockroachDB architectural concepts](overview.html) and then walks you through a few simple read and write scenarios. - -{{site.data.alerts.callout_info}} -For a more detailed trace of a query through the layers of CockroachDB's architecture, see [Life of a Distributed Transaction](life-of-a-distributed-transaction.html). -{{site.data.alerts.end}} - -## Important concepts - -{% include {{ page.version.version }}/misc/basic-terms.md %} - -As mentioned above, when a query is executed, the cluster routes the request to the leaseholder for the range containing the relevant data. If the query touches multiple ranges, the request goes to multiple leaseholders. For a read request, only the leaseholder of the relevant range retrieves the data. For a write request, the Raft consensus protocol dictates that a majority of the replicas of the relevant range must agree before the write is committed. - -Let's consider how these mechanics play out in some hypothetical queries. - -## Read scenario - -First, imagine a simple read scenario where: - -- There are 3 nodes in the cluster. -- There are 3 small tables, each fitting in a single range. -- Ranges are replicated 3 times (the default). -- A query is executed against node 2 to read from table 3. - -Perf tuning concepts - -In this case: - -1. Node 2 (the gateway node) receives the request to read from table 3. -2. The leaseholder for table 3 is on node 3, so the request is routed there. -3. Node 3 returns the data to node 2. -4. Node 2 responds to the client. - -If the query is received by the node that has the leaseholder for the relevant range, there are fewer network hops: - -Perf tuning concepts - -## Write scenario - -Now imagine a simple write scenario where a query is executed against node 3 to write to table 1: - -Perf tuning concepts - -In this case: - -1. Node 3 (the gateway node) receives the request to write to table 1. -2. The leaseholder for table 1 is on node 1, so the request is routed there. -3. The leaseholder is the same replica as the Raft leader (as is typical), so it simultaneously appends the write to its own Raft log and notifies its follower replicas on nodes 2 and 3. -4. As soon as one follower has appended the write to its Raft log (and thus a majority of replicas agree based on identical Raft logs), it notifies the leader and the write is committed to the key-values on the agreeing replicas. In this diagram, the follower on node 2 acknowledged the write, but it could just as well have been the follower on node 3. Also note that the follower not involved in the consensus agreement usually commits the write very soon after the others. -5. Node 1 returns acknowledgement of the commit to node 3. -6. Node 3 responds to the client. - -Just as in the read scenario, if the write request is received by the node that has the leaseholder and Raft leader for the relevant range, there are fewer network hops: - -Perf tuning concepts - -## Network and I/O bottlenecks - -With the above examples in mind, it's always important to consider network latency and disk I/O as potential performance bottlenecks. In summary: - -- For reads, hops between the gateway node and the leaseholder add latency. -- For writes, hops between the gateway node and the leaseholder/Raft leader, and hops between the leaseholder/Raft leader and Raft followers, add latency. In addition, since Raft log entries are persisted to disk before a write is committed, disk I/O is important. diff --git a/src/current/v19.2/architecture/replication-layer.md b/src/current/v19.2/architecture/replication-layer.md deleted file mode 100644 index 5bf02f973db..00000000000 --- a/src/current/v19.2/architecture/replication-layer.md +++ /dev/null @@ -1,154 +0,0 @@ ---- -title: Replication Layer -summary: The replication layer of CockroachDB's architecture copies data between nodes and ensures consistency between copies. -toc: true ---- - -The replication layer of CockroachDB's architecture copies data between nodes and ensures consistency between these copies by implementing our consensus algorithm. - -{{site.data.alerts.callout_info}} -If you haven't already, we recommend reading the [Architecture Overview](overview.html). -{{site.data.alerts.end}} - -## Overview - -High availability requires that your database can tolerate nodes going offline without interrupting service to your application. This means replicating data between nodes to ensure the data remains accessible. - -Ensuring consistency with nodes offline, though, is a challenge many databases fail. To solve this problem, CockroachDB uses a consensus algorithm to require that a quorum of replicas agrees on any changes to a range before those changes are committed. Because 3 is the smallest number that can achieve quorum (i.e., 2 out of 3), CockroachDB's high availability (known as multi-active availability) requires 3 nodes. - -The number of failures that can be tolerated is equal to *(Replication factor - 1)/2*. For example, with 3x replication, one failure can be tolerated; with 5x replication, two failures, and so on. You can control the replication factor at the cluster, database, and table level using [replication zones](../configure-replication-zones.html). - -When failures happen, though, CockroachDB automatically realizes nodes have stopped responding and works to redistribute your data to continue maximizing survivability. This process also works the other way around: when new nodes join your cluster, data automatically rebalances onto it, ensuring your load is evenly distributed. - -### Interactions with other layers - -In relationship to other layers in CockroachDB, the replication layer: - -- Receives requests from and sends responses to the distribution layer. -- Writes accepted requests to the storage layer. - -## Components - -### Raft - -Raft is a consensus protocol––an algorithm which makes sure that your data is safely stored on multiple machines, and that those machines agree on the current state even if some of them are temporarily disconnected. - -Raft organizes all nodes that contain a replica of a range into a group--unsurprisingly called a Raft group. Each replica in a Raft group is either a "leader" or a "follower". The leader, which is elected by Raft and long-lived, coordinates all writes to the Raft group. It heartbeats followers periodically and keeps their logs replicated. In the absence of heartbeats, followers become candidates after randomized election timeouts and proceed to hold new leader elections. - -Once a node receives a `BatchRequest` for a range it contains, it converts those KV operations into Raft commands. Those commands are proposed to the Raft group leader––which is what makes it ideal for the [leaseholder](#leases) and the Raft leader to be one in the same––and written to the Raft log. - -For a great overview of Raft, we recommend [The Secret Lives of Data](http://thesecretlivesofdata.com/raft/). - -#### Raft logs - -When writes receive a quorum, and are committed by the Raft group leader, they're appended to the Raft log. This provides an ordered set of commands that the replicas agreed on and is essentially the source of truth for consistent replication. - -Because this log is treated as serializable, it can be replayed to bring a node from a past state to its current state. This log also lets nodes that temporarily went offline to be "caught up" to the current state without needing to receive a copy of the existing data in the form of a snapshot. - -### Snapshots - -Each replica can be "snapshotted", which copies all of its data as of a specific timestamp (available because of [MVCC](storage-layer.html#mvcc)). This snapshot can be sent to other nodes during a rebalance event to expedite replication. - -After loading the snapshot, the node gets up to date by replaying all actions from the Raft group's log that have occurred since the snapshot was taken. - -### Leases - -A single node in the Raft group acts as the leaseholder, which is the only node that can serve reads or propose writes to the Raft group leader (both actions are received as `BatchRequests` from [`DistSender`](distribution-layer.html#distsender)). - -When serving reads, leaseholders bypass Raft; for the leaseholder's writes to have been committed in the first place, they must have already achieved consensus, so a second consensus on the same data is unnecessary. This has the benefit of not incurring networking round trips required by Raft and greatly increases the speed of reads (without sacrificing consistency). - -CockroachDB attempts to elect a leaseholder who is also the Raft group leader, which can also optimize the speed of writes. - -If there is no leaseholder, any node receiving a request will attempt to become the leaseholder for the range. To prevent two nodes from acquiring the lease, the requester includes a copy of the last valid lease it had; if another node became the leaseholder, its request is ignored. - -#### Co-location with Raft leadership - -The range lease is completely separate from Raft leadership, and so without further efforts, Raft leadership and the Range lease might not be held by the same replica. However, we can optimize query performance by making the same node both Raft leader and the leaseholder; it reduces network round trips if the leaseholder receiving the requests can simply propose the Raft commands to itself, rather than communicating them to another node. - -To achieve this, each lease renewal or transfer also attempts to collocate them. In practice, that means that the mismatch is rare and self-corrects quickly. - -#### Epoch-based leases (table data) - -To manage leases for table data, CockroachDB implements a notion of "epochs," which are defined as the period between a node joining a cluster and a node disconnecting from a cluster. To extend its leases, each node must periodically update its liveness record, which is stored on a system range key. When a node disconnects, it stops updating the liveness record, and the epoch is considered changed. This causes the node to immediately lose all of its leases. - -Because leases do not expire until a node disconnects from a cluster, leaseholders do not have to individually renew their own leases. Tying lease lifetimes to node liveness in this way lets us eliminate a substantial amount of traffic and Raft processing we would otherwise incur, while still tracking leases for every range. - -#### Expiration-based leases (meta and system ranges) - -A table's meta and system ranges (detailed in the [distribution layer](distribution-layer.html#meta-ranges)) are treated as normal key-value data, and therefore have leases just like table data. - -However, unlike table data, system ranges cannot use epoch-based leases because that would create a circular dependency: system ranges are already being used to implement epoch-based leases for table data. Therefore, system ranges use expiration-based leases instead. Expiration-based leases expire at a particular timestamp (typically after a few seconds). However, as long as a node continues proposing Raft commands, it continues to extend the expiration of its leases. If it doesn't, the next node containing a replica of the range that tries to read from or write to the range will become the leaseholder. - -#### Leaseholder rebalancing - -Because CockroachDB serves reads from a range's leaseholder, it benefits your cluster's performance if the replica closest to the primary geographic source of traffic holds the lease. However, as traffic to your cluster shifts throughout the course of the day, you might want to dynamically shift which nodes hold leases. - -{{site.data.alerts.callout_info}} - -This feature is also called [Follow-the-Workload](../demo-follow-the-workload.html) in our documentation. - -{{site.data.alerts.end}} - -Periodically (every 10 minutes by default in large clusters, but more frequently in small clusters), each leaseholder considers whether it should transfer the lease to another replica by considering the following inputs: - -- Number of requests from each locality -- Number of leases on each node -- Latency between localities - -##### Intra-locality - -If all the replicas are in the same locality, the decision is made entirely on the basis of the number of leases on each node that contains a replica, trying to achieve a roughly equitable distribution of leases across all of them. This means the distribution isn't perfectly equal; it intentionally tolerates small deviations between nodes to prevent thrashing (i.e., excessive adjustments trying to reach an equilibrium). - -##### Inter-locality - -If replicas are in different localities, CockroachDB attempts to calculate which replica would make the best leaseholder, i.e., provide the lowest latency. - -To enable dynamic leaseholder rebalancing, a range's current leaseholder tracks how many requests it receives from each locality as an exponentially weighted moving average. This calculation results in the locality that has recently requested the range most often being assigned the greatest weight. If another locality then begins requesting the range very frequently, this calculation would shift to assign the second region the greatest weight. - -When checking for leaseholder rebalancing opportunities, the leaseholder correlates each requesting locality's weight (i.e., the proportion of recent requests) to the locality of each replica by checking how similar the localities are. For example, if the leaseholder received requests from gateway nodes in locality `country=us,region=central`, CockroachDB would assign the following weights to replicas in the following localities: - -Replica locality | Replica rebalancing weight ------------------|------------------- -`country=us,region=central` | 100% because it is an exact match -`country=us,region=east` | 50% because only the first locality matches -`country=aus,region=central` | 0% because the first locality does not match - -The leaseholder then evaluates its own weight and latency versus the other replicas to determine an adjustment factor. The greater the disparity between weights and the larger the latency between localities, the more CockroachDB favors the node from the locality with the larger weight. - -When checking for leaseholder rebalancing opportunities, the current leaseholder evaluates each replica's rebalancing weight and adjustment factor for the localities with the greatest weights. If moving the leaseholder is both beneficial and viable, the current leaseholder will transfer the lease to the best replica. - -##### Controlling leaseholder rebalancing - -You can control leaseholder rebalancing through the `kv.allocator.load_based_lease_rebalancing.enabled` and `kv.allocator.lease_rebalancing_aggressiveness` [cluster settings](../cluster-settings.html). Note that depending on the needs of your deployment, you can exercise additional control over the location of leases and replicas by [configuring replication zones](../configure-replication-zones.html). - -### Membership changes: rebalance/repair - -Whenever there are changes to a cluster's number of nodes, the members of Raft groups change and, to ensure optimal survivability and performance, replicas need to be rebalanced. What that looks like varies depending on whether the membership change is nodes being added or going offline. - -- **Nodes added**: The new node communicates information about itself to other nodes, indicating that it has space available. The cluster then rebalances some replicas onto the new node. - -- **Nodes going offline**: If a member of a Raft group ceases to respond, after 5 minutes, the cluster begins to rebalance by replicating the data the downed node held onto other nodes. - -Rebalancing is achieved by using a snapshot of a replica from the leaseholder, and then sending the data to another node over [gRPC](distribution-layer.html#grpc). After the transfer has been completed, the node with the new replica joins that range's Raft group; it then detects that its latest timestamp is behind the most recent entries in the Raft log and it replays all of the actions in the Raft log on itself. - -#### Load-based replica rebalancing - -In addition to the rebalancing that occurs when nodes join or leave a cluster, replicas are also rebalanced automatically based on the relative load across the nodes within a cluster. For more information, see the `kv.allocator.load_based_rebalancing` and `kv.allocator.qps_rebalance_threshold` [cluster settings](../cluster-settings.html). Note that depending on the needs of your deployment, you can exercise additional control over the location of leases and replicas by [configuring replication zones](../configure-replication-zones.html). - -## Interactions with other layers - -### Replication and distribution layers - -The replication layer receives requests from its and other nodes' `DistSender`. If this node is the leaseholder for the range, it accepts the requests; if it isn't, it returns an error with a pointer to which node it believes *is* the leaseholder. These KV requests are then turned into Raft commands. - -The replication layer sends `BatchResponses` back to the distribution layer's `DistSender`. - -### Replication and storage layers - -Committed Raft commands are written to the Raft log and ultimately stored on disk through the storage layer. - -The leaseholder serves reads from its RocksDB instance, which is in the storage layer. - -## What's next? - -Learn how CockroachDB reads and writes data from disk in the [storage layer](storage-layer.html). diff --git a/src/current/v19.2/architecture/sql-layer.md b/src/current/v19.2/architecture/sql-layer.md deleted file mode 100644 index a700074aa29..00000000000 --- a/src/current/v19.2/architecture/sql-layer.md +++ /dev/null @@ -1,126 +0,0 @@ ---- -title: SQL Layer -summary: The SQL layer of CockroachDB's architecture exposes its SQL API to developers and converts SQL statements into key-value operations. -toc: true ---- - -The SQL layer of CockroachDB's architecture exposes its SQL API to developers and converts SQL statements into key-value operations used by the rest of the database. - -{{site.data.alerts.callout_info}} -If you haven't already, we recommend reading the [Architecture Overview](overview.html). -{{site.data.alerts.end}} - -## Overview - -Once CockroachDB has been deployed, developers need nothing more than a connection string to the cluster and SQL statements to start working. - -Because CockroachDB's nodes all behave symmetrically, developers can send requests to any node (which means CockroachDB works well with load balancers). Whichever node receives the request acts as the "gateway node," as other layers process the request. - -When developers send requests to the cluster, they arrive as SQL statements, but data is ultimately written to and read from the storage layer as key-value (KV) pairs. To handle this, the SQL layer converts SQL statements into a plan of KV operations, which it passes along to the transaction layer. - -### Interactions with other layers - -In relationship to other layers in CockroachDB, the SQL layer: - -- Sends requests to the transaction layer. - -## Components - -### Relational structure - -Developers experience data stored in CockroachDB in a relational structure, i.e., rows and columns. Sets of rows and columns are organized into tables. Collections of tables are organized into databases. Your cluster can contain many databases. - -Because of this structure, CockroachDB provides typical relational features like constraints (e.g., foreign keys). This lets application developers trust that the database will ensure consistent structuring of the application's data; data validation doesn't need to be built into the application logic separately. - -### SQL API - -CockroachDB implements a large portion of the ANSI SQL standard to manifest its relational structure. You can view [all of the SQL features CockroachDB supports here](../sql-feature-support.html). - -Importantly, through the SQL API, we also let developers use ACID-semantic transactions like they would through any SQL database (`BEGIN`, `END`, `COMMIT`, etc.) - -### PostgreSQL wire protocol - -SQL queries reach your cluster through the PostgreSQL wire protocol. This makes connecting your application to the cluster simple by supporting most PostgreSQL-compatible drivers, as well as many PostgreSQL ORMs, such as GORM (Go) and Hibernate (Java). - -### SQL parser, planner, executor - -After your node ultimately receives a SQL request from a client, CockroachDB parses the statement, [creates a query plan](../cost-based-optimizer.html), and then executes the plan. - -#### Parsing - -Received queries are parsed against our `yacc` file (which describes our supported syntax), and converts the string version of each query into [abstract syntax trees](https://en.wikipedia.org/wiki/Abstract_syntax_tree) (AST). - -#### Logical planning - -The AST is subsequently transformed into a query plan in three phases: - -1. The AST is transformed into a high-level logical query plan. During this transformation, CockroachDB also performs [semantic analysis](https://en.wikipedia.org/wiki/Semantic_analysis_(compilers)), which includes checking whether the query is valid, resolving names, eliminating unneeded intermediate computations, and finalizing which data types to use for intermediate results. - -2. The logical plan is *simplified* using transformation optimizations that are always valid. - -3. The logical plan is *optimized* using a [search algorithm](../cost-based-optimizer.html) that evaluates many possible ways to execute a query and selects an execution plan with the least costs. - -The result of the optimization phase is an optimized logical plan. This can be observed with [`EXPLAIN`](../explain.html). - -#### Physical planning - -The physical planning phase decides which nodes will participate in -the execution of the query, based on range locality information. This -is where CockroachDB decides to distribute a query to perform some -computations close to where the data is stored. - -The result of physical planning is a physical plan and can be observed -with [`EXPLAIN(DISTSQL)`](../explain.html). - -#### Query execution - -Components of the physical plan are sent to one or more nodes for execution. On each node, CockroachDB spawns a *logical processor* to compute a part of the query. Logical processors inside or across nodes communicate with each other over a *logical flow* of data. The combined results of the query are sent back to the first node where the query was received, to be sent further to the SQL client. - -Each processor uses an encoded form for the scalar values manipulated by the query. This is a binary form which is different from that used in SQL. So the values listed in the SQL query must be encoded, and the data communicated between logical processors, and read from disk, must be decoded before it is sent back to the SQL client. - -#### Vectorized query execution - -New in v19.2: If [vectorized execution](../vectorized-execution.html) is enabled, the physical plan is sent to nodes to be processed by the vectorized execution engine. - -Upon receiving the physical plan, the vectorized engine reads batches of table data [from disk](storage-layer.html) and converts the data from row format to columnar format. These batches of column data are stored in memory so the engine can access them quickly during execution. - -The vectorized engine uses specialized, precompiled functions that quickly iterate over the type-specific arrays of column data. The columnar output from the functions is stored in memory as the engine processes each column of data. - -After processing all columns of data in the input buffer, the engine converts the columnar output back to row format, and then returns the processed rows to the SQL interface. After a batch of table data has been fully processed, the engine reads the following batch of table data for processing, until the query has been executed. - -### Encoding - -Though SQL queries are written in parsable strings, lower layers of CockroachDB deal primarily in bytes. This means at the SQL layer, in query execution, CockroachDB must convert row data from their SQL representation as strings into bytes, and convert bytes returned from lower layers into SQL data that can be passed back to the client. - -It's also important––for indexed columns––that this byte encoding preserve the same sort order as the data type it represents. This is because of the way CockroachDB ultimately stores data in a sorted key-value map; storing bytes in the same order as the data it represents lets us efficiently scan KV data. - -However, for non-indexed columns (e.g., non-`PRIMARY KEY` columns), CockroachDB instead uses an encoding (known as "value encoding") which consumes less space but does not preserve ordering. - -You can find more exhaustive detail in the [Encoding Tech Note](https://github.com/cockroachdb/cockroach/blob/master/docs/tech-notes/encoding.md). - -### DistSQL - -Because CockroachDB is a distributed database, we've developed a Distributed SQL (DistSQL) optimization tool for some queries, which can dramatically speed up queries that involve many ranges. Though DistSQL's architecture is worthy of its own documentation, this cursory explanation can provide some insight into how it works. - -In non-distributed queries, the coordinating node receives all of the rows that match its query, and then performs any computations on the entire data set. - -However, for DistSQL-compatible queries, each node does computations on the rows it contains, and then sends the results (instead of the entire rows) to the coordinating node. The coordinating node then aggregates the results from each node, and finally returns a single response to the client. - -This dramatically reduces the amount of data brought to the coordinating node, and leverages the well-proven concept of parallel computing, ultimately reducing the time it takes for complex queries to complete. In addition, this processes data on the node that already stores it, which lets CockroachDB handle row-sets that are larger than an individual node's storage. - -To run SQL statements in a distributed fashion, we introduce a couple of concepts: - -- **Logical plan**: Similar to the AST/`planNode` tree described above, it represents the abstract (non-distributed) data flow through computation stages. -- **Physical plan**: A physical plan is conceptually a mapping of the logical plan nodes to physical machines running `cockroach`. Logical plan nodes are replicated and specialized depending on the cluster topology. Like `planNodes` above, these components of the physical plan are scheduled and run on the cluster. - -You can find much greater detail in the [DistSQL RFC](https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20160421_distributed_sql.md). - -## Technical interactions with other layers - -### SQL and transaction layer - -KV operations from executed `planNodes` are sent to the transaction layer. - -## What's next? - -Learn how CockroachDB handles concurrent requests in the [transaction layer](transaction-layer.html). diff --git a/src/current/v19.2/architecture/storage-layer.md b/src/current/v19.2/architecture/storage-layer.md deleted file mode 100644 index ddbee039801..00000000000 --- a/src/current/v19.2/architecture/storage-layer.md +++ /dev/null @@ -1,70 +0,0 @@ ---- -title: Storage Layer -summary: The storage layer of CockroachDB's architecture reads and writes data to disk. -toc: true ---- - -The storage layer of CockroachDB's architecture reads and writes data to disk. - -{{site.data.alerts.callout_info}} -If you haven't already, we recommend reading the [Architecture Overview](overview.html). -{{site.data.alerts.end}} - - -## Overview - -Each CockroachDB node contains at least one `store`, specified when the node starts, which is where the `cockroach` process reads and writes its data on disk. - -This data is stored as key-value pairs on disk using RocksDB, which is treated primarily as a black-box API. Internally, each store contains two instances of RocksDB: - -- One for storing temporary distributed SQL data -- One for all other data on the node - -In addition, there is also a block cache shared amongst all of the stores in a node. These stores in turn have a collection of range replicas. More than one replica for a range will never be placed on the same store or even the same node. - -### Interactions with other layers - -In relationship to other layers in CockroachDB, the storage layer: - -- Serves successful reads and writes from the replication layer. - -## Components - -### RocksDB - -CockroachDB uses RocksDB––an embedded key-value store––to read and write data to disk. You can find more information about it on the [RocksDB Basics GitHub page](https://github.com/facebook/rocksdb/wiki/RocksDB-Basics). - -RocksDB integrates really well with CockroachDB for a number of reasons: - -- Key-value store, which makes mapping to our key-value layer simple -- Atomic write batches and snapshots, which give us a subset of transactions - -Efficient storage for the keys is guaranteed by the underlying RocksDB engine by means of prefix compression. - -### MVCC - -CockroachDB relies heavily on [multi-version concurrency control (MVCC)](https://en.wikipedia.org/wiki/Multiversion_concurrency_control) to process concurrent requests and guarantee consistency. Much of this work is done by using [hybrid logical clock (HLC) timestamps](transaction-layer.html#time-and-hybrid-logical-clocks) to differentiate between versions of data, track commit timestamps, and identify a value's garbage collection expiration. All of this MVCC data is then stored in RocksDB. - -Despite being implemented in the storage layer, MVCC values are widely used to enforce consistency in the [transaction layer](transaction-layer.html). For example, CockroachDB maintains a [timestamp cache](transaction-layer.html#timestamp-cache), which stores the timestamp of the last time that the key was read. If a write operation occurs at a lower timestamp than the largest value in the read timestamp cache, it signifies there’s a potential anomaly and the transaction must be restarted at a later timestamp. - -#### Time-travel - -As described in the [SQL:2011 standard](https://en.wikipedia.org/wiki/SQL:2011#Temporal_support), CockroachDB supports time travel queries (enabled by MVCC). - -To do this, all of the schema information also has an MVCC-like model behind it. This lets you perform `SELECT...AS OF SYSTEM TIME`, and CockroachDB uses the schema information as of that time to formulate the queries. - -Using these tools, you can get consistent data from your database as far back as your garbage collection period. - -### Garbage collection - -CockroachDB regularly garbage collects MVCC values to reduce the size of data stored on disk. To do this, we compact old MVCC values when there is a newer MVCC value with a timestamp that's older than the garbage collection period. The garbage collection period can be set at the cluster, database, or table level by configuring the [`gc.ttlseconds` replication zone variable](../configure-replication-zones.html#gc-ttlseconds). For more information about replication zones, see [Configure Replication Zones](../configure-replication-zones.html). - -## Interactions with other layers - -### Storage and replication layers - -The storage layer commits writes from the Raft log to disk, as well as returns requested data (i.e., reads) to the replication layer. - -## What's next? - -Now that you've learned about our architecture, [start a local cluster](../install-cockroachdb.html) and start [building an app with CockroachDB](../hello-world-example-apps.html). diff --git a/src/current/v19.2/architecture/transaction-layer.md b/src/current/v19.2/architecture/transaction-layer.md deleted file mode 100644 index 9d5e62d1638..00000000000 --- a/src/current/v19.2/architecture/transaction-layer.md +++ /dev/null @@ -1,325 +0,0 @@ ---- -title: Transaction Layer -summary: The transaction layer of CockroachDB's architecture implements support for ACID transactions by coordinating concurrent operations. -toc: true ---- - -The transaction layer of CockroachDB's architecture implements support for ACID transactions by coordinating concurrent operations. - -{{site.data.alerts.callout_info}} -If you haven't already, we recommend reading the [Architecture Overview](overview.html). -{{site.data.alerts.end}} - -## Overview - -Above all else, CockroachDB believes consistency is the most important feature of a database––without it, developers cannot build reliable tools, and businesses suffer from potentially subtle and hard to detect anomalies. - -To provide consistency, CockroachDB implements full support for ACID transaction semantics in the transaction layer. However, it's important to realize that *all* statements are handled as transactions, including single statements––this is sometimes referred to as "autocommit mode" because it behaves as if every statement is followed by a `COMMIT`. - -For code samples of using transactions in CockroachDB, see our documentation on [transactions](../transactions.html#sql-statements). - -Because CockroachDB enables transactions that can span your entire cluster (including cross-range and cross-table transactions), it achieves correctness using a distributed, atomic commit protocol called [Parallel Commits](#parallel-commits). - -### Writes and reads (phase 1) - -#### Writing - -When the transaction layer executes write operations, it doesn't directly write values to disk. Instead, it creates two things that help it mediate a distributed transaction: - -- **Write intents** for all of a transaction’s writes, which represent a provisional, uncommitted state. These are essentially the same as standard [multi-version concurrency control (MVCC)](storage-layer.html#mvcc) values but also contain a pointer to the transaction record stored on the cluster. - -- A **transaction record** stored in the range where the first write occurs, which includes the transaction's current state (which is either `PENDING`, `STAGING`, `COMMITTED`, or `ABORTED`). - -As write intents are created, CockroachDB checks for newer committed values. If newer committed values exist, the transaction may be restarted. If existing write intents for the same keys exist, it is resolved as a [transaction conflict](#transaction-conflicts). - -If transactions fail for other reasons, such as failing to pass a SQL constraint, the transaction is aborted. - -#### Reading - -If the transaction has not been aborted, the transaction layer begins executing read operations. If a read only encounters standard MVCC values, everything is fine. However, if it encounters any write intents, the operation must be resolved as a [transaction conflict](#transaction-conflicts). - -### Commits (phase 2) - -CockroachDB checks the running transaction's record to see if it's been `ABORTED`; if it has, it restarts the transaction. - -In the common case, it sets the transaction record's state to `STAGING`, and checks the transaction's pending write intents to see if they have succeeded (i.e., been replicated across the cluster). - -If the transaction passes these checks, CockroachDB responds with the transaction's success to the client, and moves on to the cleanup phase. At this point, the transaction is committed, and the client is free to begin sending more requests to the cluster. - -For a more detailed walkthrough of the commit protocol, see [Parallel Commits](#parallel-commits). - -### Cleanup (asynchronous phase 3) - -After the transaction has been committed, it should be marked as such, and all of the write intents should be resolved. To do this, the coordinating node––which kept a track of all of the keys it wrote––reaches out and: - -- Moves the state of the transaction record from `STAGING` to `COMMITTED`. -- Resolves the transaction's write intents to MVCC values by removing the element that points it to the transaction record. -- Deletes the write intents. - -This is simply an optimization, though. If operations in the future encounter write intents, they always check their transaction records––any operation can resolve or remove write intents by checking the transaction record's status. - -### Interactions with other layers - -In relationship to other layers in CockroachDB, the transaction layer: - -- Receives KV operations from the SQL layer. -- Controls the flow of KV operations sent to the distribution layer. - -## Technical details and components - -### Time and hybrid logical clocks - -In distributed systems, ordering and causality are difficult problems to solve. While it's possible to rely entirely on Raft consensus to maintain serializability, it would be inefficient for reading data. To optimize performance of reads, CockroachDB implements hybrid-logical clocks (HLC) which are composed of a physical component (always close to local wall time) and a logical component (used to distinguish between events with the same physical component). This means that HLC time is always greater than or equal to the wall time. You can find more detail in the [HLC paper](http://www.cse.buffalo.edu/tech-reports/2014-04.pdf). - -In terms of transactions, the gateway node picks a timestamp for the transaction using HLC time. Whenever a transaction's timestamp is mentioned, it's an HLC value. This timestamp is used to both track versions of values (through [multi-version concurrency control](storage-layer.html#mvcc)), as well as provide our transactional isolation guarantees. - -When nodes send requests to other nodes, they include the timestamp generated by their local HLCs (which includes both physical and logical components). When nodes receive requests, they inform their local HLC of the timestamp supplied with the event by the sender. This is useful in guaranteeing that all data read/written on a node is at a timestamp less than the next HLC time. - -This then lets the node primarily responsible for the range (i.e., the leaseholder) serve reads for data it stores by ensuring the transaction reading the data is at an HLC time greater than the MVCC value it's reading (i.e., the read always happens "after" the write). - -#### Max clock offset enforcement - -CockroachDB requires moderate levels of clock synchronization to preserve data consistency. For this reason, when a node detects that its clock is out of sync with at least half of the other nodes in the cluster by 80% of the maximum offset allowed (500ms by default), **it crashes immediately**. - -While [serializable consistency](https://en.wikipedia.org/wiki/Serializability) is maintained regardless of clock skew, skew outside the configured clock offset bounds can result in violations of single-key linearizability between causally dependent transactions. It's therefore important to prevent clocks from drifting too far by running [NTP](http://www.ntp.org/) or other clock synchronization software on each node. - -For more detail about the risks that large clock offsets can cause, see [What happens when node clocks are not properly synchronized?](../operational-faqs.html#what-happens-when-node-clocks-are-not-properly-synchronized) - -### Timestamp cache - -As part of providing serializability, whenever an operation reads a value, we store the operation's timestamp in a timestamp cache, which shows the high-water mark for values being read. - -Whenever a write occurs, its timestamp is checked against the timestamp cache. If the timestamp is less than the timestamp cache's latest value, we attempt to push the timestamp for its transaction forward to a later time. Pushing the timestamp might cause the transaction to restart in the second phase of the transaction (see [read refreshing](#read-refreshing)). - -### client.Txn and TxnCoordSender - -As we mentioned in the SQL layer's architectural overview, CockroachDB converts all SQL statements into key-value (KV) operations, which is how data is ultimately stored and accessed. - -All of the KV operations generated from the SQL layer use `client.Txn`, which is the transactional interface for the CockroachDB KV layer––but, as we discussed above, all statements are treated as transactions, so all statements use this interface. - -However, `client.Txn` is actually just a wrapper around `TxnCoordSender`, which plays a crucial role in our code base by: - -- Dealing with transactions' state. After a transaction is started, `TxnCoordSender` starts asynchronously sending heartbeat messages to that transaction's transaction record, which signals that it should be kept alive. If the `TxnCoordSender`'s heartbeating stops, the transaction record is moved to the `ABORTED` status. -- Tracking each written key or key range over the course of the transaction. -- Clearing the accumulated write intent for the transaction when it's committed or aborted. All requests being performed as part of a transaction have to go through the same `TxnCoordSender` to account for all of its write intents, which optimizes the cleanup process. - -After setting up this bookkeeping, the request is passed to the `DistSender` in the distribution layer. - -### Latch manager - -As write operations occur for a range, the range's leaseholder serializes them; that is to say that they are placed into some consistent order. - -To enforce this serialization, the leaseholder creates a "latch" for the keys in the write value, providing uncontested access to the keys. If other operations come into the leaseholder for the same set of keys, they must wait for the latch to be released before they can proceed. - -Of note, only write operations generate a latch for the keys. Read operations do not block other operations from executing. - -Another way to think of a latch is like a mutex, which is only needed for the duration of a low-level operation. To coordinate longer-running, higher-level operations (i.e., client transactions), we use a durable system of [write intents](#write-intents). - -### Transaction records - -To track the status of a transaction's execution, we write a value called a transaction record to our key-value store. All of a transaction's write intents point back to this record, which lets any transaction check the status of any write intents it encounters. This kind of canonical record is crucial for supporting concurrency in a distributed environment. - -Transaction records are always written to the same range as the first key in the transaction, which is known by the `TxnCoordSender`. However, the transaction record itself isn't created until one of the following conditions occur: - -- The write operation commits -- The `TxnCoordSender` heartbeats the transaction -- An operation forces the transaction to abort - -Given this mechanism, the transaction record uses the following states: - -- `PENDING`: Indicates that the write intent's transaction is still in progress. -- `COMMITTED`: Once a transaction has completed, this status indicates that write intents can be treated as committed values. -- `STAGING`: Used to enable the [Parallel Commits](#parallel-commits) feature. Depending on the state of the write intents referenced by this record, the transaction may or may not be in a committed state. -- `ABORTED`: Indicates that the transaction was aborted and its values should be discarded. -- _Record does not exist_: If a transaction encounters a write intent whose transaction record doesn't exist, it uses the write intent's timestamp to determine how to proceed. If the write intent's timestamp is within the transaction liveness threshold, the write intent's transaction is treated as if it is `PENDING`, otherwise it's treated as if the transaction is `ABORTED`. - -The transaction record for a committed transaction remains until all its write intents are converted to MVCC values. - -### Write intents - -Values in CockroachDB are not written directly to the storage layer; instead everything is written in a provisional state known as a "write intent." These are essentially MVCC records with an additional value added to them which identifies the transaction record to which the value belongs. - -Whenever an operation encounters a write intent (instead of an MVCC value), it looks up the status of the transaction record to understand how it should treat the write intent value. If the transaction record is missing, the operation checks the write intent's timestamp and evaluates whether or not it is considered expired. - -#### Resolving write intents - -Whenever an operation encounters a write intent for a key, it attempts to "resolve" it, the result of which depends on the write intent's transaction record: - -- `COMMITTED`: The operation reads the write intent and converts it to an MVCC value by removing the write intent's pointer to the transaction record. -- `ABORTED`: The write intent is ignored and deleted. -- `PENDING`: This signals there is a [transaction conflict](#transaction-conflicts), which must be resolved. -- `STAGING`: This signals that the operation should check whether the staging transaction is still in progress by verifying that the transaction coordinator is still heartbeating the staging transaction’s record. If the coordinator is still heartbeating the record, the operation should wait. For more information, see [Parallel Commits](#parallel-commits). -- _Record does not exist_: If the write intent was created within the transaction liveness threshold, it's the same as `PENDING`, otherwise it's treated as `ABORTED`. - -### Isolation levels - -Isolation is an element of [ACID transactions](https://en.wikipedia.org/wiki/ACID), which determines how concurrency is controlled, and ultimately guarantees consistency. - -CockroachDB executes all transactions at the strongest ANSI transaction isolation level: `SERIALIZABLE`. All other ANSI transaction isolation levels (e.g., `SNAPSHOT`, `READ UNCOMMITTED`, `READ COMMITTED`, and `REPEATABLE READ`) are automatically upgraded to `SERIALIZABLE`. Weaker isolation levels have historically been used to maximize transaction throughput. However, [recent research](http://www.bailis.org/papers/acidrain-sigmod2017.pdf) has demonstrated that the use of weak isolation levels results in substantial vulnerability to concurrency-based attacks. - -CockroachDB now only supports `SERIALIZABLE` isolation. In previous versions of CockroachDB, you could set transactions to `SNAPSHOT` isolation, but that feature has been removed. - -`SERIALIZABLE` isolation does not allow any anomalies in your data, and is enforced by requiring the client to retry transactions if serializability violations are possible. - -### Transaction conflicts - -CockroachDB's transactions allow the following types of conflicts that involve running into an intent: - -- **Write/write**, where two `PENDING` transactions create write intents for the same key. -- **Write/read**, when a read encounters an existing write intent with a timestamp less than its own. - -To make this simpler to understand, we'll call the first transaction `TxnA` and the transaction that encounters its write intents `TxnB`. - -CockroachDB proceeds through the following steps: - -1. If the transaction has an explicit priority set (i.e., `HIGH` or `LOW`), the transaction with the lower priority is aborted (in the write/write case) or has its timestamp pushed (in the write/read case). - -1. If the encountered transaction is expired, it's `ABORTED` and conflict resolution succeeds. We consider a write intent expired if: - - It doesn't have a transaction record and its timestamp is outside of the transaction liveness threshold. - - Its transaction record hasn't been heartbeated within the transaction liveness threshold. - -2. `TxnB` enters the `TxnWaitQueue` to wait for `TxnA` to complete. - -Additionally, the following types of conflicts that do not involve running into intents can arise: - -- **Write after read**, when a write with a lower timestamp encounters a later read. This is handled through the [timestamp cache](#timestamp-cache). -- **Read within uncertainty window**, when a read encounters a value with a higher timestamp but it's ambiguous whether the value should be considered to be in the future or in the past of the transaction because of possible *clock skew*. This is handled by attempting to push the transaction's timestamp beyond the uncertain value (see [read refreshing](#read-refreshing)). Note that, if the transaction has to be retried, reads will never encounter uncertainty issues on any node which was previously visited, and that there's never any uncertainty on values read from the transaction's gateway node. - -### TxnWaitQueue - -The `TxnWaitQueue` tracks all transactions that could not push a transaction whose writes they encountered, and must wait for the blocking transaction to complete before they can proceed. - -The `TxnWaitQueue`'s structure is a map of blocking transaction IDs to those they're blocking. For example: - -~~~ -txnA -> txn1, txn2 -txnB -> txn3, txn4, txn5 -~~~ - -Importantly, all of this activity happens on a single node, which is the leader of the range's Raft group that contains the transaction record. - -Once the transaction does resolve––by committing or aborting––a signal is sent to the `TxnWaitQueue`, which lets all transactions that were blocked by the resolved transaction begin executing. - -Blocked transactions also check the status of their own transaction to ensure they're still active. If the blocked transaction was aborted, it's simply removed. - -If there is a deadlock between transactions (i.e., they're each blocked by each other's Write Intents), one of the transactions is randomly aborted. In the above example, this would happen if `TxnA` blocked `TxnB` on `key1` and `TxnB` blocked `TxnA` on `key2`. - -### Read refreshing - -Whenever a transaction's timestamp has been pushed, additional checks are required before allowing it to commit at the pushed timestamp: any values which the transaction previously read must be checked to verify that no writes have subsequently occurred between the original transaction timestamp and the pushed transaction timestamp. This check prevents serializability violation. The check is done by keeping track of all the reads using a dedicated `RefreshRequest`. If this succeeds, the transaction is allowed to commit (transactions perform this check at commit time if they've been pushed by a different transaction or by the timestamp cache, or they perform the check whenever they encounter a `ReadWithinUncertaintyIntervalError` immediately, before continuing). -If the refreshing is unsuccessful, then the transaction must be retried at the pushed timestamp. - -### Transaction pipelining - -Transactional writes are pipelined when being replicated and when being written to disk, dramatically reducing the latency of transactions that perform multiple writes. For example, consider the following transaction: - -{% include copy-clipboard.html %} -~~~ sql --- CREATE TABLE kv (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), key VARCHAR, value VARCHAR); -> BEGIN; -INSERT into kv (key, value) VALUES ('apple', 'red'); -INSERT into kv (key, value) VALUES ('banana', 'yellow'); -INSERT into kv (key, value) VALUES ('orange', 'orange'); -COMMIT; -~~~ - -With transaction pipelining, write intents are replicated from leaseholders in parallel, so the waiting all happens at the end, at transaction commit time. - -At a high level, transaction pipelining works as follows: - -1. For each statement, the transaction gateway node communicates with the leaseholders (*L*1, *L*2, *L*3, ..., *L*i) for the ranges it wants to write to. Since the primary keys in the table above are UUIDs, the ranges are probably split across multiple leaseholders (this is a good thing, as it decreases [transaction conflicts](#transaction-conflicts)). - -2. Each leaseholder *L*i receives the communication from the transaction gateway node and does the following in parallel: - - Creates write intents and sends them to its follower nodes. - - Responds to the transaction gateway node that the write intents have been sent. Note that replication of the intents is still in-flight at this stage. - -3. When attempting to commit, the transaction gateway node then waits for the write intents to be replicated in parallel to all of the leaseholders' followers. When it receives responses from the leaseholders that the write intents have propagated, it commits the transaction. - -In terms of the SQL snippet shown above, all of the waiting for write intents to propagate and be committed happens once, at the very end of the transaction, rather than for each individual write. This means that the cost of multiple writes is not `O(n)` in the number of SQL DML statements; instead, it's `O(1)`. - -### Parallel Commits - -New in v19.2: The *Parallel Commits* feature introduces a new, optimized atomic commit protocol that cuts the commit latency of a transaction in half, from two rounds of consensus down to one. Combined with [Transaction pipelining](#transaction-pipelining), this brings the latency incurred by common OLTP transactions to near the theoretical minimum: the sum of all read latencies plus one round of consensus latency. - -Under the new atomic commit protocol, the transaction coordinator can return to the client eagerly when it knows that the writes in the transaction have succeeded. Once this occurs, the transaction coordinator can set the transaction record's state to `COMMITTED` and resolve the transaction's write intents asynchronously. - -The transaction coordinator is able to do this while maintaining correctness guarantees because it populates the transaction record with enough information (via a new `STAGING` state, and an array of in-flight writes) for other transactions to determine whether all writes in the transaction are present, and thus prove whether or not the transaction is committed. - -For an example showing how the Parallel Commits feature works in more detail, see [Parallel Commits - step by step](#parallel-commits-step-by-step). - -{{site.data.alerts.callout_info}} -The latency until intents are resolved is unchanged by the introduction of Parallel Commits: two rounds of consensus are still required to resolve intents. This means that [contended workloads](../performance-best-practices-overview.html#understanding-and-avoiding-transaction-contention) are expected to profit less from this feature. -{{site.data.alerts.end}} - -#### Parallel Commits - step by step - -This section contains a step by step example of a transaction that writes its data using the Parallel Commits atomic commit protocol and does not encounter any errors or conflicts. - -##### Step 1 - -The client starts the transaction. A transaction coordinator is created to manage the state of that transaction. - -![parallel-commits-00.png](../../images/{{page.version.version}}/parallel-commits-00.png "Parallel Commits Diagram #1") - -##### Step 2 - -The client issues a write to the "Apple" key. The transaction coordinator begins the process of laying down a write intent on the key where the data will be written. The write intent has a timestamp and a pointer to an as-yet nonexistent transaction record. Additionally, each write intent in the transaction is assigned a unique sequence number which is used to uniquely identify it. - -The coordinator avoids creating the record for as long as possible in the transaction's lifecycle as an optimization. The fact that the transaction record does not yet exist is denoted in the diagram by its dotted lines. - -{{site.data.alerts.callout_info}} -The coordinator does not need to wait for write intents to replicate from leaseholders before moving on to the next statement from the client, since that is handled in parallel by [Transaction Pipelining](#transaction-pipelining). -{{site.data.alerts.end}} - -![parallel-commits-01.png](../../images/{{page.version.version}}/parallel-commits-01.png "Parallel Commits Diagram #2") - -##### Step 3 - -The client issues a write to the "Berry" key. The transaction coordinator lays down a write intent on the key where the data will be written. This write intent has a pointer to the same transaction record as the intent created in [Step 2](#step-2), since these write intents are part of the same transaction. - -As before, the coordinator does not need to wait for write intents to replicate from leaseholders before moving on to the next statement from the client. - -![parallel-commits-02.png](../../images/{{page.version.version}}/parallel-commits-02.png "Parallel Commits Diagram #3") - -##### Step 4 - -The client issues a request to commit the transaction's writes. The transaction coordinator creates the transaction record and immediately sets the record's state to `STAGING`, and records the keys of each write that the transaction has in flight. - -It does this without waiting to see whether the writes from Steps [2](#step-2) and [3](#step-3) have succeeded. - -![parallel-commits-03.png](../../images/{{page.version.version}}/parallel-commits-03.png "Parallel Commits Diagram #4") - -##### Step 5 - -The transaction coordinator, having received the client's `COMMIT` request, waits for the pending writes to succeed (i.e., be replicated across the cluster). Once all of the pending writes have succeeded, the coordinator returns a message to the client, letting it know that its transaction has committed successfully. - -![parallel-commits-04.png](../../images/{{page.version.version}}/parallel-commits-04.png "Parallel Commits Diagram #4") - -The transaction is now considered atomically committed, even though the state of its transaction record is still `STAGING`. The reason this is still considered an atomic commit condition is that a transaction is considered committed if it is one of the following logically equivalent states: - -1. The transaction record's state is `STAGING`, and its list of pending writes have all succeeded (i.e., the `InFlightWrites` have achieved consensus across the cluster). Any observer of this transaction can verify that its writes have replicated. Transactions in this state are *implicitly committed*. - -2. The transaction record's state is `COMMITTED`. Transactions in this state are *explicitly committed*. - -Despite their logical equivalence, the transaction coordinator now works as quickly as possible to move the transaction record from the `STAGING` to the `COMMITTED` state so that other transactions do not encounter a possibly conflicting transaction in the `STAGING` state and then have to do the work of verifying that the staging transaction's list of pending writes has succeeded. Doing that verification (also known as the "transaction status recovery process") would be slow. - -Additionally, when other transactions encounter a transaction in `STAGING` state, they check whether the staging transaction is still in progress by verifying that the transaction coordinator is still heartbeating that staging transaction’s record. If the coordinator is still heartbeating the record, the other transactions will wait, on the theory that letting the coordinator update the transaction record with the final result of the attempt to commit will be faster than going through the transaction status recovery process. This means that in practice, the transaction status recovery process is only used if the transaction coordinator dies due to an untimely crash. - -## Technical interactions with other layers - -### Transaction and SQL layer - -The transaction layer receives KV operations from `planNodes` executed in the SQL layer. - -### Transaction and distribution layer - -The `TxnCoordSender` sends its KV requests to `DistSender` in the distribution layer. - -## What's next? - -Learn how CockroachDB presents a unified view of your cluster's data in the [distribution layer](distribution-layer.html). - - - -[storage]: storage-layer.html -[sql]: sql-layer.html diff --git a/src/current/v19.2/array.md b/src/current/v19.2/array.md deleted file mode 100644 index 34138476a9b..00000000000 --- a/src/current/v19.2/array.md +++ /dev/null @@ -1,244 +0,0 @@ ---- -title: ARRAY -summary: The ARRAY data type stores one-dimensional, 1-indexed, homogeneous arrays of any non-array data types. -toc: true ---- - -The `ARRAY` data type stores one-dimensional, 1-indexed, homogeneous arrays of any non-array [data type](data-types.html). - -The `ARRAY` data type is useful for ensuring compatibility with ORMs and other tools. However, if such compatibility is not a concern, it's more flexible to design your schema with normalized tables. - -{{site.data.alerts.callout_info}} -CockroachDB does not support nested arrays, creating database indexes on arrays, and ordering by arrays. -{{site.data.alerts.end}} - -{% include {{page.version.version}}/sql/vectorized-support.md %} - -## Syntax - -A value of data type `ARRAY` can be expressed in the following ways: - -- Appending square brackets (`[]`) to any non-array [data type](data-types.html). -- Adding the term `ARRAY` to any non-array [data type](data-types.html). - -## Size - -The size of an `ARRAY` value is variable, but it's recommended to keep values under 1 MB to ensure performance. Above that threshold, [write amplification](https://en.wikipedia.org/wiki/Write_amplification) and other considerations may cause significant performance degradation. - -## Examples - -{{site.data.alerts.callout_success}} -For a complete list of array functions built into CockroachDB, see the [documentation on array functions](functions-and-operators.html#array-functions). -{{site.data.alerts.end}} - -### Creating an array column by appending square brackets - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE a (b STRING[]); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO a VALUES (ARRAY['sky', 'road', 'car']); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM a; -~~~ - -~~~ -+----------------------+ -| b | -+----------------------+ -| {"sky","road","car"} | -+----------------------+ -(1 row) -~~~ - -### Creating an array column by adding the term `ARRAY` - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE c (d INT ARRAY); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO c VALUES (ARRAY[10,20,30]); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM c; -~~~ - -~~~ -+------------+ -| d | -+------------+ -| {10,20,30} | -+------------+ -(1 row) -~~~ - -### Accessing an array element using array index -{{site.data.alerts.callout_info}} -Arrays in CockroachDB are 1-indexed. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM c; -~~~ - -~~~ -+------------+ -| d | -+------------+ -| {10,20,30} | -+------------+ -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT d[2] FROM c; -~~~ - -~~~ -+------+ -| d[2] | -+------+ -| 20 | -+------+ -(1 row) -~~~ - -### Appending an element to an array - -#### Using the `array_append` function - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM c; -~~~ - -~~~ -+------------+ -| d | -+------------+ -| {10,20,30} | -+------------+ -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE c SET d = array_append(d, 40) WHERE d[3] = 30; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM c; -~~~ - -~~~ -+---------------+ -| d | -+---------------+ -| {10,20,30,40} | -+---------------+ -(1 row) -~~~ - -#### Using the append (`||`) operator - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM c; -~~~ - -~~~ -+---------------+ -| d | -+---------------+ -| {10,20,30,40} | -+---------------+ -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE c SET d = d || 50 WHERE d[4] = 40; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM c; -~~~ - -~~~ -+------------------+ -| d | -+------------------+ -| {10,20,30,40,50} | -+------------------+ -(1 row) -~~~ - -## Supported casting and conversion - -[Casting](data-types.html#data-type-conversions-and-casts) between `ARRAY` values is supported when the data types of the arrays support casting. For example, it is possible to cast from a `BOOL` array to an `INT` array but not from a `BOOL` array to a `TIMESTAMP` array: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT ARRAY[true,false,true]::INT[]; -~~~ - -~~~ - array -+---------+ - {1,0,1} -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT ARRAY[true,false,true]::TIMESTAMP[]; -~~~ - -~~~ -pq: invalid cast: bool[] -> TIMESTAMP[] -~~~ - -You can cast an array to a `STRING` value, for compatibility with PostgreSQL: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT ARRAY[1,NULL,3]::string; -~~~ - -~~~ - array -+------------+ - {1,NULL,3} -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT ARRAY[(1,'a b'),(2,'c"d')]::string; -~~~ - -~~~ - array -+----------------------------------+ - {"(1,\"a b\")","(2,\"c\"\"d\")"} -(1 row) -~~~ - -## See also - -[Data Types](data-types.html) diff --git a/src/current/v19.2/as-of-system-time.md b/src/current/v19.2/as-of-system-time.md deleted file mode 100644 index a4a5710156c..00000000000 --- a/src/current/v19.2/as-of-system-time.md +++ /dev/null @@ -1,190 +0,0 @@ ---- -title: AS OF SYSTEM TIME -summary: The AS OF SYSTEM TIME clause executes a statement as of a specified time. -toc: true ---- - -The `AS OF SYSTEM TIME timestamp` clause causes statements to execute -using the database contents "as of" a specified time in the past. - -This clause can be used to read historical data (also known as "[time -travel queries](https://www.cockroachlabs.com/blog/time-travel-queries-select-witty_subtitle-the_future/)") and can also be advantageous for performance as it decreases -transaction conflicts. For more details, see [SQL Performance Best -Practices](performance-best-practices-overview.html#use-as-of-system-time-to-decrease-conflicts-with-long-running-queries). - -{{site.data.alerts.callout_info}} -Historical data is available only within the garbage collection window, which is determined by the `ttlseconds` field in the [replication zone configuration](configure-replication-zones.html). -{{site.data.alerts.end}} - -## Synopsis - -The `AS OF SYSTEM TIME` clause is supported in multiple SQL contexts, -including but not limited to: - -- In [`SELECT` clauses](select-clause.html), at the very end of the `FROM` sub-clause. -- In [`BACKUP`](backup.html), after the parameters of the `TO` sub-clause. -- In [`RESTORE`](restore.html), after the parameters of the `FROM` sub-clause. -- In [`BEGIN`](begin-transaction.html), after the `BEGIN` keyword. -- In [`SET`](set-transaction.html), after the `SET TRANSACTION` keyword. - -## Parameters - -The `timestamp` argument supports the following formats: - -Format | Notes ----|--- -[`INT`](int.html) | Nanoseconds since the Unix epoch. -negative [`INTERVAL`](interval.html) | Added to `statement_timestamp()`, and thus must be negative. -[`STRING`](string.html) | A [`TIMESTAMP`](timestamp.html), [`INT`](int.html) of nanoseconds, or negative [`INTERVAL`](interval.html). -`experimental_follower_read_timestamp()`| A [function](functions-and-operators.html) that runs your queries at a time as close as possible to the present time while remaining safe for [follower reads](follower-reads.html). - -## Examples - -### Select historical data (time-travel) - -Imagine this example represents the database's current data: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT name, balance - FROM accounts - WHERE name = 'Edna Barath'; -~~~ -~~~ -+-------------+---------+ -| name | balance | -+-------------+---------+ -| Edna Barath | 750 | -| Edna Barath | 2200 | -+-------------+---------+ -~~~ - -We could instead retrieve the values as they were on October 3, 2016 at 12:45 UTC: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT name, balance - FROM accounts - AS OF SYSTEM TIME '2016-10-03 12:45:00' - WHERE name = 'Edna Barath'; -~~~ -~~~ -+-------------+---------+ -| name | balance | -+-------------+---------+ -| Edna Barath | 450 | -| Edna Barath | 2000 | -+-------------+---------+ -~~~ - - -### Using different timestamp formats - -Assuming the following statements are run at `2016-01-01 12:00:00`, they would execute as of `2016-01-01 08:00:00`: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM t AS OF SYSTEM TIME '2016-01-01 08:00:00' -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM t AS OF SYSTEM TIME 1451635200000000000 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM t AS OF SYSTEM TIME '1451635200000000000' -~~~ - -{% include copy-clipboard.html %} -~~~sql -> SELECT * FROM t AS OF SYSTEM TIME '-4h' -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM t AS OF SYSTEM TIME INTERVAL '-4h' -~~~ - -### Selecting from multiple tables - -{{site.data.alerts.callout_info}} -It is not yet possible to select from multiple tables at different timestamps. The entire query runs at the specified time in the past. -{{site.data.alerts.end}} - -When selecting over multiple tables in a single `FROM` clause, the `AS -OF SYSTEM TIME` clause must appear at the very end and applies to the -entire `SELECT` clause. - -For example: - -{% include copy-clipboard.html %} -~~~sql -> SELECT * FROM t, u, v AS OF SYSTEM TIME '-4h'; -~~~ - -{% include copy-clipboard.html %} -~~~sql -> SELECT * FROM t JOIN u ON t.x = u.y AS OF SYSTEM TIME '-4h'; -~~~ - -{% include copy-clipboard.html %} -~~~sql -> SELECT * FROM (SELECT * FROM t), (SELECT * FROM u) AS OF SYSTEM TIME '-4h'; -~~~ - -### Using `AS OF SYSTEM TIME` in subqueries - -To enable time travel, the `AS OF SYSTEM TIME` clause must appear in -at least the top-level statement. It is not valid to use it only in a -[subquery](subqueries.html). - -For example, the following is invalid: - -~~~ -SELECT * FROM (SELECT * FROM t AS OF SYSTEM TIME '-4h'), u -~~~ - -To facilitate the composition of larger queries from simpler queries, -CockroachDB allows `AS OF SYSTEM TIME` in sub-queries under the -following conditions: - -- The top level query also specifies `AS OF SYSTEM TIME`. -- All the `AS OF SYSTEM TIME` clauses specify the same timestamp. - -For example: - -{% include copy-clipboard.html %} -~~~sql -> SELECT * FROM (SELECT * FROM t AS OF SYSTEM TIME '-4h') tp - JOIN u ON tp.x = u.y - AS OF SYSTEM TIME '-4h' -- same timestamp as above - OK. - WHERE x < 123; -~~~ - -### Using `AS OF SYSTEM TIME` in transactions - -You can use the [`BEGIN`](begin-transaction.html) statement to execute the transaction using the database contents "as of" a specified time in the past. - -{% include {{ page.version.version }}/sql/begin-transaction-as-of-system-time-example.md %} - -Alternatively, you can use the [`SET`](set-transaction.html) statement to execute the transaction using the database contents "as of" a specified time in the past. - -{% include {{ page.version.version }}/sql/set-transaction-as-of-system-time-example.md %} - -## See also - -- [Select Historical Data](select-clause.html#select-historical-data-time-travel) -- [Time-Travel Queries](https://www.cockroachlabs.com/blog/time-travel-queries-select-witty_subtitle-the_future/) - -## Tech note - -{{site.data.alerts.callout_info}} -Although the following format is supported, it is not intended to be used by most users. -{{site.data.alerts.end}} - -HLC timestamps can be specified using a [`DECIMAL`](decimal.html). The -integer part is the wall time in nanoseconds. The fractional part is -the logical counter, a 10-digit integer. This is the same format as -produced by the `cluster_logical_timestamp()` function. diff --git a/src/current/v19.2/authentication.md b/src/current/v19.2/authentication.md deleted file mode 100644 index 152c5be58f3..00000000000 --- a/src/current/v19.2/authentication.md +++ /dev/null @@ -1,275 +0,0 @@ ---- -title: Authentication -summary: Learn about the authentication features for secure CockroachDB clusters. -toc: true ---- - -Authentication refers to the act of verifying the identity of the other party in communication. CockroachDB requires TLS 1.2 digital certificates for inter-node and client-node authentication, which require a Certificate Authority (CA) as well as keys and certificates for nodes, clients, and (optionally) the Admin UI. This document discusses how CockroachDB uses digital certificates and also gives [conceptual overview](#background-on-public-key-cryptography-and-digital-certificates) of public key cryptography and digital certificates. - -- If you are familiar with public key cryptography and digital certificates, then reading the [Using digital certificates with CockroachDB](#using-digital-certificates-with-cockroachdb) section should be enough. -- If you are unfamiliar with public key cryptography and digital certificates, you might want to skip over to the [conceptual overview](#background-on-public-key-cryptography-and-digital-certificates) first and then come back to the [Using digital certificates with CockroachDB](#using-digital-certificates-with-cockroachdb) section. -- If you want to know how to create CockroachDB security certificates, see [Create Security Certificates](cockroach-cert.html). - -## Using digital certificates with CockroachDB - -CockroachDB uses both TLS 1.2 server and client certificates. Each CockroachDB node in a secure cluster must have a **node certificate**, which is a TLS 1.2 server certificate. Note that the node certificate is multi-functional, which means that the same certificate is presented irrespective of whether the node is acting as a server or a client. The nodes use these certificates to establish secure connections with clients and with other nodes. Node certificates have the following requirements: - -- The hostname or address (IP address or DNS name) used to reach a node, either directly or through a load balancer, must be listed in the **Common Name** or **Subject Alternative Names** fields of the certificate: - - - The values specified in [`--listen-addr`](cockroach-start.html#networking) and [`--advertise-addr`](cockroach-start.html#networking) flags, or the node hostname and fully qualified hostname if not specified - - Any host addresses/names used to reach a specific node - - Any load balancer addresses/names or DNS aliases through which the node could be reached - - `localhost` and local address if connections are made through the loopback device on the same host - -- CockroachDB must be configured to trust the certificate authority that signed the certificate. - -Based on your security setup, you can use the [`cockroach cert` commands](cockroach-cert.html), [`openssl` commands](create-security-certificates-openssl.html), or a [custom CA](create-security-certificates-custom-ca.html) to generate all the keys and certificates. - -A CockroachDB cluster consists of multiple nodes and clients. The nodes can communicate with each other, with the SQL clients, and the Admin UI. In client-node SQL communication and client-UI communication, the node acts as a server, but in inter-node communication, a node may act as a server or a client. Hence authentication in CockroachDB involves: - -- Node authentication using [TLS 1.2](https://en.wikipedia.org/wiki/Transport_Layer_Security) digital certificates. -- Client authentication using TLS digital certificates, passwords, or [GSSAPI authentication](gssapi_authentication.html) (for Enterprise users). - -### Node authentication - -To set up a secure cluster without using an existing certificate authority, you'll need to generate the following files: - -- CA certificate -- Node certificate and key -- (Optional) UI certificate and key - -### Client authentication - -CockroachDB offers three methods for client authentication: - -- **Client certificate and key authentication**, which is available to all users. To ensure the highest level of security, we recommend only using client certificate and key authentication. - - Example: - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --certs-dir=certs --user=jpointsman - ~~~ - -- **Password authentication**, which is available to non-`root` users who you've created passwords for. Password creation is supported only in secure clusters. - - Example: - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --certs-dir=certs --user=jpointsman - ~~~ - - ~~~ - # Welcome to the CockroachDB SQL shell. - # All statements must be terminated by a semicolon. - # To exit, type: \q. - # - Enter password: - ~~~ - - Note that the client still needs the CA certificate to validate the nodes' certificates. - -- [**GSSAPI authentication**](gssapi_authentication.html), which is available to [Enterprise users](enterprise-licensing.html). - -### Using `cockroach cert` or `openssl` commands - -You can use the [`cockroach cert` commands](cockroach-cert.html) or [`openssl` commands](create-security-certificates-openssl.html) to create the CA certificate and key, and node and client certificates and keys. - -Note that the node certificate created using `cockroach cert` or`openssl` is multi-functional, which means that the same certificate is presented irrespective of whether the node is acting as a server or a client. Thus all nodes must have the following: - -- `CN=node` for the special user `node` when the node acts as a client. -- All IP addresses and DNS names for the node must be listed in the `Subject Alternative Name` field for when the node acts as a server. CockroachDB also supports [wildcard notation in DNS names](https://en.wikipedia.org/wiki/Wildcard_certificate). - -**Node key and certificates** - -A node must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate created using the `cockroach cert` command. -`node.crt` | Server certificate created using the `cockroach cert` command.

      `node.crt` must have `CN=node` and the list of IP addresses and DNS names listed in the `Subject Alternative Name` field. CockroachDB also supports [wildcard notation in DNS names](https://en.wikipedia.org/wiki/Wildcard_certificate).

      Must be signed by the CA represented by `ca.crt`. -`node.key` | Server key created using the `cockroach cert` command. - -**Client key and certificates** - -A client must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate created using the `cockroach cert` command. -`client..crt` | Client certificate for `` (e.g., `client.root.crt` for user `root`).

      Each `client..crt` must have `CN=` (for example, `CN=marc` for `client.marc.crt`).

      Must be signed by the CA represented by `ca.crt`. -`client..key` | Client key created using the `cockroach cert` command. - -Alternatively, you can use [password authentication](#client-authentication). Remember, the client still needs `ca.crt` for node authentication. - -### Using a custom CA - -In the previous section, we discussed the scenario where the node and client certificates are signed by the CA created using the `cockroach cert` command. But what if you want to use an external CA, like your organizational CA or a public CA? In that case, our certificates might need some modification. Here’s why: - -As mentioned earlier, the node certificate is multi-functional, as in the same certificate is presented irrespective of whether the node is acting as a server or client. To make the certificate multi-functional, the `node.crt` must have `CN=node` and the list of IP addresses and DNS names listed in the `Subject Alternative Names` field. - -But some CAs will not sign a certificate containing a `CN` that is not an IP address or domain name. Here's why: The TLS client certificates are used to authenticate the client connecting to a server. Because most client certificates authenticate a user instead of a device, the certificates contain usernames instead of hostnames. This makes it difficult for public CAs to verify the client's identity and hence most public CAs will not sign a client certificate. - -To get around this issue, we can split the node key and certificate into two: - -- `node.crt` and `node.key`: The node certificate to be presented when the node acts as a server and the corresponding key. `node.crt` must have the list of IP addresses and DNS names listed in the `Subject Alternative Names`. -- `client.node.crt` and `client.node.key`: The node certificate to be presented when the node acts as a client for another node, and the corresponding key. `client.node.crt` must have `CN=node`. - -**Node key and certificates** - -A node must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate issued by the public CA or your organizational CA. -`node.crt` | Node certificate for when node acts as server.

      All IP addresses and DNS names for the node must be listed in the `Subject Alternative Name`. CockroachDB also supports [wildcard notation in DNS names](https://en.wikipedia.org/wiki/Wildcard_certificate).

      Must be signed by the CA represented by `ca.crt`. -`node.key` | Server key corresponding to `node.crt`. -`client.node.crt` | Node certificate for when node acts as client.

      Must have `CN=node`.

      Must be signed by the CA represented by `ca.crt`. -`client.node.key` | Client key corresponding to `client.node.crt`. - -Optionally, if you have a certificate issued by a public CA to securely access the Admin UI, you need to place the certificate and key (`ui.crt` and `ui.key` respectively) in the directory specified by the `--certs-dir` flag. - -**Client key and certificates** - -A client must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate issued by the public CA or your organizational CA. -`client..crt` | Client certificate for `` (e.g., `client.root.crt` for user `root`).

      Each `client..crt` must have `CN=` (for example, `CN=marc` for `client.marc.crt`).

      Must be signed by the CA represented by `ca.crt`. -`client..key` | Client key corresponding to `client..crt`. - -Alternatively, you can use [password authentication](#client-authentication). Remember, the client still needs `ca.crt` for node authentication. - -### Using a public CA certificate to access the Admin UI for a secure cluster - -One of the limitations of using `cockroach cert` or `openssl` is that the browsers used to access the CockroachDB Admin UI do not trust the node certificates presented to them. Web browsers come preloaded with CA certificates from well-established entities (e.g., GlobalSign and DigiTrust). The CA certificate generated using the `cockroach cert` or `openssl` is not preloaded in the browser. Hence on accessing the Admin UI for a secure cluster, you get the “Unsafe page” warning. Now you could add the CA certificate to the browser to avoid the warning, but that is not a recommended practice. Instead, you can use the established CAs (for example, Let’s Encrypt), to create a certificate and key to access the Admin UI. - -Once you have the UI cert and key, add it to the Certificates directory specified by the `--certs-dir` flag in the `cockroach cert` command. The next time the browser tries to access the UI, the node will present the UI cert instead of the node cert, and you’ll not see the “unsafe site” warning anymore. - -**Node key and certificates** - -A node must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate created using the `cockroach cert` command. -`node.crt` | Server certificate created using the `cockroach cert` command.

      `node.crt` must have `CN=node` and the list of IP addresses and DNS names listed in the `Subject Alternative Name` field. CockroachDB also supports [wildcard notation in DNS names](https://en.wikipedia.org/wiki/Wildcard_certificate).

      Must be signed by the CA represented by `ca.crt`. -`node.key` | Server key created using the `cockroach cert` command. -`ui.crt` | UI certificate signed by the public CA. `ui.crt` must have the IP addresses and DNS names used to reach the Admin UI listed in the `Subject Alternative Name`. -`ui.key` | UI key corresponding to `ui.crt`. - -**Client key and certificates** - -A client must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate created using the `cockroach cert` command. -`client..crt` | Client certificate for `` (e.g., `client.root.crt` for user `root`).

      Each `client..crt` must have `CN=` (for example, `CN=marc` for `client.marc.crt`).

      Must be signed by the CA represented by `ca.crt`. -`client..key` | Client key created using the `cockroach cert` command. - -Alternatively, you can use [password authentication](#client-authentication). Remember, the client still needs `ca.crt` for node authentication. - -### Using split CA certificates - -{{site.data.alerts.callout_danger}} -We do not recommend you use split CA certificates unless your organizational security practices mandate you to do so. -{{site.data.alerts.end}} - -You might encounter situations where you need separate CAs to sign and verify node and client certificates. In that case, you would need two CAs and their respective certificates and keys: `ca.crt` and `ca-client.crt`. - -**Node key and certificates** - -A node must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate to verify node certificates. -`ca-client.crt` | CA certificate to verify client certificates. -`node.crt` | Node certificate for when node acts as server.

      All IP addresses and DNS names for the node must be listed in the `Subject Alternative Name`. CockroachDB also supports [wildcard notation in DNS names](https://en.wikipedia.org/wiki/Wildcard_certificate).

      Must be signed by the CA represented by `ca.crt`. -`node.key` | Server key corresponding to `node.crt`. -`client.node.crt` | Node certificate for when node acts as client. This certificate must be signed by the CA represented by `ca-client.crt`.

      Must have `CN=node`. -`client.node.key` | Client key corresponding to `client.node.crt`. - -Optionally, if you have a certificate issued by a public CA to securely access the Admin UI, you need to place the certificate and key (`ui.crt` and `ui.key` respectively) in the directory specified by the `--certs-dir` flag. - -**Client key and certificates** - -A client must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate. -`client..crt` | Client certificate for `` (e.g., `client.root.crt` for user `root`).

      Each `client..crt` must have `CN=` (for example, `CN=marc` for `client.marc.crt`).

      Must be signed by the CA represented by `ca-client.crt`. -`client..key` | Client key corresponding to `client..crt`. - -## Authentication for cloud storage - -See [Backup file URLs](backup.html#backup-file-urls) - -## Authentication best practice - -As a security best practice, we recommend that you rotate the node, client, or CA certificates in the following scenarios: - -- The node, client, or CA certificates are expiring soon. -- Your organization's compliance policy requires periodical certificate rotation. -- The key (for a node, client, or CA) is compromised. -- You need to modify the contents of a certificate, for example, to add another DNS name or the IP address of a load balancer through which a node can be reached. In this case, you would need to rotate only the node certificates. - -For details about when and how to change security certificates without restarting nodes, see [Rotate Security Certificates](rotate-certificates.html). - -## Background on public key cryptography and digital certificates - -As mentioned above, CockroachDB uses the [TLS 1.2](https://en.wikipedia.org/wiki/Transport_Layer_Security) security protocol that takes advantage of both symmetric (to encrypt data in flight) as well as asymmetric encryption (to establish a secure channel as well as **authenticate** the communicating parties). - -Authentication refers to the act of verifying the identity of the other party in communication. CockroachDB uses TLS 1.2 digital certificates for inter-node and client-node authentication, which require a Certificate Authority (CA) as well as keys and certificates for nodes, clients, and (optionally) the Admin UI. - -To understand how CockroachDB uses digital certificates, let's first understand what each of these terms means. - -Consider two people: Amy and Rosa, who want to communicate securely over an insecure computer network. The traditional solution is to use symmetric encryption that involves encrypting and decrypting a plaintext message using a shared key. Amy encrypts her message using the key and sends the encrypted message across the insecure channel. Rosa decrypts the message using the same key and reads the message. This seems like a logical solution until you realize that you need a secure communication channel to send the encryption key. - -To solve this problem, cryptographers came up with **asymmetric encryption** to set up a secure communication channel over which an encryption key can be shared. - -### Asymmetric encryption - -Asymmetric encryption involves a pair of keys instead of a single key. The two keys are called the **public key** and the **private key**. The keys consist of very long numbers linked mathematically in a way such that a message encrypted using a public key can only be decrypted using the private key and vice versa. The message cannot be decrypted using the same key that was used to encrypt the message. - -So going back to our example, Amy and Rosa both have their own public-private key pairs. They keep their private keys safe with themselves and publicly distribute their public keys. Now when Amy wants to send a message to Rosa, she requests Rosa's public key, encrypts the message using Rosa’s public key, and sends the encrypted message. Rosa uses her own private key to decrypt the message. - -But what if a malicious imposter intercepts the communication? The imposter might pose as Rosa and send their public key instead of Rosa’s. There's no way for Amy to know that the public key she received isn’t Rosa’s, so she would end up using the imposter's public key to encrypt the message and send it to the imposter. The imposter can use their own private key and decrypt and read the message, thus compromising the secure communication channel between Amy and Rosa. - -To prevent this security risk, Amy needs to be sure that the public key she received was indeed Rosa’s. That’s where the Certificate Authority (CA) comes into the picture. - -### Certificate authority - -Certificate authorities are established entities with their own public and private key pairs. They act as a root of trust and verify the identities of the communicating parties and validate their public keys. CAs can be public and paid entities (e.g., GeoTrust and Comodo), or public and free CAs (e.g., Let’s Encrypt), or your own organizational CA (e.g., CockroachDB CA). The CAs' public keys are typically widely distributed (e.g., your browser comes preloaded with certs from popular CAs like DigiCert, GeoTrust, and so on). - -Think of the CA as the passport authority of a country. When you want to get your passport as your identity proof, you submit an application to your country's passport authority. The application contains important identifying information about you: your name, address, nationality, date of birth, and so on. The passport authority verifies the information they received and validates your identity. They then issue a document - the passport - that can be presented anywhere in the world to verify your identity. For example, the TSA agent at the airport does not know you and has no reason to trust you are who you say you are. However, they trust the passport authority and thus accept your identity as presented on your passport because it has been verified and issued by the passport authority. - -Going back to our example and assuming that we trust the CA, Rosa needs to get her public key verified by the CA. She sends a CSR (Certificate Signing Request) to the CA that contains her public key and relevant identifying information. The CA will verify that it is indeed Rosa’s public key and information, _sign_ the CSR using the CA's own private key, and generate a digital document called the **digital certificate**. In our passport analogy, this is Rosa's passport containing verified identifying information about her and trusted by everyone who trusts the CA. The next time Rosa wants to establish her identity, she will present her digital certificate. - -### Digital certificate - -A public key is shared using a digital certificate signed by a CA using the CA's private key. The digital certificate contains: - -- The certificate owner’s public key -- Information about the certificate owner -- The CA's digital signature - -### Digital signature - -The CA's digital signature works as follows: The certificate contents are put through a mathematical function to create a **hash value**. This hash value is encrypted using the CA's private key to generate the **digital signature**. The digital signature is added to the digital certificate. In our example, the CA adds their digital signature to Rosa's certificate validating her identity and her public key. - -As discussed [earlier](#certificate-authority), the CA's public key is widely distributed. In our example, Amy already has the CA's public key. Now when Rosa presents her digital certificate containing her public key, Amy uses the CA's public key to decrypt the digital signature on Rosa's certificate and gets the hash value encoded in the digital signature. Amy also generates the hash value for the certificate on her own. If the hash values match, then Amy can be sure that the certificate and hence the public key it contains indeed belongs to Rosa; otherwise, she can determine that the communication channel has been compromised and refuse further contact. - -### How it all works together - -Let's see how the digital certificate is used in client-server communication: The client (e.g., a web browser) has the CA certificate (containing the CA's public key). When the client receives a server's certificate signed by the same CA, it can use the CA certificate to verify the server's certificate, thus validating the server's identity, and securely connect to the server. The important thing here is that the client needs to have the CA certificate. If you use your own organizational CA instead of a publicly established CA, you need to make sure you distribute the CA certificate to all the clients. - -## See also - -- [Client Connection Parameters](connection-parameters.html) -- [Manual Deployment](manual-deployment.html) -- [Orchestrated Deployment](orchestration.html) -- [Local Deployment](secure-a-cluster.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/authorization.md b/src/current/v19.2/authorization.md deleted file mode 100644 index fed9925cab6..00000000000 --- a/src/current/v19.2/authorization.md +++ /dev/null @@ -1,516 +0,0 @@ ---- -title: Authorization -summary: Learn about the authorization features for secure CockroachDB clusters. -toc: true ---- - -User authorization is the act of defining access policies for authenticated CockroachDB users. CockroachDB allows you to create, manage, and remove your cluster's [users](#sql-users) and assign SQL-level [privileges](#assign-privileges) to the users. Additionally, if you have an [enterprise license](get-started-with-enterprise-trial.html), you can use [role-based access management (RBAC)](#roles) for simplified user management. - -## SQL users - -A SQL user can interact with a CockroachDB database using the [built-in SQL shell](cockroach-sql.html) or through an application. - -### Create and manage users - -Use the [`CREATE USER`](create-user.html) and [`DROP USER`](drop-user.html) statements to create and remove users, the [`ALTER USER`](alter-user.html) statement to add or change a user's password, the [`GRANT `](grant.html) and [`REVOKE `](revoke.html) statements to manage the user’s privileges, and the [`SHOW USERS`](show-users.html) statement to list users. - -A new user must be granted the required privileges for each database and table that the user needs to access. - -{{site.data.alerts.callout_info}} -By default, a new user belongs to the `public` role and has no privileges other than those assigned to the `public` role. For more information, see [Public role](#public-role). -{{site.data.alerts.end}} - -### `root` user - -The `root` user is created by default for each cluster. The `root` user is assigned to the [`admin` role](#admin-role) and has all privileges across the cluster. - -## Roles - -{{site.data.alerts.callout_info}} -[`CREATE ROLE`](create-role.html) is an enterprise feature. To request a 30-day trial license, see [Get CockroachDB](https://www.cockroachlabs.com/get-cockroachdb/enterprise/). -{{site.data.alerts.end}} - -A role is a group of users and/or other roles for which you can grant or revoke privileges as a whole. To simplify access management, create a role and grant privileges to the role, then create SQL users and grant them membership to the role. - -{{site.data.alerts.callout_info}} -PostgreSQL uses the term "role" to mean either a database user or a group of database users. CockroachDB, however, uses the term "user" to mean an individual database user and "role" to mean a group of database users. -{{site.data.alerts.end}} - -### Create and manage roles - -To create and manage your cluster's roles, use the following statements: - -Statement | Description -----------|------------ -[`CREATE ROLE` (enterprise)](create-role.html) | Create SQL roles. -[`DROP ROLE` (enterprise)](drop-role.html) | Remove one or more SQL roles. -[`GRANT `](grant-roles.html) | Add a role or user as a member to a role. -[`REVOKE `](revoke-roles.html) | Revoke a role or user's membership to a role. -[`GRANT `](grant.html) | Manage each role or user's SQL privileges for interacting with specific databases and tables. -[`REVOKE `](revoke.html) | Revoke privileges from users and/or roles. -[`SHOW ROLES`](show-roles.html) | List the roles for all databases. -[`SHOW GRANTS`](show-grants.html) | List the privileges granted to users. - -### Default roles - -The `admin` and `public` roles exist by default for both core and enterprise clusters. - -#### `admin` role - -The `admin` role is created by default and cannot be dropped. Users belonging to the `admin` role have all privileges for all database objects across the cluster. The `root` user belongs to the `admin` role by default. - -An `admin` user is a member of the `admin` role. Only `admin` users can use [`CREATE ROLE`](create-role.html) and [`DROP ROLE`](drop-role.html). - -To assign a user to the `admin` role: - -{% include copy-clipboard.html %} -~~~ sql -> GRANT admin TO ; -~~~ - -#### `public` role - -All new users and roles belong to the `public` role by default. You can grant and revoke the privileges on the `public` role. - -### Terminology - -#### Role admin - -A `role admin` is a member of the role that's allowed to grant or revoke role membership to other users for that specific role. To create a `role admin`, use [`WITH ADMIN OPTION`](grant-roles.html#grant-the-admin-option). - -{{site.data.alerts.callout_success}} -The terms "`admin` role" and "`role admin`" can be confusing. A user who is a member of the `admin` role has all privileges on all database objects across the entire cluster, whereas a `role admin` has privileges limited to the role they are a member of. Assign the `admin` role to a SQL user if you want the user to have privileges across the cluster. Make a SQL user the `role admin` if you want to limit the user’s privileges to its current role, but with an option to grant or revoke role membership to other users. -{{site.data.alerts.end}} - -#### Direct member - -A user or role that is an immediate member of the role. - -Example: A is a member of B. - -#### Indirect member - -A user or role that is a member of the role by association. - -Example: A is a member of C ... is a member of B where "..." is an arbitrary number of memberships. - -## Privileges - -When a user connects to a database, either via the built-in SQL client or a client driver, CockroachDB checks the user and role's privileges for each statement executed. If the user does not have sufficient privileges for a statement, CockroachDB gives an error. - -### Assign privileges - -Use the [`GRANT `](grant.html) and [`REVOKE `](revoke.html) statements to manage privileges for users and roles. - -Take the following points into consideration while granting privileges to roles and users: - -- When a role or user is granted privileges for a database, new tables created in the database will inherit the privileges, but the privileges can then be changed. To grant privileges to a user on all existing tables in a database, see [Grant privileges on all tables in a database](grant.html#grant-privileges-on-all-tables-in-a-database) - - {{site.data.alerts.callout_info}} - The user does not get privileges to existing tables in the database. - {{site.data.alerts.end}} - -- When a role or user is granted privileges for a table, the privileges are limited to the table. -- In CockroachDB, privileges are granted to users and roles at the database and table levels. They are not yet supported for other granularities such as columns or rows. -- The `root` user automatically belongs to the `admin` role and has the `ALL` privilege for new databases. -- For privileges required by specific statements, see the documentation for the respective [SQL statement](sql-statements.html). - -You can manage the following privileges for databases and tables: - -- `ALL` -- `CREATE` -- `DROP` -- `GRANT` -- `SELECT` -- `INSERT` -- `DELETE` -- `UPDATE` - -## Authorization best practices - -We recommend the following best practices to set up access control for your clusters: - -- Use the `root` user only for database administration tasks such as creating and managing other [users](#sql-users), creating and managing [roles](#roles), and creating and managing databases. Do not use the `root` user for applications; instead, create users with specific [privileges](#assign-privileges) based on your application’s access requirements. -- Enterprise customers: Create roles with specific privileges, create users, and then add the users to the relevant roles. -- Use the ["least privilege model"](https://en.wikipedia.org/wiki/Principle_of_least_privilege) to grant privileges to users and roles. - -## Example - -
      - - -
      - -
      - -The following example uses MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB SQL statements. For more information about the MovR example application and dataset, see [MovR: A Global Vehicle-sharing App](movr.html). - -Let's say we want to create the following access control setup for the `movr` database: - -- One database admin (named `db_admin`) who can perform all database operations for existing tables as well as for tables added in the future. -- One app user (named `app_user`) who can add, read update, and delete vehicles from the `vehicles` table. -- One user (named `report_user`) who can only read the `vehicles` table. - -1. Use the [`cockroach demo`](cockroach-demo.html) command to load the `movr` database and dataset into a CockroachDB cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach demo - ~~~ - -2. Create the database admin (named `db_admin`) who can perform all database operations for existing tables as well as for tables added in the future: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER db_admin; - ~~~ - -3. Grant all privileges on database `movr` to user `db_admin`: - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT ALL ON DATABASE movr TO db_admin; - ~~~ - -4. Grant all privileges on all tables in database `movr` to user `db_admin`: - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT ALL ON TABLE * TO db_admin; - ~~~ - -5. Verify that `db_admin` has all privileges: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW GRANTS FOR db_admin; - ~~~ - - ~~~ - database_name | schema_name | table_name | grantee | privilege_type - +---------------+--------------------+----------------------------+----------+----------------+ - movr | crdb_internal | NULL | db_admin | ALL - movr | information_schema | NULL | db_admin | ALL - movr | pg_catalog | NULL | db_admin | ALL - movr | public | NULL | db_admin | ALL - movr | public | promo_codes | db_admin | ALL - movr | public | rides | db_admin | ALL - movr | public | user_promo_codes | db_admin | ALL - movr | public | users | db_admin | ALL - movr | public | vehicle_location_histories | db_admin | ALL - movr | public | vehicles | db_admin | ALL - (10 rows) - ~~~ - -6. As the `root` user, create a SQL user named `app_user` with permissions to add, read, update, and delete vehicles in the `vehicles` table: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER app_user; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT INSERT, DELETE, UPDATE, SELECT ON vehicles TO app_user; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW GRANTS FOR app_user; - ~~~ - - ~~~ - database_name | schema_name | table_name | grantee | privilege_type - +---------------+-------------+------------+----------+----------------+ - movr | public | vehicles | app_user | DELETE - movr | public | vehicles | app_user | INSERT - movr | public | vehicles | app_user | SELECT - movr | public | vehicles | app_user | UPDATE - (4 rows) - ~~~ - -7. As the `root` user, create a SQL user named `report_user` with permissions to only read from the `vehicles` table: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER report_user; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT SELECT ON vehicles TO report_user; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW GRANTS FOR report_user; - ~~~ - - ~~~ - database_name | schema_name | table_name | grantee | privilege_type - +---------------+-------------+------------+-------------+----------------+ - movr | public | vehicles | report_user | SELECT - (1 row) - ~~~ - -
      - -
      - -{{site.data.alerts.callout_info}} -The [`CREATE ROLE`](create-role.html) command used in this example is an enterprise-only feature. To request a 30-day trial license, see [Get CockroachDB](https://www.cockroachlabs.com/get-cockroachdb/enterprise/). - -Note that [`GRANT `](grant-roles.html) does not require an enterprise license. All users can grant the [default roles](#default-roles). -{{site.data.alerts.end}} - -The following example uses MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB [SQL statements](sql-statements.html). For more information about the MovR example application and dataset, see [MovR: A Global Vehicle-sharing App](movr.html). - -Let's say we want to create the following access control setup for the `movr` database: - -- Two database admins (named `db_admin_1` and `db_admin_2`) who can perform all database operations for existing tables as well as for tables added in the future. -- Three app users (named `app_user_1`, `app_user_2`, and `app_user_3`) who can add, read update, and delete vehicles from the `vehicles` table. -- Five users (named `report_user_1`, `report_user_2`, `report_user_3`, `report_user_4`, `report_user_5`) who can only read the `vehicles` table. - -1. Use the [`cockroach demo`](cockroach-demo.html) command to load the `movr` database and dataset into a CockroachDB cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach demo - ~~~ - -{{site.data.alerts.callout_info}} -Each `cockroach demo` instance runs with a temporary enterprise license that enables you to try out enterprise features such as [`CREATE ROLE`](create-role.html). The license expires after an hour. -{{site.data.alerts.end}} - -2. Create the database admin role (named `db_admin_role`) whose members can perform all database operations for existing tables as well as for tables added in the future: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE ROLE db_admin_role; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ROLES; - ~~~ - - ~~~ - role_name - +---------------+ - admin - db_admin_role - (2 rows) - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT ALL ON DATABASE movr TO db_admin_role; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT ALL ON TABLE * TO db_admin_role; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW GRANTS ON DATABASE movr; - ~~~ - - ~~~ - database_name | schema_name | grantee | privilege_type - +---------------+--------------------+---------------+----------------+ - movr | crdb_internal | admin | ALL - movr | crdb_internal | db_admin_role | ALL - movr | crdb_internal | root | ALL - movr | information_schema | admin | ALL - movr | information_schema | db_admin_role | ALL - movr | information_schema | root | ALL - movr | pg_catalog | admin | ALL - movr | pg_catalog | db_admin_role | ALL - movr | pg_catalog | root | ALL - movr | public | admin | ALL - movr | public | db_admin_role | ALL - movr | public | root | ALL - (12 rows) - ~~~ - -3. Create two database admin users (named `db_admin_1` and `db_admin_2`) and grant them membership to the `db_admin_role` role: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER db_admin_1; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER db_admin_2; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT db_admin_role TO db_admin_1, db_admin_2; - ~~~ - -4. Create a role named `app_user_role` whose members can add, read update, and delete vehicles to the `vehicles` table. - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE ROLE app_user_role; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ROLES; - ~~~ - - ~~~ - role_name - +---------------+ - admin - app_user_role - db_admin_role - (3 rows) - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT INSERT, UPDATE, DELETE, SELECT ON TABLE vehicles TO app_user_role; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW GRANTS ON vehicles; - ~~~ - - ~~~ - database_name | schema_name | table_name | grantee | privilege_type - +---------------+-------------+------------+---------------+----------------+ - movr | public | vehicles | admin | ALL - movr | public | vehicles | app_user_role | DELETE - movr | public | vehicles | app_user_role | INSERT - movr | public | vehicles | app_user_role | SELECT - movr | public | vehicles | app_user_role | UPDATE - movr | public | vehicles | db_admin_role | ALL - movr | public | vehicles | root | ALL - (7 rows) - ~~~ - -5. Create three app users (named `app_user_1`, `app_user_2`, and `app_user_3`) and grant them membership to the `app_user_role` role: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER app_user_1; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER app_user_2; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER app_user_3; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT app_user_role TO app_user_1, app_user_2, app_user_3; - ~~~ - -6. Create a role named `report_user_role` whose members can only read the `vehicles` table. - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE ROLE report_user_role; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ROLES; - ~~~ - - ~~~ - role_name - +------------------+ - admin - app_user_role - db_admin_role - report_user_role - (4 rows) - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT SELECT ON vehicles TO report_user_role; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW GRANTS ON vehicles; - ~~~ - - ~~~ - database_name | schema_name | table_name | grantee | privilege_type - +---------------+-------------+------------+------------------+----------------+ - movr | public | vehicles | admin | ALL - movr | public | vehicles | app_user_role | DELETE - movr | public | vehicles | app_user_role | INSERT - movr | public | vehicles | app_user_role | SELECT - movr | public | vehicles | app_user_role | UPDATE - movr | public | vehicles | db_admin_role | ALL - movr | public | vehicles | report_user_role | SELECT - movr | public | vehicles | root | ALL - (8 rows) - ~~~ - -7. Create five report users (named `report_user_1`, `report_user_2`, `report_user_3`, `report_user_4`, and `report_user_5`) and grant them membership to the `report_user_role` role: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER report_user_1; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER report_user_2; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER report_user_3; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER report_user_4; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER report_user_5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT report_user_role TO report_user_1, report_user_2, report_user_3, report_user_4, report_user_5; - ~~~ - -
      - -## See also - -- [Client Connection Parameters](connection-parameters.html) -- [SQL Statements](sql-statements.html) -- [`CREATE USER`](create-user.html) -- [`ALTER USER`](alter-user.html) -- [`DROP USER`](drop-user.html) -- [`SHOW USERS`](show-users.html) -- [`CREATE ROLE` (enterprise)](create-role.html) -- [`DROP ROLE` (enterprise)](drop-role.html) -- [`SHOW ROLES`](show-roles.html) -- [`GRANT `](grant.html) -- [`GRANT `](grant-roles.html) -- [`REVOKE `](revoke.html) -- [`REVOKE `](revoke-roles.html) -- [`SHOW GRANTS`](show-grants.html) diff --git a/src/current/v19.2/backup-and-restore.md b/src/current/v19.2/backup-and-restore.md deleted file mode 100644 index 5814eb9562b..00000000000 --- a/src/current/v19.2/backup-and-restore.md +++ /dev/null @@ -1,230 +0,0 @@ ---- -title: Back up and Restore Data -summary: Learn how to back up and restore a CockroachDB database. -toc: true ---- - -Because CockroachDB is designed with high fault tolerance, backups are primarily needed for disaster recovery (i.e., if your cluster loses a majority of its nodes). Isolated issues (such as small-scale node outages) do not require any intervention. However, as an operational best practice, we recommend taking regular backups of your data. - -Based on your [license type](https://www.cockroachlabs.com/pricing/), CockroachDB offers two methods to back up and restore your cluster's data: Enterprise and Core. - -## Perform Enterprise backup and restore - -If you have an [Enterprise license](enterprise-licensing.html), you can use the [`BACKUP`][backup] statement to efficiently back up your cluster's schemas and data to popular cloud services such as AWS S3, Google Cloud Storage, or NFS, and the [`RESTORE`][restore] statement to efficiently restore schema and data as necessary. - -### Manual full backups - -In most cases, it's recommended to use the [`BACKUP`][backup] command to take full nightly backups of each database in your cluster: - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP DATABASE TO ''; -~~~ - -If it's ever necessary, you can then use the [`RESTORE`][restore] command to restore a database: - -{% include copy-clipboard.html %} -~~~ sql -> RESTORE DATABASE FROM ''; -~~~ - -### Manual full and incremental backups - -If a database increases to a size where it is no longer feasible to take nightly full backups, you might want to consider taking periodic full backups (e.g., weekly) with nightly incremental backups. Incremental backups are storage efficient and faster than full backups for larger databases. - -Periodically run the [`BACKUP`][backup] command to take a full backup of your database: - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP DATABASE TO ''; -~~~ - -Then create nightly incremental backups based off of the full backups you've already created. - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP DATABASE TO 'incremental_backup_location' -INCREMENTAL FROM '', ''; -~~~ - -If it's ever necessary, you can then use the [`RESTORE`][restore] command to restore a database: - -{% include copy-clipboard.html %} -~~~ sql -> RESTORE FROM '', ''; -~~~ - -{{site.data.alerts.callout_success}} -[Restoring from incremental backups](restore.html#restore-from-incremental-backups) requires previous full and incremental backups. -{{site.data.alerts.end}} - -### Automated full and incremental backups - -You can automate your backups using scripts and your preferred method of automation, such as cron jobs. - -For your reference, we have created this [sample backup script](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{ page.version.version }}/prod-deployment/backup.sh) that you can customize to automate your backups. - -In the sample script, configure the day of the week for which you want to create full backups. Running the script daily will create a full backup on the configured day, and on other days, it'll create incremental backups. The script tracks the recently created backups in a separate file titled `backup.txt` and uses this file as a base for the subsequent incremental backups. - -1. Download the [sample backup script](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{ page.version.version }}/prod-deployment/backup.sh): - - {% include copy-clipboard.html %} - ~~~ shell - $ wget -qO- https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{ page.version.version }}/prod-deployment/backup.sh - ~~~ - - Alternatively, you can create the file yourself and copy the script into it: - - {% include copy-clipboard.html %} - ~~~ shell - #!/bin/bash - - set -euo pipefail - - # This script creates full backups when run on the configured - # day of the week and incremental backups when run on other days, and tracks - # recently created backups in a file to pass as the base for incremental backups. - - full_day="" # Must match (including case) the output of `LC_ALL=C date +%A`. - what="DATABASE " # The name of the database you want to back up. - base="/backups" # The URL where you want to store the backup. - extra="" # Any additional parameters that need to be appended to the BACKUP URI (e.g., AWS key params). - recent=recent_backups.txt # File in which recent backups are tracked. - backup_parameters= # e.g., "WITH revision_history" - - # Customize the `cockroach sql` command with `--host`, `--certs-dir` or `--insecure`, and additional flags as needed to connect to the SQL client. - runsql() { cockroach sql --insecure -e "$1"; } - - destination="${base}/$(date +"%Y%m%d-%H%M")${extra}" - - prev= - while read -r line; do - [[ "$prev" ]] && prev+=", " - prev+="'$line'" - done < "$recent" - - if [[ "$(LC_ALL=C date +%A)" = "$full_day" || ! "$prev" ]]; then - runsql "BACKUP $what TO '$destination' AS OF SYSTEM TIME '-1m' $backup_parameters" - echo "$destination" > "$recent" - else - destination="${base}/$(date +"%Y%m%d-%H%M")-inc${extra}" - runsql "BACKUP $what TO '$destination' AS OF SYSTEM TIME '-1m' INCREMENTAL FROM $prev $backup_parameters" - echo "$destination" >> "$recent" - fi - - echo "backed up to ${destination}" - ~~~ - -2. In the sample backup script, customize the values for the following variables: - - Variable | Description - -----|------------ - `full_day` | The day of the week on which you want to take a full backup. - `what` | The name of the database you want to back up (i.e., create backups of all tables and views in the database). - `base` | The URL where you want to store the backup.

      URL format: `[scheme]://[host]/[path]`

      For information about the components of the URL, see [Backup File URLs](backup.html#backup-file-urls). - `extra`| The parameters required for the storage.

      Parameters format: `?[parameters]`

      For information about the storage parameters, see [Backup File URLs](backup.html#backup-file-urls). - `backup_parameters` | Additional [backup parameters](backup.html#parameters) you might want to specify. - - Also customize the `cockroach sql` command with `--host`, `--certs-dir` or `--insecure`, and [additional flags](cockroach-sql.html#flags) as required. - -3. Change the file permissions to make the script executable: - - {% include copy-clipboard.html %} - ~~~ shell - $ chmod +x backup.sh - ~~~ - -4. Run the backup script: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./backup.sh - ~~~ - -{{site.data.alerts.callout_info}} -If you miss an incremental backup, delete the `recent_backups.txt` file and run the script. It'll take a full backup for that day and incremental backups for subsequent days. -{{site.data.alerts.end}} - -### Locality-aware backup and restore - -New in v19.2: You can create locality-aware backups such that each node writes files only to the backup destination that matches the [node locality](configure-replication-zones.html#descriptive-attributes-assigned-to-nodes) configured at [node startup](cockroach-start.html). - -This is useful for: - -- Reducing cloud storage data transfer costs by keeping data within cloud regions. -- Helping you comply with data domiciling requirements. - -#### How it works - -A locality-aware backup is specified by a list of URIs, each of which has a `COCKROACH_LOCALITY` URL parameter whose single value is either `default` or a single locality key-value pair such as `region=us-east`. At least one `COCKROACH_LOCALITY` must be the `default`. Given a list of URIs that together contain the locations of all of the files for a single locality-aware backup, [`RESTORE`][restore] can read in that backup. - -During locality-aware backups, backup file placement is determined by leaseholder placement, as each node is responsible for backing up the ranges for which it is the leaseholder. Nodes write files to the backup storage location whose locality matches their own node localities, with a preference for more specific values in the locality hierarchy. If there is no match, the `default` locality is used. - -{{site.data.alerts.callout_info}} -The list of URIs passed to [`RESTORE`][restore] may be different from the URIs originally passed to [`BACKUP`][backup]. This is because the files of a locality-aware backup can be moved to different locations, or even consolidated into the same location. The only restriction is that all the files originally written to the same location must remain together. In order for [`RESTORE`][restore] to succeed, all of the files originally written during [`BACKUP`][backup] must be accounted for in the list of location URIs provided. -{{site.data.alerts.end}} - -#### Usage - -For example, to create a locality-aware backup where nodes with the locality `region=us-west` write backup files to `s3://us-west-bucket`, and all other nodes write to `s3://us-east-bucket` by default, run: - -{% include copy-clipboard.html %} -~~~ sql -BACKUP DATABASE foo TO ('s3://us-east-bucket?COCKROACH_LOCALITY=default', 's3://us-west-bucket?COCKROACH_LOCALITY=region%3Dus-west'); -~~~ - -To restore the backup created above, run the statement below. Note that the first URI in the list has to be the URI specified as the `default` URI when the backup was created. If you have moved your backups to a different location since the backup was originally taken, the first URI must be the new location of the files originally written to the `default` location. - -{% include copy-clipboard.html %} -~~~ sql -RESTORE DATABASE foo FROM ('s3://us-east-bucket', 's3://us-west-bucket'); -~~~ - -A list of multiple URIs (surrounded by parentheses) specifying a locality-aware backup can also be used in place of any incremental backup URI in [`RESTORE`][restore]. If the original backup was an incremental backup, it can be restored using: - -{% include copy-clipboard.html %} -~~~ sql -RESTORE DATABASE foo FROM 's3://other-full-backup-uri', ('s3://us-east-bucket', 's3://us-west-bucket'); -~~~ - -For more detailed examples, see [Create locality-aware backups](backup.html#create-locality-aware-backups) and [Restore from a locality-aware backup based on node locality](restore.html#restore-from-a-locality-aware-backup). - -{{site.data.alerts.callout_info}} -The locality query string parameters must be [URL-encoded](https://en.wikipedia.org/wiki/Percent-encoding) as shown below. - -[`RESTORE`][restore] is not truly locality-aware; while restoring from backups, a node may read from a store that does not match its locality. This can happen because [`BACKUP`][backup] does not back up [zone configurations](configure-replication-zones.html), so [`RESTORE`][restore] has no way of knowing how to take node localities into account when restoring data from a backup. -{{site.data.alerts.end}} - -## Perform Core backup and restore - -In case you do not have an Enterprise license, you can perform a Core backup. Run the [`cockroach dump`](cockroach-dump.html) command to dump all the tables in the database to a new file (`backup.sql` in the following example): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach dump > backup.sql -~~~ - -To restore a database from a Core backup, [use the `cockroach sql` command to execute the statements in the backup file](cockroach-dump.html#restore-a-table-from-a-backup-file): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --database=[database name] < backup.sql -~~~ - -{{site.data.alerts.callout_success}} -If you created a backup from another database and want to import it into CockroachDB, see [Import data](migration-overview.html). -{{site.data.alerts.end}} - -## See also - -- [`BACKUP`][backup] -- [`RESTORE`][restore] -- [`SQL DUMP`](cockroach-dump.html) -- [`IMPORT`](migration-overview.html) -- [Use the Built-in SQL Client](cockroach-sql.html) -- [Other Cockroach Commands](cockroach-commands.html) - - - -[backup]: backup.html -[restore]: restore.html diff --git a/src/current/v19.2/backup.md b/src/current/v19.2/backup.md deleted file mode 100644 index 1b115e91d2b..00000000000 --- a/src/current/v19.2/backup.md +++ /dev/null @@ -1,287 +0,0 @@ ---- -title: BACKUP -summary: Back up your CockroachDB cluster to a cloud storage services such as AWS S3, Google Cloud Storage, or other NFS. -toc: true ---- - -{{site.data.alerts.callout_danger}} -The `BACKUP` feature is only available to [enterprise](https://www.cockroachlabs.com/product/cockroachdb/) users. For non-enterprise backups, see [`cockroach dump`](cockroach-dump.html). -{{site.data.alerts.end}} - -CockroachDB's `BACKUP` [statement](sql-statements.html) allows you to create full or incremental backups of your cluster's schema and data that are consistent as of a given timestamp. Backups can be with or without [revision history](backup.html#backups-with-revision-history). - -Because CockroachDB is designed with high fault tolerance, these backups are designed primarily for disaster recovery (i.e., if your cluster loses a majority of its nodes) through [`RESTORE`](restore.html). Isolated issues (such as small-scale node outages) do not require any intervention. - - -## Functional details - -### Backup targets - -You can backup entire tables (which automatically includes their indexes) or [views](views.html). Backing up a database simply backs up all of its tables and views. - -{{site.data.alerts.callout_info}} -`BACKUP` only offers table-level granularity; it _does not_ support backing up subsets of a table. -{{site.data.alerts.end}} - -### Object dependencies - -Dependent objects must be backed up at the same time as the objects they depend on. - -Object | Depends On --------|----------- -Table with [foreign key](foreign-key.html) constraints | The table it `REFERENCES`; however, this dependency can be [removed during the restore](restore.html#skip_missing_foreign_keys). -Table with a [sequence](create-sequence.html) | The sequence it uses; however, this dependency can be [removed during the restore](restore.html#skip_missing_sequences). -[Views](views.html) | The tables used in the view's `SELECT` statement. -[Interleaved tables](interleave-in-parent.html) | The parent table in the [interleaved hierarchy](interleave-in-parent.html#interleaved-hierarchy). - -### Users and privileges - -The `system.users` table stores your users and their passwords. To restore your users, you must first backup the `system.users` table, and then use [this procedure](restore.html#restoring-users-from-system-users-backup). - -Restored tables inherit privilege grants from the target database; they do not preserve privilege grants from the backed up table because the restoring cluster may have different users. - -Table-level privileges must be [granted to users](grant.html) after the restore is complete. - -### Backup types - -CockroachDB offers two types of backups: full and incremental. - -#### Full backups - -Full backups contain an unreplicated copy of your data and can always be used to restore your cluster. These files are roughly the size of your data and require greater resources to produce than incremental backups. You can take full backups as of a given timestamp and (optionally) include the available [revision history](backup.html#backups-with-revision-history). - -#### Incremental backups - -Incremental backups are smaller and faster to produce than full backups because they contain only the data that has changed since a base set of backups you specify (which must include one full backup, and can include many incremental backups). You can take incremental backups either as of a given timestamp or with full [revision history](backup.html#backups-with-revision-history). - -**Note the following restriction:** Incremental backups can only be created within the garbage collection period of the base backup's most recent timestamp. This is because incremental backups are created by finding which data has been created or modified since the most recent timestamp in the base backup––that timestamp data, though, is deleted by the garbage collection process. - -You can configure garbage collection periods using the `ttlseconds` [replication zone setting](configure-replication-zones.html). - -### Backups with revision history - -{% include {{ page.version.version }}/misc/beta-warning.md %} - -You can create full or incremental backups with revision history: - -- Taking full backups with revision history allows you to back up every change made within the garbage collection period leading up to and including the given timestamp. -- Taking incremental backups with revision history allows you to back up every change made since the last backup and within the garbage collection period leading up to and including the given timestamp. You can take incremental backups with revision history even when your previous full or incremental backups were taken without revision history. - -You can configure garbage collection periods using the `ttlseconds` [replication zone setting](configure-replication-zones.html). Taking backups with revision history allows for point-in-time restores within the revision history. - -## Performance - -The `BACKUP` process minimizes its impact to the cluster's performance by distributing work to all nodes. Each node backs up only a specific subset of the data it stores (those for which it serves writes; more details about this architectural concept forthcoming), with no two nodes backing up the same data. - -For best performance, we also recommend always starting backups with a specific [timestamp](timestamp.html) at least 10 seconds in the past. For example: - -~~~ sql -> BACKUP...AS OF SYSTEM TIME '-10s'; -~~~ - -This improves performance by decreasing the likelihood that the `BACKUP` will be [retried because it contends with other statements/transactions](transactions.html#transaction-retries). However, because `AS OF SYSTEM TIME` returns historical data, your reads might be stale. - -## Automating backups - -We recommend automating daily backups of your cluster. - -To automate backups, you must have a client send the `BACKUP` statement to the cluster. - -Once the backup is complete, your client will receive a `BACKUP` response. - -## Viewing and controlling backups jobs - -After CockroachDB successfully initiates a backup, it registers the backup as a job, which you can view with [`SHOW JOBS`](show-jobs.html). - -After the backup has been initiated, you can control it with [`PAUSE JOB`](pause-job.html), [`RESUME JOB`](resume-job.html), and [`CANCEL JOB`](cancel-job.html). - -{{site.data.alerts.callout_info}} -If initiated correctly, the statement returns when the backup is finished or if it encounters an error. In some cases, the backup can continue after an error has been returned (the error message will tell you that the backup has resumed in background). -{{site.data.alerts.end}} - -## Synopsis - -
      -{% include {{ page.version.version }}/sql/diagrams/backup.html %} -
      - -{{site.data.alerts.callout_info}} -The `BACKUP` statement cannot be used within a [transaction](transactions.html). -{{site.data.alerts.end}} - -## Required privileges - -Only members of the `admin` role can run `BACKUP`. By default, the `root` user belongs to the `admin` role. - -## Parameters - -| Parameter | Description | -|-----------|-------------| -| `table_pattern` | The table or [view](views.html) you want to back up. | -| `name` | The name of the database you want to back up (i.e., create backups of all tables and views in the database).| -| `destination` | The URL where you want to store the backup.

      For information about this URL structure, see [Backup File URLs](#backup-file-urls). | -| `AS OF SYSTEM TIME timestamp` | Back up data as it existed as of [`timestamp`](as-of-system-time.html). The `timestamp` must be more recent than your cluster's last garbage collection (which defaults to occur every 25 hours, but is [configurable per table](configure-replication-zones.html#replication-zone-variables)). | -| `WITH revision_history` | Create a backup with full [revision history](backup.html#backups-with-revision-history) that records every change made to the cluster within the garbage collection period leading up to and including the given timestamp. | -| `INCREMENTAL FROM full_backup_location` | Create an incremental backup using the full backup stored at the URL `full_backup_location` as its base. For information about this URL structure, see [Backup File URLs](#backup-file-urls).

      **Note:** It is not possible to create an incremental backup if one or more tables were [created](create-table.html), [dropped](drop-table.html), or [truncated](truncate.html) after the full backup. In this case, you must create a new [full backup](#full-backups). | -| `incremental_backup_location` | Create an incremental backup that includes all backups listed at the provided URLs.

      Lists of incremental backups must be sorted from oldest to newest. The newest incremental backup's timestamp must be within the table's garbage collection period.

      For information about this URL structure, see [Backup File URLs](#backup-file-urls).

      For more information about garbage collection, see [Configure Replication Zones](configure-replication-zones.html#replication-zone-variables). | - -### Backup file URLs - -We will use the URL provided to construct a secure API call to the service you specify. The path to each backup must be unique, and the URL for your backup's destination/locations must use the following format: - -{% include {{ page.version.version }}/misc/external-urls.md %} - -## Examples - -Per our guidance in the [Performance](#performance) section, we recommend starting backups from a time at least 10 seconds in the past using [`AS OF SYSTEM TIME`](as-of-system-time.html). - -### Backup a single table or view - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP bank.customers \ -TO 'gs://acme-co-backup/database-bank-2017-03-27-weekly' \ -AS OF SYSTEM TIME '-10s'; -~~~ - -### Backup multiple tables - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP bank.customers, bank.accounts \ -TO 'gs://acme-co-backup/database-bank-2017-03-27-weekly' \ -AS OF SYSTEM TIME '-10s'; -~~~ - -### Backup an entire database - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP DATABASE bank \ -TO 'gs://acme-co-backup/database-bank-2017-03-27-weekly' \ -AS OF SYSTEM TIME '-10s'; -~~~ - -### Backup with revision history - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP DATABASE bank \ -TO 'gs://acme-co-backup/database-bank-2017-03-27-weekly' \ -AS OF SYSTEM TIME '-10s' WITH revision_history; -~~~ - -### Create incremental backups - -Incremental backups must be based off of full backups you've already created. - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP DATABASE bank \ -TO 'gs://acme-co-backup/db/bank/2017-03-29-nightly' \ -AS OF SYSTEM TIME '-10s' \ -INCREMENTAL FROM 'gs://acme-co-backup/database-bank-2017-03-27-weekly', 'gs://acme-co-backup/database-bank-2017-03-28-nightly'; -~~~ - -### Create incremental backups with revision history - -{% include copy-clipboard.html %} -~~~ sql -> BACKUP DATABASE bank \ -TO 'gs://acme-co-backup/database-bank-2017-03-29-nightly' \ -AS OF SYSTEM TIME '-10s' \ -INCREMENTAL FROM 'gs://acme-co-backup/database-bank-2017-03-27-weekly', 'gs://acme-co-backup/database-bank-2017-03-28-nightly' WITH revision_history; -~~~ - -### Create locality-aware backups - -New in v19.2: You can create locality-aware backups such that each node writes files only to the backup destination that matches the [node locality](configure-replication-zones.html#descriptive-attributes-assigned-to-nodes) configured at [node startup](cockroach-start.html). - -A locality-aware backup is specified by a list of URIs, each of which has a `COCKROACH_LOCALITY` URL parameter whose single value is either `default` or a single locality key-value pair such as `region=us-east`. At least one `COCKROACH_LOCALITY` must be the `default`. - -Backup file placement is determined by leaseholder placement, as each node is responsible for backing up the ranges for which it is the leaseholder. Nodes write files to the backup storage location whose locality matches their own node localities, with a preference for more specific values in the locality hierarchy. If there is no match, the `default` locality is used. - -{{site.data.alerts.callout_info}} -Note that the locality query string parameters must be [URL-encoded](https://en.wikipedia.org/wiki/Percent-encoding) as shown below. -{{site.data.alerts.end}} - -#### Example - Create a locality-aware backup - -For example, to create a locality-aware backup where nodes with the locality `region=us-west` write backup files to `s3://us-west-bucket`, and all other nodes write to `s3://us-east-bucket` by default, run: - -{% include copy-clipboard.html %} -~~~ sql -BACKUP DATABASE bank TO ('s3://us-east-bucket?COCKROACH_LOCALITY=default', 's3://us-west-bucket?COCKROACH_LOCALITY=region%3Dus-west'); -~~~ - -The backup created above can be restored by running: - -{% include copy-clipboard.html %} -~~~ sql -RESTORE DATABASE bank FROM ('s3://us-east-bucket', 's3://us-west-bucket'); -~~~ - -#### Example - Create an incremental locality-aware backup - -To make an incremental locality-aware backup from a full locality-aware backup, the syntax is just like for [regular incremental backups](#create-incremental-backups): - -{% include copy-clipboard.html %} -~~~ sql -BACKUP DATABASE foo TO (${uri_1}, ${uri_2}, ...) INCREMENTAL FROM ${full_backup_uri} ...; -~~~ - -For example, to create an incremental locality-aware backup from a previous full locality-aware backup where nodes with the locality `region=us-west` write backup files to `s3://us-west-bucket`, and all other nodes write to `s3://us-east-bucket` by default, run: - -{% include copy-clipboard.html %} -~~~ sql -BACKUP DATABASE bank TO -('s3://us-east-bucket/database-bank-2019-10-08-nightly?COCKROACH_LOCALITY=default', 's3://us-west-bucket/database-bank-2019-10-08-nightly?COCKROACH_LOCALITY=region%3Dus-west') -INCREMENTAL FROM 's3://us-east-bucket/database-bank-2019-10-07-weekly'; -~~~ - -{{site.data.alerts.callout_info}} -Note that only the backup URIs you set as the `default` when you created the previous backup(s) are needed in the `INCREMENTAL FROM` clause of your incremental `BACKUP` statement (as shown in the example). This is because the `default` destination for a locality-aware backup contains a manifest file that contains all the metadata required to create additional incremental backups based on it. -{{site.data.alerts.end}} - -#### Example - Create an incremental locality-aware backup from a previous locality-aware backup - -To make an incremental locality-aware backup from another locality-aware backup, the syntax is as follows: - -{% include copy-clipboard.html %} -~~~ sql -BACKUP DATABASE foo TO ({uri_1}, {uri_2}, ...) INCREMENTAL FROM {full_backup}, {incr_backup_1}, {incr_backup_2}, ...; -~~~ - -For example, let's say you normally run a full backup every Monday, followed by incremental backups on the remaining days of the week. - -By default, all nodes send their backups to your `s3://us-east-bucket`, except for nodes in `region=us-west`, which will send their backups to `s3://us-west-bucket`. - -If today is Thursday, October 10th, 2019, your `BACKUP` statement will list the following backup URIs: - -- The full locality-aware backup URI from Monday, e.g., - - `s3://us-east-bucket/database-bank-2019-10-07-weekly` -- The incremental backup URIs from Tuesday and Wednesday, e.g., - - `s3://us-east-bucket/database-bank-2019-10-08-nightly` - - `s3://us-east-bucket/database-bank-2019-10-09-nightly` - -Given the above, to take the incremental locality-aware backup scheduled for today (Thursday), you will run: - -{% include copy-clipboard.html %} -~~~ sql -BACKUP DATABASE bank TO - ('s3://us-east-bucket/database-bank-2019-10-10-nightly?COCKROACH_LOCALITY=default', 's3://us-west-bucket/database-bank-2019-10-10-nightly?COCKROACH_LOCALITY=region%3Dus-west') -INCREMENTAL FROM - 's3://us-east-bucket/database-bank-2019-10-07-weekly', - 's3://us-east-bucket/database-bank-2019-10-08-nightly', - 's3://us-east-bucket/database-bank-2019-10-09-nightly'; -~~~ - -{{site.data.alerts.callout_info}} -Note that only the backup URIs you set as the `default` when you created the previous backup(s) are needed in the `INCREMENTAL FROM` clause of your incremental `BACKUP` statement (as shown in the example). This is because the `default` destination for a locality-aware backup contains a manifest file that contains all the metadata required to create additional incremental backups based on it. -{{site.data.alerts.end}} - -## See also - -- [`RESTORE`](restore.html) -- [Backup and Restore Data](backup-and-restore.html) -- [Configure Replication Zones](configure-replication-zones.html) diff --git a/src/current/v19.2/begin-transaction.md b/src/current/v19.2/begin-transaction.md deleted file mode 100644 index e0f94bab96b..00000000000 --- a/src/current/v19.2/begin-transaction.md +++ /dev/null @@ -1,170 +0,0 @@ ---- -title: BEGIN -summary: Initiate a SQL transaction with the BEGIN statement in CockroachDB. -toc: true ---- - -The `BEGIN` [statement](sql-statements.html) initiates a [transaction](transactions.html), which either successfully executes all of the statements it contains or none at all. - -{{site.data.alerts.callout_danger}} -When using transactions, your application should include logic to [retry transactions](transactions.html#transaction-retries) that are aborted to break a dependency cycle between concurrent transactions. -{{site.data.alerts.end}} - - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/begin_transaction.html %} -
      - -## Required privileges - -No [privileges](authorization.html#assign-privileges) are required to initiate a transaction. However, privileges are required for each statement within a transaction. - -## Aliases - -In CockroachDB, the following are aliases for the `BEGIN` statement: - -- `BEGIN TRANSACTION` -- `START TRANSACTION` - -## Parameters - - Parameter | Description ------------|------------- -`PRIORITY` | If you do not want the transaction to run with `NORMAL` priority, you can set it to `LOW` or `HIGH`.

      Transactions with higher priority are less likely to need to be retried.

      For more information, see [Transactions: Priorities](transactions.html#transaction-priorities).

      **Default**: `NORMAL` -`READ` | Set the transaction access mode to `READ ONLY` or `READ WRITE`. The current transaction access mode is also exposed as the [session variable](show-vars.html) `transaction_read_only`.

      **Default**: `READ WRITE` -`AS OF SYSTEM TIME` | Execute the transaction using the database contents "as of" a specified time in the past.

      The `AS OF SYSTEM TIME` clause can be used only when the transaction is read-only. If the transaction contains any writes, or if the `READ WRITE` mode is specified, an error will be returned.

      For more information, see [AS OF SYSTEM TIME](as-of-system-time.html).

      - - CockroachDB now only supports `SERIALIZABLE` isolation, so transactions can no longer be meaningfully set to any other `ISOLATION LEVEL`. In previous versions of CockroachDB, you could set transactions to `SNAPSHOT` isolation, but that feature has been removed. - -## Examples - -### Begin a transaction - -#### Use default settings - -Without modifying the `BEGIN` statement, the transaction uses `SERIALIZABLE` isolation and `NORMAL` priority. - -{% include copy-clipboard.html %} -~~~ sql -> BEGIN; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SAVEPOINT cockroach_restart; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE products SET inventory = 0 WHERE sku = '8675309'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders (customer, sku, status) VALUES (1001, '8675309', 'new'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> RELEASE SAVEPOINT cockroach_restart; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> COMMIT; -~~~ - -{{site.data.alerts.callout_danger}}This example assumes you're using client-side intervention to handle transaction retries.{{site.data.alerts.end}} - -#### Change priority - -You can set a transaction's priority to `LOW` or `HIGH`. - -{% include copy-clipboard.html %} -~~~ sql -> BEGIN PRIORITY HIGH; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SAVEPOINT cockroach_restart; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE products SET inventory = 0 WHERE sku = '8675309'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders (customer, sku, status) VALUES (1001, '8675309', 'new'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> RELEASE SAVEPOINT cockroach_restart; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> COMMIT; -~~~ - -You can also set a transaction's priority with [`SET TRANSACTION`](set-transaction.html). - -{{site.data.alerts.callout_danger}} -This example assumes you're using [client-side intervention to handle transaction retries](transactions.html#client-side-intervention). -{{site.data.alerts.end}} - -### Use the `AS OF SYSTEM TIME` option - -You can execute the transaction using the database contents "as of" a specified time in the past. - -{% include {{ page.version.version }}/sql/begin-transaction-as-of-system-time-example.md %} - -{{site.data.alerts.callout_success}} -You can also use the [`SET TRANSACTION`](set-transaction.html#use-the-as-of-system-time-option) statement inside the transaction to achieve the same results. This syntax is easier to use from [drivers and ORMs](install-client-drivers.html). -{{site.data.alerts.end}} - -### Begin a transaction with automatic retries - -CockroachDB will [automatically retry](transactions.html#transaction-retries) all transactions that contain both `BEGIN` and `COMMIT` in the same batch. Batching is controlled by your driver or client's behavior, but means that CockroachDB receives all of the statements as a single unit, instead of a number of requests. - -From the perspective of CockroachDB, a transaction sent as a batch looks like this: - -{% include copy-clipboard.html %} -~~~ sql -> BEGIN; - -> DELETE FROM customers WHERE id = 1; - -> DELETE orders WHERE customer = 1; - -> COMMIT; -~~~ - -However, in your application's code, batched transactions are often just multiple statements sent at once. For example, in Go, this transaction would sent as a single batch (and automatically retried): - -~~~ go -db.Exec( - "BEGIN; - - DELETE FROM customers WHERE id = 1; - - DELETE orders WHERE customer = 1; - - COMMIT;" -) -~~~ - -Issuing statements this way signals to CockroachDB that you do not need to change any of the statement's values if the transaction doesn't immediately succeed, so it can continually retry the transaction until it's accepted. - -## See also - -- [Transactions](transactions.html) -- [`COMMIT`](commit-transaction.html) -- [`SAVEPOINT`](savepoint.html) -- [`RELEASE SAVEPOINT`](release-savepoint.html) -- [`ROLLBACK`](rollback-transaction.html) diff --git a/src/current/v19.2/bit.md b/src/current/v19.2/bit.md deleted file mode 100644 index 3f391465ac4..00000000000 --- a/src/current/v19.2/bit.md +++ /dev/null @@ -1,126 +0,0 @@ ---- -title: BIT -summary: The BIT and BIT VARYING data types stores bit arrays. -toc: true ---- - -The `BIT` and `VARBIT` [data types](data-types.html) stores bit arrays. -With `BIT`, the length is fixed; with `VARBIT`, the length can be variable. - -{% include {{page.version.version}}/sql/vectorized-support.md %} - -## Aliases - -The name `BIT VARYING` is an alias for `VARBIT`. - -## Syntax - -Bit array constants are expressed as literals. For example, `B'100101'` denotes an array of 6 bits. - -For more information about bit array constants, see the [constants documentation on bit array literals](sql-constants.html#bit-array-literals). - -For usage, see the [Example](#example) below. - -## Size - -The number of bits in a `BIT` value is determined as follows: - -| Type declaration | Logical size | -|------------------|-----------------------------------| -| BIT | 1 bit | -| BIT(N) | N bits | -| VARBIT | variable with no maximum | -| VARBIT(N) | variable with a maximum of N bits | - -The effective size of a `BIT` value is larger than its logical number -of bits by a bounded constant factor. Internally, CockroachDB stores -bit arrays in increments of 64 bits plus an extra integer value to -encode the length. - -The total size of a `BIT` value can be arbitrarily large, but it is -recommended to keep values under 1 MB to ensure performance. Above -that threshold, [write -amplification](https://en.wikipedia.org/wiki/Write_amplification) and -other considerations may cause significant performance degradation. - -## Example - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE b (x BIT, y BIT(3), z VARBIT, w VARBIT(3)); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM b; -~~~ - -~~~ - column_name | data_type | is_nullable | column_default | generation_expression | indices | is_hidden -+-------------+-----------+-------------+----------------+-----------------------+-----------+-----------+ - x | BIT | true | NULL | | {} | false - y | BIT(3) | true | NULL | | {} | false - z | VARBIT | true | NULL | | {} | false - w | VARBIT(3) | true | NULL | | {} | false - rowid | INT | false | unique_rowid() | | {primary} | true -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO b(x, y, z, w) VALUES (B'1', B'101', B'1', B'1'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM b; -~~~ - -~~~ - x | y | z | w -+---+-----+---+---+ - 1 | 101 | 1 | 1 -~~~ - -For type `BIT`, the value must match exactly the specified size: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO b(x) VALUES (B'101'); -~~~ - -~~~ -pq: bit string length 3 does not match type BIT -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO b(y) VALUES (B'10'); -~~~ - -~~~ -pq: bit string length 2 does not match type BIT(3) -~~~ - -For type `VARBIT`, the value must not be larger than the specified maximum size: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO b(w) VALUES (B'1010'); -~~~ - -~~~ -pq: bit string length 4 too large for type VARBIT(3) -~~~ - -## Supported casting and conversion - -`BIT` values can be [cast](data-types.html#data-type-conversions-and-casts) to any of the following data types: - -Type | Details ------|--------- -`INT` | Converts the bit array to the corresponding numeric value, interpreting the bits as if the value was encoded using [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement). If the bit array is larger than the integer type, excess bits on the left are ignored. For example, `B'1010'::INT` equals 10. -`STRING` | Prints out the binary digits as a string. This recovers the literal representation. For example, `B'1010'::INT` equals `'1010'`. - -## See also - -[Data Types](data-types.html) diff --git a/src/current/v19.2/bool.md b/src/current/v19.2/bool.md deleted file mode 100644 index fbd5e72f4fa..00000000000 --- a/src/current/v19.2/bool.md +++ /dev/null @@ -1,81 +0,0 @@ ---- -title: BOOL -summary: The BOOL data type stores Boolean values of false or true. -toc: true ---- - -The `BOOL` [data type](data-types.html) stores a Boolean value of `false` or `true`. - - -## Aliases - -In CockroachDB, `BOOLEAN` is an alias for `BOOL`. - -## Syntax - -There are two predefined [named constants](sql-constants.html#named-constants) for `BOOL`: `TRUE` and `FALSE` (the names are case-insensitive). - -Alternately, a boolean value can be obtained by coercing a numeric value: zero is coerced to `FALSE`, and any non-zero value to `TRUE`. - -- `CAST(0 AS BOOL)` (false) -- `CAST(123 AS BOOL)` (true) - -## Size - -A `BOOL` value is 1 byte in width, but the total storage size is likely to be larger due to CockroachDB metadata. - -## Examples - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE bool (a INT PRIMARY KEY, b BOOL, c BOOLEAN); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM bool; -~~~ - -~~~ -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -| column_name | data_type | is_nullable | column_default | generation_expression | indices | -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -| a | INT | false | NULL | | {"primary"} | -| b | BOOL | true | NULL | | {} | -| c | BOOL | true | NULL | | {} | -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -(3 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO bool VALUES (12345, true, CAST(0 AS BOOL)); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM bool; -~~~ - -~~~ -+-------+------+-------+ -| a | b | c | -+-------+------+-------+ -| 12345 | true | false | -+-------+------+-------+ -~~~ - -## Supported casting and conversion - -`BOOL` values can be [cast](data-types.html#data-type-conversions-and-casts) to any of the following data types: - -Type | Details ------|-------- -`INT` | Converts `true` to `1`, `false` to `0` -`DECIMAL` | Converts `true` to `1`, `false` to `0` -`FLOAT` | Converts `true` to `1`, `false` to `0` -`STRING` | –– - -## See also - -[Data Types](data-types.html) diff --git a/src/current/v19.2/build-a-c++-app-with-cockroachdb.md b/src/current/v19.2/build-a-c++-app-with-cockroachdb.md deleted file mode 100644 index 3c62814e96f..00000000000 --- a/src/current/v19.2/build-a-c++-app-with-cockroachdb.md +++ /dev/null @@ -1,200 +0,0 @@ ---- -title: Build a C++ App with CockroachDB and libpqxx -summary: Learn how to use CockroachDB from a simple C++ application with a low-level client driver. -toc: true -twitter: false ---- - -This tutorial shows you how build a simple C++ application with CockroachDB and the C++ libpqxx driver. - -We have tested the [C++ libpqxx driver](https://github.com/jtv/libpqxx) enough to claim **beta-level** support. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the libpqxx driver - -Install the C++ libpqxx driver as described in the [official documentation](https://github.com/jtv/libpqxx). - -{{site.data.alerts.callout_info}} -If you are running macOS, you need to install version 4.0.1 or higher of the libpqxx driver. -{{site.data.alerts.end}} - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the C++ code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, creating a table, inserting rows, and reading and printing the rows. - -Download the basic-sample.cpp file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ cpp -{% include {{ page.version.version }}/app/basic-sample.cpp %} -~~~ - -To build the `basic-sample.cpp` source code to an executable file named `basic-sample`, run the following command from the directory that contains the code: - -{% include copy-clipboard.html %} -~~~ shell -$ g++ -std=c++11 basic-sample.cpp -lpq -lpqxx -o basic-sample -~~~ - -Then run the `basic-sample` file from that directory: - -{% include copy-clipboard.html %} -~~~ shell -$ ./basic-sample -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -{{site.data.alerts.callout_info}} -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic **retry function** that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. -{{site.data.alerts.end}} - -Download the txn-sample.cpp file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ cpp -{% include {{ page.version.version }}/app/txn-sample.cpp %} -~~~ - -To build the `txn-sample.cpp` source code to an executable file named `txn-sample`, run the following command from the directory that contains the code: - -{% include copy-clipboard.html %} -~~~ shell -$ g++ -std=c++11 txn-sample.cpp -lpq -lpqxx -o txn-sample -~~~ - -Then run the `txn-sample` file from that directory: - -{% include copy-clipboard.html %} -~~~ shell -$ ./txn-sample -~~~ - -After running the code, use the [built-in SQL client](cockroach-sql.html) to verify that funds were transferred from one account to another: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -id | balance -+----+---------+ - 1 | 900 - 2 | 350 -(2 rows) -~~~ - - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the C++ code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, creating a table, inserting rows, and reading and printing the rows. - -Download the basic-sample.cpp file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ cpp -{% include {{ page.version.version }}/app/insecure/basic-sample.cpp %} -~~~ - -To build the `basic-sample.cpp` source code to an executable file named `basic-sample`, run the following command from the directory that contains the code: - -{% include copy-clipboard.html %} -~~~ shell -$ g++ -std=c++11 basic-sample.cpp -lpq -lpqxx -o basic-sample -~~~ - -Then run the `basic-sample` file from that directory: - -{% include copy-clipboard.html %} -~~~ shell -$ ./basic-sample -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -{{site.data.alerts.callout_info}} -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic **retry function** that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. -{{site.data.alerts.end}} - -Download the txn-sample.cpp file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ cpp -{% include {{ page.version.version }}/app/insecure/txn-sample.cpp %} -~~~ - -To build the `txn-sample.cpp` source code to an executable file named `txn-sample`, run the following command from the directory that contains the code: - -{% include copy-clipboard.html %} -~~~ shell -$ g++ -std=c++11 txn-sample.cpp -lpq -lpqxx -o txn-sample -~~~ - -Then run the `txn-sample` file from that directory: - -{% include copy-clipboard.html %} -~~~ shell -$ ./txn-sample -~~~ - -After running the code, use the [built-in SQL client](cockroach-sql.html) to verify that funds were transferred from one account to another: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -id | balance -+----+---------+ - 1 | 900 - 2 | 350 -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [C++ libpqxx driver](https://github.com/jtv/libpqxx). - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-clojure-app-with-cockroachdb.md b/src/current/v19.2/build-a-clojure-app-with-cockroachdb.md deleted file mode 100644 index 5417c8f0347..00000000000 --- a/src/current/v19.2/build-a-clojure-app-with-cockroachdb.md +++ /dev/null @@ -1,222 +0,0 @@ ---- -title: Build a Clojure App with CockroachDB and java.jdbc -summary: Learn how to use CockroachDB from a simple Clojure application with a low-level client driver. -toc: true -twitter: false ---- - -This tutorial shows you how build a simple Clojure application with CockroachDB using [leiningen](https://leiningen.org/) and the Closure java.jdbc driver. - -We have tested the [Clojure java.jdbc driver](https://clojure-doc.org/articles/ecosystem/java_jdbc/home/) in conjunction with the [PostgreSQL JDBC driver](https://jdbc.postgresql.org/) enough to claim **beta-level** support. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install `leiningen` - -Install the Clojure `lein` utility as described in its [official documentation](https://leiningen.org/). - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -New in v19.1: Pass the [`--also-generate-pkcs8-key` flag](cockroach-cert.html#flag-pkcs8) to generate a key in [PKCS#8 format](https://tools.ietf.org/html/rfc5208), which is the standard key encoding format in Java. In this case, the generated PKCS8 key will be named `client.maxroach.key.pk8`. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key --also-generate-pkcs8-key -~~~ - -## Step 4. Create a table in the new database - -As the `maxroach` user, use the [built-in SQL client](cockroach-sql.html) to create an `accounts` table in the new database. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql \ ---certs-dir=certs \ ---database=bank \ ---user=maxroach \ --e 'CREATE TABLE accounts (id INT PRIMARY KEY, balance INT)' -~~~ - -## Step 5. Run the Clojure code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Create a basic Clojure/JDBC project - -1. Create a new directory `myapp`. -2. Create a file `myapp/project.clj` and populate it with the following code, or download it directly. - - {% include copy-clipboard.html %} - ~~~ clojure - {% include {{ page.version.version }}/app/project.clj %} - ~~~ - -3. Create a file `myapp/src/test/util.clj` and populate it with the code from this file. Be sure to place the file in the subdirectory `src/test` in your project. - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, inserting rows and reading and printing the rows. - -Create a file `myapp/src/test/test.clj` and copy the code below to it, or download it directly. Be sure to rename this file to `test.clj` in the subdirectory `src/test` in your project. - -{% include copy-clipboard.html %} -~~~ clojure -{% include {{ page.version.version }}/app/basic-sample.clj %} -~~~ - -Run with: - -{% include copy-clipboard.html %} -~~~ shell -$ lein run -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Copy the code below to `myapp/src/test/test.clj` or -download it directly. Again, preserve the file name `test.clj`. - -{{site.data.alerts.callout_info}} -CockroachDB may require the -[client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic **retry function** that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ clojure -{% include {{ page.version.version }}/app/txn-sample.clj %} -~~~ - -Run with: - -{% include copy-clipboard.html %} -~~~ shell -$ lein run -~~~ - -After running the code, use the [built-in SQL client](cockroach-sql.html) to verify that funds were transferred from one account to another: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -id | balance -+----+---------+ - 1 | 900 - 2 | 350 -(2 rows) -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Create a table in the new database - -As the `maxroach` user, use the [built-in SQL client](cockroach-sql.html) to create an `accounts` table in the new database. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---database=bank \ ---user=maxroach \ --e 'CREATE TABLE accounts (id INT PRIMARY KEY, balance INT)' -~~~ - -## Step 4. Run the Clojure code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Create a basic Clojure/JDBC project - -1. Create a new directory `myapp`. -2. Create a file `myapp/project.clj` and populate it with the following code, or download it directly. - - {% include copy-clipboard.html %} - ~~~ clojure - {% include {{ page.version.version }}/app/project.clj %} - ~~~ - -3. Create a file `myapp/src/test/util.clj` and populate it with the code from this file. Be sure to place the file in the subdirectory `src/test` in your project. - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, inserting rows and reading and printing the rows. - -Create a file `myapp/src/test/test.clj` and copy the code below to it, or download it directly. Be sure to rename this file to `test.clj` in the subdirectory `src/test` in your project. - -{% include copy-clipboard.html %} -~~~ clojure -{% include {{ page.version.version }}/app/insecure/basic-sample.clj %} -~~~ - -Run with: - -{% include copy-clipboard.html %} -~~~ shell -$ lein run -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Copy the code below to `myapp/src/test/test.clj` or -download it directly. Again, preserve the file name `test.clj`. - -{{site.data.alerts.callout_info}} -CockroachDB may require the -[client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic **retry function** that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ clojure -{% include {{ page.version.version }}/app/insecure/txn-sample.clj %} -~~~ - -Run with: - -{% include copy-clipboard.html %} -~~~ shell -$ lein run -~~~ - -After running the code, use the [built-in SQL client](cockroach-sql.html) to verify that funds were transferred from one account to another: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -id | balance -+----+---------+ - 1 | 900 - 2 | 350 -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [Clojure java.jdbc driver](https://clojure-doc.org/articles/ecosystem/java_jdbc/home/). - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-csharp-app-with-cockroachdb.md b/src/current/v19.2/build-a-csharp-app-with-cockroachdb.md deleted file mode 100644 index 604516658c5..00000000000 --- a/src/current/v19.2/build-a-csharp-app-with-cockroachdb.md +++ /dev/null @@ -1,235 +0,0 @@ ---- -title: Build a C# App with CockroachDB and the .NET Npgsql Driver -summary: Learn how to use CockroachDB from a simple C# (.NET) application with a low-level client driver. -toc: true -twitter: true ---- - -This tutorial shows you how build a simple C# application with CockroachDB and the .NET Npgsql driver. - -We have tested the [.NET Npgsql driver](http://www.npgsql.org/) enough to claim **beta-level** support. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Create a .NET project - -{% include copy-clipboard.html %} -~~~ shell -$ dotnet new console -o cockroachdb-test-app -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cd cockroachdb-test-app -~~~ - -The `dotnet` command creates a new app of type `console`. The `-o` parameter creates a directory named `cockroachdb-test-app` where your app will be stored and populates it with the required files. The `cd cockroachdb-test-app` command puts you into the newly created app directory. - -## Step 2. Install the Npgsql driver - -Install the latest version of the [Npgsql driver](https://www.nuget.org/packages/Npgsql/) into the .NET project using the built-in nuget package manager: - -{% include copy-clipboard.html %} -~~~ shell -$ dotnet add package Npgsql -~~~ - -
      - -## Step 3. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 4. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 5. Convert the key file for use by C# programs - -The private key generated for user `maxroach` by CockroachDB is [PEM encoded](https://tools.ietf.org/html/rfc1421). To read the key in a C# application, you will need to convert it into PKCS#12 format. - -To convert the key to PKCS#12 format, run the following OpenSSL command on the `maxroach` user's key file in the directory where you stored your certificates: - -{% include copy-clipboard.html %} -~~~ shell -$ openssl pkcs12 -inkey client.maxroach.key -password pass: -in client.maxroach.crt -export -out client.maxroach.pfx -~~~ - -As of December 2018, you need to provide a password for this to work on macOS. See . - -## Step 6. Run the C# code - -Now that you have created a database and set up encryption keys, in this section you will: - -- [Create a table and insert some rows](#basic-example) -- [Execute a batch of statements as a transaction](#transaction-example-with-retry-logic) - -### Basic example - -Replace the contents of `cockroachdb-test-app/Program.cs` with the following code: - -{% include copy-clipboard.html %} -~~~ csharp -{% include {{ page.version.version }}/app/basic-sample.cs %} -~~~ - -Then, run the code to connect as the `maxroach` user. This time, execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted: - -{% include copy-clipboard.html %} -~~~ shell -$ dotnet run -~~~ - -The output should be: - -~~~ -Initial balances: - account 1: 1000 - account 2: 250 -~~~ - -### Transaction example (with retry logic) - -Open `cockroachdb-test-app/Program.cs` again and replace the contents with the code shown below. - -{% include {{page.version.version}}/client-transaction-retry.md %} - -{% include copy-clipboard.html %} -~~~ csharp -{% include {{ page.version.version }}/app/txn-sample.cs %} -~~~ - -Then, run the code to connect as the `maxroach` user. This time, execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted: - -{% include copy-clipboard.html %} -~~~ shell -$ dotnet run -~~~ - -The output should be: - -~~~ -Initial balances: - account 1: 1000 - account 2: 250 -Final balances: - account 1: 900 - account 2: 350 -~~~ - -However, if you want to verify that funds were transferred from one account to another, use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --database=bank -e 'SELECT id, balance FROM accounts' -~~~ - -~~~ - id | balance -+----+---------+ - 1 | 900 - 2 | 350 -(2 rows) -~~~ - -
      - -
      - -## Step 3. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 4. Run the C# code - -Now that you have created a database and set up encryption keys, in this section you will: - -- [Create a table and insert some rows](#basic2) -- [Execute a batch of statements as a transaction](#transaction2) - - - -### Basic example - -Replace the contents of `cockroachdb-test-app/Program.cs` with the following code: - -{% include copy-clipboard.html %} -~~~ csharp -{% include {{ page.version.version }}/app/insecure/basic-sample.cs %} -~~~ - -Then, run the code to connect as the `maxroach` user and execute some basic SQL statements: creating a table, inserting rows, and reading and printing the rows: - -{% include copy-clipboard.html %} -~~~ shell -$ dotnet run -~~~ - -The output should be: - -~~~ -Initial balances: - account 1: 1000 - account 2: 250 -~~~ - - - -### Transaction example (with retry logic) - -Open `cockroachdb-test-app/Program.cs` again and replace the contents with the code shown below. - -{% include {{page.version.version}}/client-transaction-retry.md %} - -{% include copy-clipboard.html %} -~~~ csharp -{% include {{ page.version.version }}/app/insecure/txn-sample.cs %} -~~~ - -Then, run the code to connect as the `maxroach` user. This time, execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted: - -{% include copy-clipboard.html %} -~~~ shell -$ dotnet run -~~~ - -The output should be: - -~~~ -Initial balances: - account 1: 1000 - account 2: 250 -Final balances: - account 1: 900 - account 2: 350 -~~~ - -However, if you want to verify that funds were transferred from one account to another, use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --database=bank -e 'SELECT id, balance FROM accounts' -~~~ - -~~~ - id | balance -+----+---------+ - 1 | 900 - 2 | 350 -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [.NET Npgsql driver](http://www.npgsql.org/). - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-go-app-with-cockroachdb-gorm.md b/src/current/v19.2/build-a-go-app-with-cockroachdb-gorm.md deleted file mode 100644 index 0b7a980f075..00000000000 --- a/src/current/v19.2/build-a-go-app-with-cockroachdb-gorm.md +++ /dev/null @@ -1,148 +0,0 @@ ---- -title: Build a Go App with CockroachDB and GORM -summary: Learn how to use CockroachDB from a simple Go application with the GORM ORM. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Go application with CockroachDB and the GORM ORM. - -{{site.data.alerts.callout_success}} -For another use of GORM with CockroachDB, see our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. -{{site.data.alerts.end}} - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the GORM ORM - -To install [GORM](http://gorm.io), run the following commands: - -{% include copy-clipboard.html %} -~~~ shell -$ go get -u github.com/lib/pq # dependency -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ go get -u github.com/jinzhu/gorm -~~~ - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the Go code - -The following code uses the [GORM](http://gorm.io) ORM to map Go-specific objects to SQL operations. Specifically: - -- `db.AutoMigrate(&Account{})` creates an `accounts` table based on the Account model. -- `db.Create(&Account{})` inserts rows into the table. -- `db.Find(&accounts)` selects from the table so that balances can be printed. -- The funds transfer occurs in `transferFunds()`. To ensure that we [handle retry errors](transactions.html#client-side-intervention), we write an application-level retry loop that, in case of error, sleeps before trying the funds transfer again. If it encounters another error, it sleeps again for a longer interval, implementing [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff). - -Copy the code or -download it directly. - -{{site.data.alerts.callout_success}} -To clone a version of the code below that connects to insecure clusters, run the command below. Note that you will need to edit the connection string to use the certificates that you generated when you set up your secure cluster. - -`git clone https://github.com/cockroachlabs/hello-world-go-gorm` -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/gorm-sample.go %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run gorm-sample.go -~~~ - -The output should show the account balances before and after the funds transfer: - -~~~ shell -Balance at '2019-08-06 13:37:19.311423 -0400 EDT m=+0.034072606': -1 1000 -2 250 -Balance at '2019-08-06 13:37:19.325654 -0400 EDT m=+0.048303286': -1 900 -2 350 -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Go code - -The following code uses the [GORM](http://gorm.io) ORM to map Go-specific objects to SQL operations. Specifically: - -- `db.AutoMigrate(&Account{})` creates an `accounts` table based on the Account model. -- `db.Create(&Account{})` inserts rows into the table. -- `db.Find(&accounts)` selects from the table so that balances can be printed. -- The funds transfer occurs in `transferFunds()`. To ensure that we [handle retry errors](transactions.html#client-side-intervention), we write an application-level retry loop that, in case of error, sleeps before trying the funds transfer again. If it encounters another error, it sleeps again for a longer interval, implementing [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff). - -To get the code below, clone the `hello-world-go-gorm` repo to your machine: - -{% include copy-clipboard.html %} -~~~ shell -git clone https://github.com/cockroachlabs/hello-world-go-gorm -~~~ - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/insecure/gorm-sample.go %} -~~~ - -Change to the directory where you cloned the repo and run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run main.go -~~~ - -The output should show the account balances before and after the funds transfer: - -~~~ shell -Balance at '2019-07-15 13:34:22.536363 -0400 EDT m=+0.019918599': -1 1000 -2 250 -Balance at '2019-07-15 13:34:22.540037 -0400 EDT m=+0.023592845': -1 900 -2 350 -~~~ - -
      - -## What's next? - -Read more about using the [GORM ORM](http://gorm.io), or check out a more realistic implementation of GORM with CockroachDB in our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-go-app-with-cockroachdb-pq.md b/src/current/v19.2/build-a-go-app-with-cockroachdb-pq.md deleted file mode 100644 index 8644dc3568e..00000000000 --- a/src/current/v19.2/build-a-go-app-with-cockroachdb-pq.md +++ /dev/null @@ -1,245 +0,0 @@ ---- -title: Build a Go App with CockroachDB the Go pq Driver -summary: Learn how to use CockroachDB from a simple Go application with the Go pq driver. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Go application with CockroachDB and the Go pq driver. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the Go pq driver - -To install the [Go pq driver](https://godoc.org/github.com/lib/pq), run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ go get -u github.com/lib/pq -~~~ - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -The code samples will run with `maxroach` as the user. - -## Step 4. Run the Go code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -{{site.data.alerts.callout_success}} -To clone a version of the code below that connects to insecure clusters, run the following: - -`git clone https://github.com/cockroachlabs/hello-world-go-pq/` - -Note that you will need to edit the connection string to use the certificates that you generated when you set up your secure cluster. -{{site.data.alerts.end}} - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, creating a table, inserting rows, and reading and printing the rows. - -Download the basic-sample.go file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/basic-sample.go %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run basic-sample.go -~~~ - -The output should be: - -~~~ -Initial balances: -1 1000 -2 250 -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time will execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Download the txn-sample.go file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/txn-sample.go %} -~~~ - -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic **retry function** that runs inside a transaction and retries it as needed. For Go, the CockroachDB retry function is in the `crdb` package of the CockroachDB Go client. To install, clone the library into your `$GOPATH` as follows: - -{% include copy-clipboard.html %} -~~~ shell -$ mkdir -p $GOPATH/src/github.com/cockroachdb -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cd $GOPATH/src/github.com/cockroachdb -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ git clone git@github.com:cockroachdb/cockroach-go.git -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run txn-sample.go -~~~ - -The output should be: - -~~~ -Success -~~~ - -To verify that funds were transferred from one account to another, use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Go code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -{{site.data.alerts.callout_success}} -To clone a version of the code below that connects to insecure clusters, run the following: - -`git clone https://github.com/cockroachlabs/hello-world-go-pq/` - -Note that you will need to edit the connection string to use the certificates that you generated when you set up your secure cluster. -{{site.data.alerts.end}} - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, creating a table, inserting rows, and reading and printing the rows. - -Download the basic-sample.go file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/insecure/basic-sample.go %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run basic-sample.go -~~~ - -The output should be: - -~~~ -Initial balances: -1 1000 -2 250 -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time will execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Download the txn-sample.go file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/insecure/txn-sample.go %} -~~~ - -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic **retry function** that runs inside a transaction and retries it as needed. For Go, the CockroachDB retry function is in the `crdb` package of the CockroachDB Go client. - -To install the [CockroachDB Go client](https://github.com/cockroachdb/cockroach-go), run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ go get -d github.com/cockroachdb/cockroach-go -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run txn-sample.go -~~~ - -The output should be: - -~~~ -Success -~~~ - -To verify that funds were transferred from one account to another, use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [Go pq driver](https://godoc.org/github.com/lib/pq). - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-go-app-with-cockroachdb-upperdb.md b/src/current/v19.2/build-a-go-app-with-cockroachdb-upperdb.md deleted file mode 100644 index 06cb28f019d..00000000000 --- a/src/current/v19.2/build-a-go-app-with-cockroachdb-upperdb.md +++ /dev/null @@ -1,163 +0,0 @@ ---- -title: Build a Go App with CockroachDB and upper/db -summary: Learn how to use CockroachDB from a simple Go application with the upper/db data access layer. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Go application with CockroachDB and the [upper/db](https://upper.io/) data access layer. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -
      - -## Step 1. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 2. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -The code samples will run with `maxroach` as the user. - -## Step 3. Run the Go code - -The sample code shown below uses upper/db to map Go-specific objects to SQL operations. Specifically, the code: - -- Creates the `accounts` table, if it does not already exist. -- Deletes any existing rows in the `accounts` table. -- Inserts two rows into the `accounts` table. -- Prints the rows in the `accounts` table to the terminal. -- Deletes the first row in the `accounts` table. -- Updates the rows in the `accounts` table within an explicit [transaction](transactions.html). -- Prints the rows in the `accounts` table to the terminal once more. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/upperdb-basic-sample/main.go %} -~~~ - -Note that the sample code also includes a function that simulates a transaction error (`crdbForceRetry()`). Upper/db's CockroachDB adapter [automatically retries transactions](transactions.html#client-side-intervention) when transaction errors are thrown. As a result, this function forces a transaction retry. - -To run the code, copy the sample above, or download it directly. - -{{site.data.alerts.callout_success}} -To clone a version of the code below that connects to insecure clusters, run the following command: - -`git clone https://github.com/cockroachlabs/hello-world-go-upperdb/` - -Note that you will need to edit the connection string to use the certificates that you generated when you set up your secure cluster. -{{site.data.alerts.end}} - -
      - -
      - -## Step 1. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 2. Run the Go code - -The sample code shown below uses upper/db to map Go-specific objects to SQL operations. Specifically, the code: - -- Creates the `accounts` table, if it does not already exist. -- Deletes any existing rows in the `accounts` table. -- Inserts two rows into the `accounts` table. -- Prints the rows in the `accounts` table to the terminal. -- Deletes the first row in the `accounts` table. -- Updates the rows in the `accounts` table within an explicit [transaction](transactions.html). -- Prints the rows in the `accounts` table to the terminal once more. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/insecure/upperdb-basic-sample/main.go %} -~~~ - -Note that the sample code also includes a function that simulates a transaction error (`crdbForceRetry()`). Upper/db's CockroachDB adapter [automatically retries transactions](transactions.html#client-side-intervention) when transaction errors are thrown. As a result, this function forces a transaction retry. - -Copy the code or download it directly. - -{{site.data.alerts.callout_success}} -To clone a version of the code below that connects to insecure clusters, run the following command: - -`git clone https://github.com/cockroachlabs/hello-world-go-upperdb/` -{{site.data.alerts.end}} - -
      - -Change to the directory where you cloned the repo and get the dependencies with `go mod init`: - -{% include copy-clipboard.html %} -~~~ shell -$ go mod init hello-world-go-upperdb -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run main.go -~~~ - -The output should look similar to the following: - -~~~ -go: finding module for package github.com/upper/db/v4 -go: finding module for package github.com/upper/db/v4/adapter/cockroachdb -go: found github.com/upper/db/v4 in github.com/upper/db/v4 v4.0.0 -2020/09/16 10:31:55 Balances: - accounts[590467288222990337]: 1000 - accounts[590467288229576705]: 250 -2020/09/16 10:31:55 Balances: - accounts[590467288222990337]: 500 - accounts[590467288229576705]: 999 -2020/09/16 10:31:55 upper/db: log_level=WARNING file=go/pkg/mod/github.com/upper/db/v4@v4.0.0/internal/sqladapter/session.go:642 - Session ID: 00006 - Transaction ID: 00005 - Query: SELECT crdb_internal.force_retry('1ms'::INTERVAL) - Error: pq: restart transaction: crdb_internal.force_retry(): TransactionRetryWithProtoRefreshError: forced by crdb_internal.force_retry() - Time taken: 0.00171s - Context: context.Background - -2020/09/16 10:31:55 upper/db: log_level=WARNING file=go/pkg/mod/github.com/upper/db/v4@v4.0.0/internal/sqladapter/session.go:642 - Session ID: 00006 - Transaction ID: 00005 - Query: INSERT INTO "accounts" ("balance") VALUES ($1) RETURNING "id" - Arguments: []interface {}{887} - Error: pq: current transaction is aborted, commands ignored until end of transaction block - Time taken: 0.00065s - Context: context.Background - -2020/09/16 10:31:56 Balances: - accounts[590467288229576705]: 999 - accounts[590467288342757377]: 887 - accounts[590467288350064641]: 342 -~~~ - -Note that the forced transaction errors result in errors printed to the terminal, but the transactions are retried until they succeed. - -## What's next? - -Read more about upper/db: - -- [Introduction to upper/db](https://upper.io/v4/getting-started/) -- [The upper/db tour](https://tour.upper.io/) -- [upper/db reference docs](https://pkg.go.dev/github.com/upper/db/v4) - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-go-app-with-cockroachdb.md b/src/current/v19.2/build-a-go-app-with-cockroachdb.md deleted file mode 100644 index 93824bc08d0..00000000000 --- a/src/current/v19.2/build-a-go-app-with-cockroachdb.md +++ /dev/null @@ -1,230 +0,0 @@ ---- -title: Build a Go App with CockroachDB the Go pgx Driver -summary: Learn how to use CockroachDB from a simple Go application with the Go pgx driver. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Go application with CockroachDB and the [Go pgx driver](https://pkg.go.dev/github.com/jackc/pgx). - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the pgx driver - -To install the pgx driver, run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ go get -u github.com/jackc/pgx -~~~ - -## Step 2. Install the CockroachDB Go library - -To install the [CockroachDB Go library](https://pkg.go.dev/github.com/cockroachdb/cockroach-go/crdb), run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ go get -u github.com/cockroachdb/cockroach-go/crdb -~~~ - -
      - -## Step 3. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 4. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -The code samples will run with `maxroach` as the user. - -## Step 5. Run the Go code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect to the cluster as the `maxroach` user, and then execute some basic SQL statements that create a table, insert some rows, and read and print the rows to the console. - -Download the basic-sample-pgx.go file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/basic-sample-pgx.go %} -~~~ - -Initialize the module: - -{% include copy-clipboard.html %} -~~~ shell -$ go mod init basic-sample-pgx -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run basic-sample-pgx.go -~~~ - -The output should be: - -~~~ -Initial balances: -1 1000 -2 250 -~~~ - -### Transaction (with retry logic) - -Next, use the following code to connect as `maxroach` user, and then execute a batch of statements as an atomic transaction to transfer funds from one account to another. All statements in the transaction are either committed or aborted. - -Download the txn-sample-pgx.go file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/txn-sample-pgx.go %} -~~~ - -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. The [CockroachDB Go client](https://github.com/cockroachdb/cockroach-go) includes a generic **retry function** (`ExecuteTx`) that runs inside a transaction and retries it as needed. - -To run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run txn-sample-pgx.go -~~~ - -The output should be: - -~~~ -Success -~~~ - -To verify that funds were transferred from one account to another, use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -
      - -## Step 3. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 4. Run the Go code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect to the cluster as the `maxroach` user, and then execute some basic SQL statements that create a table, insert some rows, and read and print the rows to the console. - -Download the basic-sample-pgx.go file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/insecure/basic-sample-pgx.go %} -~~~ - -Initialize the module: - -{% include copy-clipboard.html %} -~~~ shell -$ go mod init basic-sample-pgx -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run basic-sample-pgx.go -~~~ - -The output should be: - -~~~ -Initial balances: -1 1000 -2 250 -~~~ - -### Transaction (with retry logic) - -Next, use the following code to connect as `maxroach` user, and then execute a batch of statements as an atomic transaction to transfer funds from one account to another. All statements in the transaction are either committed or aborted. - -Download the txn-sample-pgx.go file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/app/insecure/txn-sample-pgx.go %} -~~~ - -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. The [CockroachDB Go client](https://github.com/cockroachdb/cockroach-go) includes a generic **retry function** (`ExecuteTx`) that runs inside a transaction and retries it as needed. - -To run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ go run txn-sample-pgx.go -~~~ - -The output should be: - -~~~ -Success -~~~ - -To verify that funds were transferred from one account to another, use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [Go pgx driver](https://pkg.go.dev/github.com/jackc/pgx?tab=doc). - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-java-app-with-cockroachdb-hibernate.md b/src/current/v19.2/build-a-java-app-with-cockroachdb-hibernate.md deleted file mode 100644 index 785da63a3d2..00000000000 --- a/src/current/v19.2/build-a-java-app-with-cockroachdb-hibernate.md +++ /dev/null @@ -1,287 +0,0 @@ ---- -title: Build a Java App with CockroachDB and Hibernate -summary: Learn how to use CockroachDB from a simple Java application with the Hibernate ORM. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Java application with CockroachDB and the Hibernate ORM. - -{% include {{page.version.version}}/app/java-version-note.md %} - -{{site.data.alerts.callout_success}} -For another use of Hibernate with CockroachDB, see our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. -{{site.data.alerts.end}} - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the Gradle build tool - -This tutorial uses the [Gradle build tool](https://gradle.org/) to get all dependencies for your application, including Hibernate. - -To install Gradle on Mac, run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ brew install gradle -~~~ - -To install Gradle on a Debian-based Linux distribution like Ubuntu: - -{% include copy-clipboard.html %} -~~~ shell -$ apt-get install gradle -~~~ - -To install Gradle on a Red Hat-based Linux distribution like Fedora: - -{% include copy-clipboard.html %} -~~~ shell -$ dnf install gradle -~~~ - -For other ways to install Gradle, see [its official documentation](https://gradle.org/install). - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -You can pass the [`--also-generate-pkcs8-key` flag](cockroach-cert.html#flag-pkcs8) to generate a key in [PKCS#8 format](https://tools.ietf.org/html/rfc5208), which is the standard key encoding format in Java. In this case, the generated PKCS8 key will be named `client.maxroach.key.pk8`. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key --also-generate-pkcs8-key -~~~ - -## Step 4. Run the Java code - -The code below uses Hibernate to map Java methods to SQL operations. It perform the following steps which roughly correspond to method calls in the `Sample` class. - -1. Create an `accounts` table in the `bank` database as specified by the Hibernate `Account` class. -2. Inserts rows into the table using `session.save(new Account(int id, int balance))` (see `Sample.addAccounts()`). -3. Transfer money from one account to another, printing out account balances before and after the transfer (see `transferFunds(long fromId, long toId, long amount)`). -4. Print out account balances before and after the transfer (see `Sample.getAccountBalance(long id)`). - -In addition, the code shows a pattern for automatically handling [transaction retries](transactions.html#client-side-intervention-example) by wrapping transactions in a higher-order function `Sample.runTransaction()`. It also includes a method for testing the retry handling logic (`Sample.forceRetryLogic()`), which will be run if you set the `FORCE_RETRY` variable to `true`. - -It does all of the above using the practices we recommend for using Hibernate (and the underlying JDBC connection) with CockroachDB, which are listed in the [Recommended Practices](#recommended-practices) section below. - -To run it: - -1. Download and extract [hibernate-basic-sample.tgz](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/hibernate-basic-sample/hibernate-basic-sample.tgz). The settings in [`hibernate.cfg.xml`](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/hibernate-basic-sample/hibernate.cfg.xml) specify how to connect to the database. - - {{site.data.alerts.callout_info}} - The version of the CockroachDB Hibernate dialect in `hibernate.cfg.xml` corresponds to a version of CockroachDB. For more information, see [Install Client Drivers: Hibernate](install-client-drivers.html). - {{site.data.alerts.end}} - -2. Compile and run the code using [`build.gradle`](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/hibernate-basic-sample/build.gradle), which will also download the dependencies. - - {% include copy-clipboard.html %} - ~~~ shell - $ gradle run - ~~~ - -{{site.data.alerts.callout_success}} -To clone a version of the code below that connects to insecure clusters, run the command below. Note that you will need to edit the connection string to use the certificates that you generated when you set up your secure cluster. - -`git clone https://github.com/cockroachlabs/example-app-java-hibernate/` -{{site.data.alerts.end}} - -The contents of [`Sample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/hibernate-basic-sample/Sample.java): - -{% include copy-clipboard.html %} -~~~ java -{% include {{page.version.version}}/app/hibernate-basic-sample/Sample.java %} -~~~ - -Toward the end of the output, you should see: - -~~~ -APP: BEGIN; -APP: addAccounts() --> 1 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(1) --> 1000 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(2) --> 250 -APP: COMMIT; -APP: getAccountBalance(1) --> 1000 -APP: getAccountBalance(2) --> 250 -APP: BEGIN; -APP: transferFunds(1, 2, 100) --> 100 -APP: COMMIT; -APP: transferFunds(1, 2, 100) --> 100 -APP: BEGIN; -APP: getAccountBalance(1) --> 900 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(2) --> 350 -APP: COMMIT; -APP: getAccountBalance(1) --> 900 -APP: getAccountBalance(2) --> 350 -~~~ - -To verify that the account balances were updated successfully, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --database=bank -~~~ - -To check the account balances, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -SELECT id, balance FROM accounts; -~~~ - -~~~ - id | balance -+----+---------+ - 1 | 900 - 2 | 350 - 3 | 314159 -(3 rows) -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Java code - -The code below uses Hibernate to map Java methods to SQL operations. It perform the following steps which roughly correspond to method calls in the `Sample` class. - -1. Create an `accounts` table in the `bank` database as specified by the Hibernate `Account` class. -2. Inserts rows into the table using `session.save(new Account(int id, int balance))` (see `Sample.addAccounts()`). -3. Transfer money from one account to another, printing out account balances before and after the transfer (see `transferFunds(long fromId, long toId, long amount)`). -4. Print out account balances before and after the transfer (see `Sample.getAccountBalance(long id)`). - -In addition, the code shows a pattern for automatically handling [transaction retries](transactions.html#client-side-intervention-example) by wrapping transactions in a higher-order function `Sample.runTransaction()`. It also includes a method for testing the retry handling logic (`Sample.forceRetryLogic()`), which will be run if you set the `FORCE_RETRY` variable to `true`. - -It does all of the above using the practices we recommend for using Hibernate (and the underlying JDBC connection) with CockroachDB, which are listed in the [Recommended Practices](#recommended-practices) section below. - -To run it: - -1. Clone the `example-app-java-hibernate` repo to your machine: - - {% include copy-clipboard.html %} - ~~~ shell - git clone https://github.com/cockroachlabs/example-app-java-hibernate/ - ~~~ - -2. Compile and run the code using [`build.gradle`](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/insecure/hibernate-basic-sample/build.gradle), which will also download the dependencies. - - {% include copy-clipboard.html %} - ~~~ shell - $ gradle run - ~~~ - -The contents of [`Sample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/insecure/hibernate-basic-sample/Sample.java): - -{% include copy-clipboard.html %} -~~~ java -{% include {{page.version.version}}/app/insecure/hibernate-basic-sample/Sample.java %} -~~~ - -Toward the end of the output, you should see: - -~~~ -APP: BEGIN; -APP: addAccounts() --> 1 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(1) --> 1000 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(2) --> 250 -APP: COMMIT; -APP: getAccountBalance(1) --> 1000 -APP: getAccountBalance(2) --> 250 -APP: BEGIN; -APP: transferFunds(1, 2, 100) --> 100 -APP: COMMIT; -APP: transferFunds(1, 2, 100) --> 100 -APP: BEGIN; -APP: getAccountBalance(1) --> 900 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(2) --> 350 -APP: COMMIT; -APP: getAccountBalance(1) --> 900 -APP: getAccountBalance(2) --> 350 -~~~ - -To verify that the account balances were updated successfully, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --database=bank -~~~ - -To check the account balances, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -SELECT id, balance FROM accounts; -~~~ - -~~~ - id | balance -+----+---------+ - 1 | 900 - 2 | 350 - 3 | 314159 -(3 rows) -~~~ - -
      - -## Recommended Practices - -### Use `IMPORT` to read in large data sets - -If you are trying to get a large data set into CockroachDB all at once (a bulk import), avoid writing client-side code altogether and use the [`IMPORT`](import.html) statement instead. It is much faster and more efficient than making a series of [`INSERT`s](insert.html) and [`UPDATE`s](update.html). It bypasses the [SQL layer](architecture/sql-layer.html) altogether and writes directly to the [storage layer](architecture/storage-layer.html) of the database. - -For more information about importing data from Postgres, see [Migrate from Postgres](migrate-from-postgres.html). - -For more information about importing data from MySQL, see [Migrate from MySQL](migrate-from-mysql.html). - -### Use `rewriteBatchedInserts` for increased speed - -We strongly recommend setting `rewriteBatchedInserts=true`; we have seen 2-3x performance improvements with it enabled. From [the JDBC connection parameters documentation](https://jdbc.postgresql.org/documentation/use/#connection-parameters): - -> This will change batch inserts from `insert into foo (col1, col2, col3) values (1,2,3)` into `insert into foo (col1, col2, col3) values (1,2,3), (4,5,6)` this provides 2-3x performance improvement - -### Retrieve large data sets in chunks using cursors - -New in v19.2: CockroachDB now supports the Postgres wire-protocol cursors for implicit transactions and explicit transactions executed to completion. This means the [PGJDBC driver](https://jdbc.postgresql.org) can use this protocol to stream queries with large result sets. This is much faster than [paginating through results in SQL using `LIMIT .. OFFSET`](selection-queries.html#paginate-through-limited-results). - -For instructions showing how to use cursors in your Java code, see [Getting results based on a cursor](https://jdbc.postgresql.org/documentation/query/#getting-results-based-on-a-cursor) from the PGJDBC documentation. - -Note that interleaved execution (partial execution of multiple statements within the same connection and transaction) is not supported when [`Statement.setFetchSize()`](https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#setFetchSize-int-) is used. - -## What's next? - -Read more about using the [Hibernate ORM](http://hibernate.org/orm/), or check out a more realistic implementation of Hibernate with CockroachDB in our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. - -{% include {{page.version.version}}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-java-app-with-cockroachdb-jooq.md b/src/current/v19.2/build-a-java-app-with-cockroachdb-jooq.md deleted file mode 100644 index 6b803073925..00000000000 --- a/src/current/v19.2/build-a-java-app-with-cockroachdb-jooq.md +++ /dev/null @@ -1,275 +0,0 @@ ---- -title: Build a Java App with CockroachDB and jOOQ -summary: Learn how to use CockroachDB from a simple Java application with jOOQ. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Java application with CockroachDB and [jOOQ](https://www.jooq.org/). - -CockroachDB is supported in jOOQ [Professional and Enterprise editions](https://www.jooq.org/download/#databases). - -{% include {{page.version.version}}/app/java-version-note.md %} - -{{site.data.alerts.callout_success}} -For another use of jOOQ with CockroachDB, see our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. -{{site.data.alerts.end}} - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install Maven - -This tutorial uses the [Maven build tool](https://gradle.org/) to manage application dependencies. - -To install Maven on Mac, run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ brew install maven -~~~ - -To install Maven on a Debian-based Linux distribution like Ubuntu: - -{% include copy-clipboard.html %} -~~~ shell -$ apt-get install maven -~~~ - -For other ways to install Maven, see [its official documentation](https://maven.apache.org/install.html). - -## Step 2. Install jOOQ - -Download the free trial of jOOQ Professional or Enterprise edition from [jOOQ's website](https://www.jooq.org/download), and unzip the file. - -To install jOOQ to your machine's local Maven repository, run the `maven-install.sh` script included in the jOOQ install folder: - -{% include copy-clipboard.html %} -~~~ shell -$ chmod +x maven-install.sh -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ ./maven-install.sh -~~~ - -
      - -## Step 3. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 4. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -The [`--also-generate-pkcs8-key` flag](cockroach-cert.html#flag-pkcs8) generates a key in [PKCS#8 format](https://tools.ietf.org/html/rfc5208), which is the standard key encoding format in Java. In this case, the generated PKCS8 key will be named `client.maxroach.key.pk8`. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key --also-generate-pkcs8-key -~~~ - -## Step 5. Run the Java code - -The code below uses jOOQ to map Java methods to SQL operations. It performs the following steps, some of which correspond to method calls of the `Sample` class. - -1. Inputs the `db.sql` file to the database. `db.sql` includes SQL statements that create an `accounts` table in the `bank` database. -2. Inserts rows into the `accounts` table using `session.save(new Account(int id, int balance))` (see `Sample.addAccounts()`). -3. Transfers money from one account to another, printing out account balances before and after the transfer (see `transferFunds(long fromId, long toId, long amount)`). -4. Prints out account balances before and after the transfer (see `Sample.getAccountBalance(long id)`). - -In addition, the code shows a pattern for automatically handling [transaction retries](transactions.html#client-side-intervention-example) by wrapping transactions in a higher-order function `Sample.runTransaction()`. It also includes a method for testing the retry handling logic (`Sample.forceRetryLogic()`), which will be run if you set the `FORCE_RETRY` variable to `true`. - -To run it: - -1. Download and unzip [jooq-basic-sample.zip](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/jooq-basic-sample/jooq-basic-sample.zip). -2. Open `jooq-basic-sample/src/main/java/com/cockroachlabs/Sample.java`, and edit the connection string passed to `DriverManager.getConnection()` in the `Sample` class's `main()` method so that the certificate paths are fully and correctly specified. -3. Compile and run the code using Maven: - - {% include copy-clipboard.html %} - ~~~ shell - $ cd jooq-basic-sample - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ mvn compile - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ mvn exec:java -Dexec.mainClass=com.cockroachlabs.Sample - ~~~ - -Here are the contents of [`Sample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/jooq-basic-sample/Sample.java), the Java file containing the main `Sample` class: - -{% include copy-clipboard.html %} -~~~ java -{% include {{page.version.version}}/app/jooq-basic-sample/Sample.java %} -~~~ - -Toward the end of the output, you should see: - -~~~ -APP: BEGIN; -APP: addAccounts() --> 1 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(1) --> 1000 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(2) --> 250 -APP: COMMIT; -APP: getAccountBalance(1) --> 1000 -APP: getAccountBalance(2) --> 250 -APP: BEGIN; -APP: transferFunds(1, 2, 100) --> 100 -APP: COMMIT; -APP: transferFunds(1, 2, 100) --> 100 -APP: BEGIN; -APP: getAccountBalance(1) --> 900 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(2) --> 350 -APP: COMMIT; -APP: getAccountBalance(1) --> 900 -APP: getAccountBalance(2) --> 350 -~~~ - -To verify that the account balances were updated successfully, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs -~~~ - -To check the account balances, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -SELECT id, balance FROM accounts; -~~~ - -~~~ - id | balance -+----+---------+ - 1 | 900 - 2 | 350 - 3 | 314159 -(3 rows) -~~~ - -
      - -
      - -## Step 3. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 4. Run the Java code - -The code below uses jOOQ to map Java methods to SQL operations. It performs the following steps, some of which correspond to method calls of the `Sample` class. - -1. Inputs the `db.sql` file to the database. `db.sql` includes SQL statements that create an `accounts` table in the `bank` database. -2. Inserts rows into the `accounts` table using `session.save(new Account(int id, int balance))` (see `Sample.addAccounts()`). -3. Transfers money from one account to another, printing out account balances before and after the transfer (see `transferFunds(long fromId, long toId, long amount)`). -4. Prints out account balances before and after the transfer (see `Sample.getAccountBalance(long id)`). - -In addition, the code shows a pattern for automatically handling [transaction retries](transactions.html#client-side-intervention-example) by wrapping transactions in a higher-order function `Sample.runTransaction()`. It also includes a method for testing the retry handling logic (`Sample.forceRetryLogic()`), which will be run if you set the `FORCE_RETRY` variable to `true`. - -To run it: - -1. Download and unzip [jooq-basic-sample.zip](https://github.com/cockroachdb/docs/raw/master/_includes/{{ page.version.version }}/app/insecure/jooq-basic-sample/jooq-basic-sample.zip). -2. Compile and run the code using Maven: - - {% include copy-clipboard.html %} - ~~~ shell - $ cd jooq-basic-sample - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ mvn compile - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ mvn exec:java -Dexec.mainClass=com.cockroachlabs.Sample - ~~~ - -Here are the contents of [`Sample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/insecure/jooq-basic-sample/Sample.java), the Java file containing the main `Sample` class: - -{% include copy-clipboard.html %} -~~~ java -{% include {{page.version.version}}/app/insecure/jooq-basic-sample/Sample.java %} -~~~ - -Toward the end of the output, you should see: - -~~~ -APP: BEGIN; -APP: addAccounts() --> 1 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(1) --> 1000 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(2) --> 250 -APP: COMMIT; -APP: getAccountBalance(1) --> 1000 -APP: getAccountBalance(2) --> 250 -APP: BEGIN; -APP: transferFunds(1, 2, 100) --> 100 -APP: COMMIT; -APP: transferFunds(1, 2, 100) --> 100 -APP: BEGIN; -APP: getAccountBalance(1) --> 900 -APP: COMMIT; -APP: BEGIN; -APP: getAccountBalance(2) --> 350 -APP: COMMIT; -APP: getAccountBalance(1) --> 900 -APP: getAccountBalance(2) --> 350 -~~~ - -To verify that the account balances were updated successfully, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -~~~ - -To check the account balances, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -SELECT id, balance FROM accounts; -~~~ - -~~~ - id | balance -+----+---------+ - 1 | 900 - 2 | 350 - 3 | 314159 -(3 rows) -~~~ - -
      - - -## What's next? - -Read more about using [jOOQ](https://www.jooq.org/), or check out a more realistic implementation of jOOQ with CockroachDB in our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. - -{% include {{page.version.version}}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-java-app-with-cockroachdb.md b/src/current/v19.2/build-a-java-app-with-cockroachdb.md deleted file mode 100644 index afea628de09..00000000000 --- a/src/current/v19.2/build-a-java-app-with-cockroachdb.md +++ /dev/null @@ -1,268 +0,0 @@ ---- -title: Build a Java App with CockroachDB and JDBC -summary: Learn how to use CockroachDB from a simple Java application with the JDBC driver. -toc: true -twitter: false ---- - - - -This tutorial shows you how to build a simple Java application with CockroachDB and the Java JDBC driver. - -{% include {{page.version.version}}/app/java-version-note.md %} - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the Java JDBC driver - -Download and set up the Java JDBC driver as described in the [official documentation](https://jdbc.postgresql.org/documentation/setup/). We recommend using the latest PostgreSQL JDBC 42.2.x driver. - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -You can pass the [`--also-generate-pkcs8-key` flag](cockroach-cert.html#flag-pkcs8) to generate a key in [PKCS#8 format](https://tools.ietf.org/html/rfc5208), which is the standard key encoding format in Java. In this case, the generated PKCS8 key will be named `client.maxroach.key.pk8`. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key --also-generate-pkcs8-key -~~~ - -## Step 4. Run the Java code - -The code below uses JDBC and the [Data Access Object (DAO)](https://en.wikipedia.org/wiki/Data_access_object) pattern to map Java methods to SQL operations. It consists of two classes: - -1. `BasicExample`, which is where the application logic lives. -2. `BasicExampleDAO`, which is used by the application to access the data store (in this case CockroachDB). This class has logic to handle [transaction retries](transactions.html#transaction-retries) (see the `BasicExampleDAO.runSQL()` method). - -It performs the following steps which roughly correspond to method calls in the `BasicExample` class. - -| Step | Method | -|------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------| -| 1. Create an `accounts` table in the `bank` database | `BasicExampleDAO.createAccounts()` | -| 2. Insert account data using a `Map` that corresponds to the input to `INSERT` on the backend | `BasicExampleDAO.updateAccounts(Map balance)` | -| 3. Transfer money from one account to another, printing out account balances before and after the transfer | `BasicExampleDAO.transferFunds(int from, int to, int amount)` | -| 4. Insert random account data using JDBC's bulk insertion support | `BasicExampleDAO.bulkInsertRandomAccountData()` | -| 5. Print out some account data | `BasicExampleDAO.readAccounts(int limit)` | -| 6. Drop the `accounts` table and perform any other necessary cleanup | `BasicExampleDAO.tearDown()` (This cleanup step means you can run this program more than once.) | - -It does all of the above using the practices we recommend for using JDBC with CockroachDB, which are listed in the [Recommended Practices](#recommended-practices) section below. - -To run it: - -1. Download [`BasicExample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/BasicExample.java), or create the file yourself and copy the code below. -2. Compile and run the code (adding the PostgreSQL JDBC driver to your classpath): - - {% include copy-clipboard.html %} - ~~~ shell - $ javac -classpath .:/path/to/postgresql.jar BasicExample.java - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ java -classpath .:/path/to/postgresql.jar BasicExample - ~~~ - -The contents of [`BasicExample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/BasicExample.java): - -{% include copy-clipboard.html %} -~~~ java -{% include {{page.version.version}}/app/BasicExample.java %} -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Java code - -The code below uses JDBC and the [Data Access Object (DAO)](https://en.wikipedia.org/wiki/Data_access_object) pattern to map Java methods to SQL operations. It consists of two classes: - -1. `BasicExample`, which is where the application logic lives. -2. `BasicExampleDAO`, which is used by the application to access the data store (in this case CockroachDB). This class has logic to handle [transaction retries](transactions.html#transaction-retries) (see the `BasicExampleDAO.runSQL()` method). - -It performs the following steps which roughly correspond to method calls in the `BasicExample` class. - -1. Create an `accounts` table in the `bank` database (`BasicExampleDAO.createAccounts()`). -2. Insert account data using a `Map` that corresponds to the input to `INSERT` on the backend (`BasicExampleDAO.updateAccounts(Map balance)`). -3. Transfer money from one account to another, printing out account balances before and after the transfer (`BasicExampleDAO.transferFunds(int from, int to, int amount)`). -4. Insert random account data using JDBC's bulk insertion support (`BasicExampleDAO.bulkInsertRandomAccountData()`). -5. Print out (some) account data (`BasicExampleDAO.readAccounts(int limit)`). -6. Drop the `accounts` table and perform any other necessary cleanup (`BasicExampleDAO.tearDown()`). (Note that you can run this program as many times as you want due to this cleanup step.) - -It does all of the above using the practices we recommend for using JDBC with CockroachDB, which are listed in the [Recommended Practices](#recommended-practices) section below. - -To run it: - -1. Download [the PostgreSQL JDBC driver](https://jdbc.postgresql.org/download/). -1. Compile and run the code (adding the PostgreSQL JDBC driver to your classpath): - - {% include copy-clipboard.html %} - ~~~ shell - $ javac -classpath .:/path/to/postgresql.jar BasicExample.java - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ java -classpath .:/path/to/postgresql.jar BasicExample - ~~~ - -The contents of [`BasicExample.java`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{page.version.version}}/app/insecure/BasicExample.java): - -{% include copy-clipboard.html %} -~~~ java -{% include {{page.version.version}}/app/insecure/BasicExample.java %} -~~~ - -
      - -The output will look like the following: - -~~~ -BasicExampleDAO.createAccounts: - 'CREATE TABLE IF NOT EXISTS accounts (id INT PRIMARY KEY, balance INT, CONSTRAINT balance_gt_0 CHECK (balance >= 0))' - -BasicExampleDAO.updateAccounts: - 'INSERT INTO accounts (id, balance) VALUES (1, 1000)' - -BasicExampleDAO.updateAccounts: - 'INSERT INTO accounts (id, balance) VALUES (2, 250)' -BasicExampleDAO.updateAccounts: - => 2 total updated accounts -main: - => Account balances at time '11:54:06.904': - ID 1 => $1000 - ID 2 => $250 - -BasicExampleDAO.transferFunds: - 'UPSERT INTO accounts (id, balance) VALUES(1, ((SELECT balance FROM accounts WHERE id = 1) - 100)),(2, ((SELECT balance FROM accounts WHERE id = 2) + 100))' -BasicExampleDAO.transferFunds: - => $100 transferred between accounts 1 and 2, 2 rows updated -main: - => Account balances at time '11:54:06.985': - ID 1 => $900 - ID 2 => $350 - -BasicExampleDAO.bulkInsertRandomAccountData: - 'INSERT INTO accounts (id, balance) VALUES (354685257, 158423397)' - => 128 row(s) updated in this batch - -BasicExampleDAO.bulkInsertRandomAccountData: - 'INSERT INTO accounts (id, balance) VALUES (206179866, 950590234)' - => 128 row(s) updated in this batch - -BasicExampleDAO.bulkInsertRandomAccountData: - 'INSERT INTO accounts (id, balance) VALUES (708995411, 892928833)' - => 128 row(s) updated in this batch - -BasicExampleDAO.bulkInsertRandomAccountData: - 'INSERT INTO accounts (id, balance) VALUES (500817884, 189050420)' - => 128 row(s) updated in this batch - -BasicExampleDAO.bulkInsertRandomAccountData: - => finished, 512 total rows inserted - -BasicExampleDAO.readAccounts: - 'SELECT id, balance FROM accounts LIMIT 10' - id => 1 - balance => 900 - id => 2 - balance => 350 - id => 190756 - balance => 966414958 - id => 1002343 - balance => 243354081 - id => 1159751 - balance => 59745201 - id => 2193125 - balance => 346719279 - id => 2659707 - balance => 770266587 - id => 6819325 - balance => 511618834 - id => 9985390 - balance => 905049643 - id => 12256472 - balance => 913034434 - -BasicExampleDAO.tearDown: - 'DROP TABLE accounts' -~~~ - -## Recommended Practices - -### Use `IMPORT` to read in large data sets - -If you are trying to get a large data set into CockroachDB all at once (a bulk import), avoid writing client-side code altogether and use the [`IMPORT`](import.html) statement instead. It is much faster and more efficient than making a series of [`INSERT`s](insert.html) and [`UPDATE`s](update.html). It bypasses the [SQL layer](architecture/sql-layer.html) altogether and writes directly to the [storage layer](architecture/storage-layer.html) of the database. - -For more information about importing data from Postgres, see [Migrate from Postgres](migrate-from-postgres.html). - -For more information about importing data from MySQL, see [Migrate from MySQL](migrate-from-mysql.html). - -### Use `rewriteBatchedInserts` for increased speed - -We strongly recommend setting `rewriteBatchedInserts=true`; we have seen 2-3x performance improvements with it enabled. From [the JDBC connection parameters documentation](https://jdbc.postgresql.org/documentation/use/#connection-parameters): - -> This will change batch inserts from `insert into foo (col1, col2, col3) values (1,2,3)` into `insert into foo (col1, col2, col3) values (1,2,3), (4,5,6)` this provides 2-3x performance improvement - -### Use a batch size of 128 - -PGJDBC's batching support only works with [powers of two](https://github.com/pgjdbc/pgjdbc/blob/7b52b0c9e5b9aa9a9c655bb68f23bf4ec57fd51c/pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java#L1597), and will split batches of other sizes up into multiple sub-batches. This means that a batch of size 128 can be 6x faster than a batch of size 250. - -The code snippet below shows a pattern for using a batch size of 128, and is taken from the longer example above (specifically, the `BasicExampleDAO.bulkInsertRandomAccountData()` method). - -Specifically, it does the following: - -1. Turn off auto-commit so you can manage the transaction lifecycle and thus the size of the batch inserts. -2. Given an overall update size of 500 rows (for example), split it into batches of size 128 and execute each batch in turn. -3. Finally, commit the batches of statements you've just executed. - -~~~ java -int BATCH_SIZE = 128; -connection.setAutoCommit(false); - -try (PreparedStatement pstmt = connection.prepareStatement("INSERT INTO accounts (id, balance) VALUES (?, ?)")) { - for (int i=0; i<=(500/BATCH_SIZE);i++) { - for (int j=0; j %s row(s) updated in this batch\n", count.length); // Verifying 128 rows in the batch - } - connection.commit(); -} -~~~ - -### Retrieve large data sets in chunks using cursors - -New in v19.2: CockroachDB now supports the Postgres wire-protocol cursors for implicit transactions and explicit transactions executed to completion. This means the [PGJDBC driver](https://jdbc.postgresql.org) can use this protocol to stream queries with large result sets. This is much faster than [paginating through results in SQL using `LIMIT .. OFFSET`](selection-queries.html#paginate-through-limited-results). - -For instructions showing how to use cursors in your Java code, see [Getting results based on a cursor](https://jdbc.postgresql.org/documentation/query/#getting-results-based-on-a-cursor) from the PGJDBC documentation. - -Note that interleaved execution (partial execution of multiple statements within the same connection and transaction) is not supported when [`Statement.setFetchSize()`](https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#setFetchSize-int-) is used. - -## What's next? - -Read more about using the [Java JDBC driver](https://jdbc.postgresql.org/). - -{% include {{page.version.version}}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-nodejs-app-with-cockroachdb-sequelize.md b/src/current/v19.2/build-a-nodejs-app-with-cockroachdb-sequelize.md deleted file mode 100644 index f4e42eaeab6..00000000000 --- a/src/current/v19.2/build-a-nodejs-app-with-cockroachdb-sequelize.md +++ /dev/null @@ -1,165 +0,0 @@ ---- -title: Build a Node.js App with CockroachDB and Sequelize -summary: Learn how to use CockroachDB from a simple Node.js application with the Sequelize ORM. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Node.js application with CockroachDB and the Sequelize ORM. - -We have tested the [Sequelize ORM](https://sequelize.org/) enough to claim **beta-level** support. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -{{site.data.alerts.callout_success}} -For a more realistic use of Sequelize with CockroachDB, see our [`examples-orms`](https://github.com/cockroachdb/examples-orms)repository. -{{site.data.alerts.end}} - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the Sequelize ORM - -To install Sequelize, as well as a [CockroachDB Node.js package](https://github.com/cockroachdb/sequelize-cockroachdb) that accounts for some minor differences between CockroachDB and PostgreSQL, run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ npm install sequelize sequelize-cockroachdb -~~~ - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the Node.js code - -The following code uses the [Sequelize](https://sequelize.org/) ORM to map Node.js-specific objects to SQL operations. Specifically, `Account.sync({force: true})` creates an `accounts` table based on the Account model (or drops and recreates the table if it already exists), `Account.bulkCreate([...])` inserts rows into the table, and `Account.findAll()` selects from the table so that balances can be printed. - -Copy the code or -download it directly. - -{% include copy-clipboard.html %} -~~~ js -{% include {{ page.version.version }}/app/sequelize-basic-sample.js %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ node sequelize-basic-sample.js -~~~ - -The output should be: - -~~~ shell -1 1000 -2 250 -~~~ - -To verify that funds were transferred from one account to another, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=/tmp/certs -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 1000 | -| 2 | 250 | -+----+---------+ -(2 rows) -~~~ - -
      - - - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Node.js code - -The following code uses the [Sequelize](https://sequelize.org/) ORM to map Node.js-specific objects to SQL operations. Specifically, `Account.sync({force: true})` creates an `accounts` table based on the Account model (or drops and recreates the table if it already exists), `Account.bulkCreate([...])` inserts rows into the table, and `Account.findAll()` selects from the table so that balances can be printed. - -Copy the code or -download it directly. - -{% include copy-clipboard.html %} -~~~ js -{% include {{ page.version.version }}/app/insecure/sequelize-basic-sample.js %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ node sequelize-basic-sample.js -~~~ - -The output should be: - -~~~ shell -1 1000 -2 250 -~~~ - -To verify that the table and rows were created successfully, you can again use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e 'SHOW TABLES' --database=bank -~~~ - -~~~ -+------------+ -| table_name | -+------------+ -| accounts | -+------------+ -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 1000 | -| 2 | 250 | -+----+---------+ -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [Sequelize ORM](https://sequelize.org/), or check out a more realistic implementation of Sequelize with CockroachDB in our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-nodejs-app-with-cockroachdb.md b/src/current/v19.2/build-a-nodejs-app-with-cockroachdb.md deleted file mode 100644 index 7b95ac721a7..00000000000 --- a/src/current/v19.2/build-a-nodejs-app-with-cockroachdb.md +++ /dev/null @@ -1,230 +0,0 @@ ---- -title: Build a Node.js App with CockroachDB and the Node.js pg Driver -summary: Learn how to use CockroachDB from a simple Node.js application with the Node.js pg driver. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Node.js application with CockroachDB and the Node.js pg driver. - -We have tested the [Node.js pg driver](https://www.npmjs.com/package/pg) enough to claim **beta-level** support. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install Node.js packages - -To let your application communicate with CockroachDB, install the [Node.js pg driver](https://www.npmjs.com/package/pg): - -{% include copy-clipboard.html %} -~~~ shell -$ npm install pg -~~~ - -The example app on this page also requires [`async`](https://www.npmjs.com/package/async): - -{% include copy-clipboard.html %} -~~~ shell -$ npm install async -~~~ - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the Node.js code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, creating a table, inserting rows, and reading and printing the rows. - -Download the [`basic-sample.js`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{ page.version.version }}/app/basic-sample.js) file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ js -{% include {{page.version.version}}/app/basic-sample.js %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ node basic-sample.js -~~~ - -The output should be: - -~~~ -Initial balances: -{ id: '1', balance: '1000' } -{ id: '2', balance: '250' } -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another and then read the updated values, where all included statements are either committed or aborted. - -Download the [`txn-sample.js`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{ page.version.version }}/app/txn-sample.js) file, or create the file yourself and copy the code into it. - -{% include {{ page.version.version }}/client-transaction-retry.md %} - -{% include copy-clipboard.html %} -~~~ js -{% include {{page.version.version}}/app/txn-sample.js %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ node txn-sample.js -~~~ - -The output should be: - -~~~ -Balances after transfer: -{ id: '1', balance: '900' } -{ id: '2', balance: '350' } -~~~ - -To verify that funds were transferred from one account to another, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --database=bank -~~~ - -To check the account balances, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT id, balance FROM accounts; -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Node.js code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, creating a table, inserting rows, and reading and printing the rows. - -Download the [`basic-sample.js`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{ page.version.version }}/app/insecure/basic-sample.js) file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ js -{% include {{page.version.version}}/app/insecure/basic-sample.js %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ node basic-sample.js -~~~ - -The output should be: - -~~~ -Initial balances: -{ id: '1', balance: '1000' } -{ id: '2', balance: '250' } -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another and then read the updated values, where all included statements are either committed or aborted. - -Download the [`txn-sample.js`](https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/{{ page.version.version }}/app/insecure/txn-sample.js) file, or create the file yourself and copy the code into it. - -{% include {{ page.version.version }}/client-transaction-retry.md %} - -{% include copy-clipboard.html %} -~~~ js -{% include {{page.version.version}}/app/insecure/txn-sample.js %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ node txn-sample.js -~~~ - -The output should be: - -~~~ -Balances after transfer: -{ id: '1', balance: '900' } -{ id: '2', balance: '350' } -~~~ - -To verify that funds were transferred from one account to another, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --database=bank -~~~ - -To check the account balances, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT id, balance FROM accounts; -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [Node.js pg driver](https://www.npmjs.com/package/pg). - -{% include {{page.version.version}}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-php-app-with-cockroachdb.md b/src/current/v19.2/build-a-php-app-with-cockroachdb.md deleted file mode 100644 index 4ea50547d6c..00000000000 --- a/src/current/v19.2/build-a-php-app-with-cockroachdb.md +++ /dev/null @@ -1,175 +0,0 @@ ---- -title: Build a PHP App with CockroachDB and php-pgsql -summary: Learn how to use CockroachDB from a simple PHP application with a low-level client driver. -toc: true -twitter: false ---- - -This tutorial shows you how build a simple PHP application with CockroachDB and the php-pgsql driver. - -We have tested the [php-pgsql driver](https://www.php.net/manual/en/book.pgsql.php) enough to claim **beta-level** support. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the php-pgsql driver - -Install the php-pgsql driver as described in the [official documentation](https://www.php.net/manual/en/book.pgsql.php). - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the PHP code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, inserting rows and reading and printing the rows. - -Download the basic-sample.php file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ php -{% include {{ page.version.version }}/app/basic-sample.php %} -~~~ - -The output should be: - -~~~ shell -Account balances: -1: 1000 -2: 250 -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Download the txn-sample.php file, or create the file yourself and copy the code into it. - -{{site.data.alerts.callout_info}} -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic **retry function** that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ php -{% include {{ page.version.version }}/app/txn-sample.php %} -~~~ - -The output should be: - -~~~ shell -Account balances after transfer: -1: 900 -2: 350 -~~~ - -To verify that funds were transferred from one account to another, use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the PHP code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, inserting rows and reading and printing the rows. - -Download the basic-sample.php file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ php -{% include {{ page.version.version }}/app/insecure/basic-sample.php %} -~~~ - -The output should be: - -~~~ shell -Account balances: -1: 1000 -2: 250 -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Download the txn-sample.php file, or create the file yourself and copy the code into it. - -{{site.data.alerts.callout_info}} -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic **retry function** that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ php -{% include {{ page.version.version }}/app/insecure/txn-sample.php %} -~~~ - -The output should be: - -~~~ shell -Account balances after transfer: -1: 900 -2: 350 -~~~ - -To verify that funds were transferred from one account to another, use the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [php-pgsql driver](https://www.php.net/manual/en/book.pgsql.php). - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-python-app-with-cockroachdb-django.md b/src/current/v19.2/build-a-python-app-with-cockroachdb-django.md deleted file mode 100644 index 0ce17a0b2b7..00000000000 --- a/src/current/v19.2/build-a-python-app-with-cockroachdb-django.md +++ /dev/null @@ -1,489 +0,0 @@ ---- -title: Build a Python App with CockroachDB and Django -summary: Learn how to use CockroachDB from a simple Django application. -toc: true -twitter: false ---- - -{% unless site.cockroachcloud %} - - - -{% endunless %} - -This tutorial shows you how build a simple Python application with CockroachDB and the [Django](https://www.djangoproject.com/) framework. - -CockroachDB supports Django versions 2.2 and 3.0. - -{% unless site.cockroachcloud %} - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -{% endunless %} - -{{site.data.alerts.callout_info}} -The example code and instructions on this page use Python 3 and Django 3.0. -{{site.data.alerts.end}} - -## Step 1. Install Django and the CockroachDB backend for Django - -Install [Django](https://docs.djangoproject.com/en/3.0/topics/install/): - -{% include copy-clipboard.html %} -~~~ shell -$ python -m pip install django==3.0.* -~~~ - -Before installing the [CockroachDB backend for Django](https://github.com/cockroachdb/django-cockroachdb), you must install one of the following psycopg2 prerequisites: - -- [psycopg2](https://pypi.org/project/psycopg2/), which has some [prerequisites](https://www.psycopg.org/docs/install.html#prerequisites) of its own. This package is recommended for production environments. - -- [psycopg2-binary](https://pypi.org/project/psycopg2-binary/). This package is recommended for development and testing. - -After you install the psycopg2 prerequisite, install the CockroachDB Django backend: - -{% include copy-clipboard.html %} -~~~ shell -$ python -m pip install django-cockroachdb==3.0.* -~~~ - -{{site.data.alerts.callout_info}} -The major version of `django-cockroachdb` must correspond to the major version of `django`. The minor release numbers do not need to match. -{{site.data.alerts.end}} - -{% unless site.cockroachcloud %} - -
      - -## Step 2. Create the `django` user and `bank` database and generate certificates - -Open a [SQL shell](cockroach-sql.html) to the running CockroachDB cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --host=localhost:26257 -~~~ - -In the SQL shell, issue the following statements to create the `django` user and `bank` database: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE USER IF NOT EXISTS django; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE bank; -~~~ - -Give the `django` user the necessary permissions: - -{% include copy-clipboard.html %} -~~~ sql -> GRANT ALL ON DATABASE bank TO django; -~~~ - -Exit the SQL shell: - -{% include copy-clipboard.html %} -~~~ sql -> \q -~~~ - -Create a certificate and key for the `django` user by running the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client django --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -
      - -
      - -## Step 2. Create the `django` user and `bank` database - -Open a [SQL shell](cockroach-sql.html) to the running CockroachDB cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --host=localhost:26257 -~~~ - -In the SQL shell, issue the following statements to create the `django` user and `bank` database: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE USER IF NOT EXISTS django; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE bank; -~~~ - -Give the `django` user the necessary permissions: - -{% include copy-clipboard.html %} -~~~ sql -> GRANT ALL ON DATABASE bank TO django; -~~~ - -Exit the SQL shell: - -{% include copy-clipboard.html %} -~~~ sql -> \q -~~~ - -
      - -{% endunless %} - -{% if site.cockroachcloud %} - -## Step 2: Connect to your CockroachCloud cluster and create the `django` user and `bank` database - -Connect to your CockroachCloud cluster using the [SQL shell](connect-to-your-cluster.html#use-the-cockroachdb-sql-client). - -In the SQL shell, issue the following statements to create the `django` user and `bank` database: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE USER IF NOT EXISTS django WITH PASSWORD 'password'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE bank; -~~~ - -Give the `django` user the necessary permissions: - -{% include copy-clipboard.html %} -~~~ sql -> GRANT ALL ON DATABASE bank TO django; -~~~ - -Exit the SQL shell: - -{% include copy-clipboard.html %} -~~~ sql -> \q -~~~ - -{% endif %} - -## Step 3. Create a Django project - -In the directory where you'd like to store your code, use the [`django-admin` command-line tool](https://docs.djangoproject.com/en/3.0/ref/django-admin/) to create an application project: - -{% include copy-clipboard.html %} -~~~ shell -$ django-admin startproject myproject -~~~ - -This creates a new project directory called `myproject`. `myproject` contains the [`manage.py` script](https://docs.djangoproject.com/en/3.0/ref/django-admin/) and a subdirectory, also named `myproject`, that contains some `.py` files. - -Open `myproject/myproject/settings.py`, and add `0.0.0.0` to the `ALLOWED_HOSTS` in your `settings.py` file, so that it reads as follows: - -{% include copy-clipboard.html %} -~~~ python -ALLOWED_HOSTS = ['0.0.0.0'] -~~~ - -In `myproject/myproject/settings.py`, add `myproject` to the list of `INSTALLED_APPS`, so that it reads as follows: - -{% include copy-clipboard.html %} -~~~ python -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'myproject', -] -~~~ - -The other installed applications listed are added to all starter Django applications by default. - -{% unless site.cockroachcloud %} - -In `myproject/myproject/settings.py`, change `DATABASES` to the following: - -
      - -{% include copy-clipboard.html %} -~~~ python -DATABASES = { - 'default': { - 'ENGINE': 'django_cockroachdb', - 'NAME': 'bank', - 'USER': 'django', - 'HOST': 'localhost', - 'PORT': '26257', - 'OPTIONS': { - 'sslmode': 'require', - 'sslrootcert': '/certs/ca.crt', - 'sslcert': '/certs/client.django.crt', - 'sslkey': '/certs/client.django.key', - }, - }, -} - -
      - -
      - -{% include copy-clipboard.html %} -~~~ python -DATABASES = { - 'default': { - 'ENGINE': 'django_cockroachdb', - 'NAME': 'bank', - 'USER': 'django', - 'HOST': 'localhost', - 'PORT': '26257', - } -} -~~~ - -
      - -{% endunless %} - -{% if site.cockroachcloud %} - -In the CockroachCloud console, generate the [connection parameters](connect-to-your-cluster.html#step-3-select-a-connection-method). Then in `myproject/myproject/settings.py`, change `DATABASES` to the following: - -{% include copy-clipboard.html %} -~~~ python -DATABASES = { - 'default': { - 'ENGINE': 'django_cockroachdb', - 'NAME': 'bank', - 'USER': 'django', - 'PASSWORD': 'password', - 'HOST': '', - 'PORT': '26257', - } -} -~~~ - -{% endif %} - -## Step 4. Write the application logic - -After you generate the initial Django project files, you need to build out the application with a few `.py` files in `myproject/myproject`. - -
      - -### Models - -Start by building some [models](https://docs.djangoproject.com/en/3.0/topics/db/models/), defined in a file called `models.py`. You can copy the sample code below and paste it into a new file, or you can download the file directly. - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/django-basic-sample/models.py %} -~~~ - -In this file, we define some simple classes that map to the tables in the example database `bank`. - -### Views - -Next, build out some [class-based views](https://docs.djangoproject.com/en/3.0/topics/class-based-views/) for the application in a file called `views.py`. You can copy the sample code below and paste it into a new file, or you can download the file directly. - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/django-basic-sample/views.py %} -~~~ - -This file defines the application's views as classes. Each view class corresponds to one of the table classes defined in `models.py`. The methods of these classes define read and write transactions on the tables in the database. - -Importantly, the file defines a [transaction retry loop](transactions.html#transaction-retries) in the decorator function `retry_on_exception()`. This function decorates each view method, ensuring that transaction ordering guarantees meet the ANSI [SERIALIZABLE](https://en.wikipedia.org/wiki/Isolation_(database_systems)#Serializable) isolation level. For more information about how transactions (and retries) work, see [Transactions](transactions.html). - -### URL routes - -Lastly, define some [URL routes](https://docs.djangoproject.com/en/3.0/topics/http/urls/) in a file called `urls.py`. The `django-admin` command-line tool generated this file when you created the Django project, so it should already exist in `myproject/myproject`. You can copy the sample code below and paste it into the existing `urls.py` file, or you can download the file directly and replace the existing one. - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/django-basic-sample/urls.py %} -~~~ - -
      - -
      - -### Models - -Start by building some [models](https://docs.djangoproject.com/en/3.0/topics/db/models/), defined in a file called `models.py`. You can copy the sample code below and paste it into a new file, or you can download the file directly. - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/insecure/django-basic-sample/models.py %} -~~~ - -In this file, we define some simple classes that map to the tables in the example database `bank`. - -### Views - -Next, build out some [class-based views](https://docs.djangoproject.com/en/3.0/topics/class-based-views/) for the application in a file called `views.py`. You can copy the sample code below and paste it into a new file, or you can download the file directly. - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/insecure/django-basic-sample/views.py %} -~~~ - -This file defines the application's views as classes. Each view class corresponds to one of the table classes defined in `models.py`. The methods of these classes define read and write transactions on the tables in the database. - -Importantly, the file defines a [transaction retry loop](transactions.html#transaction-retries) in the decorator function `retry_on_exception()`. This function decorates each view method, ensuring that transaction ordering guarantees meet the ANSI [SERIALIZABLE](https://en.wikipedia.org/wiki/Isolation_(database_systems)#Serializable) isolation level. For more information about how transactions (and retries) work, see [Transactions](transactions.html). - -### URL routes - -Lastly, define some [URL routes](https://docs.djangoproject.com/en/3.0/topics/http/urls/) in a file called `urls.py`. The `django-admin` command-line tool generated this file when you created the Django project, so it should already exist in `myproject/myproject`. You can copy the sample code below and paste it into the existing `urls.py` file, or you can download the file directly and replace the existing one. - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/insecure/django-basic-sample/urls.py %} -~~~ - -
      - -## Step 5. Set up and run the Django app - -In the top `myproject` directory, use the [`manage.py` script](https://docs.djangoproject.com/en/3.0/ref/django-admin/) to create [Django migrations](https://docs.djangoproject.com/en/3.0/topics/migrations/) that initialize the database for the application: - -{% include copy-clipboard.html %} -~~~ shell -$ python manage.py makemigrations myproject -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ python manage.py migrate -~~~ - -This initializes the `bank` database with the tables defined in `models.py`, in addition to some other tables for the admin functionality included with Django's starter application. - -{% unless site.cockroachcloud %} - -
      - -To verify that the migration succeeded, open a [SQL shell](cockroach-sql.html) to the running CockroachDB cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --host=localhost:26257 -~~~ - -
      - -
      - -To verify that the migration succeeded, open a [SQL shell](cockroach-sql.html) to the running CockroachDB cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --host=localhost:26257 -~~~ - -
      - -{% endunless %} - -{% if site.cockroachcloud %} - -To verify that the migration succeeded, connect to your CockroachCloud cluster using the [SQL shell](connect-to-your-cluster.html#use-the-cockroachdb-sql-client) and issue the following statements: - -{% endif %} - -{% include copy-clipboard.html %} -~~~ sql -> USE bank; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES; -~~~ - -~~~ - table_name -+----------------------------+ - auth_group - auth_group_permissions - auth_permission - auth_user - auth_user_groups - auth_user_user_permissions - django_admin_log - django_content_type - django_migrations - django_session - myproject_customers - myproject_orders - myproject_orders_product - myproject_products -(14 rows) -~~~ - -In a new terminal, navigate to the top of the `myproject` directory, and start the app: - -{% include copy-clipboard.html %} -~~~ shell -$ python manage.py runserver 0.0.0.0:8000 -~~~ - -To perform simple reads and writes to the database, you can send HTTP requests to the application. - -For example, in a new terminal, you can use `curl` to send a POST request to the application that inserts a new row into the `customers` table: - -{% include copy-clipboard.html %} -~~~ shell -$ curl --header "Content-Type: application/json" \ ---request POST \ ---data '{"name":"Carl"}' http://0.0.0.0:8000/customer/ -~~~ - -You can then send a GET request to read from that table: - -{% include copy-clipboard.html %} -~~~ shell -$ curl http://0.0.0.0:8000/customer/ -~~~ - -~~~ -[{"id": 523377322022797313, "name": "Carl"}] -~~~ - -You can also query the tables directly in the SQL shell to see the changes: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM myproject_customers; -~~~ - -~~~ - id | name -+--------------------+------+ - 523377322022797313 | Carl -(1 row) -~~~ - - -## What's next? - -Read more about writing a [Django app](https://docs.djangoproject.com/en/3.0/intro/tutorial01/). - -{% include {{page.version.version}}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-python-app-with-cockroachdb-sqlalchemy.md b/src/current/v19.2/build-a-python-app-with-cockroachdb-sqlalchemy.md deleted file mode 100644 index 8a41964add4..00000000000 --- a/src/current/v19.2/build-a-python-app-with-cockroachdb-sqlalchemy.md +++ /dev/null @@ -1,319 +0,0 @@ ---- -title: Build a Python App with CockroachDB and SQLAlchemy -summary: Learn how to use CockroachDB from a simple Python application with SQLAlchemy. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Python application with CockroachDB and the [SQLAlchemy](https://docs.sqlalchemy.org/en/latest/) ORM. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -{{site.data.alerts.callout_info}} -The example code on this page uses Python 3. -{{site.data.alerts.end}} - -{{site.data.alerts.callout_danger}} -SQLAlchemy relies on the existence of [foreign keys](foreign-key.html) to generate [`JOIN` expressions](joins.html) from your application code. If you remove foreign keys from your schema, SQLAlchemy will not generate joins for you. As a workaround, you can [create a "custom foreign condition" by adding a `relationship` field to your table objects](https://stackoverflow.com/questions/37806625/sqlalchemy-create-relations-but-without-foreign-key-constraint-in-db), or do the equivalent work in your application. -{{site.data.alerts.end}} - -## Step 1. Install SQLAlchemy - -To install SQLAlchemy, as well as a [CockroachDB Python package](https://github.com/cockroachdb/sqlalchemy-cockroachdb) that accounts for some differences between CockroachDB and PostgreSQL, run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ pip install sqlalchemy sqlalchemy-cockroachdb psycopg2 -~~~ - -{{site.data.alerts.callout_success}} -You can substitute psycopg2 for other alternatives that include the psycopg python package. -{{site.data.alerts.end}} - -For other ways to install SQLAlchemy, see the [official documentation](http://docs.sqlalchemy.org/en/latest/intro.html#installation-guide). - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the Python code - -The code below uses [SQLAlchemy](https://docs.sqlalchemy.org/en/latest/) to map Python objects and methods to SQL operations. - -You can run this script as many times as you want; on each run, the script will create some new accounts and shuffle money around between randomly selected accounts. - -Specifically, the script: - -1. Reads in existing account IDs (if any) from the `bank` database. -2. Creates additional accounts with randomly generated IDs. Then, it adds a bit of money to each new account. -3. Chooses two accounts at random and takes half of the money from the first and deposits it into the second. - -It does all of the above using the practices we recommend for using SQLAlchemy with CockroachDB, which are listed in the [Best practices](#best-practices) section below. - -{{site.data.alerts.callout_info}} -You must use the `cockroachdb://` prefix in the URL passed to [`sqlalchemy.create_engine`](https://docs.sqlalchemy.org/en/latest/core/engines.html?highlight=create_engine#sqlalchemy.create_engine) to make sure the [`cockroachdb`](https://github.com/cockroachdb/sqlalchemy-cockroachdb) dialect is used. Using the `postgres://` URL prefix to connect to your CockroachDB cluster will not work. -{{site.data.alerts.end}} - -Copy the code below or -download it directly. - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/sqlalchemy-basic-sample.py %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ python sqlalchemy-basic-sample.py -~~~ - -The output should look something like the following: - -~~~ shell -2018-12-06 15:59:58,999 INFO sqlalchemy.engine.base.Engine select current_schema() -2018-12-06 15:59:58,999 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,001 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1 -2018-12-06 15:59:59,001 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,001 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1 -2018-12-06 15:59:59,001 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,002 INFO sqlalchemy.engine.base.Engine select version() -2018-12-06 15:59:59,002 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,003 INFO sqlalchemy.engine.base.Engine SELECT table_name FROM information_schema.tables WHERE table_schema=%s -2018-12-06 15:59:59,004 INFO sqlalchemy.engine.base.Engine ('public',) -2018-12-06 15:59:59,005 INFO sqlalchemy.engine.base.Engine SELECT id from accounts; -2018-12-06 15:59:59,005 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,008 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) -2018-12-06 15:59:59,008 INFO sqlalchemy.engine.base.Engine SAVEPOINT cockroach_restart -2018-12-06 15:59:59,008 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,083 INFO sqlalchemy.engine.base.Engine INSERT INTO accounts (id, balance) VALUES (%(id)s, %(balance)s) -2018-12-06 15:59:59,083 INFO sqlalchemy.engine.base.Engine ({'id': 298865, 'balance': 208217}, {'id': 506738, 'balance': 962549}, {'id': 514698, 'balance': 986327}, {'id': 587747, 'balance': 210406}, {'id': 50148, 'balance': 347976}, {'id': 854295, 'balance': 420086}, {'id': 785757, 'balance': 364836}, {'id': 406247, 'balance': 787016} ... displaying 10 of 100 total bound parameter sets ... {'id': 591336, 'balance': 542066}, {'id': 33728, 'balance': 526531}) -2018-12-06 15:59:59,201 INFO sqlalchemy.engine.base.Engine RELEASE SAVEPOINT cockroach_restart -2018-12-06 15:59:59,201 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,205 INFO sqlalchemy.engine.base.Engine COMMIT -2018-12-06 15:59:59,206 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) -2018-12-06 15:59:59,206 INFO sqlalchemy.engine.base.Engine SAVEPOINT cockroach_restart -2018-12-06 15:59:59,206 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,207 INFO sqlalchemy.engine.base.Engine SELECT accounts.id AS accounts_id, accounts.balance AS accounts_balance -FROM accounts -WHERE accounts.id = %(id_1)s -2018-12-06 15:59:59,207 INFO sqlalchemy.engine.base.Engine {'id_1': 769626} -2018-12-06 15:59:59,209 INFO sqlalchemy.engine.base.Engine UPDATE accounts SET balance=%(balance)s WHERE accounts.id = %(accounts_id)s -2018-12-06 15:59:59,209 INFO sqlalchemy.engine.base.Engine {'balance': 470580, 'accounts_id': 769626} -2018-12-06 15:59:59,212 INFO sqlalchemy.engine.base.Engine UPDATE accounts SET balance=(accounts.balance + %(balance_1)s) WHERE accounts.id = %(id_1)s -2018-12-06 15:59:59,247 INFO sqlalchemy.engine.base.Engine {'balance_1': 470580, 'id_1': 158447} -2018-12-06 15:59:59,249 INFO sqlalchemy.engine.base.Engine RELEASE SAVEPOINT cockroach_restart -2018-12-06 15:59:59,250 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,251 INFO sqlalchemy.engine.base.Engine COMMIT -~~~ - -To verify that the table and rows were created successfully, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --database=bank -~~~ - -Then, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT COUNT(*) FROM accounts; -~~~ - -~~~ - count -------- - 100 -(1 row) -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Python code - -The code below uses [SQLAlchemy](https://docs.sqlalchemy.org/en/latest/) to map Python objects and methods to SQL operations. - -You can run this script as many times as you want; on each run, it will create some new accounts and shuffle money around between randomly selected accounts. - -Specifically, it: - -1. Reads in existing account IDs (if any) from the `bank` database. -2. Creates additional accounts with randomly generated IDs. Then, it adds a bit of money to each new account. -3. Chooses two accounts at random and takes half of the money from the first and deposits it into the second. - -It does all of the above using the practices we recommend for using SQLAlchemy with CockroachDB, which are listed in the [Best practices](#best-practices) section below. - -{{site.data.alerts.callout_info}} -You must use the `cockroachdb://` prefix in the URL passed to [`sqlalchemy.create_engine`](https://docs.sqlalchemy.org/en/latest/core/engines.html?highlight=create_engine#sqlalchemy.create_engine) to make sure the [`cockroachdb`](https://github.com/cockroachdb/sqlalchemy-cockroachdb) dialect is used. Using the `postgres://` URL prefix to connect to your CockroachDB cluster will not work. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/sqlalchemy-basic-sample.py %} -~~~ - -Run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ python example.py -~~~ - -The output should look something like the following: - -~~~ shell -2018-12-06 15:59:58,999 INFO sqlalchemy.engine.base.Engine select current_schema() -2018-12-06 15:59:58,999 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,001 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1 -2018-12-06 15:59:59,001 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,001 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1 -2018-12-06 15:59:59,001 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,002 INFO sqlalchemy.engine.base.Engine select version() -2018-12-06 15:59:59,002 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,003 INFO sqlalchemy.engine.base.Engine SELECT table_name FROM information_schema.tables WHERE table_schema=%s -2018-12-06 15:59:59,004 INFO sqlalchemy.engine.base.Engine ('public',) -2018-12-06 15:59:59,005 INFO sqlalchemy.engine.base.Engine SELECT id from accounts; -2018-12-06 15:59:59,005 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,008 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) -2018-12-06 15:59:59,008 INFO sqlalchemy.engine.base.Engine SAVEPOINT cockroach_restart -2018-12-06 15:59:59,008 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,083 INFO sqlalchemy.engine.base.Engine INSERT INTO accounts (id, balance) VALUES (%(id)s, %(balance)s) -2018-12-06 15:59:59,083 INFO sqlalchemy.engine.base.Engine ({'id': 298865, 'balance': 208217}, {'id': 506738, 'balance': 962549}, {'id': 514698, 'balance': 986327}, {'id': 587747, 'balance': 210406}, {'id': 50148, 'balance': 347976}, {'id': 854295, 'balance': 420086}, {'id': 785757, 'balance': 364836}, {'id': 406247, 'balance': 787016} ... displaying 10 of 100 total bound parameter sets ... {'id': 591336, 'balance': 542066}, {'id': 33728, 'balance': 526531}) -2018-12-06 15:59:59,201 INFO sqlalchemy.engine.base.Engine RELEASE SAVEPOINT cockroach_restart -2018-12-06 15:59:59,201 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,205 INFO sqlalchemy.engine.base.Engine COMMIT -2018-12-06 15:59:59,206 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) -2018-12-06 15:59:59,206 INFO sqlalchemy.engine.base.Engine SAVEPOINT cockroach_restart -2018-12-06 15:59:59,206 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,207 INFO sqlalchemy.engine.base.Engine SELECT accounts.id AS accounts_id, accounts.balance AS accounts_balance -FROM accounts -WHERE accounts.id = %(id_1)s -2018-12-06 15:59:59,207 INFO sqlalchemy.engine.base.Engine {'id_1': 769626} -2018-12-06 15:59:59,209 INFO sqlalchemy.engine.base.Engine UPDATE accounts SET balance=%(balance)s WHERE accounts.id = %(accounts_id)s -2018-12-06 15:59:59,209 INFO sqlalchemy.engine.base.Engine {'balance': 470580, 'accounts_id': 769626} -2018-12-06 15:59:59,212 INFO sqlalchemy.engine.base.Engine UPDATE accounts SET balance=(accounts.balance + %(balance_1)s) WHERE accounts.id = %(id_1)s -2018-12-06 15:59:59,247 INFO sqlalchemy.engine.base.Engine {'balance_1': 470580, 'id_1': 158447} -2018-12-06 15:59:59,249 INFO sqlalchemy.engine.base.Engine RELEASE SAVEPOINT cockroach_restart -2018-12-06 15:59:59,250 INFO sqlalchemy.engine.base.Engine {} -2018-12-06 15:59:59,251 INFO sqlalchemy.engine.base.Engine COMMIT -~~~ - -To verify that the table and rows were created successfully, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --database=bank -~~~ - -Then, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT COUNT(*) FROM accounts; -~~~ - -~~~ - count -------- - 100 -(1 row) -~~~ - -
      - -## Best practices - -### Use the `run_transaction` function - -We strongly recommend using the [`sqlalchemy_cockroachdb.run_transaction()`](https://github.com/cockroachdb/sqlalchemy-cockroachdb/blob/master/sqlalchemy_cockroachdb/transaction.py) function as shown in the code samples on this page. This abstracts the details of [transaction retries](transactions.html#transaction-retries) away from your application code. Transaction retries are more frequent in CockroachDB than in some other databases because we use [optimistic concurrency control](https://en.wikipedia.org/wiki/Optimistic_concurrency_control) rather than locking. Because of this, a CockroachDB transaction may have to be tried more than once before it can commit. This is part of how we ensure that our transaction ordering guarantees meet the ANSI [SERIALIZABLE](https://en.wikipedia.org/wiki/Isolation_(database_systems)#Serializable) isolation level. - -In addition to the above, using `run_transaction` has the following benefits: - -- Because it must be passed a [sqlalchemy.orm.session.sessionmaker](https://docs.sqlalchemy.org/en/latest/orm/session_api.html#session-and-sessionmaker) object (*not* a [session][session]), it ensures that a new session is created exclusively for use by the callback, which protects you from accidentally reusing objects via any sessions created outside the transaction. -- It abstracts away the [client-side transaction retry logic](transactions.html#client-side-intervention) from your application, which keeps your application code portable across different databases. For example, the sample code given on this page works identically when run against Postgres (modulo changes to the prefix and port number in the connection string). - - -For more information about how transactions (and retries) work, see [Transactions](transactions.html). - -### Avoid mutations of session and/or transaction state inside `run_transaction()` - -In general, this is in line with the recommendations of the [SQLAlchemy FAQs](https://docs.sqlalchemy.org/en/latest/orm/session_basics.html#session-frequently-asked-questions), which state (with emphasis added by the original author) that - -> As a general rule, the application should manage the lifecycle of the session *externally* to functions that deal with specific data. This is a fundamental separation of concerns which keeps data-specific operations agnostic of the context in which they access and manipulate that data. - -and - -> Keep the lifecycle of the session (and usually the transaction) **separate and external**. - -In keeping with the above recommendations from the official docs, we **strongly recommend** avoiding any explicit mutations of the transaction state inside the callback passed to `run_transaction`, since that will lead to breakage. Specifically, do not make calls to the following functions from inside `run_transaction`: - -- [`sqlalchemy.orm.Session.commit()`](https://docs.sqlalchemy.org/en/latest/orm/session_api.html?highlight=commit#sqlalchemy.orm.session.Session.commit) (or other variants of `commit()`): This is not necessary because `cockroachdb.sqlalchemy.run_transaction` handles the savepoint/commit logic for you. -- [`sqlalchemy.orm.Session.rollback()`](https://docs.sqlalchemy.org/en/latest/orm/session_api.html?highlight=rollback#sqlalchemy.orm.session.Session.rollback) (or other variants of `rollback()`): This is not necessary because `cockroachdb.sqlalchemy.run_transaction` handles the commit/rollback logic for you. -- [`Session.flush()`][session.flush]: This will not work as expected with CockroachDB because CockroachDB does not support nested transactions, which are necessary for `Session.flush()` to work properly. If the call to `Session.flush()` encounters an error and aborts, it will try to rollback. This will not be allowed by the currently-executing CockroachDB transaction created by `run_transaction()`, and will result in an error message like the following: `sqlalchemy.orm.exc.DetachedInstanceError: Instance is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: http://sqlalche.me/e/bhk3)`. - -### Break up large transactions into smaller units of work - -If you see an error message like `transaction is too large to complete; try splitting into pieces`, you are trying to commit too much data in a single transaction. As described in our [Cluster Settings](cluster-settings.html) docs, the size limit for transactions is defined by the `kv.transaction.max_intents_bytes` setting, which defaults to 256 KiB. Although this setting can be changed by an admin, we strongly recommend against it in most cases. - -Instead, we recommend breaking your transaction into smaller units of work (or "chunks"). A pattern that works for inserting large numbers of objects using `run_transaction` to handle retries automatically for you is shown below. - -{% include_cached copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/sqlalchemy-large-txns.py %} -~~~ - -### Use `IMPORT` to read in large data sets - -If you are trying to get a large data set into CockroachDB all at once (a bulk import), avoid writing client-side code that uses an ORM and use the [`IMPORT`](import.html) statement instead. It is much faster and more efficient than making a series of [`INSERT`s](insert.html) and [`UPDATE`s](update.html) such as are generated by calls to [`session.bulk_save_objects()`](https://docs.sqlalchemy.org/en/latest/orm/session_api.html?highlight=bulk_save_object#sqlalchemy.orm.session.Session.bulk_save_objects). - -For more information about importing data from Postgres, see [Migrate from Postgres](migrate-from-postgres.html). - -For more information about importing data from MySQL, see [Migrate from MySQL](migrate-from-mysql.html). - -### Prefer the query builder - -In general, we recommend using the query-builder APIs of SQLAlchemy (e.g., [`Engine.execute()`](https://docs.sqlalchemy.org/en/latest/core/connections.html?highlight=execute#sqlalchemy.engine.Engine.execute)) in your application over the [Session][session]/ORM APIs if at all possible. That way, you know exactly what SQL is being generated and sent to CockroachDB, which has the following benefits: - -- It's easier to debug your SQL queries and make sure they are working as expected. -- You can more easily tune SQL query performance by issuing different statements, creating and/or using different indexes, etc. For more information, see [SQL Performance Best Practices](performance-best-practices-overview.html). - -## See also - -- The [SQLAlchemy](https://docs.sqlalchemy.org/en/latest/) docs -- [Transactions](transactions.html) - -{% include {{page.version.version}}/app/see-also-links.md %} - - - -[session.flush]: https://docs.sqlalchemy.org/en/latest/orm/session_api.html#sqlalchemy.orm.session.Session.flush -[session]: https://docs.sqlalchemy.org/en/latest/orm/session.html diff --git a/src/current/v19.2/build-a-python-app-with-cockroachdb.md b/src/current/v19.2/build-a-python-app-with-cockroachdb.md deleted file mode 100644 index 181c985a690..00000000000 --- a/src/current/v19.2/build-a-python-app-with-cockroachdb.md +++ /dev/null @@ -1,139 +0,0 @@ ---- -title: Build a Python App with CockroachDB and psycopg2 -summary: Learn how to use CockroachDB from a simple Python application with the psycopg2 driver. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Python application with CockroachDB and the psycopg2 driver. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the psycopg2 driver - -To install the Python psycopg2 driver, run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ pip install psycopg2 -~~~ - -For other ways to install psycopg2, see the [official documentation](http://initd.org/psycopg/docs/install.html). - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the Python code - -Now that you have a database and a user, you'll run the code shown below to: - -- Create an `accounts` table and insert some rows. -- Transfer funds between two accounts inside a [transaction](transactions.html). To ensure that we [handle transaction retry errors](transactions.html#client-side-intervention), we write an application-level retry loop that, in case of error, sleeps before trying the funds transfer again. If it encounters another retry error, it sleeps for a longer interval, implementing [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff). -- Finally, we delete the accounts from the table before exiting so we can re-run the example code. - -{{site.data.alerts.callout_success}} -To clone a version of the code below that connects to insecure clusters, run the command below. Note that you will need to edit the connection string to use the certificates that you generated when you set up your secure cluster. - -`git clone https://github.com/cockroachlabs/hello-world-python-psycopg2/` -{{site.data.alerts.end}} - -Copy the code or download it directly. - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/basic-sample.py %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ python basic-sample.py -~~~ - -The output should show the account balances before and after the funds transfer: - -~~~ -Balances at Wed Aug 7 12:11:23 2019 -['1', '1000'] -['2', '250'] -Balances at Wed Aug 7 12:11:23 2019 -['1', '900'] -['2', '350'] -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Python code - -Now that you have a database and a user, you'll run the code shown below to: - -- Create an `accounts` table and insert some rows. -- Transfer funds between two accounts inside a [transaction](transactions.html). To ensure that we [handle transaction retry errors](transactions.html#client-side-intervention), we write an application-level retry loop that, in case of error, sleeps before trying the funds transfer again. If it encounters another retry error, it sleeps for a longer interval, implementing [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff). -- Finally, we delete the accounts from the table before exiting so we can re-run the example code. - -To get the code below, clone the `hello-world-python-psycopg2` repo to your machine: - -{% include copy-clipboard.html %} -~~~ shell -git clone https://github.com/cockroachlabs/hello-world-python-psycopg2/ -~~~ - -{% include copy-clipboard.html %} -~~~ python -{% include {{page.version.version}}/app/insecure/basic-sample.py %} -~~~ - -Change to the directory where you cloned the repo and run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ python example.py -~~~ - -The output should show the account balances before and after the funds transfer: - -~~~ -Balances at Wed Jul 24 15:58:40 2019 -['1', '1000'] -['2', '250'] -Balances at Wed Jul 24 15:58:40 2019 -['1', '900'] -['2', '350'] -~~~ - -
      - -## What's next? - -Read more about using the [Python psycopg2 driver](http://initd.org/psycopg/docs/). - -{% include {{page.version.version}}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-ruby-app-with-cockroachdb-activerecord.md b/src/current/v19.2/build-a-ruby-app-with-cockroachdb-activerecord.md deleted file mode 100644 index 06972fb8607..00000000000 --- a/src/current/v19.2/build-a-ruby-app-with-cockroachdb-activerecord.md +++ /dev/null @@ -1,171 +0,0 @@ ---- -title: Build a Ruby App with CockroachDB and ActiveRecord -summary: Learn how to use CockroachDB from a simple Ruby application with the ActiveRecord ORM. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Ruby application with CockroachDB and the ActiveRecord ORM. - -We have tested the [ActiveRecord ORM](http://guides.rubyonrails.org/active_record_basics.html) enough to claim **beta-level** support in CockroachDB v19.2. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -{{site.data.alerts.callout_success}} -For a more realistic use of ActiveRecord with CockroachDB, see our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. -{{site.data.alerts.end}} - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the ActiveRecord ORM - -To install ActiveRecord as well as the [pg driver](https://rubygems.org/gems/pg) and a [CockroachDB Ruby package](https://github.com/cockroachdb/activerecord-cockroachdb-adapter) that accounts for some minor differences between CockroachDB and PostgreSQL, run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ gem install activerecord pg activerecord-cockroachdb-adapter -~~~ - -{{site.data.alerts.callout_info}} -The exact command above will vary depending on the desired version of ActiveRecord. Specifically, version 4.2.x of ActiveRecord requires version 0.1.x of the adapter; version 5.1.x of ActiveRecord requires version 0.2.x of the adapter; version 5.2.x of ActiveRecord requires version 5.2.x of the adapter. -{{site.data.alerts.end}} - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the Ruby code - -The following code uses the [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html) ORM to map Ruby-specific objects to SQL operations. Specifically, `Schema.new.change()` creates an `accounts` table based on the Account model (or drops and recreates the table if it already exists), `Account.create()` inserts rows into the table, and `Account.all` selects from the table so that balances can be printed. - -Copy the code or -download it directly. - -{% include copy-clipboard.html %} -~~~ ruby -{% include {{page.version.version}}/app/activerecord-basic-sample.rb %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ ruby activerecord-basic-sample.rb -~~~ - -The output should be: - -~~~ --- create_table(:accounts, {:force=>true}) - -> 0.0361s -1 1000 -2 250 -~~~ - -To verify that the table and rows were created successfully, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --database=bank -~~~ - -Then, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT id, balance FROM accounts; -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 1000 | -| 2 | 250 | -+----+---------+ -(2 rows) -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Ruby code - -The following code uses the [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html) ORM to map Ruby-specific objects to SQL operations. Specifically, `Schema.new.change()` creates an `accounts` table based on the Account model (or drops and recreates the table if it already exists), `Account.create()` inserts rows into the table, and `Account.all` selects from the table so that balances can be printed. - -Copy the code or -download it directly. - -{% include copy-clipboard.html %} -~~~ ruby -{% include {{page.version.version}}/app/insecure/activerecord-basic-sample.rb %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ ruby activerecord-basic-sample.rb -~~~ - -The output should be: - -~~~ --- create_table(:accounts, {:force=>true}) - -> 0.0361s -1 1000 -2 250 -~~~ - -To verify that the table and rows were created successfully, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --database=bank -~~~ - -Then, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT id, balance FROM accounts; -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 1000 | -| 2 | 250 | -+----+---------+ -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [ActiveRecord ORM](http://guides.rubyonrails.org/active_record_basics.html), or check out a more realistic implementation of ActiveRecord with CockroachDB in our [`examples-orms`](https://github.com/cockroachdb/examples-orms) repository. - -{% include {{page.version.version}}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-ruby-app-with-cockroachdb.md b/src/current/v19.2/build-a-ruby-app-with-cockroachdb.md deleted file mode 100644 index bc7c5c2237c..00000000000 --- a/src/current/v19.2/build-a-ruby-app-with-cockroachdb.md +++ /dev/null @@ -1,207 +0,0 @@ ---- -title: Build a Ruby App with CockroachDB and the Ruby pg Driver -summary: Learn how to use CockroachDB from a simple Ruby application with the pg client driver. -toc: true -twitter: false ---- - - - -This tutorial shows you how build a simple Ruby application with CockroachDB and the Ruby pg driver. - -We have tested the [Ruby pg driver](https://rubygems.org/gems/pg) enough to claim **beta-level** support. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -## Step 1. Install the Ruby pg driver - -To install the [Ruby pg driver](https://rubygems.org/gems/pg), run the following command: - -{% include copy-clipboard.html %} -~~~ shell -$ gem install pg -~~~ - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the Ruby code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -The following code connects as the `maxroach` user and executes some basic SQL statements: creating a table, inserting rows, and reading and printing the rows. - -Download the basic-sample.rb file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ ruby -{% include {{page.version.version}}/app/basic-sample.rb %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ ruby basic-sample.rb -~~~ - -The output should be: - -~~~ -Initial balances: -{"id"=>"1", "balance"=>"1000"} -{"id"=>"2", "balance"=>"250"} -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Download the txn-sample.rb file, or create the file yourself and copy the code into it. - -{% include {{ page.version.version }}/client-transaction-retry.md %} - -{% include copy-clipboard.html %} -~~~ ruby -{% include {{page.version.version}}/app/txn-sample.rb %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ ruby txn-sample.rb -~~~ - -To verify that funds were transferred from one account to another, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --database=bank -~~~ - -To check the account balances, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT id, balance FROM accounts; -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -
      - -## Step 2. Create the `maxroach` user and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Run the Ruby code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -The following code connects as the `maxroach` user and executes some basic SQL statements: creating a table, inserting rows, and reading and printing the rows. - -Download the basic-sample.rb file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ ruby -{% include {{page.version.version}}/app/insecure/basic-sample.rb %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ ruby basic-sample.rb -~~~ - -The output should be: - -~~~ -Initial balances: -{"id"=>"1", "balance"=>"1000"} -{"id"=>"2", "balance"=>"250"} -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Download the txn-sample.rb file, or create the file yourself and copy the code into it. - -{% include {{ page.version.version }}/client-transaction-retry.md %} - -{% include copy-clipboard.html %} -~~~ ruby -{% include {{page.version.version}}/app/insecure/txn-sample.rb %} -~~~ - -Then run the code: - -{% include copy-clipboard.html %} -~~~ shell -$ ruby txn-sample.rb -~~~ - -To verify that funds were transferred from one account to another, start the [built-in SQL client](cockroach-sql.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --database=bank -~~~ - -To check the account balances, issue the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT id, balance FROM accounts; -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the [Ruby pg driver](https://rubygems.org/gems/pg). - -{% include {{page.version.version}}/app/see-also-links.md %} diff --git a/src/current/v19.2/build-a-rust-app-with-cockroachdb.md b/src/current/v19.2/build-a-rust-app-with-cockroachdb.md deleted file mode 100644 index 098c487ea56..00000000000 --- a/src/current/v19.2/build-a-rust-app-with-cockroachdb.md +++ /dev/null @@ -1,161 +0,0 @@ ---- -title: Build a Rust App with CockroachDB and the Rust Postgres Driver -summary: Learn how to use CockroachDB from a simple Rust application with a low-level client driver. -toc: true -twitter: false ---- - -This tutorial shows you how build a simple Rust application with CockroachDB and the Rust Postgres driver. - -We have tested the Rust Postgres driver enough to claim **beta-level** support. If you encounter problems, please [open an issue](https://github.com/cockroachdb/cockroach/issues/new) with details to help us make progress toward full support. - -## Before you begin - -{% include {{page.version.version}}/app/before-you-begin.md %} - -
      - -## Step 1. Specify the Rust Postgres driver as a dependency - -Update your `Cargo.toml` file to specify a dependency on the Rust Postgres driver, as described in the official documentation. - -Additionally, include the OpenSSL bindings and Rust Postgres OpenSSL crates as dependencies. - -## Step 2. Create the `maxroach` users and `bank` database - -{% include {{page.version.version}}/app/create-maxroach-user-and-bank-database.md %} - -## Step 3. Generate a certificate for the `maxroach` user - -Create a certificate and key for the `maxroach` user by running the following command. The code samples will run as this user. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client maxroach --certs-dir=certs --ca-key=my-safe-directory/ca.key -~~~ - -## Step 4. Run the Rust code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, inserting rows and reading and printing the rows. - -Download the basic-sample.rs file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ rust -{% include {{ page.version.version }}/app/basic-sample.rs %} -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Download the txn-sample.rs file, or create the file yourself and copy the code into it. - -{{site.data.alerts.callout_info}} -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic retry function that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ rust -{% include {{ page.version.version }}/app/txn-sample.rs %} -~~~ - -After running the code, use the [built-in SQL client](cockroach-sql.html) to verify that funds were transferred from one account to another: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -
      - -## Step 1. Specify the Rust Postgres driver as a dependency - -Update your `Cargo.toml` file to specify a dependency on the Rust Postgres driver, as described in the official documentation. - -## Step 2. Create the `maxroach` users and `bank` database - -{% include {{page.version.version}}/app/insecure/create-maxroach-user-and-bank-database.md %} - -## Step 3. Create a table in the new database - -As the `maxroach` user, use the [built-in SQL client](cockroach-sql.html) to create an `accounts` table in the new database. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---database=bank \ ---user=maxroach \ --e 'CREATE TABLE accounts (id INT PRIMARY KEY, balance INT)' -~~~ - -## Step 4. Run the Rust code - -Now that you have a database and a user, you'll run code to create a table and insert some rows, and then you'll run code to read and update values as an atomic [transaction](transactions.html). - -### Basic statements - -First, use the following code to connect as the `maxroach` user and execute some basic SQL statements, inserting rows and reading and printing the rows. - -Download the basic-sample.rs file, or create the file yourself and copy the code into it. - -{% include copy-clipboard.html %} -~~~ rust -{% include {{ page.version.version }}/app/insecure/basic-sample.rs %} -~~~ - -### Transaction (with retry logic) - -Next, use the following code to again connect as the `maxroach` user but this time execute a batch of statements as an atomic transaction to transfer funds from one account to another, where all included statements are either committed or aborted. - -Download the txn-sample.rs file, or create the file yourself and copy the code into it. - -{{site.data.alerts.callout_info}} -CockroachDB may require the [client to retry a transaction](transactions.html#transaction-retries) in case of read/write contention. CockroachDB provides a generic retry function that runs inside a transaction and retries it as needed. You can copy and paste the retry function from here into your code. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ rust -{% include {{ page.version.version }}/app/insecure/txn-sample.rs %} -~~~ - -After running the code, use the [built-in SQL client](cockroach-sql.html) to verify that funds were transferred from one account to another: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e 'SELECT id, balance FROM accounts' --database=bank -~~~ - -~~~ -+----+---------+ -| id | balance | -+----+---------+ -| 1 | 900 | -| 2 | 350 | -+----+---------+ -(2 rows) -~~~ - -
      - -## What's next? - -Read more about using the Rust Postgres driver. - -{% include {{ page.version.version }}/app/see-also-links.md %} diff --git a/src/current/v19.2/bytes.md b/src/current/v19.2/bytes.md deleted file mode 100644 index 88de70a795e..00000000000 --- a/src/current/v19.2/bytes.md +++ /dev/null @@ -1,126 +0,0 @@ ---- -title: BYTES -summary: The BYTES data type stores binary strings of variable length. -toc: true ---- - -The `BYTES` [data type](data-types.html) stores binary strings of variable length. - - -## Aliases - -In CockroachDB, the following are aliases for `BYTES`: - -- `BYTEA` -- `BLOB` - -## Syntax - -To express a byte array constant, see the section on -[byte array literals](sql-constants.html#byte-array-literals) for more -details. For example, the following three are equivalent literals for the same -byte array: `b'abc'`, `b'\141\142\143'`, `b'\x61\x62\x63'`. - -In addition to this syntax, CockroachDB also supports using -[string literals](sql-constants.html#string-literals), including the -syntax `'...'`, `e'...'` and `x'....'` in contexts where a byte array -is otherwise expected. - -## Size - -The size of a `BYTES` value is variable, but it's recommended to keep values under 1 MB to ensure performance. Above that threshold, [write amplification](https://en.wikipedia.org/wiki/Write_amplification) and other considerations may cause significant performance degradation. - -## Example - -~~~ sql -> CREATE TABLE bytes (a INT PRIMARY KEY, b BYTES); - -> -- explicitly typed BYTES literals -> INSERT INTO bytes VALUES (1, b'\141\142\143'), (2, b'\x61\x62\x63'), (3, b'\141\x62\c'); - -> -- string literal implicitly typed as BYTES -> INSERT INTO bytes VALUES (4, 'abc'); - - -> SELECT * FROM bytes; -~~~ -~~~ -+---+-----+ -| a | b | -+---+-----+ -| 1 | abc | -| 2 | abc | -| 3 | abc | -| 4 | abc | -+---+-----+ -(4 rows) -~~~ - -## Supported conversions - -`BYTES` values can be -[cast](data-types.html#data-type-conversions-and-casts) explicitly to -[`STRING`](string.html). This conversion always succeeds. Two -conversion modes are supported, controlled by the -[session variable](set-vars.html#supported-variables) `bytea_output`: - -- `hex` (default): The output of the conversion starts with the two - characters `\`, `x` and the rest of the string is composed by the - hexadecimal encoding of each byte in the input. For example, - `x'48AA'::STRING` produces `'\x48AA'`. - -- `escape`: The output of the conversion contains each byte in the - input, as-is if it is an ASCII character, or encoded using the octal - escape format `\NNN` otherwise. For example, `x'48AA'::STRING` - produces `'0\252'`. - -`STRING` values can be cast explicitly to `BYTES`. This conversion -will fail if the hexadecimal digits are not valid, or if there is an -odd number of them. Two conversion modes are supported: - -- If the string starts with the two special characters `\` and `x` - (e.g., `\xAABB`), the rest of the string is interpreted as a sequence - of hexadecimal digits. The string is then converted to a byte array - where each pair of hexadecimal digits is converted to one byte. - -- Otherwise, the string is converted to a byte array that contains its - UTF-8 encoding. - -### `STRING` vs. `BYTES` - -While both `STRING` and `BYTES` can appear to have similar behavior in many situations, one should understand their nuance before casting one into the other. - -`STRING` treats all of its data as characters, or more specifically, Unicode code points. `BYTES` treats all of its data as a byte string. This difference in implementation can lead to dramatically different behavior. For example, let's take a complex Unicode character such as ☃ ([the snowman emoji](https://emojipedia.org/snowman/)): - -{% include copy-clipboard.html %} -~~~ sql -> SELECT length('☃'::string); -~~~ - -~~~ - length -+--------+ - 1 -~~~ - -~~~ sql -> SELECT length('☃'::bytes); -~~~ -~~~ - length -+--------+ - 3 -~~~ - -In this case, [`LENGTH(string)`](functions-and-operators.html#string-and-byte-functions) measures the number of Unicode code points present in the string, whereas [`LENGTH(bytes)`](functions-and-operators.html#string-and-byte-functions) measures the number of bytes required to store that value. Each character (or Unicode code point) can be encoded using multiple bytes, hence the difference in output between the two. - -#### Translating literals to `STRING` vs. `BYTES` - -A literal entered through a SQL client will be translated into a different value based on the type: - -+ `BYTES` give a special meaning to the pair `\x` at the beginning, and translates the rest by substituting pairs of hexadecimal digits to a single byte. For example, `\xff` is equivalent to a single byte with the value of 255. For more information, see [SQL Constants: String literals with character escapes](sql-constants.html#string-literals-with-character-escapes). -+ `STRING` does not give a special meaning to `\x`, so all characters are treated as distinct Unicode code points. For example, `\xff` is treated as a `STRING` with length 4 (`\`, `x`, `f`, and `f`). - -## See also - -[Data Types](data-types.html) diff --git a/src/current/v19.2/cancel-job.md b/src/current/v19.2/cancel-job.md deleted file mode 100644 index 314a39fd919..00000000000 --- a/src/current/v19.2/cancel-job.md +++ /dev/null @@ -1,76 +0,0 @@ ---- -title: CANCEL JOB -summary: The CANCEL JOB statement stops long-running jobs such as imports, backups, and schema changes. -toc: true ---- - -The `CANCEL JOB` [statement](sql-statements.html) lets you stop long-running jobs, which include [`IMPORT`](import.html) jobs, enterprise [`BACKUP`](backup.html) and [`RESTORE`](restore.html) jobs, schema changes, [user-created table statistics](create-statistics.html) jobs, [automatic table statistics](cost-based-optimizer.html#table-statistics) jobs, and [changefeeds](change-data-capture.html). - - -## Limitations - -When an enterprise [`RESTORE`](restore.html) is canceled, partially restored data is properly cleaned up. This can have a minor, temporary impact on cluster performance. - -## Required privileges - -Only members of the `admin` role can cancel a job. By default, the `root` user belongs to the `admin` role. - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/cancel_job.html %} -
      - -## Parameters - -Parameter | Description -----------|------------ -`job_id` | The ID of the job you want to cancel, which can be found with [`SHOW JOBS`](show-jobs.html). -`select_stmt` | A [selection query](selection-queries.html) that returns `job_id`(s) to cancel. - -## Examples - -### Cancel a single job - -~~~ sql -> SHOW JOBS; -~~~ -~~~ -+----------------+---------+-------------------------------------------+... -| id | type | description |... -+----------------+---------+-------------------------------------------+... -| 27536791415282 | RESTORE | RESTORE db.* FROM 'azure://backup/db/tbl' |... -+----------------+---------+-------------------------------------------+... -~~~ -~~~ sql -> CANCEL JOB 27536791415282; -~~~ - -### Cancel multiple jobs - -To cancel multiple jobs, nest a [`SELECT` clause](select-clause.html) that retrieves `job_id`(s) inside the `CANCEL JOBS` statement: - -{% include copy-clipboard.html %} -~~~ sql -> CANCEL JOBS (SELECT job_id FROM [SHOW JOBS] - WHERE user_name = 'maxroach'); -~~~ - -All jobs created by `maxroach` will be cancelled. - -### Cancel automatic table statistics jobs - -Canceling an automatic table statistics job is not useful since the system will automatically restart the job immediately. To permanently disable automatic table statistics jobs, disable the `sql.stats.automatic_collection.enabled` [cluster setting](cluster-settings.html): - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false; -~~~ - -## See also - -- [`SHOW JOBS`](show-jobs.html) -- [`BACKUP`](backup.html) -- [`RESTORE`](restore.html) -- [`IMPORT`](import.html) -- [`CREATE CHANGEFEED`](create-changefeed.html) diff --git a/src/current/v19.2/cancel-query.md b/src/current/v19.2/cancel-query.md deleted file mode 100644 index c8f4a84d3c3..00000000000 --- a/src/current/v19.2/cancel-query.md +++ /dev/null @@ -1,81 +0,0 @@ ---- -title: CANCEL QUERY -summary: The CANCEL QUERY statement cancels a running SQL query. -toc: true ---- - -The `CANCEL QUERY` [statement](sql-statements.html) cancels a running SQL query. - - -## Considerations - -- Schema changes are treated differently than other SQL queries. You can use SHOW JOBS to monitor the progress of schema changes and CANCEL JOB to cancel schema changes that are taking longer than expected. -- In rare cases where a query is close to completion when a cancellation request is issued, the query may run to completion. - -## Required privileges - -Members of the `admin` role (include `root`, which belongs to `admin` by default) can cancel any currently active. User that are not members of the `admin` role can cancel only their own currently active queries. - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/cancel_query.html %} -
      - -## Parameters - -Parameter | Description -----------|------------ -`query_id` | A [scalar expression](scalar-expressions.html) that produces the ID of the query to cancel.

      `CANCEL QUERY` accepts a single query ID. If a subquery is used and returns multiple IDs, the `CANCEL QUERY` statement will fail. To cancel multiple queries, use `CANCEL QUERIES`. -`select_stmt` | A [selection query](selection-queries.html) whose result you want to cancel. - -## Response - -When a query is successfully cancelled, CockroachDB sends a `query execution canceled` error to the client that issued the query. - -- If the canceled query was a single, stand-alone statement, no further action is required by the client. -- If the canceled query was part of a larger, multi-statement [transaction](transactions.html), the client should then issue a [`ROLLBACK`](rollback-transaction.html) statement. - -## Examples - -### Cancel a query via the query ID - -In this example, we use the [`SHOW QUERIES`](show-queries.html) statement to get the ID of a query and then pass the ID into the `CANCEL QUERY` statement: - -~~~ sql -> SHOW QUERIES; -~~~ - -~~~ -+----------------------------------+---------+----------+----------------------------------+----------------------------------+--------------------+------------------+-------------+-----------+ -| query_id | node_id | username | start | query | client_address | application_name | distributed | phase | -+----------------------------------+---------+----------+----------------------------------+----------------------------------+--------------------+------------------+-------------+-----------+ -| 14dacc1f9a781e3d0000000000000001 | 2 | mroach | 2017-08-10 14:08:22.878113+00:00 | SELECT * FROM test.kv ORDER BY k | 192.168.0.72:56194 | test_app | false | executing | -+----------------------------------+---------+----------+----------------------------------+----------------------------------+--------------------+------------------+-------------+-----------+ -| 14dacc206c47a9690000000000000002 | 2 | root | 2017-08-14 19:11:05.309119+00:00 | SHOW CLUSTER QUERIES | 127.0.0.1:50921 | | NULL | preparing | -+----------------------------------+---------+----------+----------------------------------+----------------------------------+--------------------+------------------+-------------+-----------+ -~~~ - -~~~ sql -> CANCEL QUERY '14dacc1f9a781e3d0000000000000001'; -~~~ - -### Cancel a query via a subquery - -In this example, we nest a [`SELECT` clause](select-clause.html) that retrieves the ID of a query inside the `CANCEL QUERY` statement: - -~~~ sql -> CANCEL QUERY (SELECT query_id FROM [SHOW CLUSTER QUERIES] - WHERE client_address = '192.168.0.72:56194' - AND username = 'mroach' - AND query = 'SELECT * FROM test.kv ORDER BY k'); -~~~ - -{{site.data.alerts.callout_info}}CANCEL QUERY accepts a single query ID. If a subquery is used and returns multiple IDs, the CANCEL QUERY statement will fail. To cancel multiple queries, use CANCEL QUERIES.{{site.data.alerts.end}} - -## See also - -- [Manage Long-Running Queries](manage-long-running-queries.html) -- [`SHOW QUERIES`](show-queries.html) -- [`CANCEL SESSION`](cancel-session.html) -- [SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/cancel-session.md b/src/current/v19.2/cancel-session.md deleted file mode 100644 index c50142f1ca9..00000000000 --- a/src/current/v19.2/cancel-session.md +++ /dev/null @@ -1,93 +0,0 @@ ---- -title: CANCEL SESSION -summary: The CANCEL SESSION statement stops long-running sessions. -toc: true ---- - -The `CANCEL SESSION` [statement](sql-statements.html) lets you stop long-running sessions. `CANCEL SESSION` will attempt to cancel the currently active query and end the session. - - -## Required privileges - -Only members of the `admin` role and the user that the session belongs to can cancel a session. By default, the `root` user belongs to the `admin` role. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/cancel_session.html %}
      - -## Parameters - -Parameter | Description -----------|------------ -`session_id` | The ID of the session you want to cancel, which can be found with [`SHOW SESSIONS`](show-sessions.html).

      `CANCEL SESSION` accepts a single session ID. If a subquery is used and returns multiple IDs, the `CANCEL SESSION` statement will fail. To cancel multiple sessions, use `CANCEL SESSIONS`. -`select_stmt` | A [selection query](selection-queries.html) that returns `session_id`(s) to cancel. - -## Example - -### Cancel a single session - -In this example, we use the [`SHOW SESSIONS`](show-sessions.html) statement to get the ID of a session and then pass the ID into the `CANCEL SESSION` statement: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW SESSIONS; -~~~ -~~~ -+---------+----------------------------------+-----------+... -| node_id | session_id | user_name |... -+---------+----------------------------------+-----------+... -| 1 | 1530c309b1d8d5f00000000000000001 | root |... -+---------+----------------------------------+-----------+... -| 1 | 1530fe0e46d2692e0000000000000001 | maxroach |... -+---------+----------------------------------+-----------+... -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CANCEL SESSION '1530fe0e46d2692e0000000000000001'; -~~~ - -You can also cancel a session using a subquery that returns a single session ID: - -{% include copy-clipboard.html %} -~~~ sql -> CANCEL SESSIONS (SELECT session_id FROM [SHOW SESSIONS] - WHERE username = 'root'); -~~~ - -### Cancel multiple sessions - -Use the [`SHOW SESSIONS`](show-sessions.html) statement to view all active sessions: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW SESSIONS; -~~~ -~~~ -+---------+----------------------------------+-----------+... -| node_id | session_id | user_name |... -+---------+----------------------------------+-----------+... -| 1 | 1530c309b1d8d5f00000000000000001 | root |... -+---------+----------------------------------+-----------+... -| 1 | 1530fe0e46d2692e0000000000000001 | maxroach |... -+---------+----------------------------------+-----------+... -| 1 | 15310cc79671fc6a0000000000000001 | maxroach |... -+---------+----------------------------------+-----------+... -~~~ - -To cancel multiple sessions, nest a [`SELECT` clause](select-clause.html) that retrieves `session_id`(s) inside the `CANCEL SESSIONS` statement: - -{% include copy-clipboard.html %} -~~~ sql -> CANCEL SESSIONS (SELECT session_id FROM [SHOW SESSIONS] - WHERE user_name = 'maxroach'); -~~~ - -All sessions created by `maxroach` will be cancelled. - -## See also - -- [`SHOW SESSIONS`](show-sessions.html) -- [`SET` (session variable)](set-vars.html) -- [`SHOW` (session variable)](show-vars.html) -- [SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/change-data-capture.md b/src/current/v19.2/change-data-capture.md deleted file mode 100644 index 410c5590208..00000000000 --- a/src/current/v19.2/change-data-capture.md +++ /dev/null @@ -1,799 +0,0 @@ ---- -title: Change Data Capture (CDC) -summary: Change data capture (CDC) provides efficient, distributed, row-level change subscriptions. -toc: true ---- - -Change data capture (CDC) provides efficient, distributed, row-level change feeds into a configurable sink for downstream processing such as reporting, caching, or full-text indexing. Change data capture is used for high-latency data exports from CockroachDB to a data warehouse. It is not a low-latency publish-subscribe mechanism. - -## What is change data capture? - -While CockroachDB is an excellent system of record, it also needs to coexist with other systems. For example, you might want to keep your data mirrored in full-text indexes, analytics engines, or big data pipelines. - -The main feature of CDC is the changefeed, which targets an allowlist of tables, called the "watched rows". There are two implementations of changefeeds: - -- [Core changefeeds](#create-a-core-changefeed), which stream row-level changes to the client indefinitely until the underlying connection is closed or the changefeed is canceled. -- [Enterprise changefeeds](#configure-a-changefeed-enterprise), where every change to a watched row is emitted as a record in a configurable format (`JSON` or Avro) to a configurable sink ([Kafka](https://kafka.apache.org/)). - -## Enable rangefeeds - -Changefeeds connect to a long-lived request (i.e., a rangefeed), which pushes changes as they happen. This reduces the latency of row changes, as well as reduces transaction restarts on tables being watched by a changefeed for some workloads. - -**Rangefeeds must be enabled for a changefeed to work.** To [enable the cluster setting](set-cluster-setting.html): - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING kv.rangefeed.enabled = true; -~~~ - -Any created changefeed will error until this setting is enabled. Note that enabling rangefeeds currently has a small performance cost (about a 5-10% increase in latencies), whether or not the rangefeed is being used in a changefeed. - -The `kv.closed_timestamp.target_duration` [cluster setting](cluster-settings.html) can be used with changefeeds. Resolved timestamps will always be behind by at least this setting's duration; however, decreasing the duration leads to more transaction restarts in your cluster, which can affect performance. - -## Ordering guarantees - -- In most cases, each version of a row will be emitted once. However, some infrequent conditions (e.g., node failures, network partitions) will cause them to be repeated. This gives our changefeeds an **at-least-once delivery guarantee**. - -- Once a row has been emitted with some timestamp, no previously unseen versions of that row will be emitted with a lower timestamp. That is, you will never see a _new_ change for that row at an earlier timestamp. - - For example, if you ran the following: - - ~~~ sql - > CREATE TABLE foo (id INT PRIMARY KEY DEFAULT unique_rowid(), name STRING); - > CREATE CHANGEFEED FOR TABLE foo INTO 'kafka://localhost:9092' WITH UPDATED; - > INSERT INTO foo VALUES (1, 'Carl'); - > UPDATE foo SET name = 'Petee' WHERE id = 1; - ~~~ - - You'd expect the changefeed to emit: - - ~~~ shell - [1] {"__crdb__": {"updated": }, "id": 1, "name": "Carl"} - [1] {"__crdb__": {"updated": }, "id": 1, "name": "Petee"} - ~~~ - - It is also possible that the changefeed emits an out of order duplicate of an earlier value that you already saw: - - ~~~ shell - [1] {"__crdb__": {"updated": }, "id": 1, "name": "Carl"} - [1] {"__crdb__": {"updated": }, "id": 1, "name": "Petee"} - [1] {"__crdb__": {"updated": }, "id": 1, "name": "Carl"} - ~~~ - - However, you will **never** see an output like the following (i.e., an out of order row that you've never seen before): - - ~~~ shell - [1] {"__crdb__": {"updated": }, "id": 1, "name": "Petee"} - [1] {"__crdb__": {"updated": }, "id": 1, "name": "Carl"} - ~~~ - -- If a row is modified more than once in the same transaction, only the last change will be emitted. - -- Rows are sharded between Kafka partitions by the row’s [primary key](primary-key.html). - -- The `UPDATED` option adds an "updated" timestamp to each emitted row. You can also use the `RESOLVED` option to emit periodic "resolved" timestamp messages to each Kafka partition. A "resolved" timestamp is a guarantee that no (previously unseen) rows with a lower update timestamp will be emitted on that partition. - - For example: - - ~~~ shell - {"__crdb__": {"updated": "1532377312562986715.0000000000"}, "id": 1, "name": "Petee H"} - {"__crdb__": {"updated": "1532377306108205142.0000000000"}, "id": 2, "name": "Carl"} - {"__crdb__": {"updated": "1532377358501715562.0000000000"}, "id": 3, "name": "Ernie"} - {"__crdb__":{"resolved":"1532379887442299001.0000000000"}} - {"__crdb__":{"resolved":"1532379888444290910.0000000000"}} - {"__crdb__":{"resolved":"1532379889448662988.0000000000"}} - ... - {"__crdb__":{"resolved":"1532379922512859361.0000000000"}} - {"__crdb__": {"updated": "1532379923319195777.0000000000"}, "id": 4, "name": "Lucky"} - ~~~ - -- With duplicates removed, an individual row is emitted in the same order as the transactions that updated it. However, this is not true for updates to two different rows, even two rows in the same table. - - To compare two different rows for [happens-before](https://en.wikipedia.org/wiki/Happened-before), compare the "updated" timestamp. This works across anything in the same cluster (e.g., tables, nodes, etc.). - - Resolved timestamp notifications on every Kafka partition can be used to provide strong ordering and global consistency guarantees by buffering records in between timestamp closures. Use the "resolved" timestamp to see every row that changed at a certain time. - - The complexity with timestamps is necessary because CockroachDB supports transactions that can affect any part of the cluster, and it is not possible to horizontally divide the transaction log into independent changefeeds. For more information about this, [read our blog post on CDC](https://www.cockroachlabs.com/blog/change-data-capture/). - -## Avro schema changes - -To ensure that the Avro schemas that CockroachDB publishes will work with the schema compatibility rules used by the Confluent schema registry, CockroachDB emits all fields in Avro as nullable unions. This ensures that Avro and Confluent consider the schemas to be both backward- and forward-compatible, since the Confluent Schema Registry has a different set of rules than Avro for schemas to be backward- and forward-compatible. - -Note that the original CockroachDB column definition is also included in the schema as a doc field, so it's still possible to distinguish between a `NOT NULL` CockroachDB column and a `NULL` CockroachDB column. - -## Schema changes with column backfill - -When schema changes with column backfill (e.g., adding a column with a default, adding a computed column, adding a `NOT NULL` column, dropping a column) are made to watched rows, the changefeed will emit some duplicates during the backfill. When it finishes, CockroachDB outputs all watched rows using the new schema. When using Avro, rows that have been backfilled by a schema change are always re-emitted. - -For an example of a schema change with column backfill, start with the changefeed created in the [example below](#create-a-changefeed-connected-to-kafka): - -~~~ shell -[1] {"id": 1, "name": "Petee H"} -[2] {"id": 2, "name": "Carl"} -[3] {"id": 3, "name": "Ernie"} -~~~ - -Add a column to the watched table: - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE office_dogs ADD COLUMN likes_treats BOOL DEFAULT TRUE; -~~~ - -The changefeed emits duplicate records 1, 2, and 3 before outputting the records using the new schema: - -~~~ shell -[1] {"id": 1, "name": "Petee H"} -[2] {"id": 2, "name": "Carl"} -[3] {"id": 3, "name": "Ernie"} -[1] {"id": 1, "name": "Petee H"} # Duplicate -[2] {"id": 2, "name": "Carl"} # Duplicate -[3] {"id": 3, "name": "Ernie"} # Duplicate -[1] {"id": 1, "likes_treats": true, "name": "Petee H"} -[2] {"id": 2, "likes_treats": true, "name": "Carl"} -[3] {"id": 3, "likes_treats": true, "name": "Ernie"} -~~~ - -## Create a changefeed (Core) - -A core changefeed streams row-level changes to the client indefinitely until the underlying connection is closed or the changefeed is canceled. - -To create a core changefeed: - -{% include copy-clipboard.html %} -~~~ sql -> EXPERIMENTAL CHANGEFEED FOR name; -~~~ - -For more information, see [`CHANGEFEED FOR`](changefeed-for.html). - -## Configure a changefeed (Enterprise) - -An enterprise changefeed streams row-level changes in a configurable format to a configurable sink (i.e., Kafka or a cloud storage sink). You can [create](#create), [pause](#pause), [resume](#resume), [cancel](#cancel), [monitor](#monitor-a-changefeed), and [debug](#debug-a-changefeed) an enterprise changefeed. - -### Create - -To create an enterprise changefeed: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE CHANGEFEED FOR TABLE table_name, table_name2 INTO 'scheme://host:port'; -~~~ - -For more information, see [`CREATE CHANGEFEED`](create-changefeed.html). - -### Pause - -To pause an enterprise changefeed: - -{% include copy-clipboard.html %} -~~~ sql -> PAUSE JOB job_id; -~~~ - -For more information, see [`PAUSE JOB`](pause-job.html). - -### Resume - -To resume a paused enterprise changefeed: - -{% include copy-clipboard.html %} -~~~ sql -> RESUME JOB job_id; -~~~ - -For more information, see [`RESUME JOB`](resume-job.html). - -### Cancel - -To cancel an enterprise changefeed: - -{% include copy-clipboard.html %} -~~~ sql -> CANCEL JOB job_id; -~~~ - -For more information, see [`CANCEL JOB`](cancel-job.html). - -## Monitor a changefeed - -{{site.data.alerts.callout_info}} -Monitoring is only available for enterprise changefeeds. -{{site.data.alerts.end}} - -Changefeed progress is exposed as a high-water timestamp that advances as the changefeed progresses. This is a guarantee that all changes before or at the timestamp have been emitted. You can monitor a changefeed: - -- On the [Changefeed Dashboard](admin-ui-cdc-dashboard.html) of the Admin UI. -- On the [Jobs page](admin-ui-jobs-page.html) of the Admin UI. Hover over the high-water timestamp to view the [system time](as-of-system-time.html). -- Using `crdb_internal.jobs`: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT * FROM crdb_internal.jobs WHERE job_id = ; - ~~~ - ~~~ - job_id | job_type | description | ... | high_water_timestamp | error | coordinator_id - +--------------------+------------+------------------------------------------------------------------------+ ... +--------------------------------+-------+----------------+ - 383870400694353921 | CHANGEFEED | CREATE CHANGEFEED FOR TABLE office_dogs INTO 'kafka://localhost:9092' | ... | 1537279405671006870.0000000000 | | 1 - (1 row) - ~~~ - -- Setting up an alert on the `changefeed.max_behind_nanos` metric to track when a changefeed's high-water mark timestamp is at risk of falling behind the cluster's [garbage collection window](configure-replication-zones.html#replication-zone-variables). For more information, see [Monitoring and Alerting](monitoring-and-alerting.html#changefeed-is-experiencing-high-latency). - -{{site.data.alerts.callout_info}} -You can use the high-water timestamp to [start a new changefeed where another ended](create-changefeed.html#start-a-new-changefeed-where-another-ended). -{{site.data.alerts.end}} - -## Debug a changefeed - -### Using logs - -For enterprise changefeeds, [use log information](debug-and-error-logs.html) to debug connection issues (i.e., `kafka: client has run out of available brokers to talk to (Is your cluster reachable?)`). Debug by looking for lines in the logs with `[kafka-producer]` in them: - -~~~ -I190312 18:56:53.535646 585 vendor/github.com/Shopify/sarama/client.go:123 [kafka-producer] Initializing new client -I190312 18:56:53.535714 585 vendor/github.com/Shopify/sarama/client.go:724 [kafka-producer] client/metadata fetching metadata for all topics from broker localhost:9092 -I190312 18:56:53.536730 569 vendor/github.com/Shopify/sarama/broker.go:148 [kafka-producer] Connected to broker at localhost:9092 (unregistered) -I190312 18:56:53.537661 585 vendor/github.com/Shopify/sarama/client.go:500 [kafka-producer] client/brokers registered new broker #0 at 172.16.94.87:9092 -I190312 18:56:53.537686 585 vendor/github.com/Shopify/sarama/client.go:170 [kafka-producer] Successfully initialized new client -~~~ - -### Using `SHOW JOBS` - -For enterprise changefeeds, you can check the status by using: - -{% include copy-clipboard.html %} -~~~ sql -SELECT * FROM [SHOW JOBS] WHERE job_type='CHANGEFEED'; -~~~ - -Or: - -{% include copy-clipboard.html %} -~~~ sql -SELECT * from crdb_internal.jobs WHERE job_type='CHANGEFEED'; -~~~ - -For more information, see [`SHOW JOBS`](show-jobs.html). - -## Usage examples - -### Create a core changefeed - -{% include {{ page.version.version }}/cdc/create-core-changefeed.md %} - -### Create a core changefeed using Avro - -{% include {{ page.version.version }}/cdc/create-core-changefeed-avro.md %} - -### Create a changefeed connected to Kafka - -{{site.data.alerts.callout_info}} -[`CREATE CHANGEFEED`](create-changefeed.html) is an [enterprise-only](enterprise-licensing.html) feature. For the core version, see [the `CHANGEFEED FOR` example above](#create-a-core-changefeed). -{{site.data.alerts.end}} - -In this example, you'll set up a changefeed for a single-node cluster that is connected to a Kafka sink. The changefeed will watch two tables. - -1. If you do not already have one, [request a trial enterprise license](enterprise-licensing.html). - -2. Use the [`cockroach start-single-node`](cockroach-start-single-node.html) command to start a single-node cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start-single-node --insecure --listen-addr=localhost --background - ~~~ - -3. Download and extract the [Confluent Open Source platform](https://www.confluent.io/download/) (which includes Kafka). - -4. Move into the extracted `confluent-` directory and start Confluent: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/confluent start - ~~~ - - Only `zookeeper` and `kafka` are needed. To troubleshoot Confluent, see [their docs](https://docs.confluent.io/current/installation/installing_cp.html#zip-and-tar-archives). - -5. Create two Kafka topics: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/kafka-topics \ - --create \ - --zookeeper localhost:2181 \ - --replication-factor 1 \ - --partitions 1 \ - --topic office_dogs - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/kafka-topics \ - --create \ - --zookeeper localhost:2181 \ - --replication-factor 1 \ - --partitions 1 \ - --topic employees - ~~~ - - {{site.data.alerts.callout_info}} - You are expected to create any Kafka topics with the necessary number of replications and partitions. [Topics can be created manually](https://kafka.apache.org/documentation/#basic_ops_add_topic) or [Kafka brokers can be configured to automatically create topics](https://kafka.apache.org/documentation/#topicconfigs) with a default partition count and replication factor. - {{site.data.alerts.end}} - -6. As the `root` user, open the [built-in SQL client](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - -7. Set your organization name and [enterprise license](enterprise-licensing.html) key that you received via email: - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING cluster.organization = ''; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING enterprise.license = ''; - ~~~ - -8. Enable the `kv.rangefeed.enabled` [cluster setting](cluster-settings.html): - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING kv.rangefeed.enabled = true; - ~~~ - -9. Create a database called `cdc_demo`: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE DATABASE cdc_demo; - ~~~ - -10. Set the database as the default: - - {% include copy-clipboard.html %} - ~~~ sql - > SET DATABASE = cdc_demo; - ~~~ - -11. Create a table and add data: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE office_dogs ( - id INT PRIMARY KEY, - name STRING); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO office_dogs VALUES - (1, 'Petee'), - (2, 'Carl'); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > UPDATE office_dogs SET name = 'Petee H' WHERE id = 1; - ~~~ - -12. Create another table and add data: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE employees ( - dog_id INT REFERENCES office_dogs (id), - employee_name STRING); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO employees VALUES - (1, 'Lauren'), - (2, 'Spencer'); - ~~~ - -13. Start the changefeed: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE CHANGEFEED FOR TABLE office_dogs, employees INTO 'kafka://localhost:9092'; - ~~~ - ~~~ - - job_id - +--------------------+ - 360645287206223873 - (1 row) - ~~~ - - This will start up the changefeed in the background and return the `job_id`. The changefeed writes to Kafka. - -14. In a new terminal, move into the extracted `confluent-` directory and start watching the Kafka topics: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/kafka-console-consumer \ - --bootstrap-server=localhost:9092 \ - --from-beginning \ - --whitelist 'office_dogs|employees' - ~~~ - - ~~~ shell - {"after": {"id": 1, "name": "Petee H"}} - {"after": {"id": 2, "name": "Carl"}} - {"after": {"id": 1, "name": "Lauren", "rowid": 528514320239329281}} - {"after": {"id": 2, "name": "Spencer", "rowid": 528514320239362049}} - ~~~ - - The initial scan displays the state of the tables as of when the changefeed started (therefore, the initial value of `"Petee"` is omitted). - - {% include {{ page.version.version }}/cdc/print-key.md %} - -15. Back in the SQL client, insert more data: - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO office_dogs VALUES (3, 'Ernie'); - ~~~ - -16. Back in the terminal where you're watching the Kafka topics, the following output has appeared: - - ~~~ shell - {"after": {"id": 3, "name": "Ernie"}} - ~~~ - -17. When you are done, exit the SQL shell (`\q`). - -18. To stop `cockroach`, run: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure - ~~~ - -19. To stop Kafka, move into the extracted `confluent-` directory and stop Confluent: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/confluent stop - ~~~ - -### Create a changefeed connected to Kafka using Avro - -{{site.data.alerts.callout_info}} -[`CREATE CHANGEFEED`](create-changefeed.html) is an [enterprise-only](enterprise-licensing.html) feature. For the core version, see [the `CHANGEFEED FOR` example above](#create-a-core-changefeed-using-avro). -{{site.data.alerts.end}} - -In this example, you'll set up a changefeed for a single-node cluster that is connected to a Kafka sink and emits [Avro](https://avro.apache.org/docs/1.8.2/spec.html) records. The changefeed will watch two tables. - -1. If you do not already have one, [request a trial enterprise license](enterprise-licensing.html). - -2. Use the [`cockroach start-single-node`](cockroach-start-single-node.html) command to start a single-node cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start-single-node --insecure --listen-addr=localhost --background - ~~~ - -3. Download and extract the [Confluent Open Source platform](https://www.confluent.io/download/) (which includes Kafka). - -4. Move into the extracted `confluent-` directory and start Confluent: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/confluent start - ~~~ - - Only `zookeeper`, `kafka`, and `schema-registry` are needed. To troubleshoot Confluent, see [their docs](https://docs.confluent.io/current/installation/installing_cp.html#zip-and-tar-archives). - -5. Create two Kafka topics: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/kafka-topics \ - --create \ - --zookeeper localhost:2181 \ - --replication-factor 1 \ - --partitions 1 \ - --topic office_dogs - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/kafka-topics \ - --create \ - --zookeeper localhost:2181 \ - --replication-factor 1 \ - --partitions 1 \ - --topic employees - ~~~ - - {{site.data.alerts.callout_info}} - You are expected to create any Kafka topics with the necessary number of replications and partitions. [Topics can be created manually](https://kafka.apache.org/documentation/#basic_ops_add_topic) or [Kafka brokers can be configured to automatically create topics](https://kafka.apache.org/documentation/#topicconfigs) with a default partition count and replication factor. - {{site.data.alerts.end}} - -6. As the `root` user, open the [built-in SQL client](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - -7. Set your organization name and [enterprise license](enterprise-licensing.html) key that you received via email: - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING cluster.organization = ''; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING enterprise.license = ''; - ~~~ - -8. Enable the `kv.rangefeed.enabled` [cluster setting](cluster-settings.html): - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING kv.rangefeed.enabled = true; - ~~~ - -9. Create a database called `cdc_demo`: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE DATABASE cdc_demo; - ~~~ - -10. Set the database as the default: - - {% include copy-clipboard.html %} - ~~~ sql - > SET DATABASE = cdc_demo; - ~~~ - -11. Create a table and add data: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE office_dogs ( - id INT PRIMARY KEY, - name STRING); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO office_dogs VALUES - (1, 'Petee'), - (2, 'Carl'); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > UPDATE office_dogs SET name = 'Petee H' WHERE id = 1; - ~~~ - -12. Create another table and add data: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE employees ( - dog_id INT REFERENCES office_dogs_avro (id), - employee_name STRING); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO employees VALUES - (1, 'Lauren'), - (2, 'Spencer'); - ~~~ - -13. Start the changefeed: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE CHANGEFEED FOR TABLE office_dogs, employees INTO 'kafka://localhost:9092' WITH format = experimental_avro, confluent_schema_registry = 'http://localhost:8081'; - ~~~ - - ~~~ - job_id - +--------------------+ - 360645287206223873 - (1 row) - ~~~ - - This will start up the changefeed in the background and return the `job_id`. The changefeed writes to Kafka. - -14. In a new terminal, move into the extracted `confluent-` directory and start watching the Kafka topics: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/kafka-avro-console-consumer \ - --bootstrap-server=localhost:9092 \ - --from-beginning \ - --whitelist 'office_dogs|employees' - ~~~ - - ~~~ shell - {"after":{"office_dogs":{"id":{"long":1},"name":{"string":"Petee H"}}}} - {"after":{"office_dogs":{"id":{"long":2},"name":{"string":"Carl"}}}} - {"after":{"employees":{"dog_id":{"long":1},"employee_name":{"string":"Lauren"},"rowid":{"long":528537452042682369}}}} - {"after":{"employees":{"dog_id":{"long":2},"employee_name":{"string":"Spencer"},"rowid":{"long":528537452042747905}}}} - ~~~ - - The initial scan displays the state of the table as of when the changefeed started (therefore, the initial value of `"Petee"` is omitted). - - {% include {{ page.version.version }}/cdc/print-key.md %} - -15. Back in the SQL client, insert more data: - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO office_dogs VALUES (3, 'Ernie'); - ~~~ - -16. Back in the terminal where you're watching the Kafka topics, the following output has appeared: - - ~~~ shell - {"after":{"office_dogs":{"id":{"long":3},"name":{"string":"Ernie"}}}} - ~~~ - -17. When you are done, exit the SQL shell (`\q`). - -18. To stop `cockroach`, run: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure - ~~~ - -19. To stop Kafka, move into the extracted `confluent-` directory and stop Confluent: - - {% include copy-clipboard.html %} - ~~~ shell - $ ./bin/confluent stop - ~~~ - -### Create a changefeed connected to a cloud storage sink - -{{site.data.alerts.callout_info}} -[`CREATE CHANGEFEED`](create-changefeed.html) is an [enterprise-only](enterprise-licensing.html) feature. For the core version, see [the `CHANGEFEED FOR` example above](#create-a-core-changefeed). -{{site.data.alerts.end}} - -{% include {{ page.version.version }}/misc/experimental-warning.md %} - -In this example, you'll set up a changefeed for a single-node cluster that is connected to an AWS S3 sink. The changefeed watches two tables. Note that you can set up changefeeds for any of [these cloud storage providers](create-changefeed.html#cloud-storage-sink). - -1. If you do not already have one, [request a trial enterprise license](enterprise-licensing.html). - -2. Use the [`cockroach start-single-node`](cockroach-start-single-node.html) command to start a single-node cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start-single-node --insecure --listen-addr=localhost --background - ~~~ - -3. As the `root` user, open the [built-in SQL client](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - -4. Set your organization name and [enterprise license](enterprise-licensing.html) key that you received via email: - - {% include copy-clipboard.html %} - ~~~ shell - > SET CLUSTER SETTING cluster.organization = ''; - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - > SET CLUSTER SETTING enterprise.license = ''; - ~~~ - -5. Enable the `kv.rangefeed.enabled` [cluster setting](cluster-settings.html): - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING kv.rangefeed.enabled = true; - ~~~ - -6. Create a database called `cdc_demo`: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE DATABASE cdc_demo; - ~~~ - -7. Set the database as the default: - - {% include copy-clipboard.html %} - ~~~ sql - > SET DATABASE = cdc_demo; - ~~~ - -8. Create a table and add data: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE office_dogs ( - id INT PRIMARY KEY, - name STRING); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO office_dogs VALUES - (1, 'Petee'), - (2, 'Carl'); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > UPDATE office_dogs SET name = 'Petee H' WHERE id = 1; - ~~~ - -9. Create another table and add data: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE employees ( - dog_id INT REFERENCES office_dogs_avro (id), - employee_name STRING); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO employees VALUES - (1, 'Lauren'), - (2, 'Spencer'); - ~~~ - -10. Start the changefeed: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE CHANGEFEED FOR TABLE office_dogs, employees INTO 'experimental-s3://example-bucket-name/test?AWS_ACCESS_KEY_ID=enter_key-here&AWS_SECRET_ACCESS_KEY=enter_key_here' with updated, resolved='10s'; - ~~~ - - ~~~ - job_id - +--------------------+ - 360645287206223873 - (1 row) - ~~~ - - This will start up the changefeed in the background and return the `job_id`. The changefeed writes to AWS. - -11. Monitor your changefeed on the Admin UI. For more information, see [Changefeeds Dashboard](admin-ui-cdc-dashboard.html). - -12. When you are done, exit the SQL shell (`\q`). - -13. To stop `cockroach`, run: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure - ~~~ - -## Known limitations - -The following are limitations in the current release and will be addressed in the future: - -{% include {{ page.version.version }}/known-limitations/cdc.md %} - -## See also - -- [`CREATE CHANGEFEED`](create-changefeed.html) -- [`CHANGEFEED FOR`](changefeed-for.html) -- [`PAUSE JOB`](pause-job.html) -- [`CANCEL JOB`](cancel-job.html) -- [Other SQL Statements](sql-statements.html) -- [Changefeed Dashboard](admin-ui-cdc-dashboard.html) diff --git a/src/current/v19.2/changefeed-for.md b/src/current/v19.2/changefeed-for.md deleted file mode 100644 index dda3cd117f6..00000000000 --- a/src/current/v19.2/changefeed-for.md +++ /dev/null @@ -1,85 +0,0 @@ ---- -title: EXPERIMENTAL CHANGEFEED FOR -summary: which streams row-level changes to the client indefinitely until the underlying connection is closed or the changefeed is canceled. -toc: true ---- - -{{site.data.alerts.callout_info}} -`EXPERIMENTAL CHANGEFEED FOR` is the core implementation of changefeeds. For the [enterprise-only](enterprise-licensing.html) version, see [`CREATE CHANGEFEED`](create-changefeed.html). -{{site.data.alerts.end}} - -The `EXPERIMENTAL CHANGEFEED FOR` [statement](sql-statements.html) creates a new core changefeed, which streams row-level changes to the client indefinitely until the underlying connection is closed or the changefeed is canceled. - -For more information, see [Change Data Capture](change-data-capture.html). - -{% include {{ page.version.version }}/misc/experimental-warning.md %} - -## Required privileges - -Changefeeds can only be created by superusers, i.e., [members of the `admin` role](authorization.html#create-and-manage-roles). The admin role exists by default with `root` as the member. - -## Considerations - -- Because core changefeeds return results differently than other SQL statements, they require a dedicated database connection with specific settings around result buffering. In normal operation, CockroachDB improves performance by buffering results server-side before returning them to a client; however, result buffering is automatically turned off for core changefeeds. Core changefeeds also have different cancellation behavior than other queries: they can only be canceled by closing the underlying connection or issuing a [`CANCEL QUERY`](cancel-query.html) statement on a separate connection. Combined, these attributes of changefeeds mean that applications should explicitly create dedicated connections to consume changefeed data, instead of using a connection pool as most client drivers do by default. - - This cancellation behavior (i.e., close the underlying connection to cancel the changefeed) also extends to client driver usage; in particular, when a client driver calls `Rows.Close()` after encountering errors for a stream of rows. The pgwire protocol requires that the rows be consumed before the connection is again usable, but in the case of a core changefeed, the rows are never consumed. It is therefore critical that you close the connection, otherwise the application will be blocked forever on `Rows.Close()`. - -- In most cases, each version of a row will be emitted once. However, some infrequent conditions (e.g., node failures, network partitions) will cause them to be repeated. This gives our changefeeds an at-least-once delivery guarantee. For more information, see [Change Data Capture - Ordering Guarantees](change-data-capture.html#ordering-guarantees). -## Synopsis - -~~~ -> EXPERIMENTAL CHANGEFEED FOR table_name [ WITH (option [= value] [, ...]) ]; -~~~ - -## Parameters - -Parameter | Description -----------|------------ -`table_name` | The name of the table (or tables in a comma separated list) to create a changefeed for. -`option` / `value` | For a list of available options and their values, see [Options](#options) below. - - - -### Options - -Option | Value | Description --------|-------|------------ -`updated` | N/A | Include updated timestamps with each row. -`resolved` | [`INTERVAL`](interval.html) | Periodically emit resolved timestamps to the changefeed. Optionally, set a minimum duration between emitting resolved timestamps. If unspecified, all resolved timestamps are emitted.

      Example: `resolved='10s'` -`envelope` | `key_only` / `row` | Use `key_only` to emit only the key and no value, which is faster if you only want to know when the key changes.

      Default: `envelope=row` -`cursor` | [Timestamp](as-of-system-time.html#parameters) | Emits any changes after the given timestamp, but does not output the current state of the table first. If `cursor` is not specified, the changefeed starts by doing a consistent scan of all the watched rows and emits the current value, then moves to emitting any changes that happen after the scan.

      `cursor` can be used to start a new changefeed where a previous changefeed ended.

      Example: `CURSOR=1536242855577149065.0000000000` -`format` | `json` / `experimental_avro` | Format of the emitted record. Currently, support for [Avro is limited and experimental](#avro-limitations).

      Default: `format=json`. -`confluent_schema_registry` | Schema Registry address | The [Schema Registry](https://docs.confluent.io/current/schema-registry/docs/index.html#sr) address is required to use `experimental_avro`. - -#### Avro limitations - -Currently, support for Avro is limited and experimental. Below is a list of unsupported SQL types and values for Avro changefeeds: - -- [Decimals](decimal.html) must have precision specified. -- [Decimals](decimal.html) with `NaN` or infinite values cannot be written in Avro. - - {{site.data.alerts.callout_info}} - To avoid `NaN` or infinite values, add a [`CHECK` constraint](check.html) to prevent these values from being inserted into decimal columns. - {{site.data.alerts.end}} - -- [`time`, `date`, `interval`](https://github.com/cockroachdb/cockroach/issues/32472), [`uuid`, `inet`](https://github.com/cockroachdb/cockroach/issues/34417), [`array`](https://github.com/cockroachdb/cockroach/issues/34420), and [`jsonb`](https://github.com/cockroachdb/cockroach/issues/34421) are not supported in Avro yet. - -## Examples - -### Create a changefeed - -{% include {{ page.version.version }}/cdc/create-core-changefeed.md %} - -### Create a changefeed with Avro - -{% include {{ page.version.version }}/cdc/create-core-changefeed-avro.md %} - - - -## See also - -- [Change Data Capture](change-data-capture.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/check.md b/src/current/v19.2/check.md deleted file mode 100644 index 976a63394c1..00000000000 --- a/src/current/v19.2/check.md +++ /dev/null @@ -1,113 +0,0 @@ ---- -title: CHECK Constraint -summary: The CHECK constraint specifies that values for the column in INSERT or UPDATE statements must satisfy a Boolean expression. -toc: true ---- - -The `CHECK` [constraint](constraints.html) specifies that values for the column in [`INSERT`](insert.html) or [`UPDATE`](update.html) statements must return `TRUE` or `NULL` for a Boolean expression. If any values return `FALSE`, the entire statement is rejected. - -## Details - -- If you add a `CHECK` constraint to an existing table, CockroachDB will run a background job to validate existing table data in the process of adding the constraint. If a row is found that violates the constraint during the validation step, the [`ADD CONSTRAINT`](add-constraint.html) statement will fail. This differs from previous versions of CockroachDB, which allowed you to add a check constraint that was enforced for writes but could be violated by rows that existed prior to adding the constraint. -- Check constraints can be added to columns that were created earlier in the same transaction. For an example, see [Add the `CHECK` constraint](add-constraint.html#add-the-check-constraint). -- `CHECK` constraints may be specified at the column or table level and can reference other columns within the table. Internally, all column-level `CHECK` constraints are converted to table-level constraints so they can be handled consistently. -- You can have multiple `CHECK` constraints on a single column but ideally, for performance optimization, these should be combined using the logical operators. For example: - - ~~~ sql - warranty_period INT CHECK (warranty_period >= 0) CHECK (warranty_period <= 24) - ~~~ - - should be specified as: - - ~~~ sql - warranty_period INT CHECK (warranty_period BETWEEN 0 AND 24) - ~~~ -- When a column with a `CHECK` constraint is dropped, the `CHECK` constraint is also dropped. - -## Syntax - -`CHECK` constraints can be defined at the [table level](#table-level). However, if you only want the constraint to apply to a single column, it can be applied at the [column level](#column-level). - -{{site.data.alerts.callout_info}}You can also add the CHECK constraint to existing tables through ADD CONSTRAINT.{{site.data.alerts.end}} - -### Column level - -
      - {% include {{ page.version.version }}/sql/diagrams/check_column_level.html %} -
      - - Parameter | Description ------------|------------- - `table_name` | The name of the table you're creating. - `column_name` | The name of the constrained column. - `column_type` | The constrained column's [data type](data-types.html). - `check_expr` | An expression that returns a Boolean value; if the expression evaluates to `FALSE`, the value cannot be inserted. - `column_constraints` | Any other column-level [constraints](constraints.html) you want to apply to this column. - `column_def` | Definitions for any other columns in the table. - `table_constraints` | Any table-level [constraints](constraints.html) you want to apply. - -**Example** - -~~~ sql -> CREATE TABLE inventories ( - product_id INT NOT NULL, - warehouse_id INT NOT NULL, - quantity_on_hand INT NOT NULL CHECK (quantity_on_hand > 0), - PRIMARY KEY (product_id, warehouse_id) - ); -~~~ - -### Table level - -
      - {% include {{ page.version.version }}/sql/diagrams/check_table_level.html %} -
      - - Parameter | Description ------------|------------- - `table_name` | The name of the table you're creating. - `column_def` | Definitions for any other columns in the table. - `name` | The name you want to use for the constraint, which must be unique to its table and follow these [identifier rules](keywords-and-identifiers.html#identifiers). - `check_expr` | An expression that returns a Boolean value; if the expression evaluates to `FALSE`, the value cannot be inserted. - `table_constraints` | Any other table-level [constraints](constraints.html) you want to apply. - -**Example** - -~~~ sql -> CREATE TABLE inventories ( - product_id INT NOT NULL, - warehouse_id INT NOT NULL, - quantity_on_hand INT NOT NULL, - PRIMARY KEY (product_id, warehouse_id), - CONSTRAINT ok_to_supply CHECK (quantity_on_hand > 0 AND warehouse_id BETWEEN 100 AND 200) - ); -~~~ - -## Usage example - -`CHECK` constraints may be specified at the column or table level and can reference other columns within the table. Internally, all column-level `CHECK` constraints are converted to table-level constraints so they can be handled in a consistent fashion. - -~~~ sql -> CREATE TABLE inventories ( - product_id INT NOT NULL, - warehouse_id INT NOT NULL, - quantity_on_hand INT NOT NULL CHECK (quantity_on_hand > 0), - PRIMARY KEY (product_id, warehouse_id) - ); - -> INSERT INTO inventories (product_id, warehouse_id, quantity_on_hand) VALUES (1, 2, 0); -~~~ -~~~ -pq: failed to satisfy CHECK constraint (quantity_on_hand > 0) -~~~ - -## See also - -- [Constraints](constraints.html) -- [`DROP CONSTRAINT`](drop-constraint.html) -- [`DEFAULT` constraint](default-value.html) -- [`REFERENCES` constraint (Foreign Key)](foreign-key.html) -- [`NOT NULL` constraint](not-null.html) -- [`PRIMARY KEY` constraint](primary-key.html) -- [`UNIQUE` constraint](unique.html) -- [`SHOW CONSTRAINTS`](show-constraints.html) diff --git a/src/current/v19.2/cluster-settings.md b/src/current/v19.2/cluster-settings.md deleted file mode 100644 index 5aa97c2ef54..00000000000 --- a/src/current/v19.2/cluster-settings.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -title: Cluster Settings -summary: Learn about cluster settings that apply to all nodes of a CockroachDB cluster. -toc: false ---- - -Cluster settings apply to all nodes of a CockroachDB cluster and control, for example, whether or not to share diagnostic details with Cockroach Labs as well as advanced options for debugging and cluster tuning. - -They can be updated anytime after a cluster has been started, but only by a member of the `admin` role, to which the `root` user belongs by default. - -{{site.data.alerts.callout_info}} -In contrast to cluster-wide settings, node-level settings apply to a single node. They are defined by flags passed to the `cockroach start` command when starting a node and cannot be changed without stopping and restarting the node. For more details, see [Start a Node](cockroach-start.html). -{{site.data.alerts.end}} - -## Settings - -{{site.data.alerts.callout_danger}} -Many cluster settings are intended for tuning CockroachDB internals. Before changing these settings, we strongly encourage you to discuss your goals with Cockroach Labs; otherwise, you use them at your own risk. -{{site.data.alerts.end}} - -{% remote_include https://raw.githubusercontent.com/cockroachdb/cockroach/{{ page.release_info.crdb_branch_name }}/docs/generated/settings/settings.html %} - -## View current cluster settings - -Use the [`SHOW CLUSTER SETTING`](show-cluster-setting.html) statement. - -## Change a cluster setting - -Use the [`SET CLUSTER SETTING`](set-cluster-setting.html) statement. - -Before changing a cluster setting, please note the following: - -- Changing a cluster setting is not instantaneous, as the change must be propagated to other nodes in the cluster. - -- Do not change cluster settings while [upgrading to a new version of CockroachDB](upgrade-cockroach-version.html). Wait until all nodes have been upgraded before you make the change. - -## See also - -- [`SET CLUSTER SETTING`](set-cluster-setting.html) -- [`SHOW CLUSTER SETTING`](show-cluster-setting.html) -- [Diagnostics Reporting](diagnostics-reporting.html) -- [Start a Node](cockroach-start.html) -- [Use the Built-in SQL Client](cockroach-sql.html) diff --git a/src/current/v19.2/cluster-setup-troubleshooting.md b/src/current/v19.2/cluster-setup-troubleshooting.md deleted file mode 100644 index 83409d1b39a..00000000000 --- a/src/current/v19.2/cluster-setup-troubleshooting.md +++ /dev/null @@ -1,503 +0,0 @@ ---- -title: Troubleshoot Cluster Setup -summary: Learn how to troubleshoot issues with starting CockroachDB clusters -toc: true ---- - -If you're having trouble starting or scaling your cluster, this page will help you troubleshoot the issue. - -To use this guide, it's important to understand some of CockroachDB's terminology: - - - A **Cluster** acts as a single logical database, but is actually made up of many cooperating nodes. - - **Nodes** are single instances of the `cockroach` binary running on a machine. It's possible (though atypical) to have multiple nodes running on a single machine. - -## Cannot run a single-node CockroachDB cluster - -Try running: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start-single-node --insecure --logtostderr -~~~ - -If the process exits prematurely, check for the following: - -### An existing storage directory - -When starting a node, the directory you choose to store the data in also contains metadata identifying the cluster the data came from. This causes conflicts when you've already started a node on the server, have quit `cockroach`, and then tried to start another cluster using the same directory. Because the existing directory's cluster ID doesn't match the new cluster ID, the node cannot start. - -**Solution:** Disassociate the node from the existing directory where you've stored CockroachDB data. For example, you can do either of the following: - -- Choose a different directory to store the CockroachDB data: - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start-single-node --store= --insecure - ~~~ -- Remove the existing directory and start the node again: - {% include copy-clipboard.html %} - ~~~ shell - $ rm -r cockroach-data/ - ~~~ - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start-single-node --insecure --logtostderr - ~~~ - -### Toolchain incompatibility - -The components of the toolchain might have some incompatibilities that need to be resolved. For example, a few months ago, there was an incompatibility between Xcode 8.3 and Go 1.8 that caused any Go binaries created with that toolchain combination to crash immediately. - -### Incompatible CPU - -If the `cockroach` process had exit status `132 (SIGILL)`, it attempted to use an instruction that is not supported by your CPU. Non-release builds of CockroachDB may not be able to run on older hardware platforms than the one used to build them. Release builds should run on any x86-64 CPU. - -### Default ports already in use - -Other services may be running on port 26257 or 8080 (CockroachDB's default `--listen-addr` port and `--http-addr` port respectively). You can either stop those services or start your node with different ports, specified in the [`--listen-addr` and `--http-addr` flags](cockroach-start.html#networking). - - If you change the port, you will need to include the `--port=` flag in each subsequent cockroach command or change the `COCKROACH_PORT` environment variable. - -### Single-node networking issues - -Networking issues might prevent the node from communicating with itself on its hostname. You can control the hostname CockroachDB uses with the [`--listen-addr` flag](cockroach-start.html#networking). - - If you change the host, you will need to include `--host=` in each subsequent cockroach command. - -### CockroachDB process hangs when trying to start a node in the background - -See [Why is my process hanging when I try to start it in the background?](operational-faqs.html#why-is-my-process-hanging-when-i-try-to-start-it-in-the-background) - -## Cannot run SQL statements using built-in SQL client - -If the CockroachDB node appeared to [start successfully](start-a-local-cluster.html), in a separate terminal run: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -e "show databases" -~~~ - -You should see a list of the built-in databases: - -~~~ - database_name -+---------------+ - defaultdb - postgres - system -(3 rows) -~~~ - -If you’re not seeing the output above, check for the following: - -- `connection refused` error, which indicates you have not included some flag that you used to start the node. We have additional troubleshooting steps for this error [here](common-errors.html#connection-refused). -- The node crashed. To ascertain if the node crashed, run `ps | grep cockroach` to look for the `cockroach` process. If you cannot locate the `cockroach` process (i.e., it crashed), [file an issue](file-an-issue.html), including the logs from your node and any errors you received. - -## Cannot run a multi-node CockroachDB cluster on the same machine - -{{site.data.alerts.callout_info}} -Running multiple nodes on a single host is useful for testing out CockroachDB, but it's not recommended for production deployments. To run a physically distributed cluster in production, see [Manual Deployment](manual-deployment.html) or [Orchestrated Deployment](orchestration.html). Also be sure to review the [Production Checklist](recommended-production-settings.html). -{{site.data.alerts.end}} - -If you are trying to run all nodes on the same machine, you might get the following errors: - -### Store directory already exists - -~~~ -ERROR: could not cleanup temporary directories from record file: could not lock temporary directory /Users/amruta/go/src/github.com/cockroachdb/cockroach/cockroach-data/cockroach-temp301343769, may still be in use: IO error: While lock file: /Users/amruta/go/src/github.com/cockroachdb/cockroach/cockroach-data/cockroach-temp301343769/TEMP_DIR.LOCK: Resource temporarily unavailable -~~~ - -**Explanation:** When starting a new node on the same machine, the directory you choose to store the data in also contains metadata identifying the cluster the data came from. This causes conflicts when you've already started a node on the server and then tried to start another cluster using the same directory. - -**Solution:** Choose a different directory to store the CockroachDB data. - -### Port already in use - -~~~ -ERROR: cockroach server exited with error: consider changing the port via --listen-addr: listen tcp 127.0.0.1:26257: bind: address already in use -~~~ - -**Solution:** Change the `--port`, `--http-port` flags for each new node that you want to run on the same machine. - -## Cannot join a node to an existing CockroachDB cluster - -### Store directory already exists - -When joining a node to a cluster, you might receive one of the following errors: - -~~~ -no resolvers found; use --join to specify a connected node - -node belongs to cluster {"cluster hash"} but is attempting to connect to a gossip network for cluster {"another cluster hash"} -~~~ - -**Explanation:** When starting a node, the directory you choose to store the data in also contains metadata identifying the cluster the data came from. This causes conflicts when you've already started a node on the server, have quit the `cockroach` process, and then tried to join another cluster. Because the existing directory's cluster ID doesn't match the new cluster ID, the node cannot join it. - -**Solution:** Disassociate the node from the existing directory where you've stored CockroachDB data. For example, you can do either of the following: - -- Choose a different directory to store the CockroachDB data: - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start --store= --join= - ~~~ -- Remove the existing directory and start a node joining the cluster again: - {% include copy-clipboard.html %} - ~~~ shell - $ rm -r cockroach-data/ - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start --join=:26257 - ~~~ - -### Incorrect `--join` address - -If you try to add another node to the cluster, but the `--join` address is not pointing at any of the existing nodes, then the process will never complete, and you'll see a continuous stream of warnings like this: - -~~~ -W180817 17:01:56.506968 886 vendor/google.golang.org/grpc/clientconn.go:942 Failed to dial localhost:20000: grpc: the connection is closing; please retry. -W180817 17:01:56.510430 914 vendor/google.golang.org/grpc/clientconn.go:1293 grpc: addrConn.createTransport failed to connect to {localhost:20000 0 }. Err :connection error: desc = "transport: Error while dialing dial tcp [::1]:20000: connect: connection refused". Reconnecting… -~~~ - -**Explanation:** These warnings tell you that the node cannot establish a connection with the address specified in the `--join` flag. Without a connection to the cluster, the node cannot join. - -**Solution:** To successfully join the node to the cluster, start the node again, but this time include a correct `--join` address. - -### Missing `--join` address - -If you try to add another node to the cluster, but the `--join` address is missing entirely, then the new node will initialize itself as a new cluster instead of joining the existing cluster. You can see this in the status field printed to stdout: - -~~~ -CockroachDB node starting at 2018-02-08 16:30:26.690638 +0000 UTC (took 0.2s) -build: CCL v2.2.0-alpha.20181217 @ 2018/01/08 17:30:06 (go1.8.3) -admin: https://localhost:8085 -sql: postgresql://root@localhost:26262?sslcert=certs%2Fclient.root.crt&sslkey=certs%2Fclient.root.key&sslmode=verify-full&sslrootcert=certs%2Fca.crt -logs: /Users/jesseseldess/cockroachdb-training/cockroach-v2.2.0-alpha.20181217.darwin-10.9-amd64/node6/logs -store[0]: path=/Users/jesseseldess/cockroachdb-training/cockroach-v2.2.0-alpha.20181217.darwin-10.9-amd64/node6 -status: initialized new cluster -clusterID: cfcd80ee-9005-4975-9ae9-9c36d9aaa57e -nodeID: 1 -~~~ - -If you then stop the node and start it again with a correct `--join` address, the startup process will fail because the cluster will notice that the node's cluster ID does not match the cluster ID of the nodes it is trying to join to: - -~~~ -W180815 17:21:00.316845 237 gossip/client.go:123 [n1] failed to start gossip client to localhost:26258: initial connection heartbeat failed: rpc error: code = Unknown desc = client cluster ID "9a6ed934-50e8-472a-9d55-c6ecf9130984" doesn't match server cluster ID "ab6960bb-bb61-4e6f-9190-992f219102c6" -~~~ - -**Solution:** To successfully join the node to the cluster, you need to remove the node's data directory, which is where its incorrect cluster ID is stored, and start the node again. - -## Client connection issues - -If a client cannot connect to the cluster, check basic network connectivity (`ping`), port connectivity (`telnet`), and certificate validity. - -### Networking issues - -Most networking-related issues are caused by one of two issues: - -- Firewall rules, which require your network administrator to investigate -- Inaccessible hostnames on your nodes, which can be controlled with the `--listen-addr` and `--advertise-addr` flags on [`cockroach start`](cockroach-start.html#networking) - - -**Solution:** - -To check your networking setup: - -1. Use `ping`. Every machine you are using as a CockroachDB node should be able to ping every other machine, using the hostnames or IP addresses used in the `--join` flags (and the `--advertise-host` flag if you are using it). - -2. If the machines are all pingable, check if you can connect to the appropriate ports. With your CockroachDB nodes still running, log in to each node and use `telnet` or` nc` to verify machine to machine connectivity on the desired port. For instance, if you are running CockroachDB on the default port of 26257, run either: - - `telnet 26257` - - `nc 26257` - - Both `telnet` and `nc` will exit immediately if a connection cannot be established. If you are running in a firewalled environment, the firewall might be blocking traffic to the desired ports even though it is letting ping packets through. - -To efficiently troubleshoot the issue, it's important to understand where and why it's occurring. We recommend checking the following network-related issues: - -- By default, CockroachDB advertises itself to other nodes using its hostname. If your environment doesn't support DNS or the hostname is not resolvable, your nodes cannot connect to one another. In these cases, you can: - - Change the hostname each node uses to advertises itself with `--advertise-addr` - - Set `--listen-addr=` if the IP is a valid interface on the machine -- Every node in the cluster should be able to ping each other node on the hostnames or IP addresses you use in the `--join`, `--listen-addr`, or `--advertise-addr` flags. -- Every node should be able to connect to other nodes on the port you're using for CockroachDB (26257 by default) through `telnet` or `nc`: - - `telnet 26257` - - `nc 26257` - -Again, firewalls or hostname issues can cause any of these steps to fail. - -### Network partition - -If the Admin UI lists live nodes in the **Dead Nodes** table, then you might have a network partition. - -**Explanation:** A network partition indicates that the nodes cannot communicate with each other in one or both directions because of a configuration problem with the network itself. A symmetric partition is one where the communication is broken in both directions. An asymmetric partition means the connection works in one direction but not the other. An example of a scenario that can cause a network partition is when specific IP addresses or hostnames are allowed by the firewall, and then those addresses or names change after tearing down and rebuilding a node. - -The effect of a network partition depends on which nodes are partitioned and where the ranges are located. It depends to a large extent on whether localities have been defined. - -A partition is a lot like an outage, where all nodes in a smaller partition are down. If you don’t provide localities, a partition that cuts off at least (n-1)/2 nodes will cause data unavailability. - -**Solution:** - -To identify a network partition: - -1. [Access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui). -2. Click the gear icon on the left-hand navigation bar to access the **Advanced Debugging** page. -3. Click **Network Latency**. -4. In the **Latencies** table, check if any cells are marked as “X”. If yes, it indicates that the nodes cannot communicate with those nodes, and might indicate a network partition. - -## CockroachDB authentication issues - -### Missing certificate - -If you try to add a node to a secure cluster without providing the node's security certificate, you will get the following error: - -~~~ -problem with CA certificate: not found -* -* ERROR: cannot load certificates. -* Check your certificate settings, set --certs-dir, or use --insecure for insecure clusters. -* -* problem with CA certificate: not found -* -Failed running "start" -~~~ - -**Explanation:** The error tells you that because the cluster is secure, it requires the new node to provide its security certificate in order to join. - -**Solution:** To successfully join the node to the cluster, start the node again, but this time include the `--certs-dir` flag - -### Certification expiration - -If you’re running a secure cluster, be sure to monitor your certificate expiration. If one of the inter-node certificates expires, nodes will no longer be able to communicate which can look like a network partition. - -To check the certificate expiration date: - -1. [Access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui). -2. Click the gear icon on the left-hand navigation bar to access the **Advanced Debugging** page. -3. Scroll down to the **Even More Advanced Debugging** section. Click **All Nodes**. The **Node Diagnostics** page appears. Click the certificates for each node and check the expiration date for each certificate in the Valid Until field. - -### Client password not set - -While connecting to a secure cluster as a user, CockroachDB first checks if the client certificate exists in the `cert` directory. If the client certificate doesn’t exist, it prompts for a password. If password is not set and you press Enter, the connection attempt fails, and the following error is printed to `stderr`: - -~~~ -Error: pq: invalid password -Failed running "sql" -~~~ - -**Solution:** To successfully connect to the cluster, you must first either generate a client certificate or create a password for the user. - -## Clock sync issues - -### Node clocks are not properly synchronized - -See the following FAQs: - -- [What happens when node clocks are not properly synchronized](operational-faqs.html#what-happens-when-node-clocks-are-not-properly-synchronized) -- [How can I tell how well node clocks are synchronized](operational-faqs.html#how-can-i-tell-how-well-node-clocks-are-synchronized) - -## Capacity planning issues - -Following are some of the possible issues you might have while planning capacity for your cluster: - -- Running CPU at close to 100% utilization with high run queue will result in poor performance. -- Running RAM at close to 100% utilization triggers Linux OOM and/or swapping that will result in poor performance or stability issues. -- Running storage at 100% capacity causes writes to fail, which in turn can cause various processes to stop. -- Running storage at 100% utilization read/write will causes poor service time. -- Running network at 100% utilization causes response between databases and client to be poor. - -**Solution:** [Access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) and navigate to **Metrics > Hardware** dashboard to monitor the following metrics: - -First, check adequate capacity was available for the incident for the following components. - -Type | Time Series | What to look for ---------|--------|--------| -RAM capacity | Memory Usage | Any non-zero value -CPU capacity | CPU Percent | Consistent non-zero values -Network capacity | Network Bytes Received
      Network Bytes Sent | Any non-zero value - -Check Near Out of Capacity Conditions: - -Type | Time Series | What to look for ---------|--------|--------| -RAM capacity | Memory Usage | Consistently more than 80% -CPU capacity | CPU Percent | Consistently less than 20% in idle (i.e.:80% busy) -Network capacity | Network Bytes Received
      Network Bytes Sent | Consistently more than 50% capacity for both - -## Storage issues - -### Disks filling up - -Like any database system, if you run out of disk space the system will no longer be able to accept writes. Additionally, a CockroachDB node needs a small amount of disk space (a few GBs to be safe) to perform basic maintenance functionality. For more information about this issue, see: - -- [Why is memory usage increasing despite lack of traffic?](operational-faqs.html#why-is-memory-usage-increasing-despite-lack-of-traffic) -- [Why is disk usage increasing despite lack of writes?](operational-faqs.html#why-is-disk-usage-increasing-despite-lack-of-writes) -- [Can I reduce or disable the storage of timeseries data?](operational-faqs.html#can-i-reduce-or-disable-the-storage-of-timeseries-data) - -## Memory issues - -### Suspected memory leak - -A CockroachDB node will grow to consume all of the memory allocated for its `cache`. The default size for the cache is ¼ of physical memory which can be substantial depending on your machine configuration. This growth will occur even if your cluster is otherwise idle due to the internal metrics that a CockroachDB cluster tracks. See the `--cache` flag in [`cockroach start`](cockroach-start.html#general). - -CockroachDB memory usage has 3 components: - -- **Go allocated memory**: Memory allocated by the Go runtime to support query processing and various caches maintained in Go by CockroachDB. These caches are generally small in comparison to the RocksDB cache size. If Go allocated memory is larger than a few hundred megabytes, something concerning is going on. -- **CGo allocated memory**: Memory allocated by the C/C++ libraries linked into CockroachDB and primarily concerns RocksDB and the RocksDB block cache. This is the “cache” mentioned in the note above. The size of CGo allocated memory is usually very close to the configured cache size. -- **Overhead**: The process resident set size minus Go/CGo allocated memory. - -If Go allocated memory is larger than a few hundred megabytes, you might have encountered a memory leak. Go comes with a built-in heap profiler which is already enabled on your CockroachDB process. See this [excellent blog post](https://blog.golang.org/profiling-go-programs) on profiling Go programs. - -**Solution:** To determine Go/CGo allocated memory: - -1. [Access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui). - -2. Navigate to **Metrics > Runtime** dashboard, and check the **Memory Usage** graph. - -3. On hovering over the graph, the values for the following metrics are displayed: - - Metric | Description - --------|---- - RSS | Total memory in use by CockroachDB. - Go Allocated | Memory allocated by the Go layer. - Go Total | Total memory managed by the Go layer. - CGo Allocated | Memory allocated by the C layer. - CGo Total | Total memory managed by the C layer. - - - If CGo allocated memory is larger than the configured `cache` size, [file an issue](file-an-issue.html). - - If the resident set size (RSS) minus Go/CGo total memory is larger than 100 megabytes, [file an issue](file-an-issue.html). - -### Node crashes because of insufficient memory - -Often when a node exits without a trace or logging any form of error message, we’ve found that it is the operating system stopping it suddenly due to low memory. So if you're seeing node crashes where the logs just end abruptly, it's probably because the node is running out of memory. On most Unix systems, you can verify if the `cockroach` process was stopped because the node ran out of memory by running: - -~~~ shell -$ sudo dmesg | grep -iC 3 "cockroach" -~~~ - -If the command returns the following message, then you know the node crashed due to insufficient memory: - -~~~ shell -$ host kernel: Out of Memory: Killed process (cockroach). -~~~ - -To rectify the issue, you can either run the cockroachdb process on another node with sufficient memory, or [reduce the cockroachdb memory usage](cockroach-start.html#flags). - -## Decommissioning issues - -### Decommissioning process hangs indefinitely - -**Explanation:** Before decommissioning a node, you need to make sure other nodes are available to take over the range replicas from the node. If no other nodes are available, the decommission process will hang indefinitely. - -**Solution:** Confirm that there are enough nodes with sufficient storage space to take over the replicas from the node you want to remove. - -### Decommissioned nodes displayed in UI forever - -By design, decommissioned nodes are displayed in the Admin UI forever. We retain the list of decommissioned nodes for the following reasons: - -- Decommissioning is not entirely free, so showing those decommissioned nodes in the UI reminds you of the baggage your cluster will have to carry around forever. -- It also explains to future administrations why your node IDs have gaps (e.g., why the nodes are numbered n1, n2, and n8). - -You can follow the discussion here: [https://github.com/cockroachdb/cockroach/issues/24636](https://github.com/cockroachdb/cockroach/issues/24636) - -## Replication issues - -### Admin UI shows under-replicated/unavailable ranges - -When a CockroachDB node dies (or is partitioned) the under-replicated range count will briefly spike while the system recovers. - -**Explanation:** CockroachDB uses consensus replication and requires a quorum of the replicas to be available in order to allow both writes and reads to the range. The number of failures that can be tolerated is equal to (Replication factor - 1)/2. Thus CockroachDB requires (n-1)/2 nodes to achieve quorum. For example, with 3x replication, one failure can be tolerated; with 5x replication, two failures, and so on. - -- Under-replicated Ranges: When a cluster is first initialized, the few default starting ranges will only have a single replica, but as soon as other nodes are available, they will replicate to them until they've reached their desired replication factor. If a range does not have enough replicas, the range is said to be "under-replicated". - -- Unavailable Ranges: If a majority of a range's replicas are on nodes that are unavailable, then the entire range is unavailable and will be unable to process queries. - -**Solution:** - -To identify under-replicated/unavailable ranges: - -1. [Access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui). - -2. On the **Cluster Overview** page, check the **Replication Status**. If the **Under-replicated ranges** or **Unavailable ranges** count is non-zero, then you have under-replicated or unavailable ranges in your cluster. - -3. Check for a network partition: Click the gear icon on the left-hand navigation bar to access the **Advanced Debugging** page. On the Advanced Debugging page, click **Network Latency**. In the **Latencies** table, check if any cells are marked as “X”. If yes, it indicates that the nodes cannot communicate with those nodes, and might indicate a network partition. If there's no partition, and there's still no upreplication after 5 mins, then [file an issue](file-an-issue.html). - -**Add nodes to the cluster:** - -On the Admin UI’s Cluster Overview page, check if any nodes are down. If the number of nodes down is less than (n-1)/2, then that is most probably the cause of the under-replicated/unavailable ranges. Add nodes to the cluster such that the cluster has the required number of nodes to replicate ranges properly. - -If you still see under-replicated/unavailable ranges on the Cluster Overview page, investigate further: - -1. [Access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) -2. Click the gear icon on the left-hand navigation bar to access the **Advanced Debugging** page. -2. Click **Problem Ranges**. -3. In the **Connections** table, identify the node with the under-replicated/unavailable ranges and click the node ID in the Node column. -4. To view the **Range Report** for a range, click on the range number in the **Under-replicated (or slow)** table or **Unavailable** table. -5. On the Range Report page, scroll down to the **Simulated Allocator Output** section. The table contains an error message which explains the reason for the under-replicated range. Follow the guidance in the message to resolve the issue. If you need help understanding the error or the guidance, [file an issue](file-an-issue.html). Please be sure to include the full range report and error message when you submit the issue. - -## Node liveness issues - -"Node liveness" refers to whether a node in your cluster has been determined to be "dead" or "alive" by the rest of the cluster. This is achieved using checks that ensure that each node connected to the cluster is updating its liveness record. This information is shared with the rest of the cluster using an internal gossip protocol. - -Common reasons for node liveness issues include: - -- Heavy I/O load on the node. Because each node needs to update a liveness record on disk, maxing out disk bandwidth can cause liveness heartbeats to be missed. See also: [Capacity planning issues](#capacity-planning-issues). -- Outright I/O failure due to a disk stall. This will cause node liveness issues for the same reasons as listed above. -- Any [Networking issues](#networking-issues) with the node. - -The [Admin UI][admin_ui] provides several ways to check for node liveness issues in your cluster: - -- [Check node heartbeat latency](#check-node-heartbeat-latency) -- [Check node liveness record last update](#check-node-liveness-record-last-update) -- [Check command commit latency](#check-command-commit-latency) - -{{site.data.alerts.callout_info}} -For more information about how node liveness works, see [the architecture documentation on the replication layer](architecture/replication-layer.html#epoch-based-leases-table-data). -{{site.data.alerts.end}} - -### Check node heartbeat latency - -To check node heartbeat latency: - -1. In the [Admin UI][admin_ui], select the **Metrics** tab from the left-hand side of the page. - -2. From the metrics page, select **Dashboard: Distributed** from the dropdown at the top of the page. - -3. Scroll down the metrics page to find the **Node Heartbeat Latency: 99th percentile** and **Node Heartbeat Latency: 90th percentile** graphs. - -**Expected values for a healthy cluster**: Less than 100ms in addition to the network latency between nodes in the cluster. - -### Check node liveness record last update - -To see when a node last updated its liveness record: - -1. Go to the **Node Diagnostics** page of the [Admin UI][admin_ui], which lives at: - - `https://yourcluster.yourdomain/#/reports/nodes` - -2. On the Node Diagnostics page, you will see a table listing information about the nodes in your cluster. To see when a node last updated its liveness record, check the **Updated at** field at the bottom of that node's column. - -**Expected values for a healthy cluster**: When you load this page, the **Updated at** field should be within 4.5 seconds of the current time. If it's higher than that, you will see errors [in the logs](debug-and-error-logs.html). - -### Check command commit latency - -A good signal of I/O load is the **Command Commit Latency** in the **Storage** section of the dashboards. This dashboard measures how quickly [Raft commands](architecture/replication-layer.html) are being committed by nodes in the cluster. - -To view command commit latency: - -1. In the [Admin UI][admin_ui], select the **Metrics** tab from the left-hand side of the page. - -2. From the Metrics page, select **Dashboard: Storage** from the dropdown at the top of the page. - -3. Scroll down the metrics page to find the **Command Commit Latency: 90th percentile** and **Command Commit Latency: 99th percentile** graphs. - -**Expected values for a healthy cluster**: On SSDs, this should be between 1 and 100 milliseconds. On HDDs, this should be no more than 1 second. Note that we [strongly recommend running CockroachDB on SSDs](recommended-production-settings.html#storage). - -## Something else? - -If we do not have a solution here, you can try using our other [support resources](support-resources.html), including: - -- [StackOverflow](http://stackoverflow.com/questions/tagged/cockroachdb) -- [CockroachDB Community Forum](https://forum.cockroachlabs.com) -- [Chatting with our developers on Slack](https://cockroachdb.slack.com) - - - -[admin_ui]: admin-ui-access-and-navigate.html#accessing-the-admin-ui-for-a-secure-cluster diff --git a/src/current/v19.2/cockroach-cert.md b/src/current/v19.2/cockroach-cert.md deleted file mode 100644 index 2df8c8189a9..00000000000 --- a/src/current/v19.2/cockroach-cert.md +++ /dev/null @@ -1,328 +0,0 @@ ---- -title: cockroach cert -summary: A secure CockroachDB cluster uses TLS for encrypted inter-node and client-node communication. -toc: true -key: create-security-certificates.html ---- - -To secure your CockroachDB cluster's inter-node and client-node communication, you need to provide a Certificate Authority (CA) certificate that has been used to sign keys and certificates (SSLs) for: - -- Nodes -- Clients -- Admin UI (optional) - -To create these certificates and keys, use the `cockroach cert` [commands](cockroach-commands.html) with the appropriate subcommands and flags, use [`openssl` commands](https://wiki.openssl.org/index.php/), or use a [custom CA](create-security-certificates-custom-ca.html) (for example, a public CA or your organizational CA). - -
      - - - -
      - -{{site.data.alerts.callout_success}}For details about when and how to change security certificates without restarting nodes, see Rotate Security Certificates.{{site.data.alerts.end}} - -## How security certificates work - -1. Using the `cockroach cert` command, you create a CA certificate and key and then node and client certificates that are signed by the CA certificate. Since you need access to a copy of the CA certificate and key to create node and client certs, it's best to create everything in one place. - -2. You then upload the appropriate node certificate and key and the CA certificate to each node, and you upload the appropriate client certificate and key and the CA certificate to each client. - -3. When nodes establish contact to each other, and when clients establish contact to nodes, they use the CA certificate to verify each other's identity. - -## Subcommands - -Subcommand | Usage ------------|------ -`create-ca` | Create the self-signed certificate authority (CA), which you'll use to create and authenticate certificates for your entire cluster. -`create-node` | Create a certificate and key for a specific node in the cluster. You specify all addresses at which the node can be reached and pass appropriate flags. -`create-client` | Create a certificate and key for a [specific user](create-user.html) accessing the cluster from a client. You specify the username of the user who will use the certificate and pass appropriate flags. -`list` | List certificates and keys found in the certificate directory. - -## Certificate directory - -When using `cockroach cert` to create node and client certificates, you will need access to a local copy of the CA certificate and key. It is therefore recommended to create all certificates and keys in one place and then distribute node and client certificates and keys appropriately. For the CA key, be sure to store it somewhere safe and keep a backup; if you lose it, you will not be able to add new nodes or clients to your cluster. For a walkthrough of this process, see [Manual Deployment](manual-deployment.html). - -## Required keys and certificates - -The `create-*` subcommands generate the CA certificate and all node and client certificates and keys in a single directory specified by the `--certs-dir` flag, with the files named as follows: - -### Node key and certificates - -File name pattern | File usage --------------|------------ -`ca.crt` | CA certificate. -`node.crt` | Server certificate.

      `node.crt` must be signed by `ca.crt` and must have `CN=node` and the list of IP addresses and DNS names listed in `Subject Alternative Name` field. CockroachDB also supports [wildcard notation in DNS names](https://en.wikipedia.org/wiki/Wildcard_certificate). -`node.key` | Key for server certificate. - -### Client key and certificates - -File name pattern | File usage --------------|------------ -`ca.crt` | CA certificate. -`client..crt` | Client certificate for `` (e.g., `client.root.crt` for user `root`).

      Must be signed by `ca.crt`. Also, `client..crt` must have `CN=` (for example, `CN=marc` for `client.marc.crt`) -`client..key` | Key for the client certificate. - -Optionally, if you have a certificate issued by a public CA to securely access the Admin UI, you need to place the certificate and key (`ui.crt` and `ui.key` respectively) in the directory specified by the `--certs-dir` flag. For more information, refer to [Use a UI certificate and key to access the Admin UI](create-security-certificates-custom-ca.html#accessing-the-admin-ui-for-a-secure-cluster). - -Note the following: - -- By default, the `node.crt` is multi-functional, as in the same certificate is used for both incoming connections (from SQL and Admin UI clients, and from other CockroachDB nodes) and for outgoing connections to other CockroachDB nodes. To make this possible, the `node.crt` created using the `cockroach cert` command has `CN=node` and the list of IP addresses and DNS names listed in `Subject Alternative Name` field. - -- The CA key is never loaded automatically by `cockroach` commands, so it should be created in a separate directory, identified by the `--ca-key` flag. - -- Keys (files ending in `.key`) must not have group or world permissions (maximum permissions are 0700, or `rwx------`). This check can be disabled by setting the environment variable `COCKROACH_SKIP_KEY_PERMISSION_CHECK=true`. - -## Synopsis - -Create the CA certificate and key: - -~~~ shell -$ cockroach cert create-ca \ - --certs-dir=[path-to-certs-directory] \ - --ca-key=[path-to-ca-key] -~~~ - -Create a node certificate and key: - -~~~ shell -$ cockroach cert create-node \ - [node-hostname] \ - [node-other-hostname] \ - [node-yet-another-hostname] \ - [hostname-in-wildcard-notation] \ - --certs-dir=[path-to-certs-directory] \ - --ca-key=[path-to-ca-key] -~~~ - -Create a client certificate and key: - -~~~ shell -$ cockroach cert create-client \ - [username] \ - --certs-dir=[path-to-certs-directory] \ - --ca-key=[path-to-ca-key] -~~~ - -List certificates and keys: - -~~~ shell -$ cockroach cert list \ - --certs-dir=[path-to-certs-directory] -~~~ - -View help: - -~~~ shell -$ cockroach cert --help -~~~ -~~~ shell -$ cockroach cert --help -~~~ - -## Flags - -The `cert` command and subcommands support the following [general-use](#general) and [logging](#logging) flags. - -### General - -Flag | Description ------|----------- -`--certs-dir` | The path to the [certificate directory](#certificate-directory) containing all certificates and keys needed by `cockroach` commands.

      This flag is used by all subcommands.

      **Default:** `${HOME}/.cockroach-certs/` -`--ca-key` | The path to the private key protecting the CA certificate.

      This flag is required for all `create-*` subcommands. When used with `create-ca` in particular, it defines where to create the CA key; the specified directory must exist.

      **Env Variable:** `COCKROACH_CA_KEY` -`--allow-ca-key-reuse` | When running the `create-ca` subcommand, pass this flag to re-use an existing CA key identified by `--ca-key`. Otherwise, a new CA key will be generated.

      This flag is used only by the `create-ca` subcommand. It helps avoid accidentally re-using an existing CA key. -`--overwrite` | When running `create-*` subcommands, pass this flag to allow existing files in the certificate directory (`--certs-dir`) to be overwritten.

      This flag helps avoid accidentally overwriting sensitive certificates and keys. -`--lifetime` | The lifetime of the certificate, in hours, minutes, and seconds.

      Certificates are valid from the time they are created through the duration specified in `--lifetime`.

      **Default:** `87840h0m0s` (10 years) -`--key-size` | The size of the CA, node, or client key, in bits.

      **Default:** `2048` - `--also-generate-pkcs8-key` | Also create a key in [PKCS#8 format](https://tools.ietf.org/html/rfc5208), which is the standard key encoding format used by Java. For example usage, see [Build a Java App with CockroachDB](build-a-java-app-with-cockroachdb.html). - -### Logging - -By default, the `cert` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## Examples - -### Create the CA certificate and key pair - -1. Create two directories: - - {% include copy-clipboard.html %} - ~~~ shell - $ mkdir certs - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ mkdir my-safe-directory - ~~~ - - `certs`: You'll generate your CA certificate and all node and client certificates and keys in this directory and then upload some of the files to your nodes. - - `my-safe-directory`: You'll generate your CA key in this directory and then reference the key when generating node and client certificates. After that, you'll keep the key safe and secret; you will not upload it to your nodes. - -2. Generate the CA certificate and key: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-ca \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ ls -l certs - ~~~ - - ~~~ - total 8 - -rw-r--r-- 1 maxroach maxroach 1.1K Jul 10 14:12 ca.crt - ~~~ - -### Create the certificate and key pairs for nodes - -1. Generate the certificate and key for the first node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-node \ - node1.example.com \ - node1.another-example.com \ - *.dev.another-example.com \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ ls -l certs - ~~~ - - ~~~ - total 24 - -rw-r--r-- 1 maxroach maxroach 1.1K Jul 10 14:12 ca.crt - -rw-r--r-- 1 maxroach maxroach 1.2K Jul 10 14:16 node.crt - -rw------- 1 maxroach maxroach 1.6K Jul 10 14:16 node.key - ~~~ - -2. Upload certificates to the first node: - - {% include copy-clipboard.html %} - ~~~ shell - # Create the certs directory: - $ ssh @ "mkdir certs" - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - # Upload the CA certificate and node certificate and key: - $ scp certs/ca.crt \ - certs/node.crt \ - certs/node.key \ - @:~/certs - ~~~ - -3. Delete the local copy of the first node's certificate and key: - - {% include copy-clipboard.html %} - ~~~ shell - $ rm certs/node.crt certs/node.key - ~~~ - - {{site.data.alerts.callout_info}}This is necessary because the certificates and keys for additional nodes will also be named node.crt and node.key As an alternative to deleting these files, you can run the next cockroach cert create-node commands with the --overwrite flag.{{site.data.alerts.end}} - -4. Create the certificate and key for the second node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-node \ - node2.example.com \ - node2.another-example.com \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ ls -l certs - ~~~ - - ~~~ - total 24 - -rw-r--r-- 1 maxroach maxroach 1.1K Jul 10 14:12 ca.crt - -rw-r--r-- 1 maxroach maxroach 1.2K Jul 10 14:17 node.crt - -rw------- 1 maxroach maxroach 1.6K Jul 10 14:17 node.key - ~~~ - -5. Upload certificates to the second node: - - {% include copy-clipboard.html %} - ~~~ shell - # Create the certs directory: - $ ssh @ "mkdir certs" - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - # Upload the CA certificate and node certificate and key: - $ scp certs/ca.crt \ - certs/node.crt \ - certs/node.key \ - @:~/certs - ~~~ - -6. Repeat steps 3 - 5 for each additional node. - -### Create the certificate and key pair for a client - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert create-client \ -maxroach \ ---certs-dir=certs \ ---ca-key=my-safe-directory/ca.key -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ ls -l certs -~~~ - -~~~ -total 40 --rw-r--r-- 1 maxroach maxroach 1.1K Jul 10 14:12 ca.crt --rw-r--r-- 1 maxroach maxroach 1.1K Jul 10 14:13 client.maxroach.crt --rw------- 1 maxroach maxroach 1.6K Jul 10 14:13 client.maxroach.key --rw-r--r-- 1 maxroach maxroach 1.2K Jul 10 14:17 node.crt --rw------- 1 maxroach maxroach 1.6K Jul 10 14:17 node.key -~~~ - -### List certificates and keys - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach cert list \ ---certs-dir=certs -~~~ - -~~~ -Certificate directory: certs -+-----------------------+---------------------+---------------------+------------+--------------------------------------------------------+-------+ -| Usage | Certificate File | Key File | Expires | Notes | Error | -+-----------------------+---------------------+---------------------+------------+--------------------------------------------------------+-------+ -| Certificate Authority | ca.crt | | 2027/07/18 | num certs: 1 | | -| Node | node.crt | node.key | 2022/07/14 | addresses: node2.example.com,node2.another-example.com | | -| Client | client.maxroach.crt | client.maxroach.key | 2022/07/14 | user: maxroach | | -+-----------------------+---------------------+---------------------+------------+--------------------------------------------------------+-------+ -(3 rows) -~~~ - -## See also - -- [Security overview](security-overview.html) -- [Authentication](authentication.html) -- [Client Connection Parameters](connection-parameters.html) -- [Rotate Security Certificates](rotate-certificates.html) -- [Manual Deployment](manual-deployment.html) -- [Orchestrated Deployment](orchestration.html) -- [Local Deployment](secure-a-cluster.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/cockroach-commands.md b/src/current/v19.2/cockroach-commands.md deleted file mode 100644 index 9ee27c5aa20..00000000000 --- a/src/current/v19.2/cockroach-commands.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -title: Cockroach Commands -summary: Learn the commands for configuring, starting, and managing a CockroachDB cluster. -toc: true ---- - -This page introduces the `cockroach` commands for configuring, starting, and managing a CockroachDB cluster, as well as environment variables that can be used in place of certain flags. - -You can run `cockroach help` in your shell to get similar guidance. - - -## Commands - -Command | Usage ---------|---- -[`cockroach start`](cockroach-start.html) | Start a node as part of a multi-node cluster. -[`cockroach init`](cockroach-init.html) | Initialize a multi-node cluster. -[`cockroach start-single-node`](cockroach-start-single-node.html) | New in v19.2: Start a single-node cluster. -[`cockroach cert`](cockroach-cert.html) | Create CA, node, and client certificates. -[`cockroach quit`](cockroach-quit.html) | Temporarily stop a node or permanently remove a node. -[`cockroach sql`](cockroach-sql.html) | Use the built-in SQL client. -[`cockroach sqlfmt`](cockroach-sqlfmt.html) | Reformat SQL queries for enhanced clarity. -`cockroach user` | **Deprecated.** To create and manage users and roles, use [`CREATE USER`](create-user.html), [`ALTER USER`](alter-user.html), [`DROP USER`](drop-user.html), [`SHOW USERS`](show-users.html), [`CREATE ROLE`](create-role.html), [`DROP ROLE`](drop-role.html), and [`SHOW ROLES`](show-roles.html). -[`cockroach node`](cockroach-node.html) | List node IDs, show their status, decommission nodes for removal, or recommission nodes. -[`cockroach dump`](cockroach-dump.html) | Back up a table by outputting the SQL statements required to recreate the table and all its rows. -[`cockroach demo`](cockroach-demo.html) | Start a temporary, in-memory CockroachDB cluster, and open an interactive SQL shell to it. -[`cockroach gen`](cockroach-gen.html) | Generate manpages, a bash completion file, example SQL data, or an HAProxy configuration file for a running cluster. -[`cockroach version`](cockroach-version.html) | Output CockroachDB version details. -[`cockroach debug ballast`](cockroach-debug-ballast.html) | Create a large, unused file in a node's storage directory that you can delete if the node runs out of disk space. -[`cockroach debug encryption-active-key`](cockroach-debug-encryption-active-key.html) | View the encryption algorithm and store key. -[`cockroach debug zip`](cockroach-debug-zip.html) | Generate a `.zip` file that can help Cockroach Labs troubleshoot issues with your cluster. -[`cockroach debug merge-logs`](cockroach-debug-merge-logs.html) | Merge multiple log files from different machines into a single stream. -[`cockroach workload`](cockroach-workload.html) | Run a built-in load generator against a cluster. - -## Environment variables - -For many common `cockroach` flags, such as `--port` and `--user`, you can set environment variables once instead of manually passing the flags each time you execute commands. - -- To find out which flags support environment variables, see the documentation for each [command](#commands). -- To output the current configuration of CockroachDB and other environment variables, run `env`. -- When a node uses environment variables on [startup](cockroach-start.html), the variable names are printed to the node's logs; however, the variable values are not. - -CockroachDB prioritizes command flags, environment variables, and defaults as follows: - -1. If a flag is set for a command, CockroachDB uses it. -2. If a flag is not set for a command, CockroachDB uses the corresponding environment variable. -3. If neither the flag nor environment variable is set, CockroachDB uses the default for the flag. -4. If there's no flag default, CockroachDB gives an error. - -For more details, see [Client Connection Parameters](connection-parameters.html). diff --git a/src/current/v19.2/cockroach-debug-ballast.md b/src/current/v19.2/cockroach-debug-ballast.md deleted file mode 100644 index d1852cdb0b2..00000000000 --- a/src/current/v19.2/cockroach-debug-ballast.md +++ /dev/null @@ -1,59 +0,0 @@ ---- -title: cockroach debug ballast -summary: Create a large, unused file in a node's storage directory that you can delete if the node runs out of disk space. -toc: true -key: debug-ballast.html ---- - -The `cockroach debug ballast` [command](cockroach-commands.html) creates a large, unused file that you can place in a node's storage directory. In the case that a node runs out of disk space and shuts down, you can delete the ballast file to free up enough space to be able to restart the node. - -- Do not run `cockroach debug ballast` with a unix `root` user. Doing so brings the risk of mistakenly affecting system directories or files. -- Do not name the target file similar to a block device in `/dev`. Doing so brings the risk of mistyping a `/dev` prefix into the command and thereby corrupting a filesystem. -- In addition to placing a ballast file in each node's storage directory, it is important to actively [monitor remaining disk space](monitoring-and-alerting.html#events-to-alert-on). -- Ballast files may be created in many ways, including the standard `dd` command. `cockroach debug ballast` uses the `fallocate` system call when available, so it will be faster than `dd`. - -## Subcommands - -{% include {{ page.version.version }}/misc/debug-subcommands.md %} - -## Synopsis - -Create a ballast file: - -~~~ shell -$ cockroach debug ballast [path to ballast file] [flags] -~~~ - -View help: - -~~~ shell -$ cockroach debug ballast --help -~~~ - -## Flags - -Flag | Description ------|----------- -`--size`
      `-z` | The amount of space to fill, or to leave available, in a node's storage directory via a ballast file. Positive values equal the size of the ballast file. Negative values equal the amount of space to leave after creating the ballast file. This can be a percentage (notated as a decimal or with %) or any bytes-based unit, for example:

      `--size=1000000000 ----> 1000000000 bytes`
      `--size=1GiB ----> 1073741824 bytes`
      `--size=5% ----> 5% of available space`
      `--size=0.05 ----> 5% of available space`
      `--size=.05 ----> 5% of available space`

      **Default:** `1GB` New in v19.2 - -## Examples - -### Create a 1GB ballast file (default) - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach debug ballast cockroach-data/ballast.txt -~~~ - -### Create a ballast file of a different size - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach debug ballast cockroach-data/ballast.txt --size=2GB -~~~ - -## See also - -- [Other Cockroach Commands](cockroach-commands.html) -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Production Checklist](recommended-production-settings.html) diff --git a/src/current/v19.2/cockroach-debug-encryption-active-key.md b/src/current/v19.2/cockroach-debug-encryption-active-key.md deleted file mode 100644 index bb171af21ef..00000000000 --- a/src/current/v19.2/cockroach-debug-encryption-active-key.md +++ /dev/null @@ -1,44 +0,0 @@ ---- -title: cockroach debug encryption-active-key -summary: Learn the command for viewing the algorithm and store key for an encrypted store. -toc: true -key: debug-encryption-active-key.html ---- - -The `cockroach debug encryption-active-key` [command](cockroach-commands.html) displays the encryption algorithm and store key for an encrypted store. - -## Synopsis - -~~~ shell -$ cockroach debug encryption-active-key [path specified by the store flag] -~~~ - -## Subcommands - -{% include {{ page.version.version }}/misc/debug-subcommands.md %} - -## Example - -Start a node with encryption-at-rest enabled: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --store=cockroach-data --enterprise-encryption=path=cockroach-data,key=aes-128.key,old-key=plain --insecure --certs-dir=certs -~~~ - -View the encryption algorithm and store key: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach debug encryption-active-key cockroach-data -~~~ - -~~~ -AES128_CTR:be235c29239aa84a48e5e1874d76aebf7fb3c1bdc438cec2eb98de82f06a57a0 -~~~ - -## See also - -- [File an Issue](file-an-issue.html) -- [Other Cockroach Commands](cockroach-commands.html) -- [Troubleshooting Overview](troubleshooting-overview.html) diff --git a/src/current/v19.2/cockroach-debug-merge-logs.md b/src/current/v19.2/cockroach-debug-merge-logs.md deleted file mode 100644 index 79904a9a2d2..00000000000 --- a/src/current/v19.2/cockroach-debug-merge-logs.md +++ /dev/null @@ -1,75 +0,0 @@ ---- -title: cockroach debug merge-logs -summary: Learn the command for merging the collected debug logs from all nodes in your cluster. -toc: true -key: debug-merge-logs.html ---- - -The `cockroach debug merge-logs` [command](cockroach-commands.html) merges log files from multiple nodes into a single time-ordered stream of messages with an added per-message prefix to indicate the corresponding node. You can use it in conjunction with logs collected using the [`debug zip`](cockroach-debug-zip.html) command to aid in debugging. - -{{site.data.alerts.callout_danger}} -The file produced by `cockroach debug merge-log` can contain highly sensitive, unanonymized information, such as usernames, passwords, and possibly your table's data. You should share this data only with Cockroach Labs developers and only after determining the most secure method of delivery. -{{site.data.alerts.end}} - -## Subcommands - -{% include {{ page.version.version }}/misc/debug-subcommands.md %} - -## Synopsis - -~~~ shell -$ cockroach debug merge-logs [log file directory] [flags] -~~~ - -## Flags - -Use the following flags to filter the `debug merge-logs` results for a specified regular expression or time range. - -Flag | Description ------|----------- -`--filter` | Limit the results to the specified regular expression -`--from` | Start time for the time range filter. -`--to` | End time for the time range filter. - -## Example - -Generate a debug zip file: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach debug zip ./cockroach-data/logs/debug.zip --insecure -~~~ - -Unzip the file: - -{% include copy-clipboard.html %} -~~~ shell -$ unzip ./cockroach-data/logs/debug.zip -~~~ - -Merge the logs in the debug folder: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach debug merge-logs debug/nodes/*/logs/* -~~~ - -Alternatively, filter the merged logs for a specified time range: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach debug merge-logs debug/nodes/*/logs/* --from= "18:36:28.208553" --to= "18:36:29.232864" -~~~ - -You can also filter the merged logs for a regular expression: - -{% include copy-clipboard.html %} -~~~ shell -cockroach debug merge-logs debug/nodes/*/logs/* --filter="RUNNING IN INSECURE MODE" -~~~ - -## See also - -- [File an Issue](file-an-issue.html) -- [Other Cockroach Commands](cockroach-commands.html) -- [Troubleshooting Overview](troubleshooting-overview.html) diff --git a/src/current/v19.2/cockroach-debug-zip.md b/src/current/v19.2/cockroach-debug-zip.md deleted file mode 100644 index 9e2d11739d0..00000000000 --- a/src/current/v19.2/cockroach-debug-zip.md +++ /dev/null @@ -1,117 +0,0 @@ ---- -title: cockroach debug zip -summary: Learn the commands for collecting debug information from all nodes in your cluster. -toc: true -key: debug-zip.html ---- - -The `cockroach debug zip` [command](cockroach-commands.html) connects to your cluster and gathers information from each active node into a single file (inactive nodes are not included): - -- [Log files](debug-and-error-logs.html) -- Cluster events -- Schema change events -- Node liveness -- Gossip data -- Stack traces -- Range lists -- A list of databases and tables -- [Cluster Settings](cluster-settings.html) -- [Metrics](admin-ui-custom-chart-debug-page.html#available-metrics) -- Alerts -- Heap profiles -- Problem ranges -- Sessions -- Queries - -Additionally, you can run the [`debug merge-logs`](cockroach-debug-merge-logs.html) command to merge the collected logs in one file, making it easier to parse them to locate an issue with your cluster. - -{{site.data.alerts.callout_danger}} -The file produced by `cockroach debug zip` can contain highly sensitive, unanonymized information, such as usernames, hashed passwords, and possibly your table's data. You should share this data only with Cockroach Labs developers and only after determining the most secure method of delivery. -{{site.data.alerts.end}} - -## Details - -### Use cases - -There are two scenarios in which `debug zip` is useful: - -- To collect all of your nodes' logs, which you can then parse to locate issues. It's important to note, though, that `debug zip` can only access logs from active nodes. See more information [on this page](#collecting-log-files). - -- If you experience severe or difficult-to-reproduce issues with your cluster, Cockroach Labs might ask you to send us your cluster's debugging information using `cockroach debug zip`. - -### Collecting log files - -When you issue the `debug zip` command, the node that receives the request connects to each other node in the cluster. Once it's connected, the node requests the content of all log files stored on the node, the location of which is determined by the `--log-dir` value when you [started the node](cockroach-start.html). - -Because `debug zip` relies on CockroachDB's distributed architecture, this means that nodes not currently connected to the cluster cannot respond to the request, so their log files *are not* included. - -After receiving the log files from all of the active nodes, the requesting node aggregates the files and writes them to an archive file you specify. - -You can locate logs in the unarchived file's `debug/nodes/[node dir]/logs` directories. - -## Subcommands - -{% include {{ page.version.version }}/misc/debug-subcommands.md %} - -## Synopsis - -~~~ shell -$ cockroach debug zip [ZIP file destination] [flags] -~~~ - -It's important to understand that the `[flags]` here are used to connect to CockroachDB nodes. This means the values you use in those flags must connect to an active node. If no nodes are live, you must [start at least one node](cockroach-start.html). - -## Flags - -The `debug zip` subcommand supports the following [general-use](#general), [client connection](#client-connection), and [logging](#logging) flags. - -### General - -Flag | Description ------|----------- -`--certs-dir` | The path to the [certificate directory](cockroach-cert.html). The directory must contain valid certificates if running in secure mode.

      **Env Variable:** `COCKROACH_CERTS_DIR`
      **Default:** `${HOME}/.cockroach-certs/` -`--host` | The server host to connect to. This can be the address of any node in the cluster.

      **Env Variable:** `COCKROACH_HOST`
      **Default:** `localhost` -`--insecure` | Run in insecure mode. If this flag is not set, the `--certs-dir` flag must point to valid certificates.

      **Env Variable:** `COCKROACH_INSECURE`
      **Default:** `false` -`--port`
      `-p` | The server port to connect to.

      **Env Variable:** `COCKROACH_PORT`
      **Default:** `26257` - -### Client connection - -Flag | Description ------|----------- -`--url` | A [connection URL](connection-parameters.html#connect-using-a-url) to use instead of the other arguments.

      **Env Variable:** `COCKROACH_URL`
      **Default:** no URL - -### Logging - -By default, the `debug zip` command logs errors it experiences to `stderr`. Note that these are errors executing `debug zip`; these are not errors that the logs collected by `debug zip` contain. - -If you need to troubleshoot this command's behavior, you can also change its [logging behavior](debug-and-error-logs.html). - -## Examples - -### Generate a debug zip file - -{% include copy-clipboard.html %} -~~~ shell -# Generate the debug zip file for an insecure cluster: -$ cockroach debug zip ./cockroach-data/logs/debug.zip --insecure -~~~ - -{% include copy-clipboard.html %} -~~~ shell -# Generate the debug zip file for a secure cluster: -$ cockroach debug zip ./cockroach-data/logs/debug.zip -~~~ - -{% include copy-clipboard.html %} -~~~ shell -# Generate the debug zip file from a remote machine: -$ cockroach debug zip ./crdb-debug.zip --host=200.100.50.25 -~~~ - -{{site.data.alerts.callout_info}}Secure examples assume you have the appropriate certificates in the default certificate directory, ${HOME}/.cockroach-certs/.{{site.data.alerts.end}} - -## See also - -- [File an Issue](file-an-issue.html) -- [Other Cockroach Commands](cockroach-commands.html) -- [Troubleshooting Overview](troubleshooting-overview.html) diff --git a/src/current/v19.2/cockroach-demo.md b/src/current/v19.2/cockroach-demo.md deleted file mode 100644 index 1fa6eab5a02..00000000000 --- a/src/current/v19.2/cockroach-demo.md +++ /dev/null @@ -1,253 +0,0 @@ ---- -title: cockroach demo -summary: Use cockroach demo to open a SQL shell to a temporary, in-memory, single-node CockroachDB cluster. -toc: true ---- - -The `cockroach demo` [command](cockroach-commands.html) starts a temporary, in-memory CockroachDB cluster, a preloaded dataset, and opens an [interactive SQL shell](cockroach-sql.html) to the cluster. - -The in-memory cluster persists only as long as the SQL shell is open. As soon as the shell is exited, the cluster and all its data are permanently destroyed. This command is therefore recommended only as an easy way to experiment with the CockroachDB SQL dialect. - - -New in v19.2: Each instance of `cockroach demo` loads a temporary [enterprise license](https://www.cockroachlabs.com/get-cockroachdb/enterprise/) that expires after an hour. - -## Synopsis - -Start an interactive SQL shell: - -~~~ shell -$ cockroach demo -~~~ - -Load a sample dataset and start an interactive SQL shell: - -~~~ shell -$ cockroach demo -~~~ - -Execute SQL from the command line: - -~~~ shell -$ cockroach demo --execute=";" --execute="" -~~~ - -Exit the interactive SQL shell: - -~~~ shell -$ \q -ctrl-d -~~~ - -View help: - -~~~ shell -$ cockroach demo --help -~~~ - -## Datasets - -Workload | Description ----------|------------ -`bank` | A `bank` database, with one `bank` table containing account details. -`intro` | An `intro` database, with one table, `mytable`, with a hidden message. -`movr` | A `movr` database, with several tables of data for the [MovR example application](movr.html).

      New in v19.2: By default, `cockroach demo` loads the `movr` database as the [current database](sql-name-resolution.html#current-database), with sample region (`region`) and availability zone (`az`) replica localities for each node specified with the [`--nodes` flag](cockroach-demo.html#flags). -`startrek` | A `startrek` database, with two tables, `episodes` and `quotes`. -`tpcc` | A `tpcc` database, with a rich schema of multiple tables. - -## Flags - -The `demo` command supports the following general-use flags. - -Flag | Description ------|------------ -`--demo-locality` | New in v19.2: Specify [locality](cockroach-start.html#locality) information for each demo node. The input is a colon-separated list of key-value pairs, where the ith pair is the locality setting for the ith demo cockroach node.

      For example, the following option assigns node 1's region to `us-east1` and availability zone to `1`, node 2's region to `us-east2` and availability zone to `2`, and node 3's region to `us-east3` and availability zone to `3`:

      `--demo-locality=region=us-east1,az=1:region=us-east1,az=2:region=us-east1,az=3`

      By default, `cockroach demo` uses sample region (`region`) and availability zone (`az`) replica localities for each node specified with the `--nodes` flag. -`--echo-sql` | Reveal the SQL statements sent implicitly by the command-line utility. This can also be enabled within the interactive SQL shell via the `\set echo` [shell command](cockroach-sql.html#commands). -`--execute`
      `-e` | Execute SQL statements directly from the command line, without opening a shell. This flag can be set multiple times, and each instance can contain one or more statements separated by semi-colons.

      If an error occurs in any statement, the command exits with a non-zero status code and further statements are not executed. The results of each statement are printed to the standard output (see `--format` for formatting options). -`--format` | How to display table rows printed to the standard output. Possible values: `tsv`, `csv`, `table`, `raw`, `records`, `sql`, `html`.

      **Default:** `table` for sessions that [output on a terminal](cockroach-sql.html#session-and-output-types); `tsv` otherwise

      This flag corresponds to the `display_format` [client-side option](cockroach-sql.html#client-side-options) for use in interactive sessions. -`--geo-partitioned-replicas` | New in v19.2: Start a 9-node demo cluster with the [Geo-Partitioned Replicas](topology-geo-partitioned-replicas.html) topology pattern applied to the [`movr`](movr.html) database. -`--nodes` | New in v19.2: Specify the number of in-memory nodes to create for the demo.

      **Default:** 1 -`--safe-updates` | Disallow potentially unsafe SQL statements, including `DELETE` without a `WHERE` clause, `UPDATE` without a `WHERE` clause, and `ALTER TABLE ... DROP COLUMN`.

      **Default:** `true` for [interactive sessions](cockroach-sql.html#session-and-output-types); `false` otherwise

      Potentially unsafe SQL statements can also be allowed/disallowed for an entire session via the `sql_safe_updates` [session variable](set-vars.html). -`--set` | Set a [client-side option](cockroach-sql.html#client-side-options) before starting the SQL shell or executing SQL statements from the command line via `--execute`. This flag may be specified multiple times, once per option.

      After starting the SQL shell, the `\set` and `unset` commands can be use to enable and disable client-side options as well. -`--with-load` | New in v19.2: Run a demo [`movr`](movr.html) workload against the preloaded `movr` database. - -## Logging - -By default, the `demo` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## SQL shell - -All [SQL shell commands, client-side options, help, and shortcuts](cockroach-sql.html#sql-shell) supported by the `cockroach sql` command are also supported by the `cockroach demo` command. - -## Web UI - -When the SQL shell connects to the in-memory cluster, it prints a welcome text with some tips and CockroachDB version and cluster details. Most of these details resemble the [welcome text](cockroach-sql.html#welcome-message) that gets printed when connecting `cockroach sql` to a permanent cluster. However, one unique detail to note is the **Web UI** link. For the duration of the cluster, you can open the Web UI for the cluster at this link. - -~~~ shell -# -# Welcome to the CockroachDB demo database! -# -# You are connected to a temporary, in-memory CockroachDB -# instance. Your changes will not be saved! -# -# Web UI: http://127.0.0.1:60105 -# -# Server version: CockroachDB CCL v2.1.0-alpha.20180702-281-g07a11b8e8c-dirty (x86_64-apple-darwin17.6.0, built 2018/07/08 14:00:29, go1.10.1) (same version as client) -# Cluster ID: 61b41af6-fb2c-4d9a-8a91-0a31933b3d31 -# -# Enter \? for a brief introduction. -# -root@127.0.0.1:60104/defaultdb> -~~~ - -## Diagnostics Reporting - -By default, `cockroach demo` shares anonymous usage details with Cockroach Labs. To opt out, set the [`diagnostics.reporting.enabled`](diagnostics-reporting.html#after-cluster-initialization) [cluster setting](cluster-settings.html) to `false`. You can also opt out by setting the [`COCKROACH_SKIP_ENABLING_DIAGNOSTIC_REPORTING`](diagnostics-reporting.html#at-cluster-initialization) environment variable to `false` before running `cockroach demo`. - -## Example - -In these examples, we demonstrate how to start a shell with `cockroach demo`. For more SQL shell features, see the [`cockroach sql` examples](cockroach-sql.html#examples). - -### Start an interactive SQL shell - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach demo -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE drivers ( - id UUID DEFAULT gen_random_uuid(), - city STRING NOT NULL, - name STRING, - dl STRING UNIQUE, - address STRING, - CONSTRAINT primary_key PRIMARY KEY (city ASC, id ASC) -); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO drivers (city, name) VALUES ('new york', 'Catherine Nelson'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM drivers; -~~~ - -~~~ - id | city | name | dl | address -+--------------------------------------+----------+------------------+------+---------+ - df3dc272-b572-4ca4-88c8-e9974dbd381a | new york | Catherine Nelson | NULL | NULL -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> \q -~~~ - -### Load a sample dataset and start an interactive SQL shell - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach demo movr --nodes=3 --demo-locality=region=us-east1:region=us-central1:region=us-west1 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES; -~~~ - -~~~ - table_name -+----------------------------+ - promo_codes - rides - user_promo_codes - users - vehicle_location_histories - vehicles -(6 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users WHERE city = 'new york'; -~~~ - -~~~ - id | city | name | address | credit_card -+--------------------------------------+----------+------------------+-----------------------------+-------------+ - 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228 - 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892 - 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636 - 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234 - 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487 -(5 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> \q -~~~ - -### Execute SQL from the command-line - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach demo \ ---execute="CREATE TABLE drivers ( - id UUID DEFAULT gen_random_uuid(), - city STRING NOT NULL, - name STRING, - dl STRING UNIQUE, - address STRING, - CONSTRAINT primary_key PRIMARY KEY (city ASC, id ASC) -);" \ ---execute="INSERT INTO drivers (city, name) VALUES ('new york', 'Catherine Nelson');" \ ---execute="SELECT * FROM drivers;" -~~~ - -~~~ -CREATE TABLE -INSERT 1 - id | city | name | dl | address -+--------------------------------------+----------+------------------+------+---------+ - df3dc272-b572-4ca4-88c8-e9974dbd381a | new york | Catherine Nelson | NULL | NULL -(1 row) -~~~ - -### Run `cockroach demo` with a workload - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach demo --with-load -~~~ - -This command starts a demo cluster with the `movr` database preloaded and then inserts rows into each table in the `movr` database. You can monitor the workload progress on the [Admin UI](admin-ui-overview-dashboard.html#sql-queries). - -### Start a multi-region demo cluster with automatic geo-partitioning - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach demo --geo-partitioned-replicas -~~~ - -This command starts a 9-node demo cluster with the `movr` database preloaded, and [partitions](partitioning.html) and [zone constraints](configure-replication-zones.html) applied to the primary and secondary indexes. For more information, see the [Geo-Partitioned Replicas](topology-geo-partitioned-replicas.html) topology pattern. - -### Try your own scenario - -In addition to using one of the [pre-loaded dataset](#datasets), you can create your own database (e.g., [`CREATE DATABASE ;`](create-database.html)), or use the empty `defaultdb` database (e.g., [`SET DATABASE defaultdb;`](set-vars.html)) to test our your own scenario involving any CockroachDB SQL features you are interested in. - -## See also - -- [`cockroach sql`](cockroach-sql.html) -- [`cockroach workload`](cockroach-workload.html) -- [Other Cockroach Commands](cockroach-commands.html) -- [SQL Statements](sql-statements.html) -- [Learn CockroachDB SQL](learn-cockroachdb-sql.html) -- [MovR: Vehicle-Sharing App](movr.html) diff --git a/src/current/v19.2/cockroach-dump.md b/src/current/v19.2/cockroach-dump.md deleted file mode 100644 index 5dfe1956709..00000000000 --- a/src/current/v19.2/cockroach-dump.md +++ /dev/null @@ -1,402 +0,0 @@ ---- -title: cockroach dump -summary: Learn how to dump schemas and data from a CockroachDB cluster. -toc: true -key: sql-dump.html ---- - -The `cockroach dump` [command](cockroach-commands.html) outputs the SQL statements required to recreate tables, views, and sequences. This command can be used to back up or export each database in a cluster. The output should also be suitable for importing into other relational databases, with minimal adjustments. - -{{site.data.alerts.callout_success}} -CockroachDB [enterprise license](https://www.cockroachlabs.com/pricing/) users can also back up their cluster's data using [`BACKUP`](backup.html). -{{site.data.alerts.end}} - -## Considerations - -When `cockroach dump` is executed: - -- Table, sequence, and view schemas and table data are dumped as they appeared at the time that the command is started. Any changes after the command starts will not be included in the dump. -- Table and view schemas are dumped in the order in which they can successfully be recreated. This is true of sequences as well. -- If the dump takes longer than the [`ttlseconds`](configure-replication-zones.html) replication setting for the table (25 hours by default), the dump may fail. -- Reads, writes, and schema changes can happen while the dump is in progress, but will not affect the output of the dump. - -{{site.data.alerts.callout_info}} -The user must have the `SELECT` privilege on the target table(s). -{{site.data.alerts.end}} - -## Synopsis - -Dump the schemas and data of specific tables to stdout: - -~~~ shell -$ cockroach dump
      -~~~ - -Dump just the data of specific tables to stdout: - -~~~ shell -$ cockroach dump
      --dump-mode=data -~~~ - -Dump just the schemas of specific tables to stdout: - -~~~ shell -$ cockroach dump
      --dump-mode=schema -~~~ - -Dump the schemas and data of all tables in a database to stdout: - -~~~ shell -$ cockroach dump -~~~ - -Dump just the schemas of all tables in a database to stdout: - -~~~ shell -$ cockroach dump --dump-mode=schema -~~~ - -Dump just the data of all tables in a database to stdout: - -~~~ shell -$ cockroach dump --dump-mode=data -~~~ - -Dump to a file: - -~~~ shell -$ cockroach dump
      > dump-file.sql -~~~ - -View help: - -~~~ shell -$ cockroach dump --help -~~~ - -## Flags - -The `dump` command supports the following [general-use](#general) and [logging](#logging) flags. - -### General - -Flag | Description ------|------------ -`--as-of` | Dump table schema and/or data as they appear at the specified [timestamp](timestamp.html). See this [example](#dump-table-data-as-of-a-specific-time) for a demonstration.

      Note that historical data is available only within the garbage collection window, which is determined by the [`ttlseconds`](configure-replication-zones.html) replication setting for the table (25 hours by default). If this timestamp is earlier than that window, the dump will fail.

      **Default:** Current time -`--dump-mode` | Whether to dump table and view schemas, table data, or both.

      To dump just table and view schemas, set this to `schema`. To dump just table data, set this to `data`. To dump both table and view schemas and table data, leave this flag out or set it to `both`.

      Table and view schemas are dumped in the order in which they can successfully be recreated. For example, if a database includes a table, a second table with a foreign key dependency on the first, and a view that depends on the second table, the dump will list the schema for the first table, then the schema for the second table, and then the schema for the view.

      **Default:** `both` -`--echo-sql` | Reveal the SQL statements sent implicitly by the command-line utility. - -### Client connection - -{% include {{ page.version.version }}/sql/connection-parameters.md %} - -See [Client Connection Parameters](connection-parameters.html) for more details. - -{{site.data.alerts.callout_info}} -The user specified with `--user` must have the `SELECT` privilege on the target tables. -{{site.data.alerts.end}} - -### Logging - -By default, the `dump` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## Examples - -{{site.data.alerts.callout_info}} -These examples use our sample `startrek` database, which you can add to a cluster via the [`cockroach gen`](cockroach-gen.html#generate-example-data) command. Also, the examples assume that the `maxroach` user has been [granted](grant.html) the `SELECT` privilege on all target tables. -{{site.data.alerts.end}} - -### Dump a table's schema and data - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach dump startrek episodes --insecure --user=maxroach > backup.sql -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cat backup.sql -~~~ - -~~~ -CREATE TABLE episodes ( - id INT NOT NULL, - season INT NULL, - num INT NULL, - title STRING NULL, - stardate DECIMAL NULL, - CONSTRAINT "primary" PRIMARY KEY (id), - FAMILY "primary" (id, season, num), - FAMILY fam_1_title (title), - FAMILY fam_2_stardate (stardate) -); - -INSERT INTO episodes (id, season, num, title, stardate) VALUES - (1, 1, 1, 'The Man Trap', 1531.1), - (2, 1, 2, 'Charlie X', 1533.6), - (3, 1, 3, 'Where No Man Has Gone Before', 1312.4), - (4, 1, 4, 'The Naked Time', 1704.2), - (5, 1, 5, 'The Enemy Within', 1672.1), - (6, 1, 6, e'Mudd\'s Women', 1329.8), - (7, 1, 7, 'What Are Little Girls Made Of?', 2712.4), - (8, 1, 8, 'Miri', 2713.5), - (9, 1, 9, 'Dagger of the Mind', 2715.1), - (10, 1, 10, 'The Corbomite Maneuver', 1512.2), - ... -~~~ - -### Dump just a table's schema - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach dump startrek episodes --insecure --user=maxroach --dump-mode=schema > backup.sql -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cat backup.sql -~~~ - -~~~ -CREATE TABLE episodes ( - id INT NOT NULL, - season INT NULL, - num INT NULL, - title STRING NULL, - stardate DECIMAL NULL, - CONSTRAINT "primary" PRIMARY KEY (id), - FAMILY "primary" (id, season, num), - FAMILY fam_1_title (title), - FAMILY fam_2_stardate (stardate) -); -~~~ - -### Dump just a table's data - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach dump startrek episodes --insecure --user=maxroach --dump-mode=data > backup.sql -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cat backup.sql -~~~ - -~~~ -INSERT INTO episodes (id, season, num, title, stardate) VALUES - (1, 1, 1, 'The Man Trap', 1531.1), - (2, 1, 2, 'Charlie X', 1533.6), - (3, 1, 3, 'Where No Man Has Gone Before', 1312.4), - (4, 1, 4, 'The Naked Time', 1704.2), - (5, 1, 5, 'The Enemy Within', 1672.1), - (6, 1, 6, e'Mudd\'s Women', 1329.8), - (7, 1, 7, 'What Are Little Girls Made Of?', 2712.4), - (8, 1, 8, 'Miri', 2713.5), - (9, 1, 9, 'Dagger of the Mind', 2715.1), - (10, 1, 10, 'The Corbomite Maneuver', 1512.2), - ... -~~~ - -### Dump all tables in a database - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach dump startrek --insecure --user=maxroach > backup.sql -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cat backup.sql -~~~ - -~~~ -CREATE TABLE episodes ( - id INT NOT NULL, - season INT NULL, - num INT NULL, - title STRING NULL, - stardate DECIMAL NULL, - CONSTRAINT "primary" PRIMARY KEY (id), - FAMILY "primary" (id, season, num), - FAMILY fam_1_title (title), - FAMILY fam_2_stardate (stardate) -); - -CREATE TABLE quotes ( - quote STRING NULL, - characters STRING NULL, - stardate DECIMAL NULL, - episode INT NULL, - INDEX quotes_episode_idx (episode), - FAMILY "primary" (quote, rowid), - FAMILY fam_1_characters (characters), - FAMILY fam_2_stardate (stardate), - FAMILY fam_3_episode (episode) -); - -INSERT INTO episodes (id, season, num, title, stardate) VALUES - (1, 1, 1, 'The Man Trap', 1531.1), - (2, 1, 2, 'Charlie X', 1533.6), - (3, 1, 3, 'Where No Man Has Gone Before', 1312.4), - (4, 1, 4, 'The Naked Time', 1704.2), - (5, 1, 5, 'The Enemy Within', 1672.1), - (6, 1, 6, e'Mudd\'s Women', 1329.8), - (7, 1, 7, 'What Are Little Girls Made Of?', 2712.4), - (8, 1, 8, 'Miri', 2713.5), - (9, 1, 9, 'Dagger of the Mind', 2715.1), - (10, 1, 10, 'The Corbomite Maneuver', 1512.2), - ... - -INSERT INTO quotes (quote, characters, stardate, episode) VALUES - ('"... freedom ... is a worship word..." "It is our worship word too."', 'Cloud William and Kirk', NULL, 52), - ('"Beauty is transitory." "Beauty survives."', 'Spock and Kirk', NULL, 72), - ('"Can you imagine how life could be improved if we could do away with jealousy, greed, hate ..." "It can also be improved by eliminating love, tenderness, sentiment -- the other side of the coin"', 'Dr. Roger Corby and Kirk', 2712.4, 7), - ... -~~~ - -### Dump fails (user does not have `SELECT` privilege) - -In this example, the `dump` command fails for a user that does not have the `SELECT` privilege on the `episodes` table. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach dump startrek episodes --insecure --user=leslieroach > backup.sql -~~~ - -~~~ -Error: pq: user leslieroach has no privileges on table episodes -Failed running "dump" -~~~ - -### Restore a table from a backup file - -In this example, a user that has the `CREATE` privilege on the `startrek` database uses the [`cockroach sql`](cockroach-sql.html) command to recreate a table, based on a file created by the `dump` command. - -{% include copy-clipboard.html %} -~~~ shell -$ cat backup.sql -~~~ - -~~~ -CREATE TABLE quotes ( - quote STRING NULL, - characters STRING NULL, - stardate DECIMAL NULL, - episode INT NULL, - INDEX quotes_episode_idx (episode), - FAMILY "primary" (quote, rowid), - FAMILY fam_1_characters (characters), - FAMILY fam_2_stardate (stardate), - FAMILY fam_3_episode (episode) -); - -INSERT INTO quotes (quote, characters, stardate, episode) VALUES - ('"... freedom ... is a worship word..." "It is our worship word too."', 'Cloud William and Kirk', NULL, 52), - ('"Beauty is transitory." "Beauty survives."', 'Spock and Kirk', NULL, 72), - ('"Can you imagine how life could be improved if we could do away with jealousy, greed, hate ..." "It can also be improved by eliminating love, tenderness, sentiment -- the other side of the coin"', 'Dr. Roger Corby and Kirk', 2712.4, 7), - ... -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --database=startrek --user=maxroach < backup.sql -~~~ - -~~~ -CREATE TABLE -INSERT 100 -INSERT 100 -~~~ - -### Dump table data as of a specific time - -In this example, we assume there were several inserts into a table both before and after `2017-03-07 19:55:00`. - -First, let's use the built-in SQL client to view the table at the current time: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --execute="SELECT * FROM db1.dump_test" -~~~ - -~~~ -+--------------------+------+ -| id | name | -+--------------------+------+ -| 225594758537183233 | a | -| 225594758537248769 | b | -| 225594758537281537 | c | -| 225594758537314305 | d | -| 225594758537347073 | e | -| 225594758537379841 | f | -| 225594758537412609 | g | -| 225594758537445377 | h | -| 225594991654174721 | i | -| 225594991654240257 | j | -| 225594991654273025 | k | -| 225594991654305793 | l | -| 225594991654338561 | m | -| 225594991654371329 | n | -| 225594991654404097 | o | -| 225594991654436865 | p | -+--------------------+------+ -(16 rows) -~~~ - -Next, let's use a [time-travel query](select-clause.html#select-historical-data-time-travel) to view the contents of the table as of `2017-03-07 19:55:00`: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --execute="SELECT * FROM db1.dump_test AS OF SYSTEM TIME '2017-03-07 19:55:00'" -~~~ - -~~~ -+--------------------+------+ -| id | name | -+--------------------+------+ -| 225594758537183233 | a | -| 225594758537248769 | b | -| 225594758537281537 | c | -| 225594758537314305 | d | -| 225594758537347073 | e | -| 225594758537379841 | f | -| 225594758537412609 | g | -| 225594758537445377 | h | -+--------------------+------+ -(8 rows) -~~~ - -Finally, let's use `cockroach dump` with the `--as-of` flag set to dump the contents of the table as of `2017-03-07 19:55:00`. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach dump db1 dump_test --insecure --dump-mode=data --as-of='2017-03-07 19:55:00' -~~~ - -~~~ -INSERT INTO dump_test (id, name) VALUES - (225594758537183233, 'a'), - (225594758537248769, 'b'), - (225594758537281537, 'c'), - (225594758537314305, 'd'), - (225594758537347073, 'e'), - (225594758537379841, 'f'), - (225594758537412609, 'g'), - (225594758537445377, 'h'); -~~~ - -As you can see, the results of the dump are identical to the earlier time-travel query. - -## Known limitations - -{% include {{page.version.version}}/known-limitations/dump-table-with-no-columns.md %} - -## See also - -- [Import Data](migration-overview.html) -- [`IMPORT`](import.html) -- [Use the Built-in SQL Client](cockroach-sql.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/cockroach-gen.md b/src/current/v19.2/cockroach-gen.md deleted file mode 100644 index 86aa0d14c1d..00000000000 --- a/src/current/v19.2/cockroach-gen.md +++ /dev/null @@ -1,381 +0,0 @@ ---- -title: cockroach gen -summary: Use cockroach gen to generate command-line interface utlities, such as man pages, and example data. -toc: true -key: generate-cockroachdb-resources.html ---- - -The `cockroach gen` [command](cockroach-commands.html) can generate command-line interface (CLI) utilities ([`man` pages](https://en.wikipedia.org/wiki/Man_page) and a `bash` autocompletion script), example SQL data suitable to populate test databases, and an HAProxy configuration file for load balancing a running cluster. - -## Subcommands - -Subcommand | Usage ------------|------ -`man` | Generate man pages for CockroachDB. -`autocomplete` | Generate `bash` or `zsh` autocompletion script for CockroachDB.

      **Default:** `bash` -`example-data` | Generate example SQL datasets. You can also use the [`cockroach workload`](cockroach-workload.html) command to generate these sample datasets in a persistent cluster and the [`cockroach demo `](cockroach-demo.html) command to generate these datasets in a temporary, in-memory cluster. -`haproxy` | Generate an HAProxy config file for a running CockroachDB cluster. The node addresses included in the config are those advertised by the nodes. Make sure hostnames are resolvable and IP addresses are routable from HAProxy. - -## Synopsis - -Generate man pages: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen man -~~~ - -Generate bash autocompletion script: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen autocomplete -~~~ - -Generate example SQL data: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen example-data intro | cockroach sql -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen example-data startrek | cockroach sql -~~~ - -Generate an HAProxy config file for a running cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen haproxy -~~~ - -View help: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen --help -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen man --help -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen autocomplete --help -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen example-data --help -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen haproxy --help -~~~ - -## Flags - -The `gen` subcommands supports the following [general-use](#general) and [logging](#logging) flags. - -### General - -#### `man` - -Flag | Description ------|----------- -`--path` | The path where man pages will be generated.

      **Default:** `man/man1` under the current directory - -#### `autocomplete` - -Flag | Description ------|----------- -`--out` | The path where the autocomplete file will be generated.

      **Default:** `cockroach.bash` in the current directory - -#### `example-data` - -No flags are supported. See the [Generate Example Data](#generate-example-data) example for guidance. - -#### `haproxy` - -Flag | Description ------|------------ -`--host` | The server host and port number to connect to. This can be the address of any node in the cluster.

      **Env Variable:** `COCKROACH_HOST`
      **Default:** `localhost:26257` -`--port`
      `-p` | The server port to connect to. Note: The port number can also be specified via `--host`.

      **Env Variable:** `COCKROACH_PORT`
      **Default:** `26257` -`--insecure` | Use an insecure connection.

      **Env Variable:** `COCKROACH_INSECURE`
      **Default:** `false` -`--certs-dir` | The path to the [certificate directory](cockroach-cert.html) containing the CA and client certificates and client key.

      **Env Variable:** `COCKROACH_CERTS_DIR`
      **Default:** `${HOME}/.cockroach-certs/` -`--url` | A [connection URL](connection-parameters.html#connect-using-a-url) to use instead of the other arguments.

      **Env Variable:** `COCKROACH_URL`
      **Default:** no URL -`--out` | The path where the `haproxy.cfg` file will be generated. If an `haproxy.cfg` file already exists in the directory, it will be overwritten.

      **Default:** `haproxy.cfg` in the current directory -`--locality` | If nodes were started with [locality](cockroach-start.html#locality) details, you can use the `--locality` flag here to filter the nodes included in the HAProxy config file, specifying the explicit locality tier(s) or a regular expression to match against. This is useful in cases where you want specific instances of HAProxy to route to specific nodes. See the [Generate an HAProxy configuration file](#generate-an-haproxy-config-file) example for more details. - -### Logging - -By default, the `gen` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## Examples - -### Generate `man` pages - -Generate man pages: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen man -~~~ - -Move the man pages to the man directory: - -{% include copy-clipboard.html %} -~~~ shell -$ sudo mv man/man1/* /usr/share/man/man1 -~~~ - -Access man pages: - -{% include copy-clipboard.html %} -~~~ shell -$ man cockroach -~~~ - -### Generate a `bash` autocompletion script - -Generate bash autocompletion script: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen autocomplete -~~~ - -Add the script to your `.bashrc` and `.bash_profle`: - -{% include copy-clipboard.html %} -~~~ shell -$ printf "\n\n#cockroach bash autocomplete\nsource 'cockroach.bash'" >> ~/.bashrc -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ printf "\n\n#cockroach bash autocomplete\nsource 'cockroach.bash'" >> ~/.bash_profile -~~~ - -You can now use `tab` to autocomplete `cockroach` commands. - -### Generate example data - -{{site.data.alerts.callout_success}} -You can also use the [`cockroach workload`](cockroach-workload.html) command to generate these sample datasets in a persistent cluster and the [`cockroach demo `](cockroach-demo.html) command to generate these datasets in a temporary, in-memory cluster. -{{site.data.alerts.end}} - -To test out CockroachDB, you can generate an example `startrek` database, which contains 2 tables, `episodes` and `quotes`. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen example-data startrek | cockroach sql --insecure -~~~ - -~~~ -CREATE DATABASE -SET -DROP TABLE -DROP TABLE -CREATE TABLE -INSERT 79 -CREATE TABLE -INSERT 200 -~~~ - -Launch the built-in SQL client to view it: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM startrek; -~~~ -~~~ -+------------+ -| table_name | -+------------+ -| episodes | -| quotes | -+------------+ -(2 rows) -~~~ - -You can also generate an example `intro` database, which contains 1 table, `mytable`, with a hidden message: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen example-data intro | cockroach sql --insecure -~~~ - -~~~ -CREATE DATABASE -SET -DROP TABLE -CREATE TABLE -INSERT 1 -INSERT 1 -INSERT 1 -INSERT 1 -... -~~~ - -{% include copy-clipboard.html %} -~~~ shell -# Launch the built-in SQL client to view it: -$ cockroach sql --insecure -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM intro; -~~~ - -~~~ -+-------------+ -| table_name | -+-------------+ -| mytable | -+-------------+ -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM intro.mytable WHERE (l % 2) = 0; -~~~ - -~~~ -+----+------------------------------------------------------+ -| l | v | -+----+------------------------------------------------------+ -| 0 | !__aaawwmqmqmwwwaas,,_ .__aaawwwmqmqmwwaaa,, | -| 2 | !"VT?!"""^~~^"""??T$Wmqaa,_auqmWBT?!"""^~~^^""??YV^ | -| 4 | ! "?##mW##?"- | -| 6 | ! C O N G R A T S _am#Z??A#ma, Y | -| 8 | ! _ummY" "9#ma, A | -| 10 | ! vm#Z( )Xmms Y | -| 12 | ! .j####mmm#####mm#m##6. | -| 14 | ! W O W ! jmm###mm######m#mmm##6 | -| 16 | ! ]#me*Xm#m#mm##m#m##SX##c | -| 18 | ! dm#||+*$##m#mm#m#Svvn##m | -| 20 | ! :mmE=|+||S##m##m#1nvnnX##; A | -| 22 | ! :m#h+|+++=Xmm#m#1nvnnvdmm; M | -| 24 | ! Y $#m>+|+|||##m#1nvnnnnmm# A | -| 26 | ! O ]##z+|+|+|3#mEnnnnvnd##f Z | -| 28 | ! U D 4##c|+|+|]m#kvnvnno##P E | -| 30 | ! I 4#ma+|++]mmhvnnvq##P` ! | -| 32 | ! D I ?$#q%+|dmmmvnnm##! | -| 34 | ! T -4##wu#mm#pw##7' | -| 36 | ! -?$##m####Y' | -| 38 | ! !! "Y##Y"- | -| 40 | ! | -+----+------------------------------------------------------+ -(21 rows) -~~~ - -### Generate an HAProxy config file - -[HAProxy](http://www.haproxy.org/) is one of the most popular open-source TCP load balancers, and CockroachDB includes a built-in command for generating a configuration file that is preset to work with your running cluster. - -
      - - -

      - -
      -To generate an HAProxy config file for an entire secure cluster, run the `cockroach gen haproxy` command, specifying the location of [certificate directory](cockroach-cert.html) and the address of any instance running a CockroachDB node: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen haproxy \ ---certs-dir= \ ---host=
      -~~~ - -To limit the HAProxy config file to nodes matching specific ["localities"](cockroach-start.html#locality), use the `--localities` flag, specifying the explicit locality tier(s) or a regular expression to match against: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen haproxy \ ---certs-dir= \ ---host=
      ---locality=region=us.* -~~~ -
      - -
      -To generate an HAProxy config file for an entire insecure cluster, run the `cockroach gen haproxy` command, specifying the address of any instance running a CockroachDB node: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen haproxy \ ---insecure \ ---host=
      -~~~ - -To limit the HAProxy config file to nodes matching specific ["localities"](cockroach-start.html#locality), use the `--localities` flag, specifying the explicit locality tier(s) or a regular expression to match against: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen haproxy \ ---insecure \ ---host=
      ---locality=region=us.* -~~~ -
      - -By default, the generated configuration file is called `haproxy.cfg` and looks as follows, with the `server` addresses pre-populated correctly: - -~~~ -global - maxconn 4096 - -defaults - mode tcp - # Timeout values should be configured for your specific use. - # See: https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#4-timeout%20connect - timeout connect 10s - timeout client 1m - timeout server 1m - # TCP keep-alive on client side. Server already enables them. - option clitcpka - -listen psql - bind :26257 - mode tcp - balance roundrobin - option httpchk GET /health?ready=1 - server cockroach1 :26257 check port 8080 - server cockroach2 :26257 check port 8080 - server cockroach3 :26257 check port 8080 -~~~ - -The file is preset with the minimal [configurations](http://cbonte.github.io/haproxy-dconv/1.7/configuration.html) needed to work with your running cluster: - -Field | Description -------|------------ -`timeout connect`
      `timeout client`
      `timeout server` | Timeout values that should be suitable for most deployments. -`bind` | The port that HAProxy listens on. This is the port clients will connect to and thus needs to be allowed by your network configuration.

      This tutorial assumes HAProxy is running on a separate machine from CockroachDB nodes. If you run HAProxy on the same machine as a node (not recommended), you'll need to change this port, as `26257` is likely already being used by the CockroachDB node. -`balance` | The balancing algorithm. This is set to `roundrobin` to ensure that connections get rotated amongst nodes (connection 1 on node 1, connection 2 on node 2, etc.). Check the [HAProxy Configuration Manual](http://cbonte.github.io/haproxy-dconv/1.7/configuration.html#4-balance) for details about this and other balancing algorithms. -`option httpchk` | The HTTP endpoint that HAProxy uses to check node health. [`/health?ready=1`](monitoring-and-alerting.html#health-ready-1) ensures that HAProxy doesn't direct traffic to nodes that are live but not ready to receive requests. -`server` | For each included node, this field specifies the address the node advertises to other nodes in the cluster, i.e., the addressed pass in the [`--advertise-addr` flag](cockroach-start.html#networking) on node startup. Make sure hostnames are resolvable and IP addresses are routable from HAProxy. - -{{site.data.alerts.callout_info}} -For full details on these and other configuration settings, see the [HAProxy Configuration Manual](http://cbonte.github.io/haproxy-dconv/1.7/configuration.html). -{{site.data.alerts.end}} - -## See also - -- [Other Cockroach Commands](cockroach-commands.html) -- [Deploy CockroachDB On-Premises](deploy-cockroachdb-on-premises.html) (using HAProxy for load balancing) diff --git a/src/current/v19.2/cockroach-init.md b/src/current/v19.2/cockroach-init.md deleted file mode 100644 index 75e889676af..00000000000 --- a/src/current/v19.2/cockroach-init.md +++ /dev/null @@ -1,126 +0,0 @@ ---- -title: cockroach init -summary: Perform a one-time-only initialization of a CockroachDB cluster. -toc: true -key: initialize-a-cluster.html ---- - -This page explains the `cockroach init` [command](cockroach-commands.html), which you use to perform a one-time initialization of a new multi-node cluster. For a full walk-through of the cluster startup and initialization process, see one of the [Manual Deployment](manual-deployment.html) tutorials. - -{{site.data.alerts.callout_info}} -When starting a single-node cluster with [`cockroach start-single-node`](cockroach-start-single-node.html), you do not need to use the `cockroach init` command. -{{site.data.alerts.end}} - -## Synopsis - -Perform a one-time initialization of a cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init -~~~ - -View help: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init --help -~~~ - -## Flags - -The `cockroach init` command supports the following [client connection](#client-connection) and [logging](#logging) flags. - -### Client connection - -{% include {{ page.version.version }}/sql/connection-parameters.md %} - -See [Client Connection Parameters](connection-parameters.html) for details. - -### Logging - -By default, the `init` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## Examples - -Usage of `cockroach init` assumes that nodes have already been started with [`cockroach start`](cockroach-start.html) and are waiting to be initialized as a new cluster. For a more detailed walk-through, see one of the [Manual Deployment](manual-deployment.html) tutorials. - -### Initialize a Cluster on a Node's Machine - -
      - - -
      - -
      -1. SSH to the machine where the node has been started. - -2. Make sure the `client.root.crt` and `client.root.key` files for the `root` user are on the machine. - -3. Run the `cockroach init` command with the `--certs-dir` flag set to the directory containing the `ca.crt` file and the files for the `root` user, and with the `--host` flag set to the address of the current node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach init --certs-dir=certs --host=
      - ~~~ - - At this point, all the nodes complete startup and print helpful details to the [standard output](cockroach-start.html#standard-output), such as the CockroachDB version, the URL for the Admin UI, and the SQL URL for clients. -
      - -
      -1. SSH to the machine where the node has been started. - -2. Run the `cockroach init` command with the `--host` flag set to the address of the current node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach init --insecure --host=
      - ~~~ - - At this point, all the nodes complete startup and print helpful details to the [standard output](cockroach-start.html#standard-output), such as the CockroachDB version, the URL for the Admin UI, and the SQL URL for clients. -
      - -### Initialize a cluster from another machine - -
      - - -
      - -
      -1. [Install the `cockroach` binary](install-cockroachdb.html) on a machine separate from the node. - -2. Create a `certs` directory and copy the CA certificate and the client certificate and key for the `root` user into the directory. - -3. Run the `cockroach init` command with the `--certs-dir` flag set to the directory containing the `ca.crt` file and the files for the `root` user, and with the `--host` flag set to the address of any node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach init --certs-dir=certs --host=
      - ~~~ - - At this point, all the nodes complete startup and print helpful details to the [standard output](cockroach-start.html#standard-output), such as the CockroachDB version, the URL for the Admin UI, and the SQL URL for clients. -
      - -
      -1. [Install the `cockroach` binary](install-cockroachdb.html) on a machine separate from the node. - -2. Run the `cockroach init` command with the `--host` flag set to the address of any node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach init --insecure --host=
      - ~~~ - - At this point, all the nodes complete startup and print helpful details to the [standard output](cockroach-start.html#standard-output), such as the CockroachDB version, the URL for the Admin UI, and the SQL URL for clients. -
      - -## See also - -- [Manual Deployment](manual-deployment.html) -- [Orchestrated Deployment](orchestration.html) -- [Local Deployment](start-a-local-cluster.html) -- [`cockroach start`](cockroach-start.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/cockroach-node.md b/src/current/v19.2/cockroach-node.md deleted file mode 100644 index a58aa03d579..00000000000 --- a/src/current/v19.2/cockroach-node.md +++ /dev/null @@ -1,310 +0,0 @@ ---- -title: cockroach node -summary: To view details for each node in the cluster, use the cockroach node command with the appropriate subcommands and flags. -toc: true -key: view-node-details.html ---- - -To view details for each node in the cluster, use the `cockroach node` [command](cockroach-commands.html) with the appropriate subcommands and flags. - -The `cockroach node` command is also used in the process of decommissioning nodes for removal from the cluster. See [Decommission Nodes](remove-nodes.html) for more details. - -## Subcommands - -Subcommand | Usage ------------|------ -`ls` | List the ID of each node in the cluster, excluding those that have been decommissioned and are offline. -`status` | View the status of one or all nodes, excluding nodes that have been decommissioned and taken offline. Depending on flags used, this can include details about range/replicas, disk usage, and decommissioning progress. -`decommission` | Decommission nodes for removal from the cluster. See [Decommission Nodes](remove-nodes.html) for more details. -`recommission` | Recommission nodes that have been decommissioned. See [Recommission Nodes](remove-nodes.html#recommission-nodes) for more details. -`drain` | Drain nodes of SQL clients, [distributed SQL](architecture/sql-layer.html#distsql) queries, and range leases, and prevent ranges from rebalancing onto the node. This is normally done during [node shutdown](cockroach-quit.html), but the `drain` subcommand provides operators an option to interactively monitor, and if necessary intervene in, the draining process. - -## Synopsis - -List the IDs of active and inactive nodes: - -~~~ shell -$ cockroach node ls -~~~ - -Show status details for active and inactive nodes: - -~~~ shell -$ cockroach node status -~~~ - -Show status and range/replica details for active and inactive nodes: - -~~~ shell -$ cockroach node status --ranges -~~~ - -Show status and disk usage details for active and inactive nodes: - -~~~ shell -$ cockroach node status --stats -~~~ - -Show status and decommissioning details for active and inactive nodes: - -~~~ shell -$ cockroach node status --decommission -~~~ - -Show complete status details for active and inactive nodes: - -~~~ shell -$ cockroach node status --all -~~~ - -Show status details for a specific node: - -~~~ shell -$ cockroach node status -~~~ - -Decommission nodes: - -~~~ shell -$ cockroach node decommission -~~~ - -Recommission nodes: - -~~~ shell -$ cockroach node recommission -~~~ - -Drain nodes: - -~~~ shell -$ cockroach node drain -~~~ - -View help: - -~~~ shell -$ cockroach node --help -~~~ -~~~ shell -$ cockroach node --help -~~~ - -## Flags - -All `node` subcommands support the following [general-use](#general) and [logging](#logging) flags. - -### General - -Flag | Description ------|------------ -`--format` | How to display table rows printed to the standard output. Possible values: `tsv`, `csv`, `table`, `records`, `sql`, `html`.

      **Default:** `tsv` - -The `node ls` subcommand also supports the following general flags: - -Flag | Description ------|------------ -`--timeout` | Set the duration of time that the subcommand is allowed to run before it returns an error and prints partial information. The timeout is specified with a suffix of `s` for seconds, `m` for minutes, and `h` for hours. If this flag is not set, the subcommand may hang. - -The `node status` subcommand also supports the following general flags: - -Flag | Description ------|------------ -`--all` | Show all node details. -`--decommission` | Show node decommissioning details. -`--ranges` | Show node details for ranges and replicas. -`--stats` | Show node disk usage details. -`--timeout` | Set the duration of time that the subcommand is allowed to run before it returns an error and prints partial information. The timeout is specified with a suffix of `s` for seconds, `m` for minutes, and `h` for hours. If this flag is not set, the subcommand may hang. - -The `node decommission` subcommand also supports the following general flags: - -Flag | Description ------|------------ -`--wait` | When to return to the client. Possible values: `all`, `none`.

      If `all`, the command returns to the client only after all replicas on all specified nodes have been transferred to other nodes. If any specified nodes are offline, the command will not return to the client until those nodes are back online.

      If `none`, the command does not wait for the decommissioning process to complete; it returns to the client after starting the decommissioning process on all specified nodes that are online. Any specified nodes that are offline will automatically be marked as decommissioning; if they come back online, the cluster will recognize this status and will not rebalance data to the nodes.

      **Default:** `all` - -The `node drain` subcommand also supports the following general flag: - -Flag | Description ------|------------ -`--drain-wait` | Amount of time to wait for the node to drain before returning to the client.

      **Default:** `10m` - -### Client connection - -{% include {{ page.version.version }}/sql/connection-parameters.md %} - -See [Client Connection Parameters](connection-parameters.html) for more details. - -### Logging - -By default, the `node` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## Response - -The `cockroach node` subcommands return the following fields for each node. - -### `node ls` - -Field | Description -------|------------ -`id` | The ID of the node. - -### `node status` - -Field | Description -------|------------ -`id` | The ID of the node.

      **Required flag:** None -`address` | The address of the node.

      **Required flag:** None -`build` | The version of CockroachDB running on the node. If the binary was built from source, this will be the SHA hash of the commit used.

      **Required flag:** None -`locality` | The [locality](cockroach-start.html#locality) information specified for the node.

      **Required flag:** None -`updated_at` | The date and time when the node last recorded the information displayed in this command's output. When healthy, a new status should be recorded every 10 seconds or so, but when unhealthy this command's stats may be much older.

      **Required flag:** None -`started_at` | The date and time when the node was started.

      **Required flag:** None -`replicas_leaders` | The number of range replicas on the node that are the Raft leader for their range. See `replicas_leaseholders` below for more details.

      **Required flag:** `--ranges` or `--all` -`replicas_leaseholders` | The number of range replicas on the node that are the leaseholder for their range. A "leaseholder" replica handles all read requests for a range and directs write requests to the range's Raft leader (usually the same replica as the leaseholder).

      **Required flag:** `--ranges` or `--all` -`ranges` | The number of ranges that have replicas on the node.

      **Required flag:** `--ranges` or `--all` -`ranges_unavailable` | The number of unavailable ranges that have replicas on the node.

      **Required flag:** `--ranges` or `--all` -`ranges_underreplicated` | The number of underreplicated ranges that have replicas on the node.

      **Required flag:** `--ranges` or `--all` -`live_bytes` | The amount of live data used by both applications and the CockroachDB system. This excludes historical and deleted data.

      **Required flag:** `--stats` or `--all` -`key_bytes` | The amount of live and non-live data from keys in the key-value storage layer. This does not include data used by the CockroachDB system.

      **Required flag:** `--stats` or `--all` -`value_bytes` | The amount of live and non-live data from values in the key-value storage layer. This does not include data used by the CockroachDB system.

      **Required flag:** `--stats` or `--all` -`intent_bytes` | The amount of non-live data associated with uncommitted (or recently-committed) transactions.

      **Required flag:** `--stats` or `--all` -`system_bytes` | The amount of data used just by the CockroachDB system.

      **Required flag:** `--stats` or `--all` -`is_available` | If `true`, the node is currently available.

      **Required flag:** None -`is_live` | If `true`, the node is currently live.

      For unavailable clusters (with an unresponsive Admin UI), running the `node status` command and monitoring the `is_live` field is the only way to identify the live nodes in the cluster. However, you need to run the `node status` command on a live node to identify the other live nodes in an unavailable cluster. Figuring out a live node to run the command is a trial-and-error process, so run the command against each node until you get one that responds.

      See [Identify live nodes in an unavailable cluster](#identify-live-nodes-in-an-unavailable-cluster) for more details.

      **Required flag:** None -`gossiped_replicas` | The number of replicas on the node that are active members of a range. After the decommissioning process completes, this should be 0.

      **Required flag:** `--decommission` or `--all` -`is_decommissioning` | If `true`, the node's range replicas are being transferred to other nodes. This happens when a live node is marked for [decommissioning](remove-nodes.html).

      **Required flag:** `--decommission` or `--all` -`is_draining` | If `true`, the node is being drained of in-flight SQL connections and new SQL connections are rejected. This happens when a live node is being [stopped](cockroach-quit.html).

      **Required flag:** `--decommission` or `--all` - -### `node decommission` - -Field | Description -------|------------ -`id` | The ID of the node. -`is_live` | If `true`, the node is live. -`replicas` | The number of replicas on the node that are active members of a range. After the decommissioning process completes, this should be 0. -`is_decommissioning` | If `true`, the node's range replicas are being transferred to other nodes. This happens when a live node is marked for [decommissioning](remove-nodes.html). -`is_draining` | If `true`, the node is being drained of in-flight SQL connections and new SQL connections are rejected. This happens when a live node is being [stopped](cockroach-quit.html). - -### `node recommission` - -Field | Description -------|------------ -`id` | The ID of the node. -`is_live` | If `true`, the node is live. -`replicas` | The number of replicas on the node that are active members of a range. After the decommissioning process completes, this should be 0. -`is_decommissioning` | If `true`, the node's range replicas are being transferred to other nodes. This happens when a live node is marked for [decommissioning](remove-nodes.html). -`is_draining` | If `true`, the node is being drained of in-flight SQL connections and new SQL connections are rejected. This happens when a live node is being [stopped](cockroach-quit.html). - -## Examples - -### Setup - -To follow along with the examples, start [an insecure cluster](start-a-local-cluster.html), with [localities](cockroach-start.html#locality) defined. - -### List node IDs - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach node ls --insecure -~~~ - -~~~ - id -+----+ - 1 - 2 - 3 -(3 rows) -~~~ - -### Show the status of a single node - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach node status 1 --host=localhost:26257 --insecure -~~~ - -~~~ - id | address | sql_address | build | started_at | updated_at | locality | is_available | is_live -+----+-----------------+-----------------+-----------------------------------------+----------------------------------+---------------------------------+---------------------+--------------+---------+ - 1 | localhost:26257 | localhost:26257 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:54.308502+00:00 | 2019-10-01 20:05:43.85563+00:00 | region=us-east,az=1 | true | true -(1 row) -~~~ - -### Show the status of all nodes - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach node status --host=localhost:26257 --insecure -~~~ - -~~~ - id | address | sql_address | build | started_at | updated_at | locality | is_available | is_live -+----+-----------------+-----------------+-----------------------------------------+----------------------------------+----------------------------------+------------------------+--------------+---------+ - 1 | localhost:26257 | localhost:26257 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:54.308502+00:00 | 2019-10-01 20:06:15.356886+00:00 | region=us-east,az=1 | true | true - 2 | localhost:26258 | localhost:26258 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:54.551761+00:00 | 2019-10-01 20:06:15.583967+00:00 | region=us-central,az=2 | true | true - 3 | localhost:26259 | localhost:26259 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:55.178577+00:00 | 2019-10-01 20:06:16.204549+00:00 | region=us-west,az=3 | true | true -(3 rows) -~~~ - -### Identify live nodes in an unavailable cluster - -The `is_live` and `is_available` fields are marked as `true` as long as a majority of the nodes are up, and a quorum can be reached: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach quit --host=localhost:26258 --insecure -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach node status --host=localhost:26257 --insecure -~~~ - -~~~ - id | address | sql_address | build | started_at | updated_at | locality | is_available | is_live -+----+-----------------+-----------------+-----------------------------------------+----------------------------------+----------------------------------+------------------------+--------------+---------+ - 1 | localhost:26257 | localhost:26257 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:54.308502+00:00 | 2019-10-01 20:07:04.857339+00:00 | region=us-east,az=1 | true | true - 2 | localhost:26258 | localhost:26258 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:54.551761+00:00 | 2019-10-01 20:06:48.555863+00:00 | region=us-central,az=2 | false | false - 3 | localhost:26259 | localhost:26259 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:55.178577+00:00 | 2019-10-01 20:07:01.207697+00:00 | region=us-west,az=3 | true | true -(3 rows) -~~~ - -If a majority of nodes are down and a quorum cannot be reached, the `is_live` field is marked as `true` for the nodes that are up, but the `is_available` field is marked as `false` for all nodes: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach quit --host=localhost:26259 --insecure -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach node status --host=localhost:26257 --insecure -~~~ - -~~~ - id | address | sql_address | build | started_at | updated_at | locality | is_available | is_live -+----+-----------------+-----------------+-----------------------------------------+----------------------------------+----------------------------------+------------------------+--------------+---------+ - 1 | localhost:26257 | localhost:26257 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:54.308502+00:00 | 2019-10-01 20:07:37.464249+00:00 | region=us-east,az=1 | false | true - 2 | localhost:26258 | localhost:26258 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:54.551761+00:00 | 2019-10-01 20:07:37.464259+00:00 | region=us-central,az=2 | false | false - 3 | localhost:26259 | localhost:26259 | v19.2.0-alpha.20190606-2479-gd98e0839dc | 2019-10-01 20:04:55.178577+00:00 | 2019-10-01 20:07:37.464265+00:00 | region=us-west,az=3 | false | false -(3 rows) -~~~ - -{{site.data.alerts.callout_info}} -You need to run the `node status` command on a live node to identify the other live nodes in an unavailable cluster. Figuring out a live node to run the command is a trial-and-error process, so run the command against each node until you get one that responds. -{{site.data.alerts.end}} - -### Decommission nodes - -See [Remove Nodes](remove-nodes.html) - -### Recommission nodes - -See [Recommission Nodes](remove-nodes.html#recommission-nodes) - -## See also - -- [Other Cockroach Commands](cockroach-commands.html) -- [Remove Nodes](remove-nodes.html) diff --git a/src/current/v19.2/cockroach-quit.md b/src/current/v19.2/cockroach-quit.md deleted file mode 100644 index 503933f0d3f..00000000000 --- a/src/current/v19.2/cockroach-quit.md +++ /dev/null @@ -1,137 +0,0 @@ ---- -title: cockroach quit -summary: This page shows you how to use the cockroach quit command to temporarily stop a node that you plan to restart. -toc: true -key: stop-a-node.html ---- - -This page shows you how to use the `cockroach quit` [command](cockroach-commands.html) to temporarily stop a node that you plan to restart. - -You might do this, for example, during the process of [upgrading your cluster's version of CockroachDB](upgrade-cockroach-version.html) or to perform planned maintenance (e.g., upgrading system software). - -{{site.data.alerts.callout_info}} -In other scenarios, such as when downsizing a cluster or reacting to hardware failures, it's best to remove nodes from your cluster entirely. For information about this, see [Decommission Nodes](remove-nodes.html). -{{site.data.alerts.end}} - -## Overview - -### How it works - -When you stop a node, it performs the following steps: - -- Finishes in-flight requests. Note that this is a best effort that times out after the duration specified by the `server.shutdown.query_wait` [cluster setting](cluster-settings.html). -- Gossips its draining state to the cluster, so that other nodes do not try to distribute query planning to the draining node. Note that this is a best effort that times out after the duration specified by the `server.shutdown.drain_wait` [cluster setting](cluster-settings.html), so other nodes may not receive the gossip info in time. -- No new ranges are transferred to the draining node, to avoid a possible loss of quorum after the node shuts down. - -If the node then stays offline for a certain amount of time (5 minutes by default), the cluster considers the node dead and starts to transfer its **range replicas** to other nodes as well. - -After that, if the node comes back online, its range replicas will determine whether or not they are still valid members of replica groups. If a range replica is still valid and any data in its range has changed, it will receive updates from another replica in the group. If a range replica is no longer valid, it will be removed from the node. - -Basic terms: - -- **Range**: CockroachDB stores all user data and almost all system data in a giant sorted map of key value pairs. This keyspace is divided into "ranges", contiguous chunks of the keyspace, so that every key can always be found in a single range. -- **Range Replica:** CockroachDB replicates each range (3 times by default) and stores each replica on a different node. - -### Considerations - -{% include {{ page.version.version }}/faq/planned-maintenance.md %} - -## Synopsis - -Temporarily stop a node: - -~~~ shell -$ cockroach quit -~~~ - -View help: - -~~~ shell -$ cockroach quit --help -~~~ - -## Flags - -The `quit` command supports the following [general-use](#general), [client connection](#client-connection), and [logging](#logging) flags. - -### General - -Flag | Description ------|------------ -`--decommission` | If specified, the node will be removed from the cluster instead of temporarily stopped. See [Remove Nodes](remove-nodes.html) for more details. -`--drain-wait` | Amount of time to wait for the node to drain before stopping the node. See [`cockroach node drain`](cockroach-node.html) for more details.

      **Default:** `10m` - -### Client connection - -{% include {{ page.version.version }}/sql/connection-parameters.md %} - -See [Client Connection Parameters](connection-parameters.html) for more details. - -### Logging - -By default, the `quit` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## Examples - -### Stop a node from the machine where it's running - -1. SSH to the machine where the node is running. - -2. If the node is running in the background and you are using a process manager for automatic restarts, use the process manager to stop the `cockroach` process without restarting it. - - If the node is running in the background and you are not using a process manager, send a kill signal to the `cockroach` process, for example: - - {% include copy-clipboard.html %} - ~~~ shell - $ pkill cockroach - ~~~ - - If the node is running in the foreground, press `CTRL-C`. - -3. Verify that the `cockroach` process has stopped: - - {% include copy-clipboard.html %} - ~~~ shell - $ ps aux | grep cockroach - ~~~ - - Alternately, you can check the node's logs for the message `server drained and shutdown completed`. - -### Stop a node from another machine - -
      - - -
      - -
      -1. [Install the `cockroach` binary](install-cockroachdb.html) on a machine separate from the node. - -2. Create a `certs` directory and copy the CA certificate and the client certificate and key for the `root` user into the directory. - -3. Run the `cockroach quit` command without the `--decommission` flag: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --certs-dir=certs --host=
      - ~~~ -
      - -
      -1. [Install the `cockroach` binary](install-cockroachdb.html) on a machine separate from the node. - -2. Run the `cockroach quit` command without the `--decommission` flag: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=
      - ~~~ -
      - -## See also - -- [Other Cockroach Commands](cockroach-commands.html) -- [Decommission Nodes](remove-nodes.html) -- [Upgrade a Cluster's Version](upgrade-cockroach-version.html) diff --git a/src/current/v19.2/cockroach-sql.md b/src/current/v19.2/cockroach-sql.md deleted file mode 100644 index 770f5661815..00000000000 --- a/src/current/v19.2/cockroach-sql.md +++ /dev/null @@ -1,778 +0,0 @@ ---- -title: cockroach sql -summary: CockroachDB comes with a built-in client for executing SQL statements from an interactive shell or directly from the command line. -toc: true -key: use-the-built-in-sql-client.html ---- - -CockroachDB comes with a built-in client for executing SQL statements from an interactive shell or directly from the command line. To use this client, run the `cockroach sql` [command](cockroach-commands.html) as described below. - -To exit the interactive shell, use `\q`, `quit`, `exit`, or `ctrl-d`. - -{{site.data.alerts.callout_success}} -If you want to experiment with CockroachDB SQL but do not have a cluster already running, you can use the [`cockroach demo`](cockroach-demo.html) command to open a shell to a temporary, in-memory cluster. -{{site.data.alerts.end}} - -## Synopsis - -Start the interactive SQL shell: - -~~~ shell -$ cockroach sql -~~~ - -Execute SQL from the command line: - -~~~ shell -$ cockroach sql --execute=";" --execute="" -~~~ -~~~ shell -$ echo ";" | cockroach sql -~~~ -~~~ shell -$ cockroach sql < file-containing-statements.sql -~~~ - -Exit the interactive SQL shell: - -~~~ shell -$ \q -~~~ -~~~ shell -$ quit -~~~ -~~~ shell -$ exit -~~~ -~~~ shell -ctrl-d -~~~ - -View help: - -~~~ shell -$ cockroach sql --help -~~~ - -## Flags - -The `sql` command supports the following types of flags: - -- [General Use](#general) -- [Client Connection](#client-connection) -- [Logging](#logging) - -### General - -- To start an interactive SQL shell, run `cockroach sql` with all appropriate connection flags or use just the [`--url` flag](#sql-flag-url), which includes [connection details](connection-parameters.html#connect-using-a-url). -- To execute SQL statements from the command line, use the [`--execute` flag](#sql-flag-execute). - -Flag | Description ------|------------ -`--database`
      `-d` | A database name to use as [current database](sql-name-resolution.html#current-database) in the newly created session. -`--echo-sql` | Reveal the SQL statements sent implicitly by the command-line utility. For a demonstration, see the [example](#reveal-the-sql-statements-sent-implicitly-by-the-command-line-utility) below.

      This can also be enabled within the interactive SQL shell via the `\set echo` [shell command](#commands). - `--execute`
      `-e` | Execute SQL statements directly from the command line, without opening a shell. This flag can be set multiple times, and each instance can contain one or more statements separated by semi-colons. If an error occurs in any statement, the command exits with a non-zero status code and further statements are not executed. The results of each statement are printed to the standard output (see `--format` for formatting options).

      For a demonstration of this and other ways to execute SQL from the command line, see the [example](#execute-sql-statements-from-the-command-line) below. - `--format` | How to display table rows printed to the standard output. Possible values: `tsv`, `csv`, `table`, `raw`, `records`, `sql`, `html`.

      **Default:** `table` for sessions that [output on a terminal](#session-and-output-types); `tsv` otherwise

      This flag corresponds to the `display_format` [client-side option](#client-side-options). -`--safe-updates` | Disallow potentially unsafe SQL statements, including `DELETE` without a `WHERE` clause, `UPDATE` without a `WHERE` clause, and `ALTER TABLE ... DROP COLUMN`.

      **Default:** `true` for [interactive sessions](#session-and-output-types); `false` otherwise

      Potentially unsafe SQL statements can also be allowed/disallowed for an entire session via the `sql_safe_updates` [session variable](set-vars.html). -`--set` | Set a [client-side option](#client-side-options) before starting the SQL shell or executing SQL statements from the command line via `--execute`. This flag may be specified multiple times, once per option.

      After starting the SQL shell, the `\set` and `unset` commands can be use to enable and disable client-side options as well. -`--watch` | Repeat the SQL statements specified with `--execute` or `-e` until a SQL error occurs or the process is terminated. `--watch` applies to all `--execute` or `-e` flags in use.
      You must also specify an interval at which to repeat the statement, followed by a time unit. For example, to specify an interval of 5 seconds, use `5s`.

      Note that this flag is intended for simple monitoring scenarios during development and testing. See the [example](#repeat-a-sql-statement) below. - -### Client connection - -{% include {{ page.version.version }}/sql/connection-parameters.md %} - -See [Client Connection Parameters](connection-parameters.html) for more details. - -### Logging - -By default, the `sql` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## Session and output types - -`cockroach sql` exhibits different behaviors depending on whether or not the session is interactive and/or whether or not the session outputs on a terminal. - -- A session is **interactive** when `cockroach sql` is invoked without the `--execute` flag and input is not redirected from a file. In such cases: - - The [`errexit` option](#sql-option-errexit) defaults to `false`. - - The [`check_syntax` option](#sql-option-check-syntax) defaults to `true` if supported by the CockroachDB server (this is checked when the shell starts up). - - **Ctrl+C** at the prompt will only terminate the shell if no other input was entered on the same line already. - - The shell will attempt to set the `safe_updates` [session variable](set-vars.html) to `true` on the server. -- A session **outputs on a terminal** when output is not redirected to a file. In such cases: - - The [`--format` flag](#sql-flag-format) and its corresponding [`display_format` option](#sql-option-display-format) default to `table`. These default to `tsv` otherwise. - - The `show_times` option defaults to `true`. - -When a session is both interactive and outputs on a terminal, `cockroach sql` also activates the interactive prompt with a line editor that can be used to modify the current line of input. Also, command history becomes active. - -## SQL shell - -### Welcome message - -When the SQL shell connects (or reconnects) to a CockroachDB node, it prints a welcome text with some tips and CockroachDB version and cluster details: - -~~~ shell -# -# Welcome to the CockroachDB SQL shell. -# All statements must be terminated by a semicolon. -# To exit, type: \q. -# -# Server version: CockroachDB CCL {{page.release_info.version}} (x86_64-apple-darwin17.7.0, built 2019/09/13 00:07:19, go1.12.6) (same version as client) -# Cluster ID: 7fb9f5b4-a801-4851-92e9-c0db292d03f1 -# -# Enter \? for a brief introduction. -# -> -~~~ - -The **Version** and **Cluster ID** details are particularly noteworthy: - -- When the client and server versions of CockroachDB are the same, the shell prints the `Server version` followed by `(same version as client)`. -- When the client and server versions are different, the shell prints both the `Client version` and `Server version`. In this case, you may want to [plan an upgrade](upgrade-cockroach-version.html) of older client or server versions. -- Since every CockroachDB cluster has a unique ID, you can use the `Cluster ID` field to verify that your client is always connecting to the correct cluster. - -### Commands - -The following commands can be used within the interactive SQL shell: - -Command | Usage ---------|------------ -`\?`
      `help` | View this help within the shell. -`\q`
      `quit`
      `exit`
      `ctrl-d` | Exit the shell.

      When no text follows the prompt, `ctrl-c` exits the shell as well; otherwise, `ctrl-c` clears the line. -`\!` | Run an external command and print its results to `stdout`. See the [example](#run-external-commands-from-the-sql-shell) below. -\| | Run the output of an external command as SQL statements. See the [example](#run-external-commands-from-the-sql-shell) below. -`\set
      ` | New in v19.2: Show details about columns in the specified table. This command is equivalent to [`SHOW COLUMNS`](show-columns.html). - -### Client-side options - -- To view option descriptions and how they are currently set, use `\set` without any options. -- To enable or disable an option, use `\set
      - - - - -
      chickturtle
      🐥🐢
      -~~~ - -When piping output to another command or a file, `--format` defaults to `tsv`: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---execute="SELECT '🐥' AS chick, '🐢' AS turtle" > out.txt \ ---user=maxroach \ ---host=12.345.67.89 \ ---database=critterdb -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cat out.txt -~~~ - -~~~ -1 row -chick turtle -🐥 🐢 -~~~ - -However, you can explicitly set `--format` to another format, for example, `table`: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---format=table \ ---execute="SELECT '🐥' AS chick, '🐢' AS turtle" > out.txt \ ---user=maxroach \ ---host=12.345.67.89 \ ---database=critterdb -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cat out.txt -~~~ - -~~~ -+-------+--------+ -| chick | turtle | -+-------+--------+ -| 🐥 | 🐢 | -+-------+--------+ -(1 row) -~~~ - -### Make the output of `SHOW` statements selectable - -To make it possible to select from the output of `SHOW` statements, set `--format` to `raw`: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---format=raw \ ---user=maxroach \ ---host=12.345.67.89 \ ---database=critterdb -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE customers; -~~~ - -~~~ -# 2 columns -# row 1 -## 14 -test.customers -## 185 -CREATE TABLE customers ( - id INT NOT NULL, - email STRING NULL, - CONSTRAINT "primary" PRIMARY KEY (id ASC), - UNIQUE INDEX customers_email_key (email ASC), - FAMILY "primary" (id, email) -) -# 1 row -~~~ - -When `--format` is not set to `raw`, you can use the `display_format` [SQL shell option](#client-side-options) to change the output format within the interactive session: - -{% include copy-clipboard.html %} -~~~ sql -> \set display_format raw -~~~ - -~~~ -# 2 columns -# row 1 -## 14 -test.customers -## 185 -CREATE TABLE customers ( - id INT NOT NULL, - email STRING NULL, - CONSTRAINT "primary" PRIMARY KEY (id ASC), - UNIQUE INDEX customers_email_key (email ASC), - FAMILY "primary" (id, email) -) -# 1 row -~~~ - -### Execute SQL statements from a file - -In this example, we show and then execute the contents of a file containing SQL statements. - -{% include copy-clipboard.html %} -~~~ shell -$ cat statements.sql -~~~ - -~~~ -CREATE TABLE roaches (name STRING, country STRING); -INSERT INTO roaches VALUES ('American Cockroach', 'United States'), ('Brownbanded Cockroach', 'United States'); -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---user=maxroach \ ---host=12.345.67.89 \ ---database=critterdb \ -< statements.sql -~~~ - -~~~ -CREATE TABLE -INSERT 2 -~~~ - -### Run external commands from the SQL shell - -In this example, we use `\!` to look at the rows in a CSV file before creating a table and then using `\|` to insert those rows into the table. - -{{site.data.alerts.callout_info}}This example works only if the values in the CSV file are numbers. For values in other formats, use an online CSV-to-SQL converter or make your own import program.{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> \! cat test.csv -~~~ - -~~~ -12, 13, 14 -10, 20, 30 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE csv (x INT, y INT, z INT); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> \| IFS=","; while read a b c; do echo "insert into csv values ($a, $b, $c);"; done < test.csv; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM csv; -~~~ - -~~~ -+----+----+----+ -| x | y | z | -+----+----+----+ -| 12 | 13 | 14 | -| 10 | 20 | 30 | -+----+----+----+ -~~~ - -In this example, we create a table and then use `\|` to programmatically insert values. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE for_loop (x INT); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> \| for ((i=0;i<10;++i)); do echo "INSERT INTO for_loop VALUES ($i);"; done -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM for_loop; -~~~ - -~~~ -+---+ -| x | -+---+ -| 0 | -| 1 | -| 2 | -| 3 | -| 4 | -| 5 | -| 6 | -| 7 | -| 8 | -| 9 | -+---+ -~~~ - -### Edit SQL statements in an external editor - -In applications that use [GNU Readline](https://tiswww.case.edu/php/chet/readline/rltop.html) (such as [bash](https://www.gnu.org/software/bash/)), you can edit a long line in your preferred editor by typing `Ctrl-x Ctrl-e`. However, CockroachDB uses the BSD-licensed [libedit](https://thrysoee.dk/editline/), which does not include this functionality. - -If you would like to be able to edit the current line in an external editor by typing `C-x C-e` as in `bash`, do the following: - -1. Install the `vipe` program (from the [moreutils](https://joeyh.name/code/moreutils/) suite of tools). -2. Edit your `~/.editrc` to add the following line, which takes advantage of the SQL client's ability to [run external commands](#run-external-commands-from-the-sql-shell): - - {% include copy-clipboard.html %} - ~~~ - cockroach:bind -s ^X^E '^A^K\\\| echo \"^Y\" | vipe\r' - ~~~ - -This tells libedit to translate `C-x C-e` into the following commands: - -1. Move to the beginning of the current line. -2. Cut the whole line. -3. Paste the line into your editor via `vipe`. -4. Pass the edited file back to the SQL client when `vipe` exits. - -{{site.data.alerts.callout_info}} -Future versions of the SQL client may opt to use a different back-end for reading input, in which case please refer to this page for additional updates. -{{site.data.alerts.end}} - -### Allow potentially unsafe SQL statements - -The `--safe-updates` flag defaults to `true`. This prevents SQL statements that may have broad, undesired side-effects. For example, by default, we cannot use `DELETE` without a `WHERE` clause to delete all rows from a table: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --execute="SELECT * FROM db1.t1" -~~~ - -~~~ -+----+------+ -| id | name | -+----+------+ -| 1 | a | -| 2 | b | -| 3 | c | -| 4 | d | -| 5 | e | -| 6 | f | -| 7 | g | -| 8 | h | -| 9 | i | -| 10 | j | -+----+------+ -(10 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --execute="DELETE FROM db1.t1" -~~~ - -~~~ -Error: pq: rejected: DELETE without WHERE clause (sql_safe_updates = true) -Failed running "sql" -~~~ - -However, to allow an "unsafe" statement, you can set `--safe-updates=false`: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --safe-updates=false --execute="DELETE FROM db1.t1" -~~~ - -~~~ -DELETE 10 -~~~ - -{{site.data.alerts.callout_info}}Potentially unsafe SQL statements can also be allowed/disallowed for an entire session via the sql_safe_updates session variable.{{site.data.alerts.end}} - -### Reveal the SQL statements sent implicitly by the command-line utility - -In this example, we use the `--execute` flag to execute statements from the command line and the `--echo-sql` flag to reveal SQL statements sent implicitly: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---execute="CREATE TABLE t1 (id INT PRIMARY KEY, name STRING)" \ ---execute="INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c')" \ ---user=maxroach \ ---host=12.345.67.89 \ ---database=db1 ---echo-sql -~~~ - -~~~ -# Server version: CockroachDB CCL f8f3c9317 (darwin amd64, built 2017/09/13 15:05:35, go1.8) (same version as client) -# Cluster ID: 847a4ba5-c78a-465a-b1a0-59fae3aab520 -> SET sql_safe_updates = TRUE -> CREATE TABLE t1 (id INT PRIMARY KEY, name STRING) -CREATE TABLE -> INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c') -INSERT 3 -~~~ - -In this example, we start the interactive SQL shell and enable the `echo` shell option to reveal SQL statements sent implicitly: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---user=maxroach \ ---host=12.345.67.89 \ ---database=db1 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> \set echo -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO db1.t1 VALUES (4, 'd'), (5, 'e'), (6, 'f'); -~~~ - -~~~ -> INSERT INTO db1.t1 VALUES (4, 'd'), (5, 'e'), (6, 'f'); -INSERT 3 - -Time: 2.426534ms - -> SHOW TRANSACTION STATUS -> SHOW DATABASE -~~~ - -### Repeat a SQL statement - -Repeating SQL queries on a table can be useful for monitoring purposes. With the `--watch` flag, you can repeat the statements specified with a `--execute` or `-e` flag periodically, until a SQL error occurs or the process is terminated. - -For example, if you want to monitor the number of queries running on the current node, you can use `cockroach sql` with the `--watch` flag to query the node's `crdb_internal.node_statement_statistics` table for the query count: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure \ ---execute="SELECT SUM(count) FROM crdb_internal.node_statement_statistics" \ ---watch 1m -~~~ - -~~~ - sum -+-----+ - 926 -(1 row) - sum -+------+ - 4227 -(1 row) -^C -~~~ - -In this example, the statement is executed every minute. We let the process run for a couple minutes before stopping it with Ctrl+C. - -## See also - -- [Client Connection Parameters](connection-parameters.html) -- [`cockroach demo`](cockroach-demo.html) -- [Other Cockroach Commands](cockroach-commands.html) -- [SQL Statements](sql-statements.html) -- [Learn CockroachDB SQL](learn-cockroachdb-sql.html) diff --git a/src/current/v19.2/cockroach-sqlfmt.md b/src/current/v19.2/cockroach-sqlfmt.md deleted file mode 100644 index 005b7997c12..00000000000 --- a/src/current/v19.2/cockroach-sqlfmt.md +++ /dev/null @@ -1,158 +0,0 @@ ---- -title: cockroach sqlfmt -summary: Use cockroach sqlfmt to enhance the text layout of a SQL query. -toc: true -key: use-the-query-formatter.html ---- - -The `cockroach sqlfmt` -[command](cockroach-commands.html) changes the textual formatting of -one or more SQL queries. It recognizes all SQL extensions supported by -CockroachDB. - -A [web interface to this feature](https://sqlfum.pt/) is also available. - -{% include {{ page.version.version }}/misc/experimental-warning.md %} - -## Synopsis - -Use the query formatter interactively: - -~~~ shell -$ cockroach sqlfmt - -CTRL+D -~~~ - -Reformat a SQL query given on the command line: - -~~~ shell -$ cockroach sqlfmt -e "" -~~~ - -Reformat a SQL query already stored in a file: - -~~~ shell -$ cat query.sql | cockroach sqlfmt -~~~ - -## Flags - -The `sqlfmt` command supports the following flags. - -Flag | Description | Default value ------|------|---- -`--execute`
      `-e` | Reformat the given SQL query, without reading from standard input. | N/A -`--print-width` | Desired column width of the output. | 80 -`--tab-width` | Number of spaces occupied by a tab character on the final display device. | 4 -`--use-spaces` | Always use space characters for formatting; avoid tab characters. | Use tabs. -`--align` | Use vertical alignment during formatting. | Do not align vertically. -`--no-simplify` | Avoid removing optional grouping parentheses during formatting. | Remove unnecessary grouping parentheses. - -## Examples - -### Reformat a query with constrained column width - -Using the interactive query formatter, output with the default column width (80 columns): - -1. Start the interactive query formatter: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sqlfmt - ~~~ - -2. Press **Enter**. - -3. Run the query: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE animals (id INT PRIMARY KEY DEFAULT unique_rowid(), name STRING); - ~~~ -4. Press **CTRL+D**. - - ~~~ sql - CREATE TABLE animals ( - id INT PRIMARY KEY DEFAULT unique_rowid(), - name STRING - ) - ~~~ - -Using the command line, output with the column width set to `40`: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sqlfmt --print-width 40 -e "CREATE TABLE animals (id INT PRIMARY KEY DEFAULT unique_rowid(), name STRING);" -~~~ - -~~~ sql -CREATE TABLE animals ( - id - INT - PRIMARY KEY - DEFAULT unique_rowid(), - name STRING -) -~~~ - -### Reformat a query with vertical alignment - -Output with the default vertical alignment: - -~~~ shell -$ cockroach sqlfmt -e "SELECT winner, round(length / (60 * 5)) AS counter FROM players WHERE build = $1 AND (hero = $2 OR region = $3);" -~~~ - -~~~ sql -SELECT -winner, round(length / (60 * 5)) AS counter -FROM -players -WHERE -build = $1 AND (hero = $2 OR region = $3) -~~~ - -Output with vertical alignment: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sqlfmt --align -e "SELECT winner, round(length / (60 * 5)) AS counter FROM players WHERE build = $1 AND (hero = $2 OR region = $3);" -~~~ - -~~~ sql -SELECT winner, round(length / (60 * 5)) AS counter - FROM players - WHERE build = $1 AND (hero = $2 OR region = $3); -~~~ - -### Reformat a query with simplification of parentheses - -Output with the default simplification of parentheses: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sqlfmt -e "SELECT (1 * 2) + 3, (1 + 2) * 3;" -~~~ - -~~~ sql -SELECT 1 * 2 + 3, (1 + 2) * 3 -~~~ - -Output with no simplification of parentheses: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sqlfmt --no-simplify -e "SELECT (1 * 2) + 3, (1 + 2) * 3;" -~~~ - -~~~ sql -SELECT (1 * 2) + 3, (1 + 2) * 3 -~~~ - -## See also - -- [Sequel Fumpt](https://sqlfum.pt/) -- [`cockroach demo`](cockroach-demo.html) -- [`cockroach sql`](cockroach-sql.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/cockroach-start-single-node.md b/src/current/v19.2/cockroach-start-single-node.md deleted file mode 100644 index c89f79580a3..00000000000 --- a/src/current/v19.2/cockroach-start-single-node.md +++ /dev/null @@ -1,433 +0,0 @@ ---- -title: cockroach start-single-node -summary: The cockroach start-single-node command starts a single-node cluster with replication disabled. -toc: true ---- - -New in v19.2: This page explains the `cockroach start-single-node` [command](cockroach-commands.html), which you use to start a single-node cluster with replication disabled. A single-node cluster is all you need for quick SQL testing or app development. - -{{site.data.alerts.callout_success}} -To run a multi-node cluster with replicated data for availability and consistency, use [`cockroach start`](cockroach-start.html) and [`cockroach init`](cockroach-init.html). -{{site.data.alerts.end}} - -## Synopsis - -Start a single-node cluster: - -~~~ shell -$ cockroach start-single-node -~~~ - -View help: - -~~~ shell -$ cockroach start-single-node --help -~~~ - -## Flags - -The `cockroach start-single-node` command supports the following [general-use](#general), [networking](#networking), [security](#security), and [logging](#logging) flags. - -Many flags have useful defaults that can be overridden by specifying the flags explicitly. If you specify flags explicitly, however, be sure to do so each time the node is restarted, as they will not be remembered. - -{{site.data.alerts.callout_info}} -The `cockroach start-single-node` flags are identical to [`cockroach start`](cockroach-start.html#flags) flags. However, many of them are not relevant for single-node clusters but are provided for users who want to test concepts that appear in multi-node clusters. These flags are called out as such. In most cases, accepting most defaults is sufficient (see the [examples](#examples) below). -{{site.data.alerts.end}} - -### General - -Flag | Description ------|----------- -`--attrs` | **Not relevant for single-node clusters.** Arbitrary strings, separated by colons, specifying node capability, which might include specialized hardware or number of cores, for example:

      `--attrs=ram:64gb`

      These can be used to influence the location of data replicas. See [Configure Replication Zones](configure-replication-zones.html#replication-constraints) for full details. -`--background` | Set this to start the node in the background. This is better than appending `&` to the command because control is returned to the shell only once the node is ready to accept requests.

      **Note:** `--background` is suitable for writing automated test suites or maintenance procedures that need a temporary server process running in the background. It is not intended to be used to start a long-running server, because it does not fully detach from the controlling terminal. Consider using a service manager or a tool like [daemon(8)](https://www.freebsd.org/cgi/man.cgi?query=daemon&sektion=8) instead. -`--cache` | The total size for caches, shared evenly if there are multiple storage devices. This can be a percentage (notated as a decimal or with `%`) or any bytes-based unit, for example:

      `--cache=.25`
      `--cache=25%`
      `--cache=1000000000 ----> 1000000000 bytes`
      `--cache=1GB ----> 1000000000 bytes`
      `--cache=1GiB ----> 1073741824 bytes`

      Note: If you use the `%` notation, you might need to escape the `%` sign, for instance, while configuring CockroachDB through `systemd` service files. For this reason, it's recommended to use the decimal notation instead.

      **Default:** `128MiB`

      The default cache size is reasonable for local development clusters. For production deployments, this should be increased to 25% or higher. See [Recommended Production Settings](recommended-production-settings.html#cache-and-sql-memory-size) for more details. -`--external-io-dir` | The path of the external IO directory with which the local file access paths are prefixed while performing backup and restore operations using local node directories or NFS drives. If set to `disabled`, backups and restores using local node directories and NFS drives are disabled.

      **Default:** `extern` subdirectory of the first configured [`store`](#store).

      To set the `--external-io-dir` flag to the locations you want to use without needing to restart nodes, create symlinks to the desired locations from within the `extern` directory. -`--listening-url-file` | The file to which the node's SQL connection URL will be written on successful startup, in addition to being printed to the [standard output](#standard-output).

      This is particularly helpful in identifying the node's port when an unused port is assigned automatically (`--port=0`). -`--locality` | **Not relevant for single-node clusters.** Arbitrary key-value pairs that describe the location of the node. Locality might include country, region, datacenter, rack, etc. For more details, see [Locality](cockroach-start.html#locality) below. -`--max-disk-temp-storage` | The maximum on-disk storage capacity available to store temporary data for SQL queries that exceed the memory budget (see `--max-sql-memory`). This ensures that JOINs, sorts, and other memory-intensive SQL operations are able to spill intermediate results to disk. This can be a percentage (notated as a decimal or with `%`) or any bytes-based unit (e.g., `.25`, `25%`, `500GB`, `1TB`, `1TiB`).

      Note: If you use the `%` notation, you might need to escape the `%` sign, for instance, while configuring CockroachDB through `systemd` service files. For this reason, it's recommended to use the decimal notation instead. Also, if expressed as a percentage, this value is interpreted relative to the size of the first store. However, the temporary space usage is never counted towards any store usage; therefore, when setting this value, it's important to ensure that the size of this temporary storage plus the size of the first store doesn't exceed the capacity of the storage device.

      The temporary files are located in the path specified by the `--temp-dir` flag, or in the subdirectory of the first store (see `--store`) by default.

      **Default:** `32GiB` -`--max-sql-memory` | The maximum in-memory storage capacity available to store temporary data for SQL queries, including prepared queries and intermediate data rows during query execution. This can be a percentage (notated as a decimal or with `%`) or any bytes-based unit, for example:

      `--max-sql-memory=.25`
      `--max-sql-memory=25%`
      `--max-sql-memory=10000000000 ----> 1000000000 bytes`
      `--max-sql-memory=1GB ----> 1000000000 bytes`
      `--max-sql-memory=1GiB ----> 1073741824 bytes`

      The temporary files are located in the path specified by the `--temp-dir` flag, or in the subdirectory of the first store (see `--store`) by default.

      Note: If you use the `%` notation, you might need to escape the `%` sign, for instance, while configuring CockroachDB through `systemd` service files. For this reason, it's recommended to use the decimal notation instead.

      **Default:** `128MiB`

      The default SQL memory size is reasonable for local development clusters. For production deployments, this should be increased to 25% or higher. See [Recommended Production Settings](recommended-production-settings.html#cache-and-sql-memory-size) for more details. -`--pid-file` | The file to which the node's process ID will be written on successful startup. When this flag is not set, the process ID is not written to file. -`--store`
      `-s` | The file path to a storage device and, optionally, store attributes and maximum size. When using multiple storage devices for a node, this flag must be specified separately for each device, for example:

      `--store=/mnt/ssd01 --store=/mnt/ssd02`

      For more details, see [Store](#store) below. -`--temp-dir` | The path of the node's temporary store directory. On node start up, the location for the temporary files is printed to the standard output.

      **Default:** Subdirectory of the first [store](#store) - -### Networking - -Flag | Description ------|----------- -`--listen-addr` | The IP address/hostname and port to listen on for connections from clients. For IPv6, use the notation `[...]`, e.g., `[::1]` or `[fe80::f6f2:::]`.

      **Default:** Listen on all IP addresses on port `26257` -`--http-addr` | The IP address/hostname and port to listen on for Admin UI HTTP requests. For IPv6, use the notation `[...]`, e.g., `[::1]:8080` or `[fe80::f6f2:::]:8080`.

      **Default:** Listen on the address part of `--listen-addr` on port `8080` - -### Security - -Flag | Description ------|----------- -`--certs-dir` | The path to the [certificate directory](cockroach-cert.html). The directory must contain valid certificates if running in secure mode.

      **Default:** `${HOME}/.cockroach-certs/` -`--insecure` | Run in insecure mode. If this flag is not set, the `--certs-dir` flag must point to valid certificates.

      Note the following risks: An insecure cluster is open to any client that can access any node's IP addresses; any user, even `root`, can log in without providing a password; any user, connecting as `root`, can read or write any data in your cluster; and there is no network encryption or authentication, and thus no confidentiality.

      **Default:** `false` -`--enterprise-encryption` | This optional flag specifies the encryption options for one of the stores on the node. If multiple stores exist, the flag must be specified for each store.

      This flag takes a number of options. For a complete list of options, and usage instructions, see [Encryption at Rest](encryption.html).

      Note that this is an [enterprise feature](enterprise-licensing.html). - -### Store - -The `--store` flag supports the following fields. Note that commas are used to separate fields, and so are forbidden in all field values. - -{{site.data.alerts.callout_info}} -In-memory storage is not suitable for production deployments at this time. -{{site.data.alerts.end}} - -Field | Description -------|------------ -`type` | For in-memory storage, set this field to `mem`; otherwise, leave this field out. The `path` field must not be set when `type=mem`. -`path` | The file path to the storage device. When not setting `attr` or `size`, the `path` field label can be left out:

      `--store=/mnt/ssd01`

      When either of those fields are set, however, the `path` field label must be used:

      `--store=path=/mnt/ssd01,size=20GB`

      **Default:** `cockroach-data` -`attrs` | Arbitrary strings, separated by colons, specifying disk type or capability. These can be used to influence the location of data replicas. See [Configure Replication Zones](configure-replication-zones.html#replication-constraints) for full details.

      In most cases, node-level `--locality` or `--attrs` are preferable to store-level attributes, but this field can be used to match capabilities for storage of individual databases or tables. For example, an OLTP database would probably want to allocate space for its tables only on solid state devices, whereas append-only time series might prefer cheaper spinning drives. Typical attributes include whether the store is flash (`ssd`) or spinny disk (`hdd`), as well as speeds and other specs, for example:

      `--store=path=/mnt/hda1,attrs=hdd:7200rpm` -`size` | The maximum size allocated to the node. When this size is reached, CockroachDB attempts to rebalance data to other nodes with available capacity. When there's no capacity elsewhere, this limit will be exceeded. Also, data may be written to the node faster than the cluster can rebalance it away; in this case, as long as capacity is available elsewhere, CockroachDB will gradually rebalance data down to the store limit.

      The `size` can be specified either in a bytes-based unit or as a percentage of hard drive space (notated as a decimal or with `%`), for example:

      `--store=path=/mnt/ssd01,size=10000000000 ----> 10000000000 bytes`
      `--store=path=/mnt/ssd01,size=20GB ----> 20000000000 bytes`
      `--store=path=/mnt/ssd01,size=20GiB ----> 21474836480 bytes`
      `--store=path=/mnt/ssd01,size=0.02TiB ----> 21474836480 bytes`
      `--store=path=/mnt/ssd01,size=20% ----> 20% of available space`
      `--store=path=/mnt/ssd01,size=0.2 ----> 20% of available space`
      `--store=path=/mnt/ssd01,size=.2 ----> 20% of available space`

      **Default:** 100%

      For an in-memory store, the `size` field is required and must be set to the true maximum bytes or percentage of available memory, for example:

      `--store=type=mem,size=20GB`
      `--store=type=mem,size=90%`

      Note: If you use the `%` notation, you might need to escape the `%` sign, for instance, while configuring CockroachDB through `systemd` service files. For this reason, it's recommended to use the decimal notation instead. - -### Logging - -By default, `cockroach start-single-node` writes all messages to log files, and prints nothing to `stderr`. However, you can control the process's [logging](debug-and-error-logs.html) behavior with the following flags: - -{% include {{ page.version.version }}/misc/logging-flags.md %} - -#### Defaults - -`cockroach start-single-node` uses the equivalent values for these logging flags by default: - -- `--log-dir=/logs` -- `--logtostderr=NONE` - -This means, by default, CockroachDB writes all messages to log files, and never prints to `stderr`. - -## Standard output - -When you run `cockroach start-single-node`, some helpful details are printed to the standard output: - -~~~ shell -CockroachDB node starting at {{ now | date: "%Y-%m-%d %H:%M:%S.%6 +0000 UTC" }} -build: CCL {{page.release_info.version}} @ {{page.release_info.build_time}} (go1.12.6) -webui: http://localhost:8080 -sql: postgresql://root@localhost:26257?sslmode=disable -RPC client flags: cockroach --host=localhost:26257 --insecure -logs: /Users//node1/logs -temp dir: /Users//node1/cockroach-temp242232154 -external I/O path: /Users//node1/extern -store[0]: path=/Users//node1 -status: initialized new cluster -clusterID: 8a681a16-9623-4fc1-a537-77e9255daafd -nodeID: 1 -~~~ - -{{site.data.alerts.callout_success}} -These details are also written to the `INFO` log in the `/logs` directory. You can retrieve them with a command like `grep 'node starting' node1/logs/cockroach.log -A 11`. -{{site.data.alerts.end}} - -Field | Description -------|------------ -`build` | The version of CockroachDB you are running. -`webui` | The URL for accessing the Admin UI. -`sql` | The connection URL for your client. -`RPC client flags` | The flags to use when connecting to the node via [`cockroach` client commands](cockroach-commands.html). -`logs` | The directory containing debug log data. -`temp dir` | The temporary store directory of the node. -`external I/O path` | The external IO directory with which the local file access paths are prefixed while performing [backup](backup.html) and [restore](restore.html) operations using local node directories or NFS drives. -`attrs` | If node-level attributes were specified in the `--attrs` flag, they are listed in this field. These details are potentially useful for [configuring replication zones](configure-replication-zones.html). -`locality` | If values describing the locality of the node were specified in the `--locality` field, they are listed in this field. These details are potentially useful for [configuring replication zones](configure-replication-zones.html). -`store[n]` | The directory containing store data, where `[n]` is the index of the store, e.g., `store[0]` for the first store, `store[1]` for the second store.

      If store-level attributes were specified in the `attrs` field of the [`--store`](#store) flag, they are listed in this field as well. These details are potentially useful for [configuring replication zones](configure-replication-zones.html). -`status` | Whether the node is the first in the cluster (`initialized new cluster`), joined an existing cluster for the first time (`initialized new node, joined pre-existing cluster`), or rejoined an existing cluster (`restarted pre-existing node`). -`clusterID` | The ID of the cluster.

      When trying to join a node to an existing cluster, if this ID is different than the ID of the existing cluster, the node has started a new cluster. This may be due to conflicting information in the node's data directory. For additional guidance, see the [troubleshooting](common-errors.html#node-belongs-to-cluster-cluster-id-but-is-attempting-to-connect-to-a-gossip-network-for-cluster-another-cluster-id) docs. -`nodeID` | The ID of the node. - -## Examples - -### Start a single-node cluster - -
      - - -
      - -
      -1. Create two directories for certificates: - - {% include copy-clipboard.html %} - ~~~ shell - $ mkdir certs my-safe-directory - ~~~ - - Directory | Description - ----------|------------ - `certs` | You'll generate your CA certificate and all node and client certificates and keys in this directory. - `my-safe-directory` | You'll generate your CA key in this directory and then reference the key when generating node and client certificates. - -2. Create the CA (Certificate Authority) certificate and key pair: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-ca \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - -3. Create the certificate and key pair for the node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-node \ - localhost \ - $(hostname) \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - -4. Create a client certificate and key pair for the `root` user: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-client \ - root \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - -5. Start the single-node cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start-single-node \ - --certs-dir=certs \ - --listen-addr=localhost:26257 \ - --http-addr=localhost:8080 \ - --background - ~~~ -
      - -
      -

      -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start-single-node \ ---insecure \ ---listen-addr=localhost:26257 \ ---http-addr=localhost:8080 \ ---background -~~~ -
      - -### Scale to multiple nodes - -Scaling a cluster started with `cockroach start-single-node` involves restarting the first node with the `cockroach start` command instead, and then adding new nodes with that command as well, all using a `--join` flag that forms them into a single multi-node cluster. Since replication is disabled in clusters started with `start-single-node`, you also need to enable replication to get CockroachDB's availability and consistency guarantees. - -
      - - -
      - -
      -1. Stop the single-node cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit \ - --certs-dir=certs \ - --host=localhost:26257 - ~~~ - -2. Restart the node with the [`cockroach start`](cockroach-start.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --certs-dir=certs \ - --listen-addr=localhost:26257 \ - --http-addr=localhost:8080 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - The new flag to note is `--join`, which specifies the addresses and ports of the nodes that will comprise your cluster. You'll use this exact `--join` flag when starting other nodes as well. - - {% include {{ page.version.version }}/prod-deployment/join-flag-single-region.md %} - -3. Add two more nodes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --certs-dir=certs \ - --store=node2 \ - --listen-addr=localhost:26258 \ - --http-addr=localhost:8081 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --certs-dir=certs \ - --store=node3 \ - --listen-addr=localhost:26259 \ - --http-addr=localhost:8082 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - These commands are the same as before but with unique `--store`, `--listen-addr`, and `--http-addr` flags, since this all nodes are running on the same machine. Also, since all nodes use the same hostname (`localhost`), you can use the first node's certificate. Note that this is different than running a production cluster, where you would need to generate a certificate and key for each node, issued to all common names and IP addresses you might use to refer to the node as well as to any load balancer instances. - -4. Open the [built-in SQL shell](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --certs-dir=certs --host=localhost:26257 - ~~~ - -5. Update preconfigured [replication zones](configure-replication-zones.html) to replicate user data 3 times and import internal data 5 times: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE default CONFIGURE ZONE USING num_replicas = 3; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE system CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER database system CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE liveness CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE meta CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE system.public.jobs CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE system.public.replication_constraint_stats CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE system.public.replication_stats CONFIGURE ZONE USING num_replicas = 5; - ~~~ -
      - -
      -1. Stop the single-node cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit \ - --insecure \ - --host=localhost:26257 - ~~~ - -2. Restart the node with the [`cockroach start`](cockroach-start.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --listen-addr=localhost:26257 \ - --http-addr=localhost:8080 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - The new flag to note is `--join`, which specifies the addresses and ports of the nodes that will comprise your cluster. You'll use this exact `--join` flag when starting other nodes as well. - -3. Add two more nodes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=node2 \ - --listen-addr=localhost:26258 \ - --http-addr=localhost:8081 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=node3 \ - --listen-addr=localhost:26259 \ - --http-addr=localhost:8082 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - These commands are the same as before but with unique `--store`, `--listen-addr`, and `--http-addr` flags, since this all nodes are running on the same machine. - -4. Open the [built-in SQL shell](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure --host=localhost:26257 - ~~~ - -5. Update preconfigured [replication zones](configure-replication-zones.html) to replicate user data 3 times and import internal data 5 times: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE default CONFIGURE ZONE USING num_replicas = 3; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE system CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER database system CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE liveness CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE meta CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE system.public.jobs CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE system.public.replication_constraint_stats CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE system.public.replication_stats CONFIGURE ZONE USING num_replicas = 5; - ~~~ -
      - -## See also - -- Running a local multi-node cluster: - - [From Binary](start-a-local-cluster.html) - - [In Kubernetes](orchestrate-a-local-cluster-with-kubernetes.html) - - [In Docker](start-a-local-cluster-in-docker-mac.html) -- Running a distributed multi-node cluster: - - [From Binary](manual-deployment.html) - - [In Kubernetes](orchestrate-cockroachdb-with-kubernetes.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/cockroach-start.md b/src/current/v19.2/cockroach-start.md deleted file mode 100644 index 64f61f1ba3f..00000000000 --- a/src/current/v19.2/cockroach-start.md +++ /dev/null @@ -1,476 +0,0 @@ ---- -title: cockroach start -summary: Start a new multi-node cluster or add nodes to an existing multi-node cluster. -toc: true -key: start-a-node.html ---- - -This page explains the `cockroach start` [command](cockroach-commands.html), which you use to start a new multi-node cluster or add nodes to an existing cluster. - -{{site.data.alerts.callout_success}} -If you need a simple single-node backend for app development, use [`cockroach start-single-node`](cockroach-start-single-node.html) instead. For quick SQL testing, consider using [`cockroach demo`](cockroach-demo.html) to start a temporary, in-memory cluster with immediate access to an interactive SQL shell. -{{site.data.alerts.end}} - -{{site.data.alerts.callout_info}} -Node-level settings are defined by [flags](#flags) passed to the `cockroach start` command and cannot be changed without stopping and restarting the node. In contrast, some cluster-wide settings are defined via SQL statements and can be updated anytime after a cluster has been started. For more details, see [Cluster Settings](cluster-settings.html). -{{site.data.alerts.end}} - -## Synopsis - -Start a node to be part of a new multi-node cluster: - -~~~ shell -$ cockroach start -~~~ - -Initialize a new multi-node cluster: - -~~~ shell -$ cockroach init -~~~ - -Add a node to an existing cluster: - -~~~ shell -$ cockroach start -~~~ - -View help: - -~~~ shell -$ cockroach start --help -~~~ - -## Flags - -The `cockroach start` command supports the following [general-use](#general), [networking](#networking), [security](#security), and [logging](#logging) flags. - -Many flags have useful defaults that can be overridden by specifying the flags explicitly. If you specify flags explicitly, however, be sure to do so each time the node is restarted, as they will not be remembered. The one exception is the `--join` flag, which is stored in a node's data directory. We still recommend specifying the `--join` flag every time, as this will allow nodes to rejoin the cluster even if their data directory was destroyed. - -### General - -Flag | Description ------|----------- -`--attrs` | Arbitrary strings, separated by colons, specifying node capability, which might include specialized hardware or number of cores, for example:

      `--attrs=ram:64gb`

      These can be used to influence the location of data replicas. See [Configure Replication Zones](configure-replication-zones.html#replication-constraints) for full details. -`--background` | Set this to start the node in the background. This is better than appending `&` to the command because control is returned to the shell only once the node is ready to accept requests.

      **Note:** `--background` is suitable for writing automated test suites or maintenance procedures that need a temporary server process running in the background. It is not intended to be used to start a long-running server, because it does not fully detach from the controlling terminal. Consider using a service manager or a tool like [daemon(8)](https://www.freebsd.org/cgi/man.cgi?query=daemon&sektion=8) instead. -`--cache` | The total size for caches, shared evenly if there are multiple storage devices. This can be a percentage (notated as a decimal or with `%`) or any bytes-based unit, for example:

      `--cache=.25`
      `--cache=25%`
      `--cache=1000000000 ----> 1000000000 bytes`
      `--cache=1GB ----> 1000000000 bytes`
      `--cache=1GiB ----> 1073741824 bytes`

      Note: If you use the `%` notation, you might need to escape the `%` sign, for instance, while configuring CockroachDB through `systemd` service files. For this reason, it's recommended to use the decimal notation instead.

      **Default:** `128MiB`

      The default cache size is reasonable for local development clusters. For production deployments, this should be increased to 25% or higher. See [Recommended Production Settings](recommended-production-settings.html#cache-and-sql-memory-size) for more details. -`--external-io-dir` | The path of the external IO directory with which the local file access paths are prefixed while performing backup and restore operations using local node directories or NFS drives. If set to `disabled`, backups and restores using local node directories and NFS drives are disabled.

      **Default:** `extern` subdirectory of the first configured [`store`](#store).

      To set the `--external-io-dir` flag to the locations you want to use without needing to restart nodes, create symlinks to the desired locations from within the `extern` directory. -`--listening-url-file` | The file to which the node's SQL connection URL will be written as soon as the node is ready to accept connections, in addition to being printed to the [standard output](#standard-output). When `--background` is used, this happens before the process detaches from the terminal.

      This is particularly helpful in identifying the node's port when an unused port is assigned automatically (`--port=0`). -`--locality` | Arbitrary key-value pairs that describe the location of the node. Locality might include country, region, datacenter, rack, etc. For more details, see [Locality](#locality) below. -`--max-disk-temp-storage` | The maximum on-disk storage capacity available to store temporary data for SQL queries that exceed the memory budget (see `--max-sql-memory`). This ensures that JOINs, sorts, and other memory-intensive SQL operations are able to spill intermediate results to disk. This can be a percentage (notated as a decimal or with `%`) or any bytes-based unit (e.g., `.25`, `25%`, `500GB`, `1TB`, `1TiB`).

      Note: If you use the `%` notation, you might need to escape the `%` sign, for instance, while configuring CockroachDB through `systemd` service files. For this reason, it's recommended to use the decimal notation instead. Also, if expressed as a percentage, this value is interpreted relative to the size of the first store. However, the temporary space usage is never counted towards any store usage; therefore, when setting this value, it's important to ensure that the size of this temporary storage plus the size of the first store doesn't exceed the capacity of the storage device.

      The temporary files are located in the path specified by the `--temp-dir` flag, or in the subdirectory of the first store (see `--store`) by default.

      **Default:** `32GiB` -`--max-offset` | The maximum allowed clock offset for the cluster. If observed clock offsets exceed this limit, servers will crash to minimize the likelihood of reading inconsistent data. Increasing this value will increase the time to recovery of failures as well as the frequency of uncertainty-based read restarts.

      Note that this value must be the same on all nodes in the cluster and cannot be changed with a [rolling upgrade](upgrade-cockroach-version.html). In order to change it, first stop every node in the cluster. Then once the entire cluster is offline, restart each node with the new value.

      **Default:** `500ms` -`--max-sql-memory` | The maximum in-memory storage capacity available to store temporary data for SQL queries, including prepared queries and intermediate data rows during query execution. This can be a percentage (notated as a decimal or with `%`) or any bytes-based unit, for example:

      `--max-sql-memory=.25`
      `--max-sql-memory=25%`
      `--max-sql-memory=10000000000 ----> 1000000000 bytes`
      `--max-sql-memory=1GB ----> 1000000000 bytes`
      `--max-sql-memory=1GiB ----> 1073741824 bytes`

      The temporary files are located in the path specified by the `--temp-dir` flag, or in the subdirectory of the first store (see `--store`) by default.

      Note: If you use the `%` notation, you might need to escape the `%` sign, for instance, while configuring CockroachDB through `systemd` service files. For this reason, it's recommended to use the decimal notation instead.

      **Default:** `25%`

      The default SQL memory size can be adjusted for production deployments. See [Recommended Production Settings](recommended-production-settings.html#cache-and-sql-memory-size) for more details. -`--pid-file` | The file to which the node's process ID will be written as soon as the node is ready to accept connections. When `--background` is used, this happens before the process detaches from the terminal. When this flag is not set, the process ID is not written to file. -`--store`
      `-s` | The file path to a storage device and, optionally, store attributes and maximum size. When using multiple storage devices for a node, this flag must be specified separately for each device, for example:

      `--store=/mnt/ssd01 --store=/mnt/ssd02`

      For more details, see [Store](#store) below. -`--temp-dir` | The path of the node's temporary store directory. On node start up, the location for the temporary files is printed to the standard output.

      **Default:** Subdirectory of the first [store](#store) - -### Networking - -Flag | Description ------|----------- -`--advertise-addr` | The IP address/hostname and port to tell other nodes to use. If using a hostname, it must be resolvable from all nodes. If using an IP address, it must be routable from all nodes; for IPv6, use the notation `[...]`, e.g., `[::1]` or `[fe80::f6f2:::]`.

      This flag's effect depends on how it is used in combination with `--listen-addr`. For example, if the port number is different than the one used in `--listen-addr`, port forwarding is required. For more details, see [Networking](recommended-production-settings.html#networking).

      **Default:** The value of `--listen-addr`; if `--listen-addr` is not specified, advertises the node's canonical hostname and port `26257` -`--listen-addr` | The IP address/hostname and port to listen on for connections from other nodes and clients. For IPv6, use the notation `[...]`, e.g., `[::1]` or `[fe80::f6f2:::]`.

      This flag's effect depends on how it is used in combination with `--advertise-addr`. For example, the node will also advertise itself to other nodes using this value if `--advertise-addr` is not specified. For more details, see [Networking](recommended-production-settings.html#networking).

      **Default:** Listen on all IP addresses on port `26257`; if `--advertise-addr` is not specified, also advertise the node's canonical hostname to other nodes -`--http-addr` | The IP address/hostname and port to listen on for Admin UI HTTP requests. For IPv6, use the notation `[...]`, e.g., `[::1]:8080` or `[fe80::f6f2:::]:8080`.

      **Default:** Listen on the address part of `--listen-addr` on port `8080` -`--locality-advertise-addr` | The IP address/hostname and port to tell other nodes in specific localities to use. This flag is useful when running a cluster across multiple networks, where nodes in a given network have access to a private or local interface while nodes outside the network do not. In this case, you can use `--locality-advertise-addr` to tell nodes within the same network to prefer the private or local address to improve performance and use `--advertise-addr` to tell nodes outside the network to use another address that is reachable from them.

      This flag relies on nodes being started with the [`--locality`](#locality) flag and uses the `locality@address` notation, for example:

      `--locality-advertise-addr=region=us-west@10.0.0.0:26257`

      See the [example](#start-a-multi-node-cluster-across-private-networks) below for more details. -`--join`
      `-j` | The host addresses that connect nodes to the cluster and distribute the rest of the node addresses. These can be IP addresses or DNS aliases of nodes.

      When starting a cluster in a single region, specify the addresses of 3-5 initial nodes. When starting a cluster in multiple regions, specify more than 1 address per region, and select nodes that are spread across failure domains. Then run the [`cockroach init`](cockroach-init.html) command against any of these nodes to complete cluster startup. See the [example](#start-a-multi-node-cluster) below for more details.

      Use the same `--join` list for all nodes to ensure that the cluster can stabilize. Do not list every node in the cluster, because this increases the time for a new cluster to stabilize. Note that these are best practices; it is not required to restart an existing node to update its `--join` flag.

      Changed in v19.2: Running `cockroach start` without the `--join` flag has been deprecated. To start a single-node cluster, use `cockroach start-single-node` instead. -`--advertise-host` | **Deprecated.** Use `--advertise-addr` instead. -`--host` | **Deprecated.** Use `--listen-addr` instead. -`--port`
      `-p` | **Deprecated.** Specify port in `--advertise-addr` and/or `--listen-addr` instead. -`--http-host` | **Deprecated.** Use `--http-addr` instead. -`--http-port` | **Deprecated.** Specify port in `--http-addr` instead. - -### Security - -Flag | Description ------|----------- -`--certs-dir` | The path to the [certificate directory](cockroach-cert.html). The directory must contain valid certificates if running in secure mode.

      **Default:** `${HOME}/.cockroach-certs/` -`--insecure` | Run in insecure mode. If this flag is not set, the `--certs-dir` flag must point to valid certificates.

      Note the following risks: An insecure cluster is open to any client that can access any node's IP addresses; any user, even `root`, can log in without providing a password; any user, connecting as `root`, can read or write any data in your cluster; and there is no network encryption or authentication, and thus no confidentiality.

      **Default:** `false` -`--enterprise-encryption` | This optional flag specifies the encryption options for one of the stores on the node. If multiple stores exist, the flag must be specified for each store.

      This flag takes a number of options. For a complete list of options, and usage instructions, see [Encryption at Rest](encryption.html).

      Note that this is an [enterprise feature](enterprise-licensing.html). - -### Locality - -The `--locality` flag accepts arbitrary key-value pairs that describe the location of the node. Locality might include region, country, datacenter, rack, etc. The key-value pairs should be ordered into _locality tiers_ from most inclusive to least inclusive (e.g., region before datacenter as in `region=eu,dc=paris`), and the keys and order of key-value pairs must be the same on all nodes. It's typically better to include more pairs than fewer. - -- CockroachDB spreads the replicas of each piece of data across as diverse a set of localities as possible, with the order determining the priority. Locality can also be used to influence the location of data replicas in various ways using [replication zones](configure-replication-zones.html#replication-constraints). - -- When there is high latency between nodes (e.g., cross-datacenter deployments), CockroachDB uses locality to move range leases closer to the current workload, reducing network round trips and improving read performance, also known as ["follow-the-workload"](demo-follow-the-workload.html). In a deployment across more than 3 datacenters, however, to ensure that all data benefits from "follow-the-workload", you must increase your replication factor to match the total number of datacenters. - -- Locality is also a prerequisite for using the [table partitioning](partitioning.html) and [**Node Map**](enable-node-map.html) enterprise features. - -#### Example - -~~~ shell -# Locality flag for nodes in US East datacenter: ---locality=region=us,datacenter=us-east - -# Locality flag for nodes in US Central datacenter: ---locality=region=us,datacenter=us-central - -# Locality flag for nodes in US West datacenter: ---locality=region=us,datacenter=us-west -~~~ - -### Store - -The `--store` flag supports the following fields. Note that commas are used to separate fields, and so are forbidden in all field values. - -{{site.data.alerts.callout_info}} -In-memory storage is not suitable for production deployments at this time. -{{site.data.alerts.end}} - -{% include {{ page.version.version }}/misc/multi-store-nodes.md %} - -Field | Description -------|------------ -`type` | For in-memory storage, set this field to `mem`; otherwise, leave this field out. The `path` field must not be set when `type=mem`. -`path` | The file path to the storage device. When not setting `attr` or `size`, the `path` field label can be left out:

      `--store=/mnt/ssd01`

      When either of those fields are set, however, the `path` field label must be used:

      `--store=path=/mnt/ssd01,size=20GB`

      **Default:** `cockroach-data` -`attrs` | Arbitrary strings, separated by colons, specifying disk type or capability. These can be used to influence the location of data replicas. See [Configure Replication Zones](configure-replication-zones.html#replication-constraints) for full details.

      In most cases, node-level `--locality` or `--attrs` are preferable to store-level attributes, but this field can be used to match capabilities for storage of individual databases or tables. For example, an OLTP database would probably want to allocate space for its tables only on solid state devices, whereas append-only time series might prefer cheaper spinning drives. Typical attributes include whether the store is flash (`ssd`) or spinny disk (`hdd`), as well as speeds and other specs, for example:

      `--store=path=/mnt/hda1,attrs=hdd:7200rpm` -`size` | The maximum size allocated to the node. When this size is reached, CockroachDB attempts to rebalance data to other nodes with available capacity. When there's no capacity elsewhere, this limit will be exceeded. Also, data may be written to the node faster than the cluster can rebalance it away; in this case, as long as capacity is available elsewhere, CockroachDB will gradually rebalance data down to the store limit.

      The `size` can be specified either in a bytes-based unit or as a percentage of hard drive space (notated as a decimal or with `%`), for example:

      `--store=path=/mnt/ssd01,size=10000000000 ----> 10000000000 bytes`
      `--store=path=/mnt/ssd01,size=20GB ----> 20000000000 bytes`
      `--store=path=/mnt/ssd01,size=20GiB ----> 21474836480 bytes`
      `--store=path=/mnt/ssd01,size=0.02TiB ----> 21474836480 bytes`
      `--store=path=/mnt/ssd01,size=20% ----> 20% of available space`
      `--store=path=/mnt/ssd01,size=0.2 ----> 20% of available space`
      `--store=path=/mnt/ssd01,size=.2 ----> 20% of available space`

      **Default:** 100%

      For an in-memory store, the `size` field is required and must be set to the true maximum bytes or percentage of available memory, for example:

      `--store=type=mem,size=20GB`
      `--store=type=mem,size=90%`

      Note: If you use the `%` notation, you might need to escape the `%` sign, for instance, while configuring CockroachDB through `systemd` service files. For this reason, it's recommended to use the decimal notation instead. - -### Logging - -By default, `cockroach start` writes all messages to log files, and prints nothing to `stderr`. However, you can control the process's [logging](debug-and-error-logs.html) behavior with the following flags: - -{% include {{ page.version.version }}/misc/logging-flags.md %} - -#### Defaults - -`cockroach start` uses the equivalent values for these logging flags by default: - -- `--log-dir=/logs` -- `--logtostderr=NONE` - -This means, by default, CockroachDB writes all messages to log files, and never prints to `stderr`. - -## Standard output - -When you run `cockroach start`, some helpful details are printed to the standard output: - -~~~ shell -CockroachDB node starting at {{ now | date: "%Y-%m-%d %H:%M:%S.%6 +0000 UTC" }} -build: CCL {{page.release_info.version}} @ {{page.release_info.build_time}} (go1.12.6) -webui: http://localhost:8080 -sql: postgresql://root@localhost:26257?sslmode=disable -RPC client flags: cockroach --host=localhost:26257 --insecure -logs: /Users//node1/logs -temp dir: /Users//node1/cockroach-temp242232154 -external I/O path: /Users//node1/extern -store[0]: path=/Users//node1 -status: initialized new cluster -clusterID: 8a681a16-9623-4fc1-a537-77e9255daafd -nodeID: 1 -~~~ - -{{site.data.alerts.callout_success}} -These details are also written to the `INFO` log in the `/logs` directory. You can retrieve them with a command like `grep 'node starting' node1/logs/cockroach.log -A 11`. -{{site.data.alerts.end}} - -Field | Description -------|------------ -`build` | The version of CockroachDB you are running. -`webui` | The URL for accessing the Admin UI. -`sql` | The connection URL for your client. -`RPC client flags` | The flags to use when connecting to the node via [`cockroach` client commands](cockroach-commands.html). -`logs` | The directory containing debug log data. -`temp dir` | The temporary store directory of the node. -`external I/O path` | The external IO directory with which the local file access paths are prefixed while performing [backup](backup.html) and [restore](restore.html) operations using local node directories or NFS drives. -`attrs` | If node-level attributes were specified in the `--attrs` flag, they are listed in this field. These details are potentially useful for [configuring replication zones](configure-replication-zones.html). -`locality` | If values describing the locality of the node were specified in the `--locality` field, they are listed in this field. These details are potentially useful for [configuring replication zones](configure-replication-zones.html). -`store[n]` | The directory containing store data, where `[n]` is the index of the store, e.g., `store[0]` for the first store, `store[1]` for the second store.

      If store-level attributes were specified in the `attrs` field of the [`--store`](#store) flag, they are listed in this field as well. These details are potentially useful for [configuring replication zones](configure-replication-zones.html). -`status` | Whether the node is the first in the cluster (`initialized new cluster`), joined an existing cluster for the first time (`initialized new node, joined pre-existing cluster`), or rejoined an existing cluster (`restarted pre-existing node`). -`clusterID` | The ID of the cluster.

      When trying to join a node to an existing cluster, if this ID is different than the ID of the existing cluster, the node has started a new cluster. This may be due to conflicting information in the node's data directory. For additional guidance, see the [troubleshooting](common-errors.html#node-belongs-to-cluster-cluster-id-but-is-attempting-to-connect-to-a-gossip-network-for-cluster-another-cluster-id) docs. -`nodeID` | The ID of the node. - -## Known limitations - -{% include {{ page.version.version }}/known-limitations/adding-stores-to-node.md %} - -## Examples - -### Start a multi-node cluster - -
      - - -
      - -To start a multi-node cluster, run the `cockroach start` command for each node, setting the `--join` flag to the addresses of the initial nodes. - -{% include {{ page.version.version }}/prod-deployment/join-flag-single-region.md %} - -{{site.data.alerts.callout_info}} -{% include {{ page.version.version }}/prod-deployment/join-flag-multi-region.md %} -{{site.data.alerts.end}} - -
      -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---certs-dir=certs \ ---advertise-addr= \ ---join=,, \ ---cache=.25 \ ---max-sql-memory=.25 -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---certs-dir=certs \ ---advertise-addr= \ ---join=,, \ ---cache=.25 \ ---max-sql-memory=.25 -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---certs-dir=certs \ ---advertise-addr= \ ---join=,, \ ---cache=.25 \ ---max-sql-memory=.25 -~~~ -
      - -
      -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---advertise-addr= \ ---join=,, \ ---cache=.25 \ ---max-sql-memory=.25 -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---advertise-addr= \ ---join=,, \ ---cache=.25 \ ---max-sql-memory=.25 -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---advertise-addr= \ ---join=,, \ ---cache=.25 \ ---max-sql-memory=.25 -~~~ -
      - -Then run the [`cockroach init`](cockroach-init.html) command against any node to perform a one-time cluster initialization: - -
      -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init \ ---certs-dir=certs \ ---host=
      -~~~ -
      - -
      -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init \ ---insecure \ ---host=
      -~~~ -
      - -### Start a multi-node cluster across private networks - -**Scenario:** - -- You have a cluster that spans GCE and AWS. -- The nodes on each cloud can reach each other on private addresses, but the private addresses aren't reachable from the other cloud. - -**Approach:** - -1. Start each node on GCE with `--locality` set to describe its location, `--locality-advertise-addr` set to advertise its private address to other nodes in on GCE, `--advertise-addr` set to advertise its public address to nodes on AWS, and `--join` set to the public addresses of 3-5 of the initial nodes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --certs-dir=certs \ - --locality=cloud=gce \ - --locality-advertise-addr=cloud=gce@ \ - --advertise-addr= \ - --join=,, \ - --cache=.25 \ - --max-sql-memory=.25 - ~~~ - -2. Start each node on AWS with `--locality` set to describe its location, `--locality-advertise-addr` set to advertise its private address to other nodes on AWS, `--advertise-addr` set to advertise its public address to nodes on GCE, and `--join` set to the public addresses of 3-5 of the initial nodes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --certs-dir=certs \ - --locality=cloud=aws \ - --locality-advertise-addr=cloud=aws@ \ - --advertise-addr= \ - --join=,, \ - --cache=.25 \ - --max-sql-memory=.25 - ~~~ - -3. Run the [`cockroach init`](cockroach-init.html) command against any node to perform a one-time cluster initialization: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach init \ - --certs-dir=certs \ - --host=
      - ~~~ - -### Add a node to a cluster - -
      - - -
      - -To add a node to an existing cluster, run the `cockroach start` command, setting the `--join` flag to the same addresses you used when [starting the cluster](#start-a-multi-node-cluster): - -
      -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---certs-dir=certs \ ---advertise-addr= \ ---join=,, \ ---cache=.25 \ ---max-sql-memory=.25 -~~~ -
      - -
      -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---advertise-addr= \ ---join=,, \ ---cache=.25 \ ---max-sql-memory=.25 -~~~ -
      - -### Create a table with node locality information - -Start a three-node cluster with locality information specified in the `cockroach start` commands: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --insecure --port=26257 --http-port=26258 --store=cockroach-data/1 --cache=256MiB --locality=region=eu-west-1,cloud=aws,zone=eu-west-1a -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --insecure --port=26259 --http-port=26260 --store=cockroach-data/2 --cache=256MiB --join=localhost:26257 --locality=region=eu-west-1,cloud=aws,zone=eu-west-1b -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --insecure --port=26261 --http-port=26262 --store=cockroach-data/3 --cache=256MiB --join=localhost:26257 --locality=region=eu-west-1,cloud=aws,zone=eu-west-1c -~~~ - -You can use the [`crdb_internal.locality_value`](functions-and-operators.html#system-info-functions) built-in function to return the current node's locality information from inside a SQL shell. The example below uses the output of `crdb_internal.locality_value('zone')` as the `DEFAULT` value to use for the `zone` column of new rows. Other available locality keys for the running three-node cluster include `region` and `cloud`. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE charges ( - zone STRING NOT NULL DEFAULT crdb_internal.locality_value('zone'), - id INT PRIMARY KEY NOT NULL -); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO charges (id) VALUES (1); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM charges WHERE id = 1; -~~~ - -~~~ - zone | id -+------------+----+ - eu-west-1a | 1 -(1 row) -~~~ - -The `zone ` column has the zone of the node on which the row was created. - -In a separate terminal window, open a SQL shell to a different node on the cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --port 26259 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO charges (id) VALUES (2); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM charges WHERE id = 2; -~~~ - -~~~ - zone | id -+------------+----+ - eu-west-1b | 2 -(1 row) -~~~ - -In a separate terminal window, open a SQL shell to the third node: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --port 26261 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO charges (id) VALUES (3); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM charges WHERE id = 3; -~~~ - -~~~ - zone | id -+------------+----+ - eu-west-1c | 3 -(1 row) -~~~ - - -## See also - -- [Initialize a Cluster](cockroach-init.html) -- [Manual Deployment](manual-deployment.html) -- [Orchestrated Deployment](orchestration.html) -- [Local Deployment](start-a-local-cluster.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/cockroach-version.md b/src/current/v19.2/cockroach-version.md deleted file mode 100644 index 4f7c44b0537..00000000000 --- a/src/current/v19.2/cockroach-version.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -title: cockroach version -summary: To view version details for a specific cockroach binary, run the cockroach version command. -toc: false -key: view-version-details.html ---- - -To view version details for a specific `cockroach` binary, run the `cockroach version` [command](cockroach-commands.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach version -~~~ - -~~~ -Build Tag: {{page.release_info.version}} -Build Time: {{page.release_info.build_time}} -Distribution: CCL -Platform: darwin amd64 -Go Version: go1.8.3 -C Compiler: 4.2.1 Compatible Clang 3.8.0 (tags/RELEASE_380/final) -Build SHA-1: 5b757262d33d814bda1deb2af20161a1f7749df3 -Build Type: release -~~~ - -The `cockroach version` command outputs the following fields: - -Field | Description -------|------------ -`Build Tag` | The CockroachDB version. -`Build Time` | The date and time when the binary was built. -`Distribution` | The scope of the binary. If `CCL`, the binary contains functionality covered by both the CockroachDB Community License (CCL) and the Business Source License (BSL). If `OSS`, the binary contains only functionality covered by the Apache 2.0 license. The v19.2 release converts to Apache 2.0 as of Oct 1, 2022, at which time you can use the `make buildoss` command to build a pure open-source binary. For more details about licensing, see the [Licensing FAQs](licensing-faqs.html). -`Platform` | The platform that the binary can run on. -`Go Version` | The version of Go in which the source code is written. -`C Compiler` | The C compiler used to build the binary. -`Build SHA-1` | The SHA-1 hash of the commit used to build the binary. -`Build Type` | The type of release. If `release`, `release-gnu`, or `release-musl`, the binary is for a [production release](../releases/#production-releases). If `development`, the binary is for a [testing release](../releases/#testing-releases). - -## See also - -- [Install CockroachDB](install-cockroachdb.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/cockroach-workload.md b/src/current/v19.2/cockroach-workload.md deleted file mode 100644 index e5b56a270ac..00000000000 --- a/src/current/v19.2/cockroach-workload.md +++ /dev/null @@ -1,657 +0,0 @@ ---- -title: cockroach workload -summary: Use cockroach workload to run a load generator against a CockroachDB cluster. -toc: true ---- - -CockroachDB comes with built-in load generators for simulating different types of client workloads, printing per-operation statistics and totals after a specific duration or max number of operations. To run one of these load generators, use the `cockroach workload` [command](cockroach-commands.html) as described below. - -{{site.data.alerts.callout_danger}} -The `cockroach workload` command is experimental. The interface and output are subject to change. -{{site.data.alerts.end}} - -## Synopsis - -Create the schema for a workload: - -~~~ shell -$ cockroach workload init '' -~~~ - -Run a workload: - -~~~ shell -$ cockroach workload run '' -~~~ - -View help: - -~~~ shell -$ cockroach workload --help -~~~ -~~~ shell -$ cockroach workload init --help -~~~ -~~~ shell -$ cockroach workload init --help -~~~ -~~~ shell -$ cockroach workload run --help -~~~ -~~~ shell -$ cockroach workload run --help -~~~ - -## Subcommands - -Command | Usage ---------|------ -`init` | Load the schema for the workload. You run this command once for a given schema. -`run` | Run a workload. You can run this command multiple times from different machines to increase concurrency. See [Concurrency](#concurrency) for more details. - -## Concurrency - -There are two ways to increase the concurrency of a workload: - -- **Increase the concurrency of a single workload instance** by running `cockroach workload run ` with the `--concurrency` flag set to a value higher than the default. Note that not all workloads support this flag. -- **Run multiple instances of a workload in parallel** by running `cockroach workload run ` multiple times from different terminals/machines. - -## Workloads - -Workload | Description ----------|------------ -[`bank`](#bank-workload) | Models a set of accounts with currency balances.

      For this workload, you run `workload init` to load the schema and then `workload run` to generate data. -[`intro`](#intro-and-startrek-workloads) | Loads an `intro` database, with one table, `mytable`, with a hidden message.

      For this workload, you run only `workload init` to load the data. The `workload run` subcommand is not applicable. -[`kv`](#kv-workload) | Reads and writes to keys spread (by default, uniformly at random) across the cluster.

      For this workload, you run `workload init` to load the schema and then `workload run` to generate data. -[`movr`](#movr-workload) | New in v19.2: Simulates a workload for the [MovR example application](movr.html).

      For this workload, you run `workload init` to load the schema and then `workload run` to generate data. -[`startrek`](#intro-and-startrek-workloads) | Loads a `startrek` database, with two tables, `episodes` and `quotes`.

      For this workload, you run only `workload init` to load the data. The `workload run` subcommand is not applicable. -[`tpcc`](#tpcc-workload) | Simulates a transaction processing workload using a rich schema of multiple tables.

      For this workload, you run `workload init` to load the schema and then `workload run` to generate data. -[`ycsb`](#ycsb-workload) | Simulates a high-scale key value workload, either read-heavy, write-heavy, or scan-based, with additional customizations.

      For this workload, you run `workload init` to load the schema and then `workload run` to generate data. - -## Flags - -{{site.data.alerts.callout_info}} -The `cockroach workload` command does not support connection or security flags like other [`cockroach` commands](cockroach-commands.html). Instead, you must use a [connection string](connection-parameters.html) at the end of the command. -{{site.data.alerts.end}} - -### `bank` workload - -Flag | Description ------|------------ -`--concurrency` | The number of concurrent workers.

      **Applicable commands:** `init` or `run`
      **Default:** 2 * number of CPUs -`--db` | The SQL database to use.

      **Applicable commands:** `init` or `run`
      **Default:** `bank` -`--display-every` | New in v19.2: The frequency for printing per-operation statistics. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `1s` -`--display-format` | New in v19.2: The format for printing per-operation statistics (`simple`, `incremental-json`). When using `incremental-json`, note that totals are not printed at the end of the workload's duration.

      **Applicable command:** `run`
      **Default:** `simple` -`--drop` | Drop the existing database, if it exists.

      **Applicable commands:** `init` or `run`. For the `run` command, this flag must be used in conjunction with `--init`. -`--duration` | The duration to run, with a required time unit suffix. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable commands:** `init` or `run`
      **Default:** `0`, which means run forever. -`--histograms` | The file to write per-op incremental and cumulative histogram data to.

      **Applicable command:** `run` -`--init` | **Deprecated.** Use the `init` command instead.

      **Applicable command:** `run` -`--max-ops` | The maximum number of operations to run.

      **Applicable command:** `run` -`--max-rate` | The maximum frequency of operations (reads/writes).

      **Applicable command:** `run`
      **Default:** `0`, which means unlimited. -`--payload-bytes` | The size of the payload field in each initial row.

      **Applicable commands:** `init` or `run`
      **Default:** `100` -`--ramp` | The duration over which to ramp up load.

      **Applicable command:** `run` -`--ranges` | The initial number of ranges in the `bank` table.

      **Applicable commands:** `init` or `run`
      **Default:** `10` -`--rows` | The initial number of accounts in the `bank` table.

      **Applicable commands:** `init` or `run`
      **Default:** `1000` -`--seed` | The key hash seed.

      **Applicable commands:** `init` or `run`
      **Default:** `1` -`--tolerate-errors` | Keep running on error.

      **Applicable command:** `run` - -### `intro` and `startrek` workloads - -{{site.data.alerts.callout_info}} -These workloads generate data but do not offer the ability to run continuous load. Thus, only the `init` subcommand is supported. -{{site.data.alerts.end}} - -Flag | Description ------|------------ -`--drop` | Drop the existing database, if it exists, before loading the dataset. - - -### `kv` workload - -Flag | Description ------|------------ -`--batch` | The number of blocks to insert in a single SQL statement.

      **Applicable commands:** `init` or `run`
      **Default:** `1` -`--concurrency` | The number of concurrent workers.

      **Applicable commands:** `init` or `run`
      **Default:** `8` `--cycle-length`| The number of keys repeatedly accessed by each writer.**Applicable commands:** `init` or `run`
      **Default:** `9223372036854775807` -`--db` | The SQL database to use.

      **Applicable commands:** `init` or `run`
      **Default:** `kv` -`--display-every` | New in v19.2: The frequency for printing per-operation statistics. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `1s` -`--display-format` | New in v19.2: The format for printing per-operation statistics (`simple`, `incremental-json`). When using `incremental-json`, note that totals are not printed at the end of the workload's duration.

      **Applicable command:** `run`
      **Default:** `simple` -`--drop` | Drop the existing database, if it exists.

      **Applicable commands:** `init` or `run` -`--duration` | The duration to run, with a required time unit suffix. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `0`, which means run forever. -`--histograms` | The file to write per-op incremental and cumulative histogram data to.

      **Applicable command:** `run` -`--init` | **Deprecated.** Use the `init` command instead.

      **Applicable command:** `run` -`--max-block-bytes` | The maximum amount of raw data written with each insertion.

      **Applicable commands:** `init` or `run`
      **Default:** `2` -`--max-ops` | The maximum number of operations to run.

      **Applicable command:** `run` -`--max-rate` | The maximum frequency of operations (reads/writes).

      **Applicable command:** `run`
      **Default:** `0`, which means unlimited. -`--min-block-bytes` | The minimum amount of raw data written with each insertion.

      **Applicable commands:** `init` or `run`
      **Default:** `1` -`--ramp` | The duration over which to ramp up load.

      **Applicable command:** `run` -`--read-percent` | The percent (0-100) of operations that are reads of existing keys.

      **Applicable commands:** `init` or `run` -`--seed` | The key hash seed.

      **Applicable commands:** `init` or `run`
      **Default:** `1` -`--sequential` | Pick keys sequentially instead of randomly.

      **Applicable commands:** `init` or `run` -`--splits` | The number of splits to perform before starting normal operations.

      **Applicable commands:** `init` or `run` -`--tolerate-errors` | Keep running on error.

      **Applicable command:** `run` -`--use-opt` | Use [cost-based optimizer](cost-based-optimizer.html).

      **Applicable commands:** `init` or `run`
      **Default:** `true` -`--write-seq` | Initial write sequence value.

      **Applicable commands:** `init` or `run` - -### `movr` workload - -Flag | Description ------|------------ -`--data-loader` | How to load initial table data. Valid options are `INSERT` and `IMPORT`.

      **Applicable commands:** `init` or `run`
      **Default:** `INSERT` -`--db` | The SQL database to use.

      **Applicable commands:** `init` or `run`
      **Default:** `movr` -`--display-every` | New in v19.2: The frequency for printing per-operation statistics. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `1s` -`--display-format` | New in v19.2: The format for printing per-operation statistics (`simple`, `incremental-json`). When using `incremental-json`, note that totals are not printed at the end of the workload's duration.

      **Applicable command:** `run`
      **Default:** `simple` -`--drop` | Drop the existing database, if it exists.

      **Applicable commands:** `init` or `run` -`--duration` | The duration to run, with a required time unit suffix. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `0`, which means run forever. -`--histograms` | The file to write per-op incremental and cumulative histogram data to.

      **Applicable command:** `run` -`--histograms-max-latency` | Expected maximum latency of running a query, with a required time unit suffix. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `1m40s` -`--max-ops` | The maximum number of operations to run.

      **Applicable command:** `run` -`--max-rate` | The maximum frequency of operations (reads/writes).

      **Applicable command:** `run`
      **Default:** `0`, which means unlimited. -`--method` | The SQL issue method (`prepare`, `noprepare`, `simple`).

      **Applicable commands:** `init` or `run`
      **Default:** `prepare` -`--num-histories` | The initial number of ride location histories.

      **Applicable commands:** `init` or `run`
      **Default:** `1000` -`--num-promo-codes` | The initial number of promo codes.

      **Applicable commands:** `init` or `run`
      **Default:** `1000` -`--num-rides` | Initial number of rides.

      **Applicable commands:** `init` or `run`
      **Default:** `500` -`--num-users` | Initial number of users.

      **Applicable commands:** `init` or `run`
      **Default:** `50` -`--num-vehicles` | Initial number of vehicles.

      **Applicable commands:** `init` or `run`
      **Default:** `15` -`--ramp` | The duration over which to ramp up load.

      **Applicable command:** `run` -`--seed` | The random number generator seed.

      **Applicable commands:** `init` or `run`
      **Default:** `1` -`--tolerate-errors` | Keep running on error.

      **Applicable command:** `run` - -### `tpcc` workload - -Flag | Description ------|------------ -`--active-warehouses` | Run the load generator against a specific number of warehouses.

      **Applicable commands:** `init` or `run`
      **Defaults:** Value of `--warehouses` -`--db` | The SQL database to use.

      **Applicable commands:** `init` or `run`
      **Default:** `tpcc` -`--display-every` | New in v19.2: The frequency for printing per-operation statistics. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `1s` -`--display-format` | New in v19.2: The format for printing per-operation statistics (`simple`, `incremental-json`). When using `incremental-json`, note that totals are not printed at the end of the workload's duration.

      **Applicable command:** `run`
      **Default:** `simple` -`--drop` | Drop the existing database, if it exists.

      **Applicable commands:** `init` or `run`. For the `run` command, this flag must be used in conjunction with `--init`. -`--duration` | The duration to run, with a required time unit suffix. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `0`, which means run forever. -`--fks` | Add foreign keys.

      **Applicable commands:** `init` or `run`
      **Default:** `true` -`--histograms` | The file to write per-op incremental and cumulative histogram data to.

      **Applicable command:** `run` -`--init` | **Deprecated.** Use the `init` command instead.

      **Applicable command:** `run` -`--interleaved` | Use [interleaved tables](interleave-in-parent.html).

      **Applicable commands:** `init` or `run` -`--max-ops` | The maximum number of operations to run.

      **Applicable command:** `run` -`--max-rate` | The maximum frequency of operations (reads/writes).

      **Applicable command:** `run`
      **Default:** `0`, which means unlimited. -`--mix` | Weights for the transaction mix.

      **Applicable commands:** `init` or `run`
      **Default:** `newOrder=10,payment=10,orderStatus=1,delivery=1,stockLevel=1`, which matches the [TPC-C specification](http://tpc.org/tpc_documents_current_versions/current_specifications5.asp). -`--partition-affinity` | Run the load generator against a specific partition. This flag must be used in conjunction with `--partitions`.

      **Applicable commands:** `init` or `run`
      **Default:** `-1` -`--partitions` | Partition tables. This flag must be used in conjunction with `--split`.

      **Applicable commands:** `init` or `run` -`--ramp` | The duration over which to ramp up load.

      **Applicable command:** `run` -`--scatter` | Scatter ranges.

      **Applicable commands:** `init` or `run` -`--seed` | The random number generator seed.

      **Applicable commands:** `init` or `run`
      **Default:** `1` -`--serializable` | Force serializable mode. CockroachDB only supports `SERIALIZABLE` isolation, so this flag is not necessary.

      **Applicable command:** `init` -`--split` | [Split tables](split-at.html).

      **Applicable commands:** `init` or `run` -`--tolerate-errors` | Keep running on error.

      **Applicable command:** `run` -`--wait` | Run in wait mode, i.e., include think/keying sleeps.

      **Applicable commands:** `init` or `run`
      **Default:** `true` -`--warehouses` | The number of warehouses for loading initial data, at approximately 200 MB per warehouse.

      **Applicable commands:** `init` or `run`
      **Default:** `1` -`--workers` | The number of concurrent workers.

      **Applicable commands:** `init` or `run`
      **Default:** `--warehouses` * 10 -`--zones` | The number of [replication zones](configure-replication-zones.html) for partitioning. This number should match the number of `--partitions` and the zones used to start the cluster.

      **Applicable command:** `init` - -### `ycsb` workload - -Flag | Description ------|------------ -`--concurrency` | The number of concurrent workers.

      **Applicable commands:** `init` or `run`
      **Default:** `8` -`--db` | The SQL database to use.

      **Applicable commands:** `init` or `run`
      **Default:** `ycsb` -`--display-every` | New in v19.2: The frequency for printing per-operation statistics. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `1s` -`--display-format` | New in v19.2: The format for printing per-operation statistics (`simple`, `incremental-json`). When using `incremental-json`, note that totals are not printed at the end of the workload's duration.

      **Applicable command:** `run`
      **Default:** `simple` -`--drop` | Drop the existing database, if it exists.

      **Applicable commands:** `init` or `run`. For the `run` command, this flag must be used in conjunction with `--init`. -`--duration` | The duration to run, with a required time unit suffix. Valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)) are `ns`, `us`, `ms`, `s`, `m`, and `h`.

      **Applicable command:** `run`
      **Default:** `0`, which means run forever. -`--families` | Place each column in its own [column family](column-families.html).

      **Applicable commands:** `init` or `run` -`--histograms` | The file to write per-op incremental and cumulative histogram data to.

      **Applicable command:** `run` -`--init` | **Deprecated.** Use the `init` command instead.

      **Applicable command:** `run` -`--initial-count` | Initial number of rows to sequentially insert before beginning random number generation.

      **Applicable commands:** `init` or `run`
      **Default:** `10000` -`--json` | Use JSONB rather than relational data.

      **Applicable commands:** `init` or `run` -`--max-ops` | The maximum number of operations to run.

      **Applicable command:** `run` -`--max-rate` | The maximum frequency of operations (reads/writes).

      **Applicable command:** `run`
      **Default:** `0`, which means unlimited. -`--method` | The SQL issue method (`prepare`, `noprepare`, `simple`).

      **Applicable commands:** `init` or `run`
      **Default:** `prepare` -`--ramp` | The duration over which to ramp up load.

      **Applicable command:** `run` -`--request-distribution` | Distribution for the random number generator (`zipfian`, `uniform`).

      **Applicable commands:** `init` or `run`.
      **Default:** `zipfian` -`--seed` | The random number generator seed.

      **Applicable commands:** `init` or `run`
      **Default:** `1` -`--splits` | Number of [splits](split-at.html) to perform before starting normal operations.

      **Applicable commands:** `init` or `run` -`--tolerate-errors` | Keep running on error.

      **Applicable command:** `run` -`--workload` | The type of workload to run (`A`, `B`, `C`, `D`, or `F`). For details about these workloads, see [YCSB Workloads](https://github.com/brianfrankcooper/YCSB/wiki/Core-Workloads).

      **Applicable commands:** `init` or `run`
      **Default:** `B` - -### Logging - -By default, the `cockroach workload` command logs errors to `stderr`. - -If you need to troubleshoot this command's behavior, you can change its [logging behavior](debug-and-error-logs.html). - -## Examples - -These examples assume that you have already [started an insecure cluster locally](start-a-local-cluster.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---listen-addr=localhost -~~~ - -### Run the `bank` workload - -1. Load the initial schema: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init bank \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Run the workload for 1 minute: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run bank \ - --duration=1m \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - - You'll see per-operation statistics print to standard output every second: - - ~~~ - _elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms) - 1s 0 1608.6 1702.2 4.5 7.3 12.6 65.0 transfer - 2s 0 1725.3 1713.8 4.5 7.9 13.1 19.9 transfer - 3s 0 1721.1 1716.2 4.5 7.3 11.5 21.0 transfer - 4s 0 1328.7 1619.2 5.5 10.5 17.8 39.8 transfer - 5s 0 1389.3 1573.3 5.2 11.5 16.3 23.1 transfer - 6s 0 1640.0 1584.4 5.0 7.9 12.1 16.3 transfer - 7s 0 1594.0 1585.8 5.0 7.9 10.5 15.7 transfer - 8s 0 1652.8 1594.2 4.7 7.9 11.5 29.4 transfer - 9s 0 1451.9 1578.4 5.2 10.0 15.2 26.2 transfer - 10s 0 1653.3 1585.9 5.0 7.6 10.0 18.9 transfer - ... - ~~~ - - After the specified duration (1 minute in this case), the workload will stop and you'll see totals printed to standard output: - - ~~~ - _elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)__result - 60.0s 0 84457 1407.6 5.7 5.5 10.0 15.2 167.8 - ~~~ - -### Run the `kv` workload - -1. Load the initial schema: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init kv \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Run the workload for 1 minute: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run kv \ - --duration=1m \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - - You'll see per-operation statistics print to standard output every second: - - ~~~ - _elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms) - 1s 0 5095.8 5123.7 1.5 2.5 3.3 7.3 write - 2s 0 4795.4 4959.6 1.6 2.8 3.5 8.9 write - 3s 0 3456.5 4458.5 2.0 4.5 7.3 24.1 write - 4s 0 2787.9 4040.8 2.4 6.3 12.6 30.4 write - 5s 0 3558.7 3944.4 2.0 4.2 6.8 11.5 write - 6s 0 3733.8 3909.3 1.9 4.2 6.0 12.6 write - 7s 0 3565.6 3860.1 2.0 4.7 7.9 25.2 write - 8s 0 3469.3 3811.4 2.0 5.0 6.8 22.0 write - 9s 0 3937.6 3825.4 1.8 3.7 7.3 29.4 write - 10s 0 3822.9 3825.1 1.8 4.7 8.9 37.7 write - ... - ~~~ - - After the specified duration (1 minute in this case), the workload will stop and you'll see totals printed to standard output: - - ~~~ - _elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)__result - 60.0s 0 276067 4601.0 1.7 1.6 3.1 5.2 96.5 - ~~~ - -### Load the `intro` dataset - -1. Load the dataset: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init intro \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Launch the built-in SQL client to view it: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW TABLES FROM intro; - ~~~ - - ~~~ - table_name - +------------+ - mytable - (1 row) - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ SELECT * FROM intro.mytable WHERE (l % 2) = 0; - ~~~ - - ~~~ - l | v - +----+------------------------------------------------------+ - 0 | !__aaawwmqmqmwwwaas,,_ .__aaawwwmqmqmwwaaa,, - 2 | !"VT?!"""^~~^"""??T$Wmqaa,_auqmWBT?!"""^~~^^""??YV^ - 4 | ! "?##mW##?"- - 6 | ! C O N G R A T S _am#Z??A#ma, Y - 8 | ! _ummY" "9#ma, A - 10 | ! vm#Z( )Xmms Y - 12 | ! .j####mmm#####mm#m##6. - 14 | ! W O W ! jmm###mm######m#mmm##6 - 16 | ! ]#me*Xm#m#mm##m#m##SX##c - 18 | ! dm#||+*$##m#mm#m#Svvn##m - 20 | ! :mmE=|+||S##m##m#1nvnnX##; A - 22 | ! :m#h+|+++=Xmm#m#1nvnnvdmm; M - 24 | ! Y $#m>+|+|||##m#1nvnnnnmm# A - 26 | ! O ]##z+|+|+|3#mEnnnnvnd##f Z - 28 | ! U D 4##c|+|+|]m#kvnvnno##P E - 30 | ! I 4#ma+|++]mmhvnnvq##P` ! - 32 | ! D I ?$#q%+|dmmmvnnm##! - 34 | ! T -4##wu#mm#pw##7' - 36 | ! -?$##m####Y' - 38 | ! !! "Y##Y"- - 40 | ! - (21 rows) - ~~~ - -### Load the `startrek` dataset - -1. Load the dataset: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init startrek \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Launch the built-in SQL client to view it: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW TABLES FROM startrek; - ~~~ - - ~~~ - table_name - +------------+ - episodes - quotes - (2 rows) - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT * FROM startrek.episodes WHERE stardate > 5500; - ~~~ - - ~~~ - id | season | num | title | stardate - +----+--------+-----+-----------------------------------+----------+ - 60 | 3 | 5 | Is There in Truth No Beauty? | 5630.7 - 62 | 3 | 7 | Day of the Dove | 5630.3 - 64 | 3 | 9 | The Tholian Web | 5693.2 - 65 | 3 | 10 | Plato's Stepchildren | 5784.2 - 66 | 3 | 11 | Wink of an Eye | 5710.5 - 69 | 3 | 14 | Whom Gods Destroy | 5718.3 - 70 | 3 | 15 | Let That Be Your Last Battlefield | 5730.2 - 73 | 3 | 18 | The Lights of Zetar | 5725.3 - 74 | 3 | 19 | Requiem for Methuselah | 5843.7 - 75 | 3 | 20 | The Way to Eden | 5832.3 - 76 | 3 | 21 | The Cloud Minders | 5818.4 - 77 | 3 | 22 | The Savage Curtain | 5906.4 - 78 | 3 | 23 | All Our Yesterdays | 5943.7 - 79 | 3 | 24 | Turnabout Intruder | 5928.5 - (14 rows) - ~~~ - -### Load the `movr` dataset - -1. Load the dataset: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init movr \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Launch the built-in SQL client to view it: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW TABLES FROM movr; - ~~~ - - ~~~ - table_name -+----------------------------+ - promo_codes - rides - user_promo_codes - users - vehicle_location_histories - vehicles -(6 rows) - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT * FROM movr.users WHERE city='new york'; - ~~~ - - ~~~ - id | city | name | address | credit_card -+--------------------------------------+----------+------------------+-----------------------------+-------------+ - 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228 - 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892 - 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636 - 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234 - 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487 -(5 rows) - ~~~ - -### Run the `movr` workload - -1. Load the initial schema: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init movr \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Initialize and run the workload for 1 minute: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run movr \ - --duration=1m \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - - You'll see per-operation statistics print to standard output every second: - - ~~~ - _elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms) - 1.0s 0 31.9 32.0 0.5 0.6 1.4 1.4 addUser - 1.0s 0 6.0 6.0 1.2 1.4 1.4 1.4 addVehicle - 1.0s 0 10.0 10.0 2.2 6.3 6.3 6.3 applyPromoCode - 1.0s 0 2.0 2.0 0.5 0.6 0.6 0.6 createPromoCode - 1.0s 0 9.0 9.0 0.9 1.6 1.6 1.6 endRide - 1.0s 0 1407.5 1407.8 0.3 0.5 0.7 4.1 readVehicles - 1.0s 0 27.0 27.0 2.1 3.1 4.7 4.7 startRide - 1.0s 0 86.8 86.9 4.7 8.4 11.5 15.2 updateActiveRides - 2.0s 0 26.0 29.0 0.5 1.1 1.4 1.4 addUser - 2.0s 0 8.0 7.0 1.2 2.8 2.8 2.8 addVehicle - 2.0s 0 2.0 6.0 2.6 2.8 2.8 2.8 applyPromoCode - 2.0s 0 0.0 1.0 0.0 0.0 0.0 0.0 createPromoCode - 2.0s 0 6.0 7.5 0.8 1.7 1.7 1.7 endRide - 2.0s 0 1450.4 1429.1 0.3 0.6 0.9 2.6 readVehicles - 2.0s 0 17.0 22.0 2.1 3.3 5.5 5.5 startRide - 2.0s 0 59.0 72.9 6.3 11.5 11.5 14.2 updateActiveRides - ... - ~~~ - - After the specified duration (1 minute in this case), the workload will stop and you'll see totals printed to standard output: - - ~~~ - _elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)__result - 60.0s 0 85297 1421.6 0.7 0.3 2.6 7.1 30.4 - ~~~ - -### Run the `tpcc` workload - -1. Load the initial schema and data: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init tpcc \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Run the workload for 10 minutes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run tpcc \ - --duration=10m \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - - You'll see per-operation statistics print to standard output every second: - - ~~~ - _elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms) - 1s 0 1443.4 1494.8 4.7 9.4 27.3 67.1 transfer - 2s 0 1686.5 1590.9 4.7 8.1 15.2 28.3 transfer - 3s 0 1735.7 1639.0 4.7 7.3 11.5 28.3 transfer - 4s 0 1542.6 1614.9 5.0 8.9 12.1 21.0 transfer - 5s 0 1695.9 1631.1 4.7 7.3 11.5 22.0 transfer - 6s 0 1569.2 1620.8 5.0 8.4 11.5 15.7 transfer - 7s 0 1614.6 1619.9 4.7 8.1 12.1 16.8 transfer - 8s 0 1344.4 1585.6 5.8 10.0 15.2 31.5 transfer - 9s 0 1351.9 1559.5 5.8 10.0 16.8 54.5 transfer - 10s 0 1514.8 1555.0 5.2 8.1 12.1 16.8 transfer - ... - ~~~ - - After the specified duration (10 minutes in this case), the workload will stop and you'll see totals printed to standard output: - - ~~~ - _elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)__result - 600.0s 0 823902 1373.2 5.8 5.5 10.0 15.2 209.7 - ~~~ - -### Run the `ycsb` workload - -1. Load the initial schema and data: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init ycsb \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Run the workload for 10 minutes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run ycsb \ - --duration=10m \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - - You'll see per-operation statistics print to standard output every second: - - ~~~ - _elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms) - 1s 0 9258.1 9666.6 0.7 1.3 2.0 8.9 read - 1s 0 470.1 490.9 1.7 2.9 4.1 5.0 update - 2s 0 10244.6 9955.6 0.7 1.2 2.0 6.6 read - 2s 0 559.0 525.0 1.6 3.1 6.0 7.3 update - 3s 0 9870.8 9927.4 0.7 1.4 2.4 10.0 read - 3s 0 500.0 516.6 1.6 4.2 7.9 15.2 update - 4s 0 9847.2 9907.3 0.7 1.4 2.4 23.1 read - 4s 0 506.8 514.2 1.6 3.7 7.6 17.8 update - 5s 0 10084.4 9942.6 0.7 1.3 2.1 7.1 read - 5s 0 537.2 518.8 1.5 3.5 10.0 15.2 update - ... - ~~~ - - After the specified duration (10 minutes in this case), the workload will stop and you'll see totals printed to standard output: - - ~~~ - _elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)__result - 600.0s 0 4728286 7880.2 1.0 0.9 2.2 5.2 268.4 - ~~~ - -### Customize the frequency and format of per-operation statistics - -To customize the frequency of per-operation statistics, use the `--display-every` flag, with `ns`, `us`, `ms`, `s`, `m`, and `h` as valid [time units](https://en.wikipedia.org/wiki/Orders_of_magnitude_(time)). To customize the format of per-operation statistics, use the `--display-format` flag, with `incremental-json` or `simple` (default) as options. - -1. Load the initial schema and data: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init ycsb \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Run the workload for 1 minute, printing the output every 5 seconds as JSON: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run ycsb \ - --duration=1m \ - --display-every=5s \ - --display-format=incremental-json \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - - ~~~ - {"time":"2019-09-13T03:25:03.950621Z","errs":0,"avgt":8434.5,"avgl":8471.0,"p50l":0.8,"p95l":1.6,"p99l":3.1,"maxl":19.9,"type":"read"} - {"time":"2019-09-13T03:25:03.950621Z","errs":0,"avgt":438.1,"avgl":440.0,"p50l":1.5,"p95l":2.8,"p99l":4.5,"maxl":14.7,"type":"update"} - {"time":"2019-09-13T03:25:08.95061Z","errs":0,"avgt":7610.6,"avgl":8040.8,"p50l":0.8,"p95l":2.0,"p99l":4.2,"maxl":65.0,"type":"read"} - {"time":"2019-09-13T03:25:08.95061Z","errs":0,"avgt":391.8,"avgl":415.9,"p50l":1.6,"p95l":3.5,"p99l":5.8,"maxl":21.0,"type":"update"} - {"time":"2019-09-13T03:25:13.950727Z","errs":0,"avgt":7242.0,"avgl":7774.5,"p50l":0.8,"p95l":2.2,"p99l":4.7,"maxl":75.5,"type":"read"} - {"time":"2019-09-13T03:25:13.950727Z","errs":0,"avgt":382.0,"avgl":404.6,"p50l":1.6,"p95l":4.7,"p99l":10.5,"maxl":24.1,"type":"update"} - ... - ~~~ - - When using `incremental-json`, note that totals are not printed at the end of the workload's duration. - -## See also - -- [`cockroach demo`](cockroach-demo.html) -- [Other Cockroach Commands](cockroach-commands.html) -- [Performance Benchmarking with TPC-C](performance-benchmarking-with-tpc-c-1k-warehouses.html) diff --git a/src/current/v19.2/cockroachdb-in-comparison.md b/src/current/v19.2/cockroachdb-in-comparison.md deleted file mode 100644 index abe3d29214d..00000000000 --- a/src/current/v19.2/cockroachdb-in-comparison.md +++ /dev/null @@ -1,351 +0,0 @@ ---- -title: CockroachDB in Comparison -summary: Learn how CockroachDB compares to other popular databases like PostgreSQL, Cassandra, MongoDB, Google Cloud Spanner, and more. -tags: mongodb, mysql, dynamodb -toc: false -comparison: true ---- - -This page shows you how the key features of CockroachDB stack up against other databases. Hover over the features for their intended meanings, and click CockroachDB answers to view related documentation. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      - - - - CockroachDB
      - Database horizontal scale - - tooltip icon - - - Manual Sharding - Add on configuration - Node based, automated read scale, limited write - Node based, automated for both reads and writes - - Manual Sharding - Add on configuration - Node based, automated read scale, limited write - Node based, automated for both reads and writes - Node based, automated for both reads and writes
      - Database load balancing (internal) - - tooltip icon - - - Manual - not part of database - None and full copies across regions - Even distribution to optimize storage - - Manual - not part of database - None and full copies across regions - Even distribution to optimize storage - Detailed options to optimize storage, compute and latency
      - Failover - - tooltip icon - - - Manual - not part of database - Automated for reads, limited for writes to one region - Automated for reads, limited guarantees for writes - Fully automated for both reads and writes - - Manual - not part of database - Automated for reads, limited for writes to one region - Automated for reads, limited guarantees for writes - Fully automated for both reads and writes - Fully automated for both reads and writes
      - Automated repair and RPO(Recovery Point Objective) - - tooltip icon - - - Manual repair RPO ~1-60 mins - Automated RPO ~1 -5 mins - Manual & automated repair RPO <1 min - "Automated repair RPO <10 sec" - - Manual repair RPO ~1-60 mins - Automated RPO ~1 -5 mins - Manual & automated repair RPO <1 min - "Automated repair RPO <10 sec" - Automated repair RPO = 0 sec
      - Distributed reads - - tooltip icon - - - Manual - asynchronous - Yes - - Manual - asynchronous - Yes - Yes
      - Distributed transactions - - tooltip icon - - - No - Lightweight transactions only - Yes - - No - Lightweight transactions only - Yes - Yes
      - Database isolation levels - - tooltip icon - - - Single region consistent default - Snapshot highest - Serializable - Eventual consistent default - Read uncommitted highest - Snapshot read - Eventual consistent - No transaction isolation guarantees - Default - Snapshot highest - Serializable - - Single region consistent default - Snapshot highest - Serializable - Eventual consistent default - Read uncommitted highest - Snapshot read - Eventual consistent - No transaction isolation guarantees - Default - Snapshot highest - Serializable - Guaranteed consistent default - Serializable highest - Serializable
      - Potential data issues (default) - - tooltip icon - - - Phantom reads, non-repeatable reads, write skew - Dirty reads, phantom reads, non-repeatable reads, write skew - Dirty reads, phantom reads, non-repeatable reads, write conflicts - None - Phantom reads, non-repeatable reads - - Phantom reads, non-repeatable reads, write skew - Dirty reads, phantom reads, non-repeatable reads, write skew - Dirty reads, phantom reads, non-repeatable reads, write conflicts - None - Phantom reads, non-repeatable reads - None
      - SQL - - tooltip icon - - - Yes - No - Yes - with limitations - - Yes - No - Yes - with limitations - Yes - wire compatible with PostgreSQL
      - Database schema change - - tooltip icon - - - Yes - Offline - Online, Active and Dynamic - - Yes - Offline - Online, Active and Dynamic - Online, Active and Dynamic
      - Cost based optimization - - tooltip icon - - - Yes - No - ? - No - - Yes - No - ? - No - Yes
      - Data Geo-partitioning - - tooltip icon - - - No - Yes, object level - Yes - No - - No - Yes, object level - Yes - No - Yes, row level
      - Upgrade method - - tooltip icon - - - Offline - Online, rolling - - Offline - Online, rolling - Online, rolling
      - Multi-region - - tooltip icon - - - Yes - manual - Yes, but not for writes - Yes, for both reads and writes - - Yes - manual - Yes, but not for writes - Yes, for both reads and writes - Yes for both reads and writes
      - Multi-cloud - - tooltip icon - - - No - Yes - - No - Yes - Yes
      - - diff --git a/src/current/v19.2/collate.md b/src/current/v19.2/collate.md deleted file mode 100644 index 633745b53ef..00000000000 --- a/src/current/v19.2/collate.md +++ /dev/null @@ -1,272 +0,0 @@ ---- -title: COLLATE -summary: The COLLATE feature lets you sort strings according to language- and country-specific rules. -toc: true ---- - -The `COLLATE` feature lets you sort [`STRING`](string.html) values according to language- and country-specific rules, known as collations. - -Collated strings are important because different languages have [different rules for alphabetic order](https://en.wikipedia.org/wiki/Alphabetical_order#Language-specific_conventions), especially with respect to accented letters. For example, in German accented letters are sorted with their unaccented counterparts, while in Swedish they are placed at the end of the alphabet. A collation is a set of rules used for ordering and usually corresponds to a language, though some languages have multiple collations with different rules for sorting; for example Portuguese has separate collations for Brazilian and European dialects (`pt-BR` and `pt-PT` respectively). - -{% include {{page.version.version}}/sql/vectorized-support.md %} - -## Details - -- Operations on collated strings cannot involve strings with a different collation or strings with no collation. However, it is possible to add or overwrite a collation on the fly. - -- Only use the collation feature when you need to sort strings by a specific collation. We recommend this because every time a collated string is constructed or loaded into memory, CockroachDB computes its collation key, whose size is linear in relationship to the length of the collated string, which requires additional resources. - -- Collated strings can be considerably larger than the corresponding uncollated strings, depending on the language and the string content. For example, strings containing the character `é` produce larger collation keys in the French locale than in Chinese. - -- Collated strings that are indexed require additional disk space as compared to uncollated strings. In case of indexed collated strings, collation keys must be stored in addition to the strings from which they are derived, creating a constant factor overhead. - -## Supported collations - -CockroachDB supports collations identified by [Unicode locale identifiers](https://cldr.unicode.org/development/core-specification#h.vgyyng33o798). For example, `en-US` identifies US English, `es` identifies Spanish, and `fr-CA` identifies Canadian French. Collation names are case-insensitive, and hyphens and underscores are interchangeable. - -{{site.data.alerts.callout_info}} -If a hyphen is used in a SQL query, the collation name must be enclosed in double quotes, as single quotes are used for SQL string literals. -{{site.data.alerts.end}} - -A list of supported collations can be found in the `pg_catalog.pg_collation` table: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT collname from pg_catalog.pg_collation; -~~~ - -~~~ - collname ------------------------ - und - aa - af - ar -... -(95 rows) -~~~ - -CockroachDB supports standard aliases for the collations listed in `pg_collation`. For example, `es-419` (Latin American Spanish) and `zh-Hans` (Simplified Chinese) are supported, but they do not appear in the `pg_collations` table because they are equivalent to the `es` and `zh` collations listed in the table. - -CockroachDB also supports the following Unicode locale extensions: - -- `co` (collation type) -- `ks` (strength) -- `kc` (case level) -- `kb` (backwards second level weight) -- `kn` (numeric) -- `ks` (strength) -- `ka` (alternate handling) - -To use a locale extension, append `-u-` to the base locale name, followed by the extension. For example, `en-US-u-ks-level2` is case-insensitive US English. The `ks` modifier changes the "strength" of the collation, causing it to treat certain classes of characters as equivalent (PostgreSQL calls these "non-deterministic collations"). Setting the `ks` to `level2` makes the collation case-insensitive (for languages that have this concept). - -For more details on locale extensions, see the [Unicode Collation Algorithm](https://en.wikipedia.org/wiki/Unicode_collation_algorithm). - -## Collation versioning - -While changes to collations are rare, they are possible, especially in languages with a large numbers of characters (e.g., Simplified and Traditional Chinese). CockroachDB updates its support with new versions of the Unicode standard every year, but there is currently no way to specify the version of Unicode to use. As a result, it is possible for a collation change to invalidate existing collated string data. To prevent collated data from being invalidated by Unicode changes, we recommend storing data in columns with an uncollated string type, and then using a [computed column](computed-columns.html) for the desired collation. In the event that a collation change produces undesired effects, the computed column can be dropped and recreated. - -## SQL syntax - -Collated strings are used as normal strings in SQL, but have a `COLLATE` clause appended to them. - -- **Column syntax**: `STRING COLLATE `. For example: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE foo (a STRING COLLATE en PRIMARY KEY); - ~~~ - - {{site.data.alerts.callout_info}}You can also use any of the aliases for STRING.{{site.data.alerts.end}} - -- **Value syntax**: ` COLLATE `. For example: - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO foo VALUES ('dog' COLLATE en); - ~~~ - -## Examples - -### Specify collation for a column - -You can set a default collation for all values in a `STRING` column. - -For example, you can set a column's default collation to German (`de`): - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE de_names (name STRING COLLATE de PRIMARY KEY); -~~~ - -When inserting values into this column, you must specify the collation for every value: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO de_names VALUES ('Backhaus' COLLATE de), ('Bär' COLLATE de), ('Baz' COLLATE de); -~~~ - -The sort will now honor the `de` collation that treats *ä* as *a* in alphabetic sorting: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM de_names ORDER BY name; -~~~ -~~~ - name -+----------+ - Backhaus - Bär - Baz -(3 rows) -~~~ - -### Specify collations with locale extensions - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE nocase_strings (greeting STRING COLLATE "en-US-u-ks-level2"); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO nocase_strings VALUES ('Hello, friend.' COLLATE "en-US-u-ks-level2"), ('Hi. My name is Petee.' COLLATE "en-US-u-ks-level2"); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM nocase_strings WHERE greeting = ('hi. my name is petee.' COLLATE "en-US-u-ks-level2"); -~~~ - -~~~ - greeting -+-----------------------+ - Hi. My name is Petee. -(1 row) -~~~ - -### Order by non-default collation - -You can sort a column using a specific collation instead of its default. - -For example, you receive different results if you order results by German (`de`) and Swedish (`sv`) collations: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM de_names ORDER BY name COLLATE sv; -~~~ -~~~ - name -+----------+ - Backhaus - Baz - Bär -(3 rows) -~~~ - -### Ad-hoc collation casting - -You can cast any string into a collation on the fly. - -{% include copy-clipboard.html %} -~~~ sql -> SELECT 'A' COLLATE de < 'Ä' COLLATE de; -~~~ -~~~ - ?column? -+----------+ - true -(1 row) -~~~ - -However, you cannot compare values with different collations: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT 'Ä' COLLATE sv < 'Ä' COLLATE de; -~~~ -~~~ -pq: unsupported comparison operator: < -~~~ - -You can also use casting to remove collations from values. - -{% include copy-clipboard.html %} -~~~ sql -> SELECT CAST(name AS STRING) FROM de_names ORDER BY name; -~~~ -~~~ - name -+----------+ - Backhaus - Baz - Bär -(3 rows) -~~~ - -### Show collation for strings - -New in v19.2: You can use the `pg_collation_for` [built-in function](functions-and-operators.html#string-and-byte-functions), or its alternative [syntax form](functions-and-operators.html#special-syntax-forms) `COLLATION FOR`, to return the locale name of a collated string. - -For example: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT pg_collation_for('Bär' COLLATE de); -~~~ - -~~~ - pg_collation_for -+------------------+ - de -(1 row) -~~~ - -This is equivalent to: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT COLLATION FOR ('Bär' COLLATE de); -~~~ - -~~~ - pg_collation_for -+------------------+ - de -(1 row) -~~~ - -## Known limitations - -### Collation names that include upper-case or hyphens may cause errors - -Using a [collation](collate.html) name with upper-case letters or hyphens may result in errors. - -For example, the following SQL will result in an error: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE nocase_strings (s STRING COLLATE "en-US-u-ks-level2"); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO nocase_strings VALUES ('Aaa' COLLATE "en-US-u-ks-level2"), ('Bbb' COLLATE "en-US-u-ks-level2"); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT s FROM nocase_strings WHERE s = ('bbb' COLLATE "en-US-u-ks-level2"); -~~~ - -~~~ -ERROR: internal error: "$0" = 'bbb' COLLATE en_us_u_ks_level2: unsupported comparison operator: = -~~~ - -As a workaround, only use collation names that have lower-case letters and underscores. - -[Tracking GitHub issue](https://github.com/cockroachdb/cockroach/issues/56335) - -## See also - -[Data Types](data-types.html) diff --git a/src/current/v19.2/column-families.md b/src/current/v19.2/column-families.md deleted file mode 100644 index 26ad82e11a8..00000000000 --- a/src/current/v19.2/column-families.md +++ /dev/null @@ -1,101 +0,0 @@ ---- -title: Column Families -summary: A column family is a group of columns in a table that are stored as a single key-value pair in the underlying key-value store. -toc: true ---- - -A column family is a group of columns in a table that are stored as a single key-value pair in the [underlying key-value store](architecture/storage-layer.html). Column families reduce the number of keys stored in the key-value store, resulting in improved performance during [`INSERT`](insert.html), [`UPDATE`](update.html), and [`DELETE`](delete.html) operations. - -This page explains how CockroachDB organizes columns into families as well as cases in which you might want to manually override the default behavior. - -{{site.data.alerts.callout_info}} -[Secondary indexes](indexes.html) do not respect column families. All secondary indexes store values in a single column family. -{{site.data.alerts.end}} - -## Default behavior - -When a table is created, all columns are stored as a single column family. - -This default approach ensures efficient key-value storage and performance in most cases. However, when frequently updated columns are grouped with seldom updated columns, the seldom updated columns are nonetheless rewritten on every update. Especially when the seldom updated columns are large, it's more performant to split them into a distinct family. - -## Manual override - -### Assign column families on table creation - -To manually assign a column family on [table creation](create-table.html), use the `FAMILY` keyword. - -For example, let's say we want to create a table to store an immutable blob of data (`data BYTES`) with a last accessed timestamp (`last_accessed TIMESTAMP`). Because we know that the blob of data will never get updated, we use the `FAMILY` keyword to break it into a separate column family: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE test ( - id INT PRIMARY KEY, - last_accessed TIMESTAMP, - data BYTES, - FAMILY f1 (id, last_accessed), - FAMILY f2 (data) -); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE users; -~~~ - -~~~ -+-------+---------------------------------------------+ -| Table | CreateTable | -+-------+---------------------------------------------+ -| test | CREATE TABLE test ( | -| | id INT NOT NULL, | -| | last_accessed TIMESTAMP NULL, | -| | data BYTES NULL, | -| | CONSTRAINT "primary" PRIMARY KEY (id), | -| | FAMILY f1 (id, last_accessed), | -| | FAMILY f2 (data) | -| | ) | -+-------+---------------------------------------------+ -(1 row) -~~~ - -{{site.data.alerts.callout_info}} -Columns that are part of the primary index are always assigned to the first column family. If you manually assign primary index columns to a family, it must therefore be the first family listed in the [`CREATE TABLE`](create-table.html) statement. -{{site.data.alerts.end}} - -### Assign column families when adding columns - -When using the [`ALTER TABLE .. ADD COLUMN`](add-column.html) statement to add a column to a table, you can assign the column to a new or existing column family. - -- Use the `CREATE FAMILY` keyword to assign a new column to a **new family**. For example, the following would add a `data2 BYTES` column to the `test` table above and assign it to a new column family: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE test ADD COLUMN data2 BYTES CREATE FAMILY f3; - ~~~ - -- Use the `FAMILY` keyword to assign a new column to an **existing family**. For example, the following would add a `name STRING` column to the `test` table above and assign it to family `f1`: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE test ADD COLUMN name STRING FAMILY f1; - ~~~ - -- Use the `CREATE IF NOT EXISTS FAMILY` keyword to assign a new column to an **existing family or, if the family doesn't exist, to a new family**. For example, the following would assign the new column to the existing `f1` family; if that family didn't exist, it would create a new family and assign the column to it: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE test ADD COLUMN name STRING CREATE IF NOT EXISTS FAMILY f1; - ~~~ - -- If a column is added to a table and the family is not specified, it will be added to the first column family. For example, the following would add the new column to the `f1` family, since that is the first column family: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE test ADD COLUMN last_name STRING; - ~~~ - -## See also - -- [`CREATE TABLE`](create-table.html) -- [`ADD COLUMN`](add-column.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/comment-on.md b/src/current/v19.2/comment-on.md deleted file mode 100644 index cb28e28561a..00000000000 --- a/src/current/v19.2/comment-on.md +++ /dev/null @@ -1,119 +0,0 @@ ---- -title: COMMENT ON -summary: The COMMENT ON statement associates comments to databases, tables, or columns. -toc: true ---- - -The `COMMENT ON` [statement](sql-statements.html) associates comments to [databases](create-database.html), [tables](create-table.html), or [columns](add-column.html). - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the object they are commenting on. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/comment.html %}
      - -## Parameters - - Parameter | Description -------------|-------------- -`database_name` | The name of the database you are commenting on. -`table_name` | The name of the table you are commenting on. -`column_name` | The name of the column you are commenting on. -`comment_text` | The comment ([`STRING`](string.html)) you are associating to the object. - -## Examples - -{% include {{page.version.version}}/sql/movr-statements.md %} - -### Add a comment to a database - -To add a comment to a database: - -{% include copy-clipboard.html %} -~~~ sql -> COMMENT ON DATABASE movr IS 'This database contains information about users, vehicles, and rides.'; -~~~ - -To view database comments, use [`SHOW DATABASES`](show-databases.html): - -{% include copy-clipboard.html %} -~~~ sql -> SHOW DATABASES WITH COMMENT; -~~~ - -~~~ - database_name | comment -+---------------+-------------------------------------------------------------------+ - defaultdb | NULL - movr | This database contains information about users, vehicles, and rides. - postgres | NULL - system | NULL -(4 rows) -~~~ - -### Add a comment to a table - -To add a comment to a table: - -{% include copy-clipboard.html %} -~~~ sql -> COMMENT ON TABLE vehicles IS 'This table contains information about vehicles registered with MovR.'; -~~~ - -To view table comments, use [`SHOW TABLES`](show-tables.html): - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM movr WITH COMMENT; -~~~ - -~~~ - table_name | comment -+----------------------------+----------------------------------------------------------------------+ - users | - vehicles | This table contains information about vehicles registered with MovR. - rides | - vehicle_location_histories | - promo_codes | - user_promo_codes | -(6 rows) -~~~ - -### Add a comment to a column - -To add a comment to a column: - -{% include copy-clipboard.html %} -~~~ sql -> COMMENT ON COLUMN users.credit_card IS 'This column contains user payment information.'; -~~~ - -To view column comments, use [`SHOW COLUMNS`](show-columns.html): - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM users WITH COMMENT; -~~~ - -~~~ - column_name | data_type | is_nullable | column_default | generation_expression | indices | is_hidden | comment -+-------------+-----------+-------------+----------------+-----------------------+-----------+-----------+------------------------------------------------+ - id | UUID | false | NULL | | {primary} | false | NULL - city | VARCHAR | false | NULL | | {primary} | false | NULL - name | VARCHAR | true | NULL | | {} | false | NULL - address | VARCHAR | true | NULL | | {} | false | NULL - credit_card | VARCHAR | true | NULL | | {} | false | This column contains user payment information. -(5 rows) -~~~ - - -## See also - -- [`CREATE DATABASE`](create-database.html) -- [`CREATE TABLE`](create-table.html) -- [`ADD COLUMN`](add-column.html) -- [`SHOW TABLES`](show-tables.html) -- [Other SQL Statements](sql-statements.html) -- [dBeaver](dbeaver.html) diff --git a/src/current/v19.2/commit-transaction.md b/src/current/v19.2/commit-transaction.md deleted file mode 100644 index 14952b07b6c..00000000000 --- a/src/current/v19.2/commit-transaction.md +++ /dev/null @@ -1,83 +0,0 @@ ---- -title: COMMIT -summary: Commit a transaction with the COMMIT statement in CockroachDB. -toc: true ---- - -The `COMMIT` [statement](sql-statements.html) commits the current [transaction](transactions.html) or, when using [advanced client-side transaction retries](advanced-client-side-transaction-retries.html), clears the connection to allow new transactions to begin. - -When using [advanced client-side transaction retries](advanced-client-side-transaction-retries.html), statements issued after [`SAVEPOINT`](savepoint.html) are committed when [`RELEASE SAVEPOINT`](release-savepoint.html) is issued instead of `COMMIT`. However, you must still issue a `COMMIT` statement to clear the connection for the next transaction. - -For non-retryable transactions, if statements in the transaction [generated any errors](transactions.html#error-handling), `COMMIT` is equivalent to `ROLLBACK`, which aborts the transaction and discards *all* updates made by its statements. - - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/commit_transaction.html %}
      - -## Required privileges - -No [privileges](authorization.html#assign-privileges) are required to commit a transaction. However, privileges are required for each statement within a transaction. - -## Aliases - -In CockroachDB, `END` is an alias for the `COMMIT` statement. - -## Example - -### Commit a transaction - -How you commit transactions depends on how your application handles [transaction retries](transactions.html#transaction-retries). - -#### Client-side retryable transactions - -When using [advanced client-side transaction retries](advanced-client-side-transaction-retries.html), statements are committed by [`RELEASE SAVEPOINT`](release-savepoint.html). `COMMIT` itself only clears the connection for the next transaction. - -{% include copy-clipboard.html %} -~~~ sql -> BEGIN; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SAVEPOINT cockroach_restart; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE products SET inventory = 0 WHERE sku = '8675309'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders (customer, sku, status) VALUES (1001, '8675309', 'new'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> RELEASE SAVEPOINT cockroach_restart; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> COMMIT; -~~~ - -{{site.data.alerts.callout_danger}}This example assumes you're using client-side intervention to handle transaction retries.{{site.data.alerts.end}} - -#### Automatically retried transactions - -If you are using transactions that CockroachDB will [automatically retry](transactions.html#automatic-retries) (i.e., all statements sent in a single batch), commit the transaction with `COMMIT`. - -{% include copy-clipboard.html %} -~~~ sql -> BEGIN; UPDATE products SET inventory = 100 WHERE = '8675309'; UPDATE products SET inventory = 100 WHERE = '8675310'; COMMIT; -~~~ - -## See also - -- [Transactions](transactions.html) -- [`BEGIN`](begin-transaction.html) -- [`RELEASE SAVEPOINT`](release-savepoint.html) -- [`ROLLBACK`](rollback-transaction.html) -- [`SAVEPOINT`](savepoint.html) diff --git a/src/current/v19.2/common-errors.md b/src/current/v19.2/common-errors.md deleted file mode 100644 index 4082136f7e2..00000000000 --- a/src/current/v19.2/common-errors.md +++ /dev/null @@ -1,244 +0,0 @@ ---- -title: Common Errors -summary: Understand and resolve common error messages written to stderr or logs. -toc: false ---- - -This page helps you understand and resolve error messages written to `stderr` or your [logs](debug-and-error-logs.html). - -| Topic | Message | -|----------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| Client connection | [`connection refused`](#connection-refused) | -| Client connection | [`node is running secure mode, SSL connection required`](#node-is-running-secure-mode-ssl-connection-required) | -| Transaction retries | [`restart transaction`](#restart-transaction) | -| Node startup | [`node belongs to cluster but is attempting to connect to a gossip network for cluster `](#node-belongs-to-cluster-cluster-id-but-is-attempting-to-connect-to-a-gossip-network-for-cluster-another-cluster-id) | -| Node configuration | [`clock synchronization error: this node is more than 500ms away from at least half of the known nodes`](#clock-synchronization-error-this-node-is-more-than-500ms-away-from-at-least-half-of-the-known-nodes) | -| Node configuration | [`open file descriptor limit of is under the minimum required `](#open-file-descriptor-limit-of-number-is-under-the-minimum-required-number) | -| Replication | [`replicas failing with "0 of 1 store with an attribute matching []; likely not enough nodes in cluster"`](#replicas-failing-with-0-of-1-store-with-an-attribute-matching-likely-not-enough-nodes-in-cluster) | -| Split failed | [`split failed while applying backpressure; are rows updated in a tight loop?`](#split-failed-while-applying-backpressure-are-rows-updated-in-a-tight-loop) | -| Deadline exceeded | [`context deadline exceeded`](#context-deadline-exceeded) | -| Ambiguous results | [`result is ambiguous`](#result-is-ambiguous) | -| Time zone data | [`invalid value for parameter "TimeZone"`](#invalid-value-for-parameter-timezone) | - -## connection refused - -This message indicates a client is trying to connect to a node that is either not running or is not listening on the specified interfaces (i.e., hostname or port). - -To resolve this issue, do one of the following: - -- If the node hasn't yet been started, [start the node](cockroach-start.html). -- If you specified a [`--listen-addr` and/or a `--advertise-addr` flag](cockroach-start.html#networking) when starting the node, you must include the specified IP address/hostname and port with all other [`cockroach` commands](cockroach-commands.html) or change the `COCKROACH_HOST` environment variable. - -If you're not sure what the IP address/hostname and port values might have been, you can look in the node's [logs](debug-and-error-logs.html). If necessary, you can also end the `cockroach` process, and then restart the node: - -{% include copy-clipboard.html %} -~~~ shell -$ pkill cockroach -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start [flags] -~~~ - -## node is running secure mode, SSL connection required - -This message indicates that the cluster is using TLS encryption to protect network communication, and the client is trying to open a connection without using the required TLS certificates. - -To resolve this issue, use the [`cockroach cert create-client`](cockroach-cert.html) command to generate a client certificate and key for the user trying to connect. For a secure deployment walkthrough, including generating security certificates and connecting clients, see [Manual Deployment](manual-deployment.html). - -## restart transaction - -Messages with the error code `40001` and the string `restart transaction` indicate that a transaction failed because it conflicted with another concurrent or recent transaction accessing the same data. The transaction needs to be retried by the client. See [client-side transaction retries](transactions.html#client-side-intervention) for more details. - -Several different types of transaction retry errors are described below: - -- [`read within uncertainty interval`](#read-within-uncertainty-interval) -- [`transaction deadline exceeded`](#transaction-deadline-exceeded) - -{{site.data.alerts.callout_info}} -Your application's retry logic does not need to distinguish between these types of errors. They are listed here for reference. -{{site.data.alerts.end}} - -{{site.data.alerts.callout_success}} -To understand how transactions work in CockroachDB, and why transaction retries are necessary to maintain serializable isolation in a distributed database, see: - -- [Transaction Layer](architecture/transaction-layer.html) -- [Life of a Distributed Transaction](architecture/life-of-a-distributed-transaction.html) -{{site.data.alerts.end}} - -### read within uncertainty interval - -(Error string includes: `ReadWithinUncertaintyIntervalError`) - -Uncertainty errors can occur when two transactions which start on different gateway nodes attempt to operate on the same data at close to the same time. The uncertainty comes from the fact that we cannot tell which one started first - the clocks on the two gateway nodes may not be perfectly in sync. - -For example, if the clock on node A is ahead of the clock on node B, a transaction started on node A may be able to commit a write with a timestamp that is still in the "future" from the perspective of node B. A later transaction that starts on node B should be able to see the earlier write from node A, even if B's clock has not caught up to A. The "read within uncertainty interval" occurs if we discover this situation in the middle of a transaction, when it is too late for the database to handle it automatically. When node B's transaction retries, it will unambiguously occur after the transaction from node A. - -Note that as long as the [client-side retry protocol](transactions.html#client-side-intervention) is followed, a transaction that has restarted once is much less likely to hit another uncertainty error, and the [`--max-offset` option](cockroach-start.html#flags) provides an upper limit on how long a transaction can continue to restart due to uncertainty. - -When errors like this occur, the application has the following options: - -- Prefer consistent historical reads using [AS OF SYSTEM TIME](as-of-system-time.html) to reduce contention. -- Design the schema and queries to reduce contention. For information on how to avoid contention, see [Understanding and Avoiding Transaction Contention](performance-best-practices-overview.html#understanding-and-avoiding-transaction-contention). -- Be prepared to retry on uncertainty (and other) errors. For more information, see [Transaction retries](transactions.html#transaction-retries). - -{{site.data.alerts.callout_info}} -Uncertainty errors are a form of transaction conflict. For more information about transaction conflicts, see [Transaction conflicts](architecture/transaction-layer.html#transaction-conflicts). -{{site.data.alerts.end}} - -### transaction deadline exceeded - -Errors which were previously reported to the client as opaque `TransactionStatusError`s are now transaction retry errors with the error message "transaction deadline exceeded" and error code `40001`. - -This error can occur for long-running transactions (with execution time on the order of minutes) that also experience conflicts with other transactions and thus attempt to commit at a timestamp different than their original timestamp. If the timestamp at which the transaction attempts to commit is above a "deadline" imposed by the various schema elements that the transaction has used (i.e., table structures), then this error might get returned to the client. - -When this error occurs, the application must retry the transaction. For more information about how to retry transactions, see [Transaction retries](transactions.html#transaction-retries). - -{{site.data.alerts.callout_info}} -For more information about the mechanics of the transaction conflict resolution process described above, see [Life of a Distributed Transaction](architecture/life-of-a-distributed-transaction.html). -{{site.data.alerts.end}} - - - - - -## node belongs to cluster \ but is attempting to connect to a gossip network for cluster \ - -This message usually indicates that a node tried to connect to a cluster, but the node is already a member of a different cluster. This is determined by metadata in the node's data directory. To resolve this issue, do one of the following: - -- Choose a different directory to store the CockroachDB data: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start [flags] --store=[new directory] --join=[cluster host]:26257 - ~~~ - -- Remove the existing directory and start a node joining the cluster again: - - {% include copy-clipboard.html %} - ~~~ shell - $ rm -r cockroach-data/ - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start [flags] --join=[cluster host]:26257 - ~~~ - -## clock synchronization error: this node is more than 500ms away from at least half of the known nodes - -This error indicates that a node has spontaneously shut down because it detected that its clock is out of sync with at least half of the other nodes in the cluster by 80% of the maximum offset allowed (500ms by default). CockroachDB requires moderate levels of [clock synchronization](recommended-production-settings.html#clock-synchronization) to preserve data consistency, so the node shutting down in this way avoids the risk of consistency anomalies. - -To prevent this from happening, you should run clock synchronization software on each node. For guidance on synchronizing clocks, see the tutorial for your deployment environment: - -Environment | Recommended Approach -------------|--------------------- -[Manual](deploy-cockroachdb-on-premises.html#step-1-synchronize-clocks) | Use NTP with Google's external NTP service. -[AWS](deploy-cockroachdb-on-aws.html#step-3-synchronize-clocks) | Use the Amazon Time Sync Service. -[Azure](deploy-cockroachdb-on-microsoft-azure.html#step-3-synchronize-clocks) | Disable Hyper-V time synchronization and use NTP with Google's external NTP service. -[Digital Ocean](deploy-cockroachdb-on-digital-ocean.html#step-2-synchronize-clocks) | Use NTP with Google's external NTP service. -[GCE](deploy-cockroachdb-on-google-cloud-platform.html#step-3-synchronize-clocks) | Use NTP with Google's internal NTP service. - -## open file descriptor limit of \ is under the minimum required \ - -CockroachDB can use a large number of open file descriptors, often more than is available by default. This message indicates that the machine on which a CockroachDB node is running is under CockroachDB's recommended limits. - -For more details on CockroachDB's file descriptor limits and instructions on increasing the limit on various platforms, see [File Descriptors Limit](recommended-production-settings.html#file-descriptors-limit). - -## replicas failing with "0 of 1 store with an attribute matching []; likely not enough nodes in cluster - -### When running a single-node cluster - -When running a single-node CockroachDB cluster, an error about replicas failing will eventually show up in the node's log files, for example: - -~~~ shell -E160407 09:53:50.337328 storage/queue.go:511 [replicate] 7 replicas failing with "0 of 1 store with an attribute matching []; likely not enough nodes in cluster" -~~~ - -This happens because CockroachDB expects three nodes by default. If you do not intend to add additional nodes, you can stop this error by using [`ALTER RANGE ... CONFIGURE ZONE`](configure-zone.html) to update your default zone configuration to expect only one node: - -{% include copy-clipboard.html %} -~~~ shell -# Insecure cluster: -$ cockroach sql --execute="ALTER RANGE default CONFIGURE ZONE USING num_replicas=1;" --insecure -~~~ - -{% include copy-clipboard.html %} -~~~ shell -# Secure cluster: -$ cockroach sql --execute="ALTER RANGE default CONFIGURE ZONE USING num_replicas=1;" --certs-dir=[path to certs directory] -~~~ - -The zone's replica count is reduced to 1. For more information, see [`ALTER RANGE ... CONFIGURE ZONE`](configure-zone.html) and [Configure Replication Zones](configure-replication-zones.html). - -### When running a multi-node cluster - -When running a multi-node CockroachDB cluster, if you see an error like the one above about replicas failing, some nodes might not be able to talk to each other. For recommended actions, see [Cluster Setup Troubleshooting](cluster-setup-troubleshooting.html#replication-issues). - -## split failed while applying backpressure; are rows updated in a tight loop? - -In CockroachDB, a table row is stored on disk as a key-value pair. Whenever the row is updated, CockroachDB also stores a distinct version of the key-value pair to enable concurrent request processing while guaranteeing consistency (see [multi-version concurrency control (MVCC)](architecture/storage-layer.html#mvcc)). All versions of a key-value pair belong to a larger ["range"](architecture/overview.html#terms) of the total key space, and the historical versions remain until the garbage collection period defined by the `gc.ttlseconds` variable in the applicable [zone configuration](configure-replication-zones.html#gc-ttlseconds) has passed (25 hours by default). Once a range reaches a size threshold (64 MiB by default), CockroachDB splits the range into two ranges. However, this message indicates that a range cannot be split as intended. - -One possible cause is that the range consists only of MVCC version data due to a row being repeatedly updated, and the range cannot be split because doing so would spread MVCC versions for a single row across multiple ranges. - -To resolve this issue, make sure you are not repeatedly updating a single row. If frequent updates of a row are necessary, consider one of the following: - -- Reduce the `gc.ttlseconds` variable in the applicable [zone configuration](configure-replication-zones.html#gc-ttlseconds) to reduce the garbage collection period and prevent such a large build-up of historical values. -- If a row contains large columns that are not being updated with other columns, put the large columns in separate [column families](column-families.html). - -## context deadline exceeded - -This message occurs when a component of CockroachDB gives up because it was relying on another component that has not behaved as expected, for example, another node dropped a network connection. To investigate further, look in the node's logs for the primary failure that is the root cause. - -## result is ambiguous - -In a distributed system, some errors can have ambiguous results. For -example, if you receive a `connection closed` error while processing a -`COMMIT` statement, you cannot tell whether the transaction -successfully committed or not. These errors are possible in any -database, but CockroachDB is somewhat more likely to produce them than -other databases because ambiguous results can be caused by failures -between the nodes of a cluster. These errors are reported with the -PostgreSQL error code `40003` (`statement_completion_unknown`) and the -message `result is ambiguous`. - -Ambiguous errors can be caused by nodes crashing, network failures, or -timeouts. If you experience a lot of these errors when things are -otherwise stable, look for performance issues. Note that ambiguity is -only possible for the last statement of a transaction (`COMMIT` or -`RELEASE SAVEPOINT`) or for statements outside a transaction. If a connection drops during a transaction that has not yet tried to commit, the transaction will definitely be aborted. - -In general, you should handle ambiguous errors the same way as -`connection closed` errors. If your transaction is -[idempotent](https://en.wikipedia.org/wiki/Idempotence#Computer_science_meaning), -it is safe to retry it on ambiguous errors. `UPSERT` operations are -typically idempotent, and other transactions can be written to be -idempotent by verifying the expected state before performing any -writes. Increment operations such as `UPDATE my_table SET x=x+1 WHERE -id=$1` are typical examples of operations that cannot easily be made -idempotent. If your transaction is not idempotent, then you should -decide whether to retry or not based on whether it would be better for -your application to apply the transaction twice or return an error to -the user. - -## invalid value for parameter "TimeZone" - -This error means that the machine running the CockroachDB node is missing time zone data and therefore cannot resolve location-based time zone names. - -To resolve this issue on Linux, install the [`tzdata`](https://www.iana.org/time-zones) library (sometimes called `tz` or `zoneinfo`). - -To resolve this issue on Windows, download Go's official [zoneinfo.zip](https://github.com/golang/go/raw/master/lib/time/zoneinfo.zip) and set the `ZONEINFO` environment variable to point to the zip file. For step-by-step guidance on setting environment variables on Windows, see this [external article](https://www.techjunkie.com/environment-variables-windows-10/). - -It's important for all nodes to have the same version of this data, so make sure to do this across all nodes in the cluster. - -For details about other libraries the CockroachDB binary depends on, see [Dependencies](recommended-production-settings.html#dependencies). - -## Something else? - -Try searching the rest of our docs for answers or using our other [support resources](support-resources.html), including: - -- [CockroachDB Community Forum](https://forum.cockroachlabs.com) -- [CockroachDB Community Slack](https://cockroachdb.slack.com) -- [StackOverflow](http://stackoverflow.com/questions/tagged/cockroachdb) -- [CockroachDB Support Portal](https://support.cockroachlabs.com) diff --git a/src/current/v19.2/common-table-expressions.md b/src/current/v19.2/common-table-expressions.md deleted file mode 100644 index 3d08ed0134e..00000000000 --- a/src/current/v19.2/common-table-expressions.md +++ /dev/null @@ -1,181 +0,0 @@ ---- -title: Common Table Expressions -summary: Common Table Expressions (CTEs) simplify the definition and use of subqueries -toc: true -toc_not_nested: true ---- - -Common Table Expressions, or CTEs, provide a shorthand name to a -possibly complex [subquery](subqueries.html) before it is used in a -larger query context. This improves readability of the SQL code. - -CTEs can be used in combination with [`SELECT` -clauses](select-clause.html) and [`INSERT`](insert.html), -[`DELETE`](delete.html), [`UPDATE`](update.html) and -[`UPSERT`](upsert.html) statements. - - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/with_clause.html %}
      - -
      - -## Parameters - -Parameter | Description -----------|------------ -`table_alias_name` | The name to use to refer to the common table expression from the accompanying query or statement. -`name` | A name for one of the columns in the newly defined common table expression. -`preparable_stmt` | The statement or subquery to use as common table expression. - -## Overview - -{{site.data.alerts.callout_info}} -The examples on this page use MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB SQL statements. To follow along, run [`cockroach demo`](cockroach-demo.html) from the command line to start a temporary, in-memory cluster with the `movr` dataset preloaded. - -For more information about the MovR example application and dataset, see [MovR: A Global Vehicle-sharing App](movr.html). -{{site.data.alerts.end}} - -A query or statement of the form `WITH x AS y IN z` creates the -temporary table name `x` for the results of the subquery `y`, to be -reused in the context of the query `z`. - -For example: - -{% include copy-clipboard.html %} -~~~ sql -> WITH r AS (SELECT * FROM rides WHERE revenue > 98) - SELECT * FROM users AS u, r WHERE r.rider_id = u.id; -~~~ - -~~~ - id | city | name | address | credit_card | id | city | vehicle_city | rider_id | vehicle_id | start_address | end_address | start_time | end_time | revenue -+--------------------------------------+---------------+------------------+--------------------------------+-------------+--------------------------------------+---------------+---------------+--------------------------------------+--------------------------------------+-----------------------------------+---------------------------+---------------------------+---------------------------+---------+ - ae147ae1-47ae-4800-8000-000000000022 | amsterdam | Tyler Dalton | 88194 Angela Gardens Suite 94 | 4443538758 | bbe76c8b-4395-4000-8000-00000000016f | amsterdam | amsterdam | ae147ae1-47ae-4800-8000-000000000022 | aaaaaaaa-aaaa-4800-8000-00000000000a | 45295 Brewer View Suite 52 | 62188 Jade Causeway | 2018-12-17 03:04:05+00:00 | 2018-12-17 13:04:05+00:00 | 99.00 - c7ae147a-e147-4000-8000-000000000027 | paris | Tina Miller | 97521 Mark Extensions | 8880478663 | d5810624-dd2f-4800-8000-0000000001a1 | paris | paris | c7ae147a-e147-4000-8000-000000000027 | cccccccc-cccc-4000-8000-00000000000c | 47713 Reynolds Mountains Suite 39 | 1417 Stephanie Villages | 2018-12-17 03:04:05+00:00 | 2018-12-18 22:04:05+00:00 | 99.00 - 75c28f5c-28f5-4400-8000-000000000017 | san francisco | William Wood | 36021 Steven Cove Apt. 89 | 5669281259 | 8ac08312-6e97-4000-8000-00000000010f | san francisco | san francisco | 75c28f5c-28f5-4400-8000-000000000017 | 77777777-7777-4800-8000-000000000007 | 84407 Tony Crest | 55336 Jon Manors | 2018-12-10 03:04:05+00:00 | 2018-12-11 13:04:05+00:00 | 99.00 - 8a3d70a3-d70a-4000-8000-00000000001b | san francisco | Jessica Martinez | 96676 Jennifer Knolls Suite 91 | 1601930189 | 7d70a3d7-0a3d-4000-8000-0000000000f5 | san francisco | san francisco | 8a3d70a3-d70a-4000-8000-00000000001b | 77777777-7777-4800-8000-000000000007 | 78978 Stevens Ramp Suite 8 | 7340 Alison Field Apt. 44 | 2018-12-19 03:04:05+00:00 | 2018-12-21 10:04:05+00:00 | 99.00 - 47ae147a-e147-4000-8000-00000000000e | washington dc | Patricia Herrera | 80588 Perez Camp | 6812041796 | 4083126e-978d-4000-8000-00000000007e | washington dc | washington dc | 47ae147a-e147-4000-8000-00000000000e | 44444444-4444-4400-8000-000000000004 | 33055 Julie Dale Suite 93 | 17280 Jill Drives | 2019-01-01 03:04:05+00:00 | 2019-01-01 14:04:05+00:00 | 99.00 -(5 rows) -~~~ - -In this example, the `WITH` clause defines the temporary name `r` for -the subquery over `rides`, and that name becomes a valid table name -for use in any [table expression](table-expressions.html) of the -subsequent `SELECT` clause. - -This query is equivalent to, but arguably simpler to read than: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users AS u, (SELECT * FROM rides WHERE revenue > 98) AS r - WHERE r.rider_id = u.id; -~~~ - -It is also possible to define multiple common table expressions -simultaneously with a single `WITH` clause, separated by commas. Later -subqueries can refer to earlier subqueries by name. For example, the -following query is equivalent to the two examples above: - -{% include copy-clipboard.html %} -~~~ sql -> WITH r AS (SELECT * FROM rides WHERE revenue > 98), - results AS (SELECT * FROM users AS u, r WHERE r.rider_id = u.id) - SELECT * FROM results; -~~~ - -In this example, the second CTE `results` refers to the first CTE `r` -by name. The final query refers to the CTE `results`. - -## Nested `WITH` clauses - -It is possible to use a `WITH` clause in a subquery, or even a `WITH` clause within another `WITH` clause. For example: - -{% include copy-clipboard.html %} -~~~ sql -> WITH u AS - (SELECT * FROM - (WITH u_tab AS (SELECT * FROM users) SELECT * FROM u_tab)) - SELECT * FROM u; -~~~ - -When analyzing [table expressions](table-expressions.html) that -mention a CTE name, CockroachDB will choose the CTE definition that is -closest to the table expression. For example: - -{% include copy-clipboard.html %} -~~~ sql -> WITH - u AS (SELECT * FROM users), - v AS (WITH u AS (SELECT * from vehicles) SELECT * FROM u) - SELECT * FROM v; -~~~ - -In this example, the inner subquery `SELECT * FROM v` will select from -table `vehicles` (closest `WITH` clause), not from table `users`. - -## Data modifying statements - -It is possible to use a [data-modifying statement](sql-statements.html#data-manipulation-statements) (`INSERT`, `DELETE`, -etc.) as a common table expression. - -For example: - -{% include copy-clipboard.html %} -~~~ sql -> WITH final_code AS - (INSERT INTO promo_codes(code, description, rules) - VALUES ('half_off', 'Half-price ride!', '{"type": "percent_discount", "value": "50%"}'), ('free_ride', 'Free ride!', '{"type": "percent_discount", "value": "100%"}') - returning rules) - SELECT rules FROM final_code; -~~~ - -~~~ - rules -+-----------------------------------------------+ - {"type": "percent_discount", "value": "50%"} - {"type": "percent_discount", "value": "100%"} -(2 rows) -~~~ - -{{site.data.alerts.callout_info}} -If a common table expression contains -a data-modifying statement (INSERT, DELETE, -etc.), the modifications are performed fully even if only part -of the results are used, e.g., with LIMIT. See Data -Writes in Subqueries for details. -{{site.data.alerts.end}} - -## Reusing common table expressions - -New in v19.2: You can reference a CTE multiple times in a single query, using a `WITH` operator. - -For example: - -{% include copy-clipboard.html %} -~~~ sql -> WITH - users_ny AS (SELECT name, id FROM users WHERE city='new york'), - vehicles_ny AS (SELECT type, id, owner_id FROM vehicles WHERE city='new york') - SELECT * FROM users_ny JOIN vehicles_ny ON users_ny.id = vehicles_ny.owner_id; -~~~ - -~~~ - name | id | type | id | owner_id -+------------------+--------------------------------------+------------+--------------------------------------+--------------------------------------+ - James Hamilton | 051eb851-eb85-4ec0-8000-000000000001 | skateboard | 00000000-0000-4000-8000-000000000000 | 051eb851-eb85-4ec0-8000-000000000001 - Catherine Nelson | 147ae147-ae14-4b00-8000-000000000004 | scooter | 11111111-1111-4100-8000-000000000001 | 147ae147-ae14-4b00-8000-000000000004 -(2 rows) -~~~ - -In this single query, you define two CTE's and then reuse them in a table join. - -## See also - -- [Subqueries](subqueries.html) -- [Selection Queries](selection-queries.html) -- [Table Expressions](table-expressions.html) -- [`EXPLAIN`](explain.html) diff --git a/src/current/v19.2/computed-columns.md b/src/current/v19.2/computed-columns.md deleted file mode 100644 index 40c6d40aaf4..00000000000 --- a/src/current/v19.2/computed-columns.md +++ /dev/null @@ -1,78 +0,0 @@ ---- -title: Computed Columns -summary: A computed column stores data generated by an expression included in the column definition. -toc: true ---- - -A computed column stores data generated from other columns by a [scalar expression](scalar-expressions.html) included in the column definition. - - -## Why use computed columns? - -Computed columns are especially useful when used with [partitioning](partitioning.html), [`JSONB`](jsonb.html) columns, or [secondary indexes](indexes.html). - -- **Partitioning** requires that partitions are defined using columns that are a prefix of the [primary key](primary-key.html). In the case of geo-partitioning, some applications will want to collapse the number of possible values in this column, to make certain classes of queries more performant. For example, if a users table has a country and state column, then you can make a stored computed column locality with a reduced domain for use in partitioning. For more information, see the [partitioning example](#create-a-table-with-geo-partitions-and-a-computed-column) below. - -- **JSONB** columns are used for storing semi-structured `JSONB` data. When the table's primary information is stored in `JSONB`, it's useful to index a particular field of the `JSONB` document. In particular, computed columns allow for the following use case: a two-column table with a `PRIMARY KEY` column and a `payload` column, whose primary key is computed as some field from the `payload` column. This alleviates the need to manually separate your primary keys from your JSON blobs. For more information, see the [`JSONB` example](#create-a-table-with-a-jsonb-column-and-a-computed-column) below. - -- **Secondary indexes** can be created on computed columns, which is especially useful when a table is frequently sorted. See the [secondary indexes example](#create-a-table-with-a-secondary-index-on-a-computed-column) below. - -## Considerations - -Computed columns: - -- Cannot be used to generate other computed columns. -- Cannot be a [foreign key](foreign-key.html) reference. -- Behave like any other column, with the exception that they cannot be written to directly. -- Are mutually exclusive with [`DEFAULT`](default-value.html). - -## Creation - -To define a computed column, use the following syntax: - -~~~ -column_name AS () STORED -~~~ - -Parameter | Description -----------|------------ -`column_name` | The [name/identifier](keywords-and-identifiers.html#identifiers) of the computed column. -`` | The [data type](data-types.html) of the computed column. -`` | The pure [scalar expression](scalar-expressions.html) used to compute column values. Any functions marked as `impure`, such as `now()` or `nextval()` cannot be used. -`STORED` | _(Required)_ The computed column is stored alongside other columns. - -## Examples - -### Create a table with a computed column - -{% include {{ page.version.version }}/computed-columns/simple.md %} - -### Create a table with geo-partitions and a computed column - -{% include {{ page.version.version }}/computed-columns/partitioning.md %} The `locality` values can then be used for geo-partitioning. - -### Create a table with a `JSONB` column and a computed column - -{% include {{ page.version.version }}/computed-columns/jsonb.md %} - -### Create a table with a secondary index on a computed column - -{% include {{ page.version.version }}/computed-columns/secondary-index.md %} - -### Add a computed column to an existing table - -{% include {{ page.version.version }}/computed-columns/add-computed-column.md %} - -For more information, see [`ADD COLUMN`](add-column.html). - -### Convert a computed column into a regular column - -{% include {{ page.version.version }}/computed-columns/convert-computed-column.md %} - -## See also - -- [Scalar Expressions](scalar-expressions.html) -- [Information Schema](information-schema.html) -- [`CREATE TABLE`](create-table.html) -- [`JSONB`](jsonb.html) -- [Define Table Partitions (Enterprise)](partitioning.html) diff --git a/src/current/v19.2/configure-replication-zones.md b/src/current/v19.2/configure-replication-zones.md deleted file mode 100644 index 37d822e0cae..00000000000 --- a/src/current/v19.2/configure-replication-zones.md +++ /dev/null @@ -1,688 +0,0 @@ ---- -title: Configure Replication Zones -summary: In CockroachDB, you use replication zones to control the number and location of replicas for specific sets of data. -keywords: ttl, time to live, availability zone -toc: true ---- - -Replication zones give you the power to control what data goes where in your CockroachDB cluster. Specifically, they are used to control the number and location of replicas for data belonging to the following objects: - -- Databases -- Tables -- Rows ([enterprise-only](enterprise-licensing.html)) -- Indexes ([enterprise-only](enterprise-licensing.html)) -- All data in the cluster, including internal system data ([via the default replication zone](#view-the-default-replication-zone)) - -For each of the above objects you can control: - -- How many copies of each range to spread through the cluster. -- Which constraints are applied to which data, e.g., "table X's data can only be stored in the German datacenters". -- The maximum size of ranges (how big ranges get before they are split). -- How long old data is kept before being garbage collected. -- Where you would like the leaseholders for certain ranges to be located, e.g., "for ranges that are already constrained to have at least one replica in `region=us-west`, also try to put their leaseholders in `region=us-west`". - -This page explains how replication zones work and how to use the [`CONFIGURE ZONE`](configure-zone.html) statement to manage them. - -{{site.data.alerts.callout_info}} -Currently, only members of the `admin` role can configure replication zones. By default, the `root` user belongs to the `admin` role. -{{site.data.alerts.end}} - -## Overview - -Every [range](architecture/overview.html#glossary) in the cluster is part of a replication zone. Each range's zone configuration is taken into account as ranges are rebalanced across the cluster to ensure that any constraints are honored. - -When a cluster starts, there are two categories of replication zone: - -1. Pre-configured replication zones that apply to internal system data. -2. A single default replication zone that applies to the rest of the cluster. - -You can adjust these pre-configured zones as well as add zones for individual databases, tables, rows, and secondary indexes as needed. Note that adding zones for rows and secondary indexes is [enterprise-only](enterprise-licensing.html). - -For example, you might rely on the [default zone](#view-the-default-replication-zone) to spread most of a cluster's data across all of your datacenters, but [create a custom replication zone for a specific database](#create-a-replication-zone-for-a-database) to make sure its data is only stored in certain datacenters and/or geographies. - -## Replication zone levels - -There are five replication zone levels for [**table data**](architecture/distribution-layer.html#table-data) in a cluster, listed from least to most granular: - -Level | Description -------|------------ -Cluster | CockroachDB comes with a pre-configured `default` replication zone that applies to all table data in the cluster not constrained by a database, table, or row-specific replication zone. This zone can be adjusted but not removed. See [View the Default Replication Zone](#view-the-default-replication-zone) and [Edit the Default Replication Zone](#edit-the-default-replication-zone) for more details. -Database | You can add replication zones for specific databases. See [Create a Replication Zone for a Database](#create-a-replication-zone-for-a-database) for more details. -Table | You can add replication zones for specific tables. See [Create a Replication Zone for a Table](#create-a-replication-zone-for-a-table). -Index ([Enterprise-only](enterprise-licensing.html)) | The [secondary indexes](indexes.html) on a table will automatically use the replication zone for the table. However, with an enterprise license, you can add distinct replication zones for secondary indexes. See [Create a Replication Zone for a Secondary Index](#create-a-replication-zone-for-a-secondary-index) for more details. -Row ([Enterprise-only](enterprise-licensing.html)) | You can add replication zones for specific rows in a table or secondary index by [defining table partitions](partitioning.html). See [Create a Replication Zone for a Table Partition](#create-a-replication-zone-for-a-partition) for more details. - -### For system data - -In addition, CockroachDB stores internal [**system data**](architecture/distribution-layer.html#monolithic-sorted-map-structure) in what are called system ranges. There are two replication zone levels for this internal system data, listed from least to most granular: - -Level | Description -------|------------ -Cluster | The `default` replication zone mentioned above also applies to all system ranges not constrained by a more specific replication zone. -System Range | CockroachDB comes with pre-configured replication zones for important system ranges, such as the "meta" and "liveness" ranges. If necessary, you can add replication zones for the "timeseries" range and other system ranges as well. Editing replication zones for system ranges may override settings from `default`. See [Create a Replication Zone for a System Range](#create-a-replication-zone-for-a-system-range) for more details.

      CockroachDB also comes with pre-configured replication zones for the internal `system` database and the `system.jobs` table, which stores metadata about long-running jobs such as schema changes and backups. - -### Level priorities - -When replicating data, whether table or system, CockroachDB always uses the most granular replication zone available. For example, for a piece of user data: - -1. If there's a replication zone for the row, CockroachDB uses it. -2. If there's no applicable row replication zone and the row is from a secondary index, CockroachDB uses the secondary index replication zone. -3. If the row isn't from a secondary index or there is no applicable secondary index replication zone, CockroachDB uses the table replication zone. -4. If there's no applicable table replication zone, CockroachDB uses the database replication zone. -5. If there's no applicable database replication zone, CockroachDB uses the `default` cluster-wide replication zone. - -## Manage replication zones - -Use the [`CONFIGURE ZONE`](configure-zone.html) statement to [add](#create-a-replication-zone-for-a-system-range), [modify](#edit-the-default-replication-zone), [reset](#reset-a-replication-zone), and [remove](#remove-a-replication-zone) replication zones. - -### Replication zone variables - -Use the [`ALTER ... CONFIGURE ZONE`](configure-zone.html) [statement](sql-statements.html) to set a replication zone: - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE t CONFIGURE ZONE USING range_min_bytes = 0, range_max_bytes = 90000, gc.ttlseconds = 89999, num_replicas = 5, constraints = '[-region=west]'; -~~~ - -{% include {{page.version.version}}/zone-configs/variables.md %} - -### Replication constraints - -The location of replicas, both when they are first added and when they are rebalanced to maintain cluster equilibrium, is based on the interplay between descriptive attributes assigned to nodes and constraints set in zone configurations. - -{{site.data.alerts.callout_success}}For demonstrations of how to set node attributes and replication constraints in different scenarios, see Scenario-based Examples below.{{site.data.alerts.end}} - -#### Descriptive attributes assigned to nodes - -When starting a node with the [`cockroach start`](cockroach-start.html) command, you can assign the following types of descriptive attributes: - -Attribute Type | Description ----------------|------------ -**Node Locality** | Using the [`--locality`](cockroach-start.html#locality) flag, you can assign arbitrary key-value pairs that describe the location of the node. Locality might include region, country, datacenter, rack, etc. The key-value pairs should be ordered into _locality tiers_ that range from most inclusive to least inclusive (e.g., region before datacenter as in `region=eu,dc=paris`), and the keys and the order of key-value pairs must be the same on all nodes. It's typically better to include more pairs than fewer. For example:

      `--locality=region=east,datacenter=us-east-1`
      `--locality=region=east,datacenter=us-east-2`
      `--locality=region=west,datacenter=us-west-1`

      CockroachDB attempts to spread replicas evenly across the cluster based on locality, with the order of locality tiers determining the priority. Locality can also be used to influence the location of data replicas in various ways using replication zones.

      When there is high latency between nodes, CockroachDB uses locality to move range leases closer to the current workload, reducing network round trips and improving read performance. See [Follow-the-workload](demo-follow-the-workload.html) for more details. -**Node Capability** | Using the `--attrs` flag, you can specify node capability, which might include specialized hardware or number of cores, for example:

      `--attrs=ram:64gb` -**Store Type/Capability** | Using the `attrs` field of the `--store` flag, you can specify disk type or capability, for example:

      `--store=path=/mnt/ssd01,attrs=ssd`
      `--store=path=/mnt/hda1,attrs=hdd:7200rpm` - -#### Types of constraints - -The node-level and store-level descriptive attributes mentioned above can be used as the following types of constraints in replication zones to influence the location of replicas. However, note the following general guidance: - -- When locality is the only consideration for replication, it's recommended to set locality on nodes without specifying any constraints in zone configurations. In the absence of constraints, CockroachDB attempts to spread replicas evenly across the cluster based on locality. -- Required and prohibited constraints are useful in special situations where, for example, data must or must not be stored in a specific country or on a specific type of machine. -- Avoid conflicting constraints. CockroachDB returns an error if you: - - Redefine a required constraint key within the same `constraints` definition on all replicas. For example, `constraints = '[+region=west, +region=east]'` will result in an error. - - Define a required and prohibited definition for the same key-value pair. For example, `constraints = '[-region=west, +region=west]'` will result in an error. - -Constraint Type | Description | Syntax -----------------|-------------|------- -**Required** | When placing replicas, the cluster will consider only nodes/stores with matching attributes or localities. When there are no matching nodes/stores, new replicas will not be added. | `+ssd` -**Prohibited** | When placing replicas, the cluster will ignore nodes/stores with matching attributes or localities. When there are no alternate nodes/stores, new replicas will not be added. | `-ssd` - -#### Scope of constraints - -Constraints can be specified such that they apply to all replicas in a zone or such that different constraints apply to different replicas, meaning you can effectively pick the exact location of each replica. - -Constraint Scope | Description | Syntax ------------------|-------------|------- -**All Replicas** | Constraints specified using JSON array syntax apply to all replicas in every range that's part of the replication zone. | `constraints = '[+ssd, -region=west]'` -**Per-Replica** | Multiple lists of constraints can be provided in a JSON object, mapping each list of constraints to an integer number of replicas in each range that the constraints should apply to.

      The total number of replicas constrained cannot be greater than the total number of replicas for the zone (`num_replicas`). However, if the total number of replicas constrained is less than the total number of replicas for the zone, the non-constrained replicas will be allowed on any nodes/stores.

      Note that per-replica constraints must be "required" (e.g., `'{"+region=west": 1}'`); they cannot be "prohibited" (e.g., `'{"-region=west": 1}'`). Also, when defining per-replica constraints on a database or table, `num_replicas` must be specified as well, but not when defining per-replica constraints on an index or partition.

      See the [Per-replica constraints](#per-replica-constraints-to-specific-datacenters) example for more details. | `constraints = '{"+ssd,+region=west": 2, "+region=east": 1}', num_replicas = 3` - -### Node/replica recommendations - -See [Cluster Topography](recommended-production-settings.html#topology) recommendations for production deployments. - -## View replication zones - -Use the [`SHOW ZONE CONFIGURATIONS`](#view-all-replication-zones) statement to view details about existing replication zones. - -You can also use the [`SHOW PARTITIONS`](show-partitions.html) statement to view the zone constraints on existing table partitions, or [`SHOW CREATE TABLE`](show-create.html) to view zone configurations for a table. - -{% include {{page.version.version}}/sql/crdb-internal-partitions.md %} - -## Basic examples - -{% include {{ page.version.version }}/sql/movr-statements-geo-partitioned-replicas.md %} - -These examples focus on the basic approach and syntax for working with zone configuration. For examples demonstrating how to use constraints, see [Scenario-based examples](#scenario-based-examples). - -For more examples, see [`CONFIGURE ZONE`](configure-zone.html) and [`SHOW ZONE CONFIGURATIONS`](show-zone-configurations.html). - -### View all replication zones - -{% include {{ page.version.version }}/zone-configs/view-all-replication-zones.md %} - -For more information, see [`SHOW ZONE CONFIGURATIONS`](show-zone-configurations.html). - -### View the default replication zone - -{% include {{ page.version.version }}/zone-configs/view-the-default-replication-zone.md %} - -For more information, see [`SHOW ZONE CONFIGURATIONS`](show-zone-configurations.html). - -### Edit the default replication zone - -{% include {{ page.version.version }}/zone-configs/edit-the-default-replication-zone.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -### Create a replication zone for a system range - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-system-range.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -### Create a replication zone for a database - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-database.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -### Create a replication zone for a table - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-table.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -### Create a replication zone for a secondary index - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-secondary-index.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -### Create a replication zone for a partition - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-table-partition.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -### Reset a replication zone - -{% include {{ page.version.version }}/zone-configs/reset-a-replication-zone.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -### Remove a replication zone - -{% include {{ page.version.version }}/zone-configs/remove-a-replication-zone.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -### Constrain leaseholders to specific datacenters - -{% include {{ page.version.version }}/zone-configs/constrain-leaseholders-to-specific-datacenters.md %} - -For more information, see [`CONFIGURE ZONE`](configure-zone.html). - -## Scenario-based examples - -### Even replication across datacenters - -**Scenario:** - -- You have 6 nodes across 3 datacenters, 2 nodes in each datacenter. -- You want data replicated 3 times, with replicas balanced evenly across all three datacenters. - -**Approach:** - -1. Start each node with its datacenter location specified in the [`--locality`](cockroach-start.html#locality) flag: - - Datacenter 1: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-1 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-1 \ - --join=,, - ~~~ - - Datacenter 2: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-2 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-2 \ - --join=,, - ~~~ - - Datacenter 3: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-3 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-3 \ - --join=,, - ~~~ - -2. Initialize the cluster: - - ~~~ shell - $ cockroach init --insecure --host= - ~~~ - -There's no need to make zone configuration changes; by default, the cluster is configured to replicate data three times, and even without explicit constraints, the cluster will aim to diversify replicas across node localities. - -### Per-replica constraints to specific datacenters - -**Scenario:** - -- You have 5 nodes across 5 datacenters in 3 regions, 1 node in each datacenter. -- You want data replicated 3 times, with a quorum of replicas for a database holding West Coast data centered on the West Coast and a database for nation-wide data replicated across the entire country. - -**Approach:** - -1. Start each node with its region and datacenter location specified in the [`--locality`](cockroach-start.html#locality) flag: - - Start the five nodes: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --locality=region=us-west1,datacenter=us-west1-a \ - --join=,,,, - $ cockroach start --insecure --advertise-addr= --locality=region=us-west1,datacenter=us-west1-b \ - --join=,,,, - $ cockroach start --insecure --advertise-addr= --locality=region=us-central1,datacenter=us-central1-a \ - --join=,,,, - $ cockroach start --insecure --advertise-addr= --locality=region=us-east1,datacenter=us-east1-a \ - --join=,,,, - $ cockroach start --insecure --advertise-addr= --locality=region=us-east1,datacenter=us-east1-b \ - --join=,,,, - ~~~ - - Initialize the cluster: - - ~~~ shell - $ cockroach init --insecure --host= - ~~~ - -2. On any node, open the [built-in SQL client](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - -3. Create the database for the West Coast application: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE DATABASE west_app_db; - ~~~ - -4. Configure a replication zone for the database: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER DATABASE west_app_db - CONFIGURE ZONE USING constraints = '{"+region=us-west1": 2, "+region=us-central1": 1}', num_replicas = 3; - ~~~ - - ~~~ - CONFIGURE ZONE 1 - ~~~ - -5. View the replication zone: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ZONE CONFIGURATION FOR DATABASE west_app_db; - ~~~ - - ~~~ - target | raw_config_sql - +----------------------+--------------------------------------------------------------------+ - DATABASE west_app_db | ALTER DATABASE west_app_db CONFIGURE ZONE USING - | range_min_bytes = 16777216, - | range_max_bytes = 67108864, - | gc.ttlseconds = 90000, - | num_replicas = 3, - | constraints = '{+region=us-central1: 1, +region=us-west1: 2}', - | lease_preferences = '[]' - (1 row) - ~~~ - - Two of the database's three replicas will be put in `region=us-west1` and its remaining replica will be put in `region=us-central1`. This gives the application the resilience to survive the total failure of any one datacenter while providing low-latency reads and writes on the West Coast because a quorum of replicas are located there. - -6. No configuration is needed for the nation-wide database. The cluster is configured to replicate data 3 times and spread them as widely as possible by default. Because the first key-value pair specified in each node's locality is considered the most significant part of each node's locality, spreading data as widely as possible means putting one replica in each of the three different regions. - -### Multiple applications writing to different databases - -**Scenario:** - -- You have 2 independent applications connected to the same CockroachDB cluster, each application using a distinct database. -- You have 6 nodes across 2 datacenters, 3 nodes in each datacenter. -- You want the data for application 1 to be replicated 5 times, with replicas evenly balanced across both datacenters. -- You want the data for application 2 to be replicated 3 times, with all replicas in a single datacenter. - -**Approach:** - -1. Start each node with its datacenter location specified in the [`--locality`](cockroach-start.html#locality) flag: - - Datacenter 1: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-1 \ - --join=,,,,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-1 \ - --join=,,,,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-1 \ - --join=,,,,, - ~~~ - - Datacenter 2: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-2 \ - --join=,,,,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-2 \ - --join=,,,,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-2 \ - --join=,,,,, - ~~~ - - Initialize the cluster: - - ~~~ shell - $ cockroach init --insecure --host= - ~~~ - -2. On any node, open the [built-in SQL client](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - -3. Create the database for application 1: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE DATABASE app1_db; - ~~~ - -4. Configure a replication zone for the database used by application 1: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER DATABASE app1_db CONFIGURE ZONE USING num_replicas = 5; - ~~~ - - ~~~ - CONFIGURE ZONE 1 - ~~~ - -5. View the replication zone: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ZONE CONFIGURATION FOR DATABASE app1_db; - ~~~ - - ~~~ - target | raw_config_sql - +------------------+---------------------------------------------+ - DATABASE app1_db | ALTER DATABASE app1_db CONFIGURE ZONE USING - | range_min_bytes = 16777216, - | range_max_bytes = 67108864, - | gc.ttlseconds = 90000, - | num_replicas = 5, - | constraints = '[]', - | lease_preferences = '[]' - (1 row) - ~~~ - - Nothing else is necessary for application 1's data. Since all nodes specify their datacenter locality, the cluster will aim to balance the data in the database used by application 1 between datacenters 1 and 2. - -6. Still in the SQL client, create a database for application 2: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE DATABASE app2_db; - ~~~ - -7. Configure a replication zone for the database used by application 2: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER DATABASE app2_db CONFIGURE ZONE USING constraints = '[+datacenter=us-2]'; - ~~~ - -8. View the replication zone: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ZONE CONFIGURATION FOR DATABASE app2_db; - ~~~ - - ~~~ - target | raw_config_sql - +------------------+---------------------------------------------+ - DATABASE app2_db | ALTER DATABASE app2_db CONFIGURE ZONE USING - | range_min_bytes = 16777216, - | range_max_bytes = 67108864, - | gc.ttlseconds = 90000, - | num_replicas = 3, - | constraints = '[+datacenter=us-2]', - | lease_preferences = '[]' - (1 row) - ~~~ - - The required constraint will force application 2's data to be replicated only within the `us-2` datacenter. - -### Stricter replication for a table and its secondary indexes - -**Scenario:** - -- You have 7 nodes, 5 with SSD drives and 2 with HDD drives. -- You want data replicated 3 times by default. -- Speed and availability are important for a specific table and its indexes, which are queried very frequently, however, so you want the data in the table and secondary indexes to be replicated 5 times, preferably on nodes with SSD drives. - -**Approach:** - -1. Start each node with `ssd` or `hdd` specified as store attributes: - - 5 nodes with SSD storage: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --store=path=node1,attrs=ssd \ - --join=,, - $ cockroach start --insecure --advertise-addr= --store=path=node2,attrs=ssd \ - --join=,, - $ cockroach start --insecure --advertise-addr= --store=path=node3,attrs=ssd \ - --join=,, - $ cockroach start --insecure --advertise-addr= --store=path=node4,attrs=ssd \ - --join=,, - $ cockroach start --insecure --advertise-addr= --store=path=node5,attrs=ssd \ - --join=,, - ~~~ - - 2 nodes with HDD storage: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --store=path=node6,attrs=hdd \ - --join=,, - $ cockroach start --insecure --advertise-addr= --store=path=node7,attrs=hdd \ - --join=,, - ~~~ - - Initialize the cluster: - - ~~~ shell - $ cockroach init --insecure --host= - ~~~ - -2. On any node, open the [built-in SQL client](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - -3. Create a database and table: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE DATABASE db; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE db.important_table; - ~~~ - -4. Configure a replication zone for the table that must be replicated more strictly: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE db.important_table CONFIGURE ZONE USING num_replicas = 5, constraints = '[+ssd]' - ~~~ - -5. View the replication zone: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ZONE CONFIGURATION FOR TABLE db.important_table; - ~~~ - - ~~~ - target | config_sql - +-------------------------------+---------------------------------------------+ - TABLE db.important_table | ALTER DATABASE app2_db CONFIGURE ZONE USING - | range_min_bytes = 1048576, - | range_max_bytes = 67108864, - | gc.ttlseconds = 90000, - | num_replicas = 5, - | constraints = '[+ssd]', - | lease_preferences = '[]' - (1 row) - ~~~ - - The secondary indexes on the table will use the table's replication zone, so all data for the table will be replicated 5 times, and the required constraint will place the data on nodes with `ssd` drives. - -### Tweaking the replication of system ranges - -**Scenario:** - -- You have nodes spread across 7 datacenters. -- You want data replicated 5 times by default. -- For better performance, you want a copy of the meta ranges in all of the datacenters. -- To save disk space, you only want the internal timeseries data replicated 3 times by default. - -**Approach:** - -1. Start each node with a different [locality](cockroach-start.html#locality) attribute: - - ~~~ shell - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-1 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-2 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-3 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-4 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-5 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-6 \ - --join=,, - $ cockroach start --insecure --advertise-addr= --locality=datacenter=us-7 \ - --join=,, - ~~~ - - Initialize the cluster: - - ~~~ shell - $ cockroach init --insecure --host= - ~~~ - -2. On any node, open the [built-in SQL client](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure - ~~~ - -3. Configure the default replication zone: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE default CONFIGURE ZONE USING num_replicas = 5; - ~~~ - -4. View the replication zone: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ZONE CONFIGURATION FOR RANGE default; - ~~~ - ~~~ - target | raw_config_sql - +---------------+------------------------------------------+ - RANGE default | ALTER RANGE default CONFIGURE ZONE USING - | range_min_bytes = 16777216, - | range_max_bytes = 67108864, - | gc.ttlseconds = 90000, - | num_replicas = 5, - | constraints = '[]', - | lease_preferences = '[]' - (1 row) - ~~~ - - All data in the cluster will be replicated 5 times, including both SQL data and the internal system data. - -5. Configure the `meta` replication zone: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE meta CONFIGURE ZONE USING num_replicas = 7; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ZONE CONFIGURATION FOR RANGE meta; - ~~~ - ~~~ - target | raw_config_sql - +------------+---------------------------------------+ - RANGE meta | ALTER RANGE meta CONFIGURE ZONE USING - | range_min_bytes = 16777216, - | range_max_bytes = 67108864, - | gc.ttlseconds = 3600, - | num_replicas = 7, - | constraints = '[]', - | lease_preferences = '[]' - (1 row) - ~~~ - - The `meta` addressing ranges will be replicated such that one copy is in all 7 datacenters, while all other data will be replicated 5 times. - -6. Configure the `timeseries` replication zone: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER RANGE timeseries CONFIGURE ZONE USING num_replicas = 3; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW ZONE CONFIGURATION FOR RANGE timeseries; - ~~~ - ~~~ - target | raw_config_sql - +------------------+---------------------------------------------+ - RANGE timeseries | ALTER RANGE timeseries CONFIGURE ZONE USING - | range_min_bytes = 16777216, - | range_max_bytes = 67108864, - | gc.ttlseconds = 90000, - | num_replicas = 3, - | constraints = '[]', - | lease_preferences = '[]' - (1 row) - ~~~ - - The timeseries data will only be replicated 3 times without affecting the configuration of all other data. - -## See also - -- [`SHOW ZONE CONFIGURATIONS`](show-zone-configurations.html) -- [`CONFIGURE ZONE`](configure-zone.html) -- [`SHOW PARTITIONS`](show-partitions.html) -- [SQL Statements](sql-statements.html) -- [Table Partitioning](partitioning.html) diff --git a/src/current/v19.2/configure-zone.md b/src/current/v19.2/configure-zone.md deleted file mode 100644 index daf2bf9eca5..00000000000 --- a/src/current/v19.2/configure-zone.md +++ /dev/null @@ -1,135 +0,0 @@ ---- -title: CONFIGURE ZONE -summary: Use the CONFIGURE ZONE statement to add, modify, reset, and remove replication zones. -toc: true ---- - -`CONFIGURE ZONE` is a subcommand of the `ALTER DATABASE`, `ALTER TABLE`, `ALTER INDEX`, `ALTER PARTITION`, and `ALTER RANGE` statements and is used to add, modify, reset, or remove [replication zones](configure-replication-zones.html) for those objects. To view details about existing replication zones, see [`SHOW ZONE CONFIGURATIONS`](show-zone-configurations.html). - -In CockroachDB, you can use **replication zones** to control the number and location of replicas for specific sets of data, both when replicas are first added and when they are rebalanced to maintain cluster equilibrium. - -{{site.data.alerts.callout_info}} -Adding replication zones for secondary indexes and partitions is an [enterprise-only](enterprise-licensing.html) feature. -{{site.data.alerts.end}} - -## Synopsis - -**alter_zone_database_stmt ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/alter_zone_database.html %} -
      - -**alter_zone_table_stmt ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/alter_zone_table.html %} -
      - -**alter_zone_index_stmt ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/alter_zone_index.html %} -
      - -**alter_zone_partition_stmt ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/alter_zone_partition.html %} -
      - -**alter_zone_range_stmt ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/alter_zone_range.html %} -
      - -## Required privileges - -If the target is a [`system` range](#create-a-replication-zone-for-a-system-range), the [`system` database](show-databases.html#preloaded-databases), or a table in the `system` database, the user must be an [`admin`](authorization.html#create-and-manage-roles). For all other databases and tables, the user must have the [CREATE](grant.html#supported-privileges) privilege on the target database or table. - -{{site.data.alerts.callout_danger}} -Required privileges for `CONFIGURE ZONE` statements in CockroachDB v19.2 may be backward-incompatible for users running scripted statements with restricted permissions in v19.1 and earlier.
      To add the necessary permissions, use [`GRANT` <privileges>](grant.html) or [`GRANT` <roles>](grant-roles.html) as a user with an admin role.
      For example, to grant a user the admin role, run `GRANT admin TO `.
      To grant the `CREATE` privilege on a database or table, run `GRANT CREATE ON [DATABASE | TABLE] TO `. -{{site.data.alerts.end}} - -## Parameters - - Parameter | Description ------------+------------- -`range_name` | The name of the system [range](architecture/overview.html#glossary) for which to show [replication zone configurations](configure-replication-zones.html). -`database_name` | The name of the [database](create-database.html) for which to show [replication zone configurations](configure-replication-zones.html). -`table_name` | The name of the [table](create-table.html) for which to show [replication zone configurations](configure-replication-zones.html). -`partition_name` | The name of the [partition](partitioning.html) for which to show [replication zone configurations](configure-replication-zones.html). -`index_name` | The name of the [index](indexes.html) for which to show [replication zone configurations](configure-replication-zones.html). -`variable` | The name of the [variable](#variables) to change. -`value` | The value of the variable to change. -`DISCARD` | Remove a replication zone. - -### Variables - -{% include {{ page.version.version }}/zone-configs/variables.md %} - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Examples - -{% include {{ page.version.version }}/sql/movr-statements-geo-partitioned-replicas.md %} - -### Edit a replication zone - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE users CONFIGURE ZONE USING range_min_bytes = 0, range_max_bytes = 90000, gc.ttlseconds = 89999, num_replicas = 4; -~~~ - -~~~ -CONFIGURE ZONE 1 -~~~ - -### Edit the default replication zone - -{% include {{ page.version.version }}/zone-configs/edit-the-default-replication-zone.md %} - -### Create a replication zone for a database - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-database.md %} - -### Create a replication zone for a table - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-table.md %} - -### Create a replication zone for a secondary index - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-secondary-index.md %} - -### Create a replication zone for a partition - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-table-partition.md %} - -### Create a replication zone for a system range - -{% include {{ page.version.version }}/zone-configs/create-a-replication-zone-for-a-system-range.md %} - -### Reset a replication zone - -{% include {{ page.version.version }}/zone-configs/reset-a-replication-zone.md %} - -### Remove a replication zone - -{% include {{ page.version.version }}/zone-configs/remove-a-replication-zone.md %} - -## See also - -- [Configure Replication Zones](configure-replication-zones.html) -- [`PARTITION BY`](partition-by.html) -- [`SHOW ZONE CONFIGURATIONS`](show-zone-configurations.html) -- [`ALTER DATABASE`](alter-database.html) -- [`ALTER TABLE`](alter-table.html) -- [`ALTER INDEX`](alter-index.html) -- [`ALTER PARTITION`](alter-partition.html) -- [`ALTER RANGE`](alter-range.html) -- [`SHOW JOBS`](show-jobs.html) -- [Table Partitioning](partitioning.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/connect-to-the-database.md b/src/current/v19.2/connect-to-the-database.md deleted file mode 100644 index 34848a9c8e7..00000000000 --- a/src/current/v19.2/connect-to-the-database.md +++ /dev/null @@ -1,135 +0,0 @@ ---- -title: Connect to the Database -summary: How to connect to a CockroachDB cluster from your application -toc: true ---- - -This page has instructions for connecting to a CockroachDB cluster from your application using various programming languages. Each example shows a [connection string][connection_params] for a [secure local cluster][local_secure] to a `bank` database by a user named `maxroach`. Depending on your cluster's configuration, you may need to edit this connection string. - -For a reference that lists all of the supported cluster connection parameters, see [Connection Parameters][connection_params]. - -## Before you begin - -Make sure you have already: - -- Set up a [local cluster](secure-a-cluster.html). -- [Installed a Postgres client](install-client-drivers.html). - -## Connect - -
      - - - - -
      - -
      - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --certs-dir=certs --host=localhost:26257 -~~~ - -For more information about how to use the built-in SQL client, see the [`cockroach sql`](cockroach-sql.html) reference docs. - -
      - -
      - -{% include copy-clipboard.html %} -~~~ go -import ( - "database/sql" - "fmt" - "log" - _ "github.com/lib/pq" -) - -db, err := sql.Open("postgres", - "postgresql://maxroach@localhost:26257/bank?ssl=true&sslmode=require&sslrootcert=certs/ca.crt&sslkey=certs/client.maxroach.key&sslcert=certs/client.maxroach.crt") -if err != nil { - log.Fatal("error connecting to the database: ", err) -} -defer db.Close() -~~~ - -{% include {{page.version.version}}/app/for-a-complete-example-go.md %} - -
      - -
      - -{% include copy-clipboard.html %} -~~~ java -import java.sql.*; -import javax.sql.DataSource; - -PGSimpleDataSource ds = new PGSimpleDataSource(); -ds.setServerName("localhost"); -ds.setPortNumber(26257); -ds.setDatabaseName("bank"); -ds.setUser("maxroach"); -ds.setPassword(null); -ds.setSsl(true); -ds.setSslMode("require"); -ds.setSslCert("certs/client.maxroach.crt"); -ds.setSslKey("certs/client.maxroach.key.pk8"); -ds.setReWriteBatchedInserts(true); // add `rewriteBatchedInserts=true` to pg connection string -ds.setApplicationName("BasicExample"); -~~~ - -{% include {{page.version.version}}/app/for-a-complete-example-java.md %} - -
      - -
      - -{% include copy-clipboard.html %} -~~~ python -import psycopg2 - -conn = psycopg2.connect( - database='bank', - user='maxroach', - sslmode='require', - sslrootcert='certs/ca.crt', - sslkey='certs/client.maxroach.key', - sslcert='certs/client.maxroach.crt', - port=26257, - host='localhost' -) -~~~ - -{% include {{page.version.version}}/app/for-a-complete-example-python.md %} - -
      - -## See also - -Reference information related to this task: - -- [Connection parameters][connection_params] -- [Manual deployments][manual] -- [Orchestrated deployments][orchestrated] -- [Start a local cluster (secure)][local_secure] - - - -Other common tasks: - -- [Insert Data](insert-data.html) -- [Query Data](query-data.html) -- [Update Data](update-data.html) -- [Delete Data](delete-data.html) -- [Run Multi-Statement Transactions](run-multi-statement-transactions.html) -- [Error Handling and Troubleshooting](error-handling-and-troubleshooting.html) -- [Make Queries Fast](make-queries-fast.html) -- [Hello World Example apps](hello-world-example-apps.html) - - - -[manual]: manual-deployment.html -[orchestrated]: orchestration.html -[local_secure]: secure-a-cluster.html -[connection_params]: connection-parameters.html diff --git a/src/current/v19.2/connection-parameters.md b/src/current/v19.2/connection-parameters.md deleted file mode 100644 index 60b45944e7e..00000000000 --- a/src/current/v19.2/connection-parameters.md +++ /dev/null @@ -1,233 +0,0 @@ ---- -title: Client Connection Parameters -summary: This page describes the parameters used to establish a client connection. -toc: true ---- - -Client applications, including [`cockroach` client -commands](cockroach-commands.html), work by establishing a network -connection to a CockroachDB cluster. The client connection parameters -determine which CockroachDB cluster they connect to, and how to -establish this network connection. - -## Supported connection parameters - -Most client apps, including `cockroach` client commands, determine -which CockroachDB server to connect to using a [PostgreSQL connection -URL](#connect-using-a-url). When using a URL, a client can also -specify additional SQL-level parameters. This mode provides the most -configuration flexibility. - -In addition, all `cockroach` client commands also accept [discrete -connection parameters](#connect-using-discrete-parameters) that can -specify the connection parameters separately from a URL. - -## When to use a URL and when to use discrete parameters - -Specifying client parameters using a URL may be more convenient during -experimentation, as it facilitates copy-pasting the connection -parameters (the URL) between different tools: the output of `cockroach -start`, other `cockroach` commands, GUI database visualizer, -programming tools, etc. - - -Discrete parameters may be more convenient in automation, where the -components of the configuration are filled in separately from -different variables in a script or a service manager. - -## Connect using a URL - -A connection URL has the following format: - -{% include_cached copy-clipboard.html %} -~~~ -postgres://:@:/? -~~~ - - Component | Description | Required -----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------- - `` | The [SQL user](create-user.html) that will own the client session. | ✗ - `` | The user's password. It is not recommended to pass the password in the URL directly.

      [Find more detail about how CockroachDB handles passwords](authentication.html#client-authentication). | ✗ - `` | The host name or address of a CockroachDB node or load balancer. | Required by most client drivers. - `` | The port number of the SQL interface of the CockroachDB node or load balancer. The default port number for CockroachDB is 26257. Use this value when in doubt. | Required by most client drivers. - `` | A database name to use as [current database](sql-name-resolution.html#current-database). Defaults to `defaultdb`. | ✗ - `` | [Additional connection parameters](#additional-connection-parameters), including SSL/TLS certificate settings. | ✗ - -{{site.data.alerts.callout_info}} -For cockroach commands that accept a URL, you can specify the URL with the command-line flag `--url`. -If `--url` is not specified but -the environment variable `COCKROACH_URL` is defined, the environment -variable is used. Otherwise, the `cockroach` command will use -[discrete connection parameters](#connect-using-discrete-parameters) -as described below. -{{site.data.alerts.end}} - -{{site.data.alerts.callout_info}} -The `` part is not used for [`cockroach` -commands](cockroach-commands.html) other than [`cockroach -sql`](cockroach-sql.html). A warning -is currently printed if it is mistakenly specified, and -future versions of CockroachDB may return an error in that case. -{{site.data.alerts.end}} - -### Additional connection parameters - -The following additional parameters can be passed after the `?` character in the URL: - -Parameter | Description | Default value -----------|-------------|--------------- -`application_name` | An initial value for the [`application_name` session variable](set-vars.html).

      Note: For [Java JDBC](build-a-java-app-with-cockroachdb.html), use `ApplicationName`. | Empty string. -`sslmode` | Which type of secure connection to use: `disable`, `allow`, `prefer`, `require`, `verify-ca` or `verify-full`. See [Secure Connections With URLs](#secure-connections-with-urls) for details. | `disable` -`sslrootcert` | Path to the [CA certificate](cockroach-cert.html), when `sslmode` is not `disable`. | Empty string. -`sslcert` | Path to the [client certificate](cockroach-cert.html), when `sslmode` is not `disable`. | Empty string. -`sslkey` | Path to the [client private key](cockroach-cert.html), when `sslmode` is not `disable`. | Empty string. - -### Secure connections with URLs - -The following values are supported for `sslmode`, although only the first and the last are recommended for use. - -Parameter | Description | Recommended for use -----------|-------------|-------------------- -`sslmode=disable` | Do not use an encrypted, secure connection at all. | Use during development. -`sslmode=allow` | Enable a secure connection only if the server requires it.

      **Not supported in all clients.** | -`sslmode=prefer` | Try to establish a secure connection, but accept an insecure connection if the server does not support secure connections.

      **Not supported in all clients.** | -`sslmode=require` | Force a secure connection. An error occurs if the secure connection cannot be established. | -`sslmode=verify-ca` | Force a secure connection and verify that the server certificate is signed by a known CA. | -`sslmode=verify-full` | Force a secure connection, verify that the server certificate is signed by a known CA, and verify that the server address matches that specified in the certificate. | Use for [secure deployments](secure-a-cluster.html). - -{{site.data.alerts.callout_danger}} -Some client drivers and the `cockroach` commands do not support -`sslmode=allow` and `sslmode=prefer`. Check the documentation of your -SQL driver to determine whether these options are supported. -{{site.data.alerts.end}} - -### Example URL for an insecure connection - -The following URL is suitable to connect to a CockroachDB node using an insecure connection: - -{% include_cached copy-clipboard.html %} -~~~ -postgres://root@servername:26257/mydb?sslmode=disable -~~~ - -This specifies a connection for the `root` user to server `servername` -on port 26257 (the default CockroachDB SQL port), with `mydb` set as -current database. `sslmode=disable` makes the connection insecure. - -### Example URL for a secure connection - -The following URL is suitable to connect to a CockroachDB node using a secure connection: - -{% include_cached copy-clipboard.html %} -~~~ -postgres://root@servername:26257/mydb?sslmode=verify-full&sslrootcert=path/to/ca.crt&sslcert=path/to/client.username.crt&sslkey=path/to/client.username.key -~~~ - -This uses the following components: - -- User `root` -- Host name `servername`, port number 26257 (the default CockroachDB SQL port) -- Current database `mydb` -- SSL/TLS mode `verify-full`: - - Root CA certificate `path/to/ca.crt` - - Client certificate `path/to/client.username.crt` - - Client key `path/to/client.username.key` - -For details about how to create and manage SSL/TLS certificates, see -[Create Security Certificates](cockroach-cert.html) and -[Rotate Certificates](rotate-certificates.html). - -## Connect using discrete parameters - -Most [`cockroach` commands](cockroach-commands.html) accept connection -parameters as separate, discrete command-line flags, in addition (or -in replacement) to `--url` which [specifies all parameters as a -URL](#connect-using-a-url). - -For each command-line flag that directs a connection parameter, -CockroachDB also recognizes an environment variable. The environment -variable is used when the command-line flag is not specified. - -{% include {{ page.version.version }}/sql/connection-parameters.md %} - -### Example command-line flags for an insecure connection - -The following command-line flags establish an insecure connection: - -{% include_cached copy-clipboard.html %} -~~~ ---user=root \ ---host= ---insecure -~~~ - -This specifies a connection for the `root` user to server `servername` -on port 26257 (the default CockroachDB SQL port). `--insecure` makes -the connection insecure. - -### Example command-line flags for a secure connection - -The following command-line flags establish a secure connection: - -{% include_cached copy-clipboard.html %} -~~~ ---user=root \ ---host= ---certs-dir=path/to/certs -~~~ - -This uses the following components: - -- User `root` -- Host name `servername`, port number 26257 (the default CockroachDB SQL port) -- SSL/TLS enabled, with settings: - - Root CA certificate `path/to/certs/ca.crt` - - Client certificate `path/to/client..crt` (`path/to/certs/client.root.crt` with `--user root`) - - Client key `path/to/client..key` (`path/to/certs/client.root.key` with `--user root`) - -{{site.data.alerts.callout_info}} -When using discrete connection parameters, the file names of the CA -and client certificates and client key are derived automatically from -the value of `--certs-dir`. -{{site.data.alerts.end}} - -## Using both URL and client parameters - -Most `cockroach` commands accept both a URL and client parameters. -The information contained therein is combined in the order it appears -in the command line. - -This combination is useful so that discrete command-line flags can -override settings not otherwise set in the URL. - -### Example override of the current database - -The `cockroach start` command prints out the following connection URL, which connects to the `defaultdb` database: - -{% include_cached copy-clipboard.html %} -~~~ -postgres://root@servername:26257/?sslmode=disable -~~~ - -To specify `mydb` as the current database using [`cockroach sql`](cockroach-sql.html), run the following command: - -{% include_cached copy-clipboard.html %} -~~~ -cockroach sql \ ---url "postgres://root@servername:26257/?sslmode=disable" \ ---database mydb -~~~ - -This is equivalent to: - -{% include_cached copy-clipboard.html %} -~~~ -cockroach sql --url "postgres://root@servername:26257/mydb?sslmode=disable" -~~~ - -## See also - -- [`cockroach` commands](cockroach-commands.html) -- [Create Security Certificates](cockroach-cert.html) -- [Secure a Cluster](secure-a-cluster.html) -- [Create and Manage Users](authorization.html#create-and-manage-users) diff --git a/src/current/v19.2/constraints.md b/src/current/v19.2/constraints.md deleted file mode 100644 index d92ff765232..00000000000 --- a/src/current/v19.2/constraints.md +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: Constraints -summary: Constraints offer additional data integrity by enforcing conditions on the data within a column. -toc: true ---- - -Constraints offer additional data integrity by enforcing conditions on the data within a column. Whenever values are manipulated (inserted, deleted, or updated), constraints are checked and modifications that violate constraints are rejected. - -For example, the `UNIQUE` constraint requires that all values in a column be unique from one another (except *NULL* values). If you attempt to write a duplicate value, the constraint rejects the entire statement. - - -## Supported constraints - - Constraint | Description -------------|------------- - [`CHECK`](check.html) | Values must return `TRUE` or `NULL` for a Boolean expression. - [`DEFAULT` value](default-value.html) | If a value is not defined for the constrained column in an `INSERT` statement, the `DEFAULT` value is written to the column. - [`FOREIGN KEY`](foreign-key.html) | Values must exactly match existing values from the column it references. - [`NOT NULL`](not-null.html) | Values may not be *NULL*. - [`PRIMARY KEY`](primary-key.html) | Values must uniquely identify each row *(one per table)*. This behaves as if the `NOT NULL` and `UNIQUE` constraints are applied, as well as automatically creates an [index](indexes.html) for the table using the constrained columns. - [`UNIQUE`](unique.html) | Each non-*NULL* value must be unique. This also automatically creates an [index](indexes.html) for the table using the constrained columns. - -## Using constraints - -### Add constraints - -How you add constraints depends on the number of columns you want to constrain, as well as whether or not the table is new. - -- **One column of a new table** has its constraints defined after the column's data type. For example, this statement applies the `PRIMARY KEY` constraint to `foo.a`: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE foo (a INT PRIMARY KEY); - ~~~ -- **Multiple columns of a new table** have their constraints defined after the table's columns. For example, this statement applies the `PRIMARY KEY` constraint to `foo`'s columns `a` and `b`: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE bar (a INT, b INT, PRIMARY KEY (a,b)); - ~~~ - - {{site.data.alerts.callout_info}} - The `DEFAULT` and `NOT NULL` constraints cannot be applied to multiple columns. - {{site.data.alerts.end}} - -- **Existing tables** can have the following constraints added: - - `CHECK`, `FOREIGN KEY`, and `UNIQUE` constraints can be added through [`ALTER TABLE...ADD CONSTRAINT`](add-constraint.html). For example, this statement adds the `UNIQUE` constraint to `baz.id`: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE baz ADD CONSTRAINT id_unique UNIQUE (id); - ~~~ - - - `DEFAULT` values can be added through [`ALTER TABLE...ALTER COLUMN`](alter-column.html#set-or-change-a-default-value). For example, this statement adds the Default Value constraint to `baz.bool`: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE baz ALTER COLUMN bool SET DEFAULT true; - ~~~ - - - `PRIMARY KEY` and `NOT NULL` constraints cannot be added or changed. However, you can go through [this process](#table-migrations-to-add-or-change-immutable-constraints) to migrate data from your current table to a new table with the constraints you want to apply. - -#### Order of constraints - -The order in which you list constraints is not important because constraints are applied to every modification of their respective tables or columns. - -#### Name constraints on new tables - -You can name constraints applied to new tables using the `CONSTRAINT` clause before defining the constraint: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE foo (a INT CONSTRAINT another_name PRIMARY KEY); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE bar (a INT, b INT, CONSTRAINT yet_another_name PRIMARY KEY (a,b)); -~~~ - -### View constraints - -To view a table's constraints, use [`SHOW CONSTRAINTS`](show-constraints.html) or [`SHOW CREATE`](show-create.html). - -### Remove constraints - -The procedure for removing a constraint depends on its type: - -Constraint Type | Procedure ------------------|----------- -[`CHECK`](check.html) | Use [`DROP CONSTRAINT`](drop-constraint.html) -[`DEFAULT` value](default-value.html) | Use [`ALTER COLUMN`](alter-column.html#remove-default-constraint) -[`FOREIGN KEY`](foreign-key.html) | Use [`DROP CONSTRAINT`](drop-constraint.html) -[`NOT NULL`](not-null.html) | Use [`ALTER COLUMN`](alter-column.html#remove-not-null-constraint) -[`PRIMARY KEY`](primary-key.html) | Primary Keys cannot be removed. However, you can move the table's data to a new table with [this process](#table-migrations-to-add-or-change-immutable-constraints). -[`UNIQUE`](unique.html) | The `UNIQUE` constraint cannot be dropped directly. To remove the constraint, [drop the index](drop-index.html) that was created by the constraint, e.g., `DROP INDEX my_unique_constraint`. - -### Change constraints - -The procedure for changing a constraint depends on its type: - -Constraint Type | Procedure ------------------|----------- -[`CHECK`](check.html) | [Issue a transaction](transactions.html#syntax) that adds a new `CHECK` constraint ([`ADD CONSTRAINT`](add-constraint.html)), and then remove the existing one ([`DROP CONSTRAINT`](drop-constraint.html)). -[`DEFAULT` value](default-value.html) | The `DEFAULT` value can be changed through [`ALTER COLUMN`](alter-column.html). -[`FOREIGN KEY`](foreign-key.html) | [Issue a transaction](transactions.html#syntax) that adds a new `FOREIGN KEY` constraint ([`ADD CONSTRAINT`](add-constraint.html)), and then remove the existing one ([`DROP CONSTRAINT`](drop-constraint.html)). -[`NOT NULL`](not-null.html) | The `NOT NULL` constraint cannot be changed, only removed. However, you can move the table's data to a new table with [this process](#table-migrations-to-add-or-change-immutable-constraints). -[`PRIMARY KEY`](primary-key.html) | Primary Keys cannot be modified. However, you can move the table's data to a new table with [this process](#table-migrations-to-add-or-change-immutable-constraints). -[`UNIQUE`](unique.html) | [Issue a transaction](transactions.html#syntax) that adds a new `UNIQUE` constraint ([`ADD CONSTRAINT`](add-constraint.html)), and then remove the existing one ([`DROP CONSTRAINT`](drop-constraint.html)). - -#### Table migrations to add or change immutable constraints - -If you want to make a change to an immutable constraint, you can use the following process: - -1. [Create a new table](create-table.html) with the constraints you want to apply. -2. Move the data from the old table to the new one using [`INSERT` from a `SELECT` statement](insert.html#insert-from-a-select-statement). -3. [Drop the old table](drop-table.html), and then [rename the new table to the old name](rename-table.html). This cannot be done transactionally. - -## See also - -- [`CREATE TABLE`](create-table.html) -- [`ADD CONSTRAINT`](add-constraint.html) -- [`DROP CONSTRAINT`](drop-constraint.html) -- [`SHOW CONSTRAINTS`](show-constraints.html) -- [`SHOW CREATE`](show-create.html) diff --git a/src/current/v19.2/cost-based-optimizer.md b/src/current/v19.2/cost-based-optimizer.md deleted file mode 100644 index 2d471802f5c..00000000000 --- a/src/current/v19.2/cost-based-optimizer.md +++ /dev/null @@ -1,616 +0,0 @@ ---- -title: Cost-Based Optimizer -summary: The cost-based optimizer seeks the lowest cost for a query, usually related to time. -toc: true ---- - -The cost-based optimizer seeks the lowest cost for a query, usually related to time. - -## How is cost calculated? - -A given SQL query can have thousands of equivalent query plans with vastly different execution times. The cost-based optimizer enumerates these plans and chooses the lowest cost plan. - -Cost is roughly calculated by: - -- Estimating how much time each node in the query plan will use to process all results -- Modeling how data flows through the query plan - -The most important factor in determining the quality of a plan is cardinality (i.e., the number of rows); the fewer rows each SQL operator needs to process, the faster the query will run. - -## Table statistics - -The cost-based optimizer can often find more performant query plans if it has access to statistical data on the contents of your tables. This data needs to be generated from scratch for new tables, and regenerated periodically for existing tables. - -By default, CockroachDB generates table statistics automatically when tables are [created](create-table.html), and as they are [updated](update.html). It does this [using a background job](create-statistics.html#view-statistics-jobs) that automatically determines which columns to get statistics on — specifically, it chooses: - -- Columns that are part of the primary key or an index (in other words, all indexed columns). -- Up to 100 non-indexed columns. - -{{site.data.alerts.callout_info}} -[Schema changes](online-schema-changes.html) trigger automatic statistics collection for the affected table(s). -{{site.data.alerts.end}} - -### Controlling automatic statistics - -For best query performance, most users should leave automatic statistics enabled with the default settings. The information provided in this section is useful for troubleshooting or performance tuning by advanced users. - -#### Controlling statistics refresh rate - -Statistics are refreshed in the following cases: - -1. When there are no statistics. -2. When it's been a long time since the last refresh, where "long time" is defined according to a moving average of the time across the last several refreshes. -3. After each mutation operation ([`INSERT`](insert.html), [`UPDATE`](update.html), or [`DELETE`](delete.html)), the probability of a refresh is calculated using a formula that takes the [cluster settings](cluster-settings.html) shown below as inputs. These settings define the target number of rows in a table that should be stale before statistics on that table are refreshed. Increasing either setting will reduce the frequency of refreshes. In particular, `min_stale_rows` impacts the frequency of refreshes for small tables, while `fraction_stale_rows` has more of an impact on larger tables. - -| Setting | Default Value | Details | -|------------------------------------------------------+---------------+--------------------------------------------------------------------------------------| -| `sql.stats.automatic_collection.fraction_stale_rows` | 0.2 | Target fraction of stale rows per table that will trigger a statistics refresh | -| `sql.stats.automatic_collection.min_stale_rows` | 500 | Target minimum number of stale rows per table that will trigger a statistics refresh | - -{{site.data.alerts.callout_info}} -Because the formula for statistics refreshes is probabilistic, you should not expect to see statistics update immediately after changing these settings, or immediately after exactly 500 rows have been updated. -{{site.data.alerts.end}} - -#### Turning off statistics - -If you need to turn off automatic statistics collection, follow the steps below: - -1. Run the following statement to disable the automatic statistics [cluster setting](cluster-settings.html): - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false; - ~~~ - -2. Use the [`SHOW STATISTICS`](show-statistics.html) statement to view automatically generated statistics. - -3. Delete the automatically generated statistics using the following statement: - - {% include copy-clipboard.html %} - ~~~ sql - > DELETE FROM system.table_statistics WHERE true; - ~~~ - -4. Restart the nodes in your cluster to clear the statistics caches. - -For instructions showing how to manually generate statistics, see the examples in the [`CREATE STATISTICS` documentation](create-statistics.html). - -#### Controlling histogram collection - -New in v19.2: By default, the optimizer collects histograms for all index columns (specifically the first column in each index) during automatic statistics collection. If a single column statistic is explicitly requested using manual invocation of [`CREATE STATISTICS`](create-statistics.html), a histogram will be collected, regardless of whether or not the column is part of an index. - -If you are an advanced user and need to disable histogram collection for troubleshooting or performance tuning reasons, change the [`sql.stats.histogram_collection.enabled` cluster setting](cluster-settings.html) by running [`SET CLUSTER SETTING`](set-cluster-setting.html) as follows: - -{% include copy-clipboard.html %} -~~~ sql -SET CLUSTER SETTING sql.stats.histogram_collection.enabled = false; -~~~ - -{{site.data.alerts.callout_info}} -When `sql.stats.histogram_collection.enabled` is set to `false`, histograms are never collected, either as part of automatic statistics collection or by manual invocation of [`CREATE STATISTICS`](create-statistics.html). -{{site.data.alerts.end}} - -## Query plan cache - -CockroachDB uses a cache for the query plans generated by the optimizer. This can lead to faster query execution since the database can reuse a query plan that was previously calculated, rather than computing a new plan each time a query is executed. - -The query plan cache is enabled by default. To disable it, execute the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING sql.query_cache.enabled = false; -~~~ - -Finally, note that only the following statements use the plan cache: - -- [`SELECT`](select-clause.html) -- [`INSERT`](insert.html) -- [`UPDATE`](update.html) -- [`UPSERT`](upsert.html) -- [`DELETE`](delete.html) - -## Join reordering - -The cost-based optimizer will explore additional join orderings in an attempt to find the lowest-cost execution plan for a query involving multiple joins, which can lead to significantly better performance in some cases. - -Because this process leads to an exponential increase in the number of possible execution plans for such queries, it's only used to reorder subtrees containing 4 or fewer joins by default. - -To change this setting, which is controlled by the `reorder_joins_limit` [session variable](set-vars.html), run the statement shown below. To disable this feature, set the variable to `0`. - -{% include copy-clipboard.html %} -~~~ sql -> SET reorder_joins_limit = 6; -~~~ - -{{site.data.alerts.callout_danger}} -We strongly recommend not setting this value higher than 8 to avoid performance degradation. If set too high, the cost of generating and costing execution plans can end up dominating the total execution time of the query. -{{site.data.alerts.end}} - -For more information about the difficulty of selecting an optimal join ordering, see our blog post [An Introduction to Join Ordering](https://www.cockroachlabs.com/blog/join-ordering-pt1/). - -## Join hints - -The optimizer supports hint syntax to force the use of a specific join algorithm. The algorithm is specified between the join type (`INNER`, `LEFT`, etc.) and the `JOIN` keyword, for example: - -- `INNER HASH JOIN` -- `OUTER MERGE JOIN` -- `LEFT LOOKUP JOIN` -- `CROSS HASH JOIN` - -Note that the hint cannot be specified with a bare hint keyword (e.g., `MERGE`) - in that case, the `INNER` keyword must be added. For example, `a INNER MERGE JOIN b` will work, but `a MERGE JOIN b` will not work. - -{{site.data.alerts.callout_info}} -Join hints cannot be specified with a bare hint keyword (e.g., `MERGE`) due to SQL's implicit `AS` syntax. If you're not careful, you can make `MERGE` be an alias for a table; for example, `a MERGE JOIN b` will be interpreted as having an implicit `AS` and be executed as `a AS MERGE JOIN b`, which is just a long way of saying `a JOIN b`. Because the resulting query might execute without returning any hint-related error (because it is valid SQL), it will seem like the join hint "worked", but actually it didn't affect which join algorithm was used. In this case, the correct syntax is `a INNER MERGE JOIN b`. -{{site.data.alerts.end}} - -### Supported join algorithms - -- `HASH`: Forces a hash join; in other words, it disables merge and lookup joins. A hash join is always possible, even if there are no equality columns - CockroachDB considers the nested loop join with no index a degenerate case of the hash join (i.e., a hash table with one bucket). - -- `MERGE`: Forces a merge join, even if it requires re-sorting both sides of the join. - -- `LOOKUP`: Forces a lookup join into the right side; the right side must be a table with a suitable index. Note that `LOOKUP` can only be used with `INNER` and `LEFT` joins. - -If it is not possible to use the algorithm specified in the hint, an error is signaled. - -### Additional considerations - -- This syntax is consistent with the [SQL Server syntax for join hints](https://docs.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-join?view=sql-server-2017), except that: - - - SQL Server uses `LOOP` instead of `LOOKUP`. - - - CockroachDB does not support `LOOP` and instead supports `LOOKUP` for the specific case of nested loop joins with an index. - -- When a join hint is specified, the two tables will not be reordered by the optimizer. The reordering behavior has the following characteristics, which can be affected by hints: - - - Given `a JOIN b`, CockroachDB will not try to commute to `b JOIN a`. This means that you will need to pay attention to this ordering, which is especially important for lookup joins. Without a hint, `a JOIN b` might be executed as `b INNER LOOKUP JOIN a` using an index into `a`, whereas `a INNER LOOKUP JOIN b` requires an index into `b`. - - - `(a JOIN b) JOIN c` might be changed to `a JOIN (b JOIN c)`, but this does not happen if `a JOIN b` uses a hint; the hint forces that particular join to happen as written in the query. - -- Hint usage should be reconsidered with each new release of CockroachDB. Due to improvements in the optimizer, hints specified to work with an older version may cause decreased performance in a newer version. - -## Preferring the nearest index - -Given multiple identical [indexes](indexes.html) that have different locality constraints using [replication zones](configure-replication-zones.html), the optimizer will prefer the index that is closest to the gateway node that is planning the query. In a properly configured geo-distributed cluster, this can lead to performance improvements due to improved data locality and reduced network traffic. - -{{site.data.alerts.callout_info}} -This feature is only available to users with an [enterprise license](enterprise-licensing.html). For insight into how to use this feature to get low latency, consistent reads in multi-region deployments, see the [Duplicate Indexes](topology-follower-reads.html) topology pattern. -{{site.data.alerts.end}} - -This feature enables scenarios such as: - -- Reference data such as a table of postal codes that can be replicated to different regions, and queries will use the copy in the same region. See [Example - zone constraints](#zone-constraints) for more details. -- Optimizing for local reads (potentially at the expense of writes) by adding leaseholder preferences to your zone configuration. See [Example - leaseholder preferences](#leaseholder-preferences) for more details. - -To take advantage of this feature, you need to: - -1. Have an [enterprise license](enterprise-licensing.html). -2. Determine which data consists of reference tables that are rarely updated (such as postal codes) and can therefore be easily replicated to different regions. -3. Create multiple [secondary indexes](indexes.html) on the reference tables. **Note that these indexes must include (in key or using [`STORED`](create-index.html#store-columns)) *every* column that you wish to query**. For example, if you run `SELECT * from db.table` and not every column of `db.table` is in the set of secondary indexes you created, the optimizer will have no choice but to fall back to the primary index. -4. Create [replication zones](configure-replication-zones.html) for each index. - -With the above pieces in place, the optimizer will automatically choose the index nearest the gateway node that is planning the query. - -{{site.data.alerts.callout_info}} -The optimizer does not actually understand geographic locations, i.e., the relative closeness of the gateway node to other nodes that are located to its "east" or "west". It is matching against the [node locality constraints](configure-replication-zones.html#descriptive-attributes-assigned-to-nodes) you provided when you configured your replication zones. -{{site.data.alerts.end}} - -### Examples - -#### Zone constraints - -We can demonstrate the necessary configuration steps using a local cluster. The instructions below assume that you are already familiar with: - -- How to [start a local cluster](start-a-local-cluster.html). -- The syntax for [assigning node locality when configuring replication zones](configure-replication-zones.html#descriptive-attributes-assigned-to-nodes). -- Using [the built-in SQL client](cockroach-sql.html). - -First, start 3 local nodes as shown below. Use the [`--locality`](cockroach-start.html#locality) flag to put them each in a different region as denoted by `region=usa`, `region=eu`, etc. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --locality=region=usa --insecure --store=/tmp/node0 --listen-addr=localhost:26257 \ - --http-port=8888 --join=localhost:26257,localhost:26258,localhost:26259 --background -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --locality=region=eu --insecure --store=/tmp/node1 --listen-addr=localhost:26258 \ - --http-port=8889 --join=localhost:26257,localhost:26258,localhost:26259 --background -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --locality=region=apac --insecure --store=/tmp/node2 --listen-addr=localhost:26259 \ - --http-port=8890 --join=localhost:26257,localhost:26258,localhost:26259 --background -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init --insecure --host=localhost --port=26257 -~~~ - -Next, from the SQL client, add your organization name and enterprise license: - -{% include copy-clipboard.html %} -~~~ sh -$ cockroach sql --insecure --host=localhost --port=26257 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING cluster.organization = 'FooCorp - Local Testing'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING enterprise.license = 'xxxxx'; -~~~ - -Create a test database and table. The table will have 3 indexes into the same data. Later, we'll configure the cluster to associate each of these indexes with a different datacenter using replication zones. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE IF NOT EXISTS test; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> USE test; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -CREATE TABLE postal_codes ( - id INT PRIMARY KEY, - code STRING, - INDEX idx_eu (id) STORING (code), - INDEX idx_apac (id) STORING (code) -); -~~~ - -Next, we modify the replication zone configuration via SQL so that: - -- Nodes in the USA will use the primary key index. -- Nodes in the EU will use the `postal_codes@idx_eu` index (which is identical to the primary key index). -- Nodes in APAC will use the `postal_codes@idx_apac` index (which is also identical to the primary key index). - -{% include copy-clipboard.html %} -~~~ sql -ALTER TABLE postal_codes CONFIGURE ZONE USING constraints='["+region=usa"]'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -ALTER INDEX postal_codes@idx_eu CONFIGURE ZONE USING constraints='["+region=eu"]'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -ALTER INDEX postal_codes@idx_apac CONFIGURE ZONE USING constraints='["+region=apac"]'; -~~~ - -To verify this feature is working as expected, we'll query the database from each of our local nodes as shown below. Each node has been configured to be in a different region, and it should now be using the index pinned to that region. - -{{site.data.alerts.callout_info}} -In a geo-distributed scenario with a cluster that spans multiple datacenters, it may take time for the optimizer to fetch schemas from other nodes the first time a query is planned; thereafter, the schema should be cached locally. - -For example, if you have 11 nodes, you may see 11 queries with high latency due to schema cache misses. Once all nodes have cached the schema locally, the latencies will drop. - -This behavior may also cause the [Statements page of the Web UI](admin-ui-statements-page.html) to show misleadingly high latencies until schemas are cached locally. -{{site.data.alerts.end}} - -As expected, the node in the USA region uses the primary key index. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --host=localhost --port=26257 --database=test -e 'EXPLAIN SELECT * FROM postal_codes WHERE id=1;' -~~~ - -~~~ - tree | field | description -+------+-------+----------------------+ - scan | | - | table | postal_codes@primary - | spans | /1-/1/# -(3 rows) -~~~ - -As expected, the node in the EU uses the `idx_eu` index. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --host=localhost --port=26258 --database=test -e 'EXPLAIN SELECT * FROM postal_codes WHERE id=1;' -~~~ - -~~~ - tree | field | description -+------+-------+---------------------+ - scan | | - | table | postal_codes@idx_eu - | spans | /1-/2 -(3 rows) -~~~ - -As expected, the node in APAC uses the `idx_apac` index. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --host=localhost --port=26259 --database=test -e 'EXPLAIN SELECT * FROM postal_codes WHERE id=1;' -~~~ - -~~~ - tree | field | description -+------+-------+-----------------------+ - scan | | - | table | postal_codes@idx_apac - | spans | /1-/2 -(3 rows) -~~~ - -You'll need to make changes to the above configuration to reflect your [production environment](recommended-production-settings.html), but the concepts will be the same. - -#### Leaseholder preferences - -If you provide [leaseholder preferences](configure-replication-zones.html#lease_preferences) in addition to replication zone constraints, the optimizer will attempt to take your leaseholder preferences into account as well when selecting an index for your query. There are several factors to keep in mind: - -- Zone constraints are always respected (hard constraint), whereas lease preferences are taken into account as "additional information" -- as long as they do not contradict the zone constraints. - -- The optimizer does not consider the real-time location of leaseholders when selecting an index; it is pattern matching on the text values passed in the configuration (e.g., the [`ALTER INDEX`](alter-index.html) statements shown below). For the same reason, the optimizer only matches against the first locality in your `lease_preferences` array. - -- The optimizer may use an index that satisfies your leaseholder preferences even though that index has moved to a different node/region due to [leaseholder rebalancing](architecture/replication-layer.html#leaseholder-rebalancing). This can cause slower performance than you expected. Therefore, you should only use this feature if you’re confident you know where the leaseholders will end up based on your cluster's usage patterns. We recommend thoroughly testing your configuration to ensure the optimizer is selecting the index(es) you expect. - -In this example, we'll set up an authentication service using the access token / refresh token pattern from [OAuth 2](https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2). To support fast local reads in our geo-distributed use case, we will have 3 indexes into the same authentication data: one for each region of our cluster. We configure each index using zone configurations and lease preferences so that the optimizer will use the local index for better performance. - -The instructions below assume that you are already familiar with: - -- How to [start a local cluster](start-a-local-cluster.html). -- The syntax for [assigning node locality when configuring replication zones](configure-replication-zones.html#descriptive-attributes-assigned-to-nodes). -- Using [the built-in SQL client](cockroach-sql.html). - -First, start 3 local nodes as shown below. Use the [`--locality`](cockroach-start.html#locality) flag to put them each in a different region. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --locality=region=us-east --insecure --store=/tmp/node0 --listen-addr=localhost:26257 \ - --http-port=8888 --join=localhost:26257,localhost:26258,localhost:26259 --background -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --locality=region=us-central --insecure --store=/tmp/node1 --listen-addr=localhost:26258 \ - --http-port=8889 --join=localhost:26257,localhost:26258,localhost:26259 --background -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --locality=region=us-west --insecure --store=/tmp/node2 --listen-addr=localhost:26259 \ - --http-port=8890 --join=localhost:26257,localhost:26258,localhost:26259 --background -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init --insecure --host=localhost --port=26257 -~~~ - -From the SQL client, add your organization name and enterprise license: - -{% include copy-clipboard.html %} -~~~ sh -$ cockroach sql --insecure --host=localhost --port=26257 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING cluster.organization = 'FooCorp - Local Testing'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING enterprise.license = 'xxxxx'; -~~~ - -Create an authentication database and table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE if NOT EXISTS auth; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> USE auth; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE token ( - token_id VARCHAR(100) NULL, - access_token VARCHAR(4000) NULL, - refresh_token VARCHAR(4000) NULL - ); -~~~ - -Create the indexes for each region: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX token_id_west_idx ON token (token_id) STORING (access_token, refresh_token); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX token_id_central_idx ON token (token_id) STORING (access_token, refresh_token); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX token_id_east_idx ON token (token_id) STORING (access_token, refresh_token); -~~~ - -Enter zone configurations to distribute replicas across the cluster as follows: - -- For the "East" index, store 2 replicas in the East, 2 in Central, and 1 in the West. Further, prefer that the leaseholders for that index live in the East or, failing that, in the Central region. -- Follow the same replica and leaseholder patterns for each of the Central and West regions. - -The idea is that, for example, `token_id_east_idx` will have sufficient replicas (2/5) so that even if one replica goes down, the leaseholder will stay in the East region. That way, if a query comes in that accesses the columns covered by that index from the East gateway node, the optimizer will select `token_id_east_idx` for fast reads. - -{{site.data.alerts.callout_info}} -The `ALTER TABLE` statement below is not required since it's later made redundant by the `token_id_west_idx` index. In production, you might go with the `ALTER TABLE` to put your table's lease preferences in the West, and then create only 2 indexes (for East and Central); however, the use of 3 indexes makes the example easier to understand. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE token CONFIGURE ZONE USING - num_replicas = 5, constraints = '{+region=us-east: 1, +region=us-central: 2, +region=us-west: 2}', lease_preferences = '[[+region=us-west], [+region=us-central]]'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> ALTER INDEX token_id_east_idx CONFIGURE ZONE USING num_replicas = 5, - constraints = '{+region=us-east: 2, +region=us-central: 2, +region=us-west: 1}', lease_preferences = '[[+region=us-east], [+region=us-central]]'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> ALTER INDEX token_id_central_idx CONFIGURE ZONE USING num_replicas = 5, - constraints = '{+region=us-east: 2, +region=us-central: 2, +region=us-west: 1}', lease_preferences = '[[+region=us-central], [+region=us-east]]'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> ALTER INDEX token_id_west_idx CONFIGURE ZONE USING num_replicas = 5, - constraints = '{+region=us-west: 2, +region=us-central: 2, +region=us-east: 1}', lease_preferences = '[[+region=us-west], [+region=us-central]]'; -~~~ - -Next let's [check our zone configurations](show-zone-configurations.html) to make sure they match our expectation: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW ZONE CONFIGURATIONS; -~~~ - -The output should include the following: - -~~~ -TABLE auth.public.token | ALTER TABLE auth.public.token CONFIGURE ZONE USING - | num_replicas = 5, - | constraints = '{+region=us-central: 2, +region=us-east: 1, +region=us-west: 2}', - | lease_preferences = '[[+region=us-west], [+region=us-central]]' -INDEX auth.public.token@token_id_east_idx | ALTER INDEX auth.public.token@token_id_east_idx CONFIGURE ZONE USING - | num_replicas = 5, - | constraints = '{+region=us-central: 2, +region=us-east: 2, +region=us-west: 1}', - | lease_preferences = '[[+region=us-east], [+region=us-central]]' -INDEX auth.public.token@token_id_central_idx | ALTER INDEX auth.public.token@token_id_central_idx CONFIGURE ZONE USING - | num_replicas = 5, - | constraints = '{+region=us-central: 2, +region=us-east: 2, +region=us-west: 1}', - | lease_preferences = '[[+region=us-central], [+region=us-east]]' -INDEX auth.public.token@token_id_west_idx | ALTER INDEX auth.public.token@token_id_west_idx CONFIGURE ZONE USING - | num_replicas = 5, - | constraints = '{+region=us-central: 2, +region=us-east: 1, +region=us-west: 2}', - | lease_preferences = '[[+region=us-west], [+region=us-central]]' -~~~ - -Now that we've set up our indexes the way we want them, we need to insert some data. The first statement below inserts 10,000 rows of placeholder data; the second inserts a row with a specific UUID string that we'll later query against to check which index is used. - -{{site.data.alerts.callout_info}} -On a freshly created cluster like this one, you may need to wait a moment after adding the data to give [automatic statistics](#table-statistics) time to update. Then, the optimizer can generate a query plan that uses the expected index. -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> INSERT - INTO - token (token_id, access_token, refresh_token) - SELECT - gen_random_uuid()::STRING, - gen_random_uuid()::STRING, - gen_random_uuid()::STRING - FROM - generate_series(1, 10000); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT - INTO - token (token_id, access_token, refresh_token) - VALUES - ( - '2E1B5BFE-6152-11E9-B9FD-A7E0F13211D9', - '49E36152-6152-11E9-8CDC-3682F23211D9', - '4E0E91B6-6152-11E9-BAC1-3782F23211D9' - ); -~~~ - -Finally, we [`EXPLAIN`](explain.html) a [selection query](selection-queries.html) from each node to verify which index is being queried against. For example, when running the query shown below against the `us-west` node, we expect it to use the `token_id_west_idx` index. - -{% include copy-clipboard.html %} -~~~ sh -$ cockroach sql --insecure --host=localhost --port=26259 --database=auth # "West" node -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN - SELECT - access_token, refresh_token - FROM - token - WHERE - token_id = '2E1B5BFE-6152-11E9-B9FD-A7E0F13211D9'; -~~~ - -~~~ - tree | field | description -+-----------+-------+-------------------------------------------------------------------------------------------+ - render | | - └── scan | | - | table | token@token_id_west_idx - | spans | /"2E1B5BFE-6152-11E9-B9FD-A7E0F13211D9"-/"2E1B5BFE-6152-11E9-B9FD-A7E0F13211D9"/PrefixEnd -(4 rows) - -Time: 787µs -~~~ - -Similarly, queries from the `us-east` node should use the `token_id_east_idx` index (and the same should be true for `us-central`). - -{% include copy-clipboard.html %} -~~~ sh -$ cockroach sql --insecure --host=localhost --port=26257 --database=auth # "East" node -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN - SELECT - access_token, refresh_token - FROM - token - WHERE - token_id = '2E1B5BFE-6152-11E9-B9FD-A7E0F13211D9'; -~~~ - -~~~ - tree | field | description -+-----------+-------+-------------------------------------------------------------------------------------------+ - render | | - └── scan | | - | table | token@token_id_east_idx - | spans | /"2E1B5BFE-6152-11E9-B9FD-A7E0F13211D9"-/"2E1B5BFE-6152-11E9-B9FD-A7E0F13211D9"/PrefixEnd -(4 rows) - -Time: 619µs -~~~ - -You'll need to make changes to the above configuration to reflect your [production environment](recommended-production-settings.html), but the concepts will be the same. - -## See also - -- [`SET (session variable)`](set-vars.html) -- [`SET CLUSTER SETTING`](set-cluster-setting.html) -- [`RESET CLUSTER SETTING`](reset-cluster-setting.html) -- [`SHOW (session variable)`](show-vars.html) -- [`CREATE STATISTICS`](create-statistics.html) -- [`SHOW STATISTICS`](show-statistics.html) -- [`EXPLAIN`](explain.html) diff --git a/src/current/v19.2/create-a-file-server.md b/src/current/v19.2/create-a-file-server.md deleted file mode 100644 index 753f53bd275..00000000000 --- a/src/current/v19.2/create-a-file-server.md +++ /dev/null @@ -1,125 +0,0 @@ ---- -title: Create a File Server for Imports and Backups -summary: Learn how to create a simple file server for use with CockroachDB IMPORT and BACKUP -toc: true ---- - -If you need a location to store files for the [`IMPORT`](import.html) process or [CockroachDB enterprise backups](backup.html), but do not have access to (or simply cannot use) cloud storage providers, you can run a local file server. You can then use this file server by leveraging support for our [HTTP Export Storage API](#http-export-storage-api). - -This is especially useful for: - -- Implementing a compatibility layer in front of custom or proprietary storage providers for which CockroachDB does not yet have built-in support -- Using on-premises storage - -## HTTP export storage API - -CockroachDB tasks that require reading or writing external files (such as [`IMPORT`](import.html) and [`BACKUP`](backup.html)) can use the HTTP Export Storage API by prefacing the address with `http`, e.g., `http://fileserver/mnt/cockroach-exports`. - -This API uses the `GET`, `PUT` and `DELETE` methods. This behaves like you would expect typical HTTP requests to work. After a `PUT` request to some path, a subsequent `GET` request should return the content sent in the `PUT` request body, at least until a `DELETE` request is received for that path. - -## Examples - -You can use any file server software that supports `GET`, `PUT` and `DELETE` methods, but we've included code samples for common ones: - -- [Using PHP with `IMPORT`](#using-php-with-import) -- [Using Python with `IMPORT`](#using-python-with-import) -- [Using Ruby with `IMPORT`](#using-ruby-with-import) -- [Using Caddy as a file server](#using-caddy-as-a-file-server) -- [Using nginx as a file server](#using-nginx-as-a-file-server) - -{{site.data.alerts.callout_info}}We do not recommend using any machines running cockroach as file servers. Using machines that are running cockroach as file servers could negatively impact performance if I/O operations exceed capacity.{{site.data.alerts.end}} - -### Using PHP with `IMPORT` - -The PHP language has an HTTP server built in. You can serve local files using the commands below. For more information about how to import these locally served files, see the documentation for the [`IMPORT`][import] statement. - -{% include copy-clipboard.html %} -~~~ shell -$ cd /path/to/data -$ php -S 127.0.0.1:3000 # files available at e.g., 'http://localhost:3000/data.sql' -~~~ - -### Using Python with `IMPORT` - -The Python language has an HTTP server included in the standard library. You can serve local files using the commands below. For more information about how to import these locally served files, see the documentation for the [`IMPORT`][import] statement. - -{% include copy-clipboard.html %} -~~~ shell -$ cd /path/to/data -$ python -m SimpleHTTPServer 3000 # files available at e.g., 'http://localhost:3000/data.sql' -~~~ - -If you use Python 3, try: - -{% include copy-clipboard.html %} -~~~ shell -$ cd /path/to/data -$ python -m http.server 3000 -~~~ - -### Using Ruby with `IMPORT` - -The Ruby language has an HTTP server included in the standard library. You can serve local files using the commands below. For more information about how to import these locally served files, see the documentation for the [`IMPORT`][import] statement. - -{% include copy-clipboard.html %} -~~~ shell -$ cd /path/to/data -$ ruby -run -ehttpd . -p3000 # files available at e.g., 'http://localhost:3000/data.sql' -~~~ - -### Using Caddy as a file server - -1. [Download the Caddy web server](https://caddyserver.com/download). Before downloading, in the **Customize your build** step, open the list of **Plugins** and make sure to check the `http.upload` option. - -2. Copy the `caddy` binary to the directory containing the files you want to serve, and run it [with an upload directive](https://caddyserver.com/docs/http.upload), either in the command line or via [Caddyfile](https://caddyserver.com/docs/caddyfile). - -- Command line example (with no TLS): - {% include copy-clipboard.html %} - ~~~ shell - $ caddy -root /mnt/cockroach-exports "upload / {" 'to "/mnt/cockroach-exports"' 'yes_without_tls' "}" - ~~~ -- `Caddyfile` example (using a key and cert): - {% include copy-clipboard.html %} - ~~~ shell - tls key cert - root "/mnt/cockroach-exports" - upload / { - to "/mnt/cockroach-exports" - } - ~~~ - -For more information about Caddy, see [its documentation](https://caddyserver.com/docs). - -### Using nginx as a file server - -1. Install `nginx` with the `webdav` module (often included in `-full` or similarly named packages in various distributions). - -2. In the `nginx.conf` file, add a `dav_methods PUT DELETE` directive. For example: - - {% include copy-clipboard.html %} - ~~~ nginx - events { - worker_connections 1024; - } - http { - server { - listen 20150; - location / { - dav_methods PUT DELETE; - root /mnt/cockroach-exports; - sendfile on; - sendfile_max_chunk 1m; - } - } - } - ~~~ - -## See also - -- [`IMPORT`][import] -- [`BACKUP`](backup.html) (*Enterprise only*) -- [`RESTORE`](restore.html) (*Enterprise only*) - - - -[import]: import.html diff --git a/src/current/v19.2/create-changefeed.md b/src/current/v19.2/create-changefeed.md deleted file mode 100644 index e59557a324c..00000000000 --- a/src/current/v19.2/create-changefeed.md +++ /dev/null @@ -1,283 +0,0 @@ ---- -title: CREATE CHANGEFEED -summary: The CREATE CHANGEFEED statement creates a changefeed of row-level change subscriptions in a configurable format to a configurable sink. -toc: true ---- - -{{site.data.alerts.callout_info}} -`CREATE CHANGEFEED` is an [enterprise-only](enterprise-licensing.html) feature. For the core version, see [`EXPERIMENTAL CHANGEFEED FOR`](changefeed-for.html). -{{site.data.alerts.end}} - -The `CREATE CHANGEFEED` [statement](sql-statements.html) creates a new enterprise changefeed, which targets an allowlist of tables, called "watched rows". Every change to a watched row is emitted as a record in a configurable format (`JSON` or Avro) to a configurable sink ([Kafka](https://kafka.apache.org/) or a [cloud storage sink](#cloud-storage-sink)). You can [create](#create-a-changefeed-connected-to-kafka), [pause](#pause-a-changefeed), [resume](#resume-a-paused-changefeed), or [cancel](#cancel-a-changefeed) an enterprise changefeed. - -For more information, see [Change Data Capture](change-data-capture.html). - -## Required privileges - -Changefeeds can only be created by superusers, i.e., [members of the `admin` role](authorization.html#create-and-manage-roles). The admin role exists by default with `root` as the member. - -## Considerations - -- In most cases, each version of a row will be emitted once. However, some infrequent conditions (e.g., node failures, network partitions) will cause them to be repeated. This gives our changefeeds an at-least-once delivery guarantee. For more information, see [Change Data Capture - Ordering Guarantees](change-data-capture.html#ordering-guarantees). - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/create_changefeed.html %} -
      - -## Parameters - -Parameter | Description -----------|------------ -`table_name` | The name of the table (or tables in a comma separated list) to create a changefeed for. -`sink` | The location of the configurable sink. The scheme of the URI indicates the type. For more information, see [Sink URI](#sink-uri) below. -`option` / `value` | For a list of available options and their values, see [Options](#options) below. - - - -### Sink URI - -The sink URI follows the basic format of: - -~~~ -'[scheme]://[host]:[port]?[query_parameters]' -~~~ - -The `scheme` can be [`kafka`](#kafka) or any [cloud storage sink](#cloud-storage-sink). - -#### Kafka - -Example of a Kafka sink URI: - -~~~ -'kafka://broker.address.com:9092?topic_prefix=bar_&tls_enabled=true&ca_cert=LS0tLS1CRUdJTiBDRVJUSUZ&sasl_enabled=true&sasl_user=petee&sasl_password=bones' -~~~ - -Query parameters include: - -Parameter | Value | Description -----------+-------+--------------- -`topic_prefix` | [`STRING`](string.html) | Adds a prefix to all topic names.

      For example, `CREATE CHANGEFEED FOR TABLE foo INTO 'kafka://...?topic_prefix=bar_'` would emit rows under the topic `bar_foo` instead of `foo`. -`tls_enabled=true` | [`BOOL`](bool.html) | If `true`, enable Transport Layer Security (TLS) on the connection to Kafka. This can be used with a `ca_cert` (see below). -`ca_cert` | [`STRING`](string.html) | The base64-encoded `ca_cert` file.

      Note: To encode your `ca.cert`, run `base64 -w 0 ca.cert`. -`client_cert` | [`STRING`](string.html) | The base64-encoded Privacy Enhanced Mail (PEM) certificate. This is used with `client_key`. -`client_key` | [`STRING`](string.html) | The base64-encoded private key for the PEM certificate. This is used with `client_cert`. -`sasl_enabled` | [`BOOL`](bool.html) | If `true`, [use SASL/PLAIN to authenticate](https://docs.confluent.io/current/kafka/authentication_sasl/authentication_sasl_plain.html). This requires a `sasl_user` and `sasl_password` (see below). -`sasl_user` | [`STRING`](string.html) | Your SASL username. -`sasl_password` | [`STRING`](string.html) | Your SASL password. - -#### Cloud storage sink - -Use a cloud storage sink to deliver changefeed data to OLAP or big data systems without requiring transport via Kafka. - -{{site.data.alerts.callout_info}} -Currently, cloud storage sinks only work with `JSON` and emits newline-delimited `JSON` files. -{{site.data.alerts.end}} - -Example of a cloud storage sink (i.e., AWS S3) URI: - -~~~ -'experimental-s3://test-s3encryption/test?AWS_ACCESS_KEY_ID=ABCDEFGHIJKLMNOPQ&AWS_SECRET_ACCESS_KEY=LS0tLS1CRUdJTiBDRVJUSUZ' -~~~ - -{{site.data.alerts.callout_info}} -The `scheme` for a cloud storage sink should be prepended with `experimental-`. -{{site.data.alerts.end}} - -Any of the cloud storages below can be used as a sink: - -{% include {{ page.version.version }}/misc/external-urls.md %} - -### Options - -Option | Value | Description --------|-------|------------ -`updated` | N/A | Include updated timestamps with each row.

      If a `cursor` is provided, the "updated" timestamps will match the [MVCC](architecture/storage-layer.html#mvcc) timestamps of the emitted rows, and there is no initial scan. If a `cursor` is not provided, the changefeed will perform an initial scan (as of the time the changefeed was created), and the "updated" timestamp for each change record emitted in the initial scan will be the timestamp of the initial scan. Similarly, when a [backfill is performed for a schema change](change-data-capture.html#schema-changes-with-column-backfill), the "updated" timestamp is set to the first timestamp for when the new schema is valid. -`resolved` | [`INTERVAL`](interval.html) | Periodically emit resolved timestamps to the changefeed. Optionally, set a minimum duration between emitting resolved timestamps. If unspecified, all resolved timestamps are emitted.

      Example: `resolved='10s'` -`envelope` | `key_only` / `wrapped` | Use `key_only` to emit only the key and no value, which is faster if you only want to know when the key changes.

      Default: `envelope=wrapped` -`cursor` | [Timestamp](as-of-system-time.html#parameters) | Emits any changes after the given timestamp, but does not output the current state of the table first. If `cursor` is not specified, the changefeed starts by doing an initial scan of all the watched rows and emits the current value, then moves to emitting any changes that happen after the scan.

      When starting a changefeed at a specific `cursor`, the `cursor` cannot be before the configured garbage collection window (see [`gc.ttlseconds`](configure-replication-zones.html#replication-zone-variables)) for the table you're trying to follow; otherwise, the changefeed will error. With default garbage collection settings, this means you cannot create a changefeed that starts more than 25 hours in the past.

      `cursor` can be used to [start a new changefeed where a previous changefeed ended.](#start-a-new-changefeed-where-another-ended)

      Example: `CURSOR='1536242855577149065.0000000000'` -`format` | `json` / `experimental_avro` | Format of the emitted record. Currently, support for [Avro is limited and experimental](#avro-limitations). For mappings of CockroachDB types to Avro types, [see the table below](#avro-types).

      Default: `format=json`. -`confluent_schema_registry` | Schema Registry address | The [Schema Registry](https://docs.confluent.io/current/schema-registry/docs/index.html#sr) address is required to use `experimental_avro`. -`key_in_value` | N/A | Makes the [primary key](primary-key.html) of a deleted row recoverable in sinks where each message has a value but not a key (most have a key and value in each message). `key_in_value` is automatically used for these sinks (currently only [cloud storage sinks](#cloud-storage-sink)). - -#### Avro limitations - -Currently, support for Avro is limited and experimental. Below is a list of unsupported SQL types and values for Avro changefeeds: - -- [Decimals](decimal.html) must have precision specified. -- [Decimals](decimal.html) with `NaN` or infinite values cannot be written in Avro. - - {{site.data.alerts.callout_info}} - To avoid `NaN` or infinite values, add a [`CHECK` constraint](check.html) to prevent these values from being inserted into decimal columns. - {{site.data.alerts.end}} - -- [`TIME`, `DATE`, `INTERVAL`](https://github.com/cockroachdb/cockroach/issues/32472), [`UUID`, `INET`](https://github.com/cockroachdb/cockroach/issues/34417), [`ARRAY`](https://github.com/cockroachdb/cockroach/issues/34420), [`JSONB`](https://github.com/cockroachdb/cockroach/issues/34421), `BIT`, and collated `STRING` are not supported in Avro yet. - -#### Avro types - -Below is a mapping of CockroachDB types to Avro types: - -CockroachDB Type | Avro Type | Avro Logical Type ------------------+-----------+--------------------- -[`INT`](int.html) | [`LONG`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | -[`BOOL`](bool.html) | [`BOOLEAN`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | -[`FLOAT`](float.html) | [`DOUBLE`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | -[`STRING`](string.html) | [`STRING`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | -[`DATE`](date.html) | [`INT`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | [`DATE`](https://avro.apache.org/docs/1.8.1/spec.html#Date) -[`TIME`](time.html) | [`LONG`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | [`TIME-MICROS`](https://avro.apache.org/docs/1.8.1/spec.html#Time+%28microsecond+precision%29) -[`TIMESTAMP`](timestamp.html) | [`LONG`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | [`TIME-MICROS`](https://avro.apache.org/docs/1.8.1/spec.html#Time+%28microsecond+precision%29) -[`TIMESTAMPTZ`](timestamp.html) | [`LONG`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | [`TIME-MICROS`](https://avro.apache.org/docs/1.8.1/spec.html#Time+%28microsecond+precision%29) -[`DECIMAL`](decimal.html) | [`BYTES`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | [`DECIMAL`](https://avro.apache.org/docs/1.8.1/spec.html#Decimal) -[`UUID`](uuid.html) | [`STRING`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | -[`INET`](inet.html) | [`STRING`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | -[`JSONB`](jsonb.html) | [`STRING`](https://avro.apache.org/docs/1.8.1/spec.html#schema_primitive) | - -## Responses - -The messages (i.e., keys and values) emitted to a Kafka topic are specific to the [`envelope`](#options). The default format is `wrapped`, and the output messages are composed of the following: - -- **Key**: An array always composed of the row's `PRIMARY KEY` field(s) (e.g., `[1]` for `JSON` or `{"id":{"long":1}}` for Avro). -- **Value**: - - One of three possible top-level fields: - - `after`, which contains the state of the row after the update (or `null`' for `DELETE`s). - - `updated`, which contains the updated timestamp. - - `resolved`, which is emitted for records representing resolved timestamps. These records do not include an "after" value since they only function as checkpoints. - - For [`INSERT`](insert.html) and [`UPDATE`](update.html), the current state of the row inserted or updated. - - For [`DELETE`](delete.html), `null`. - -For example: - -Statement | Response ------------------------------------------------+----------------------------------------------------------------------- -`INSERT INTO office_dogs VALUES (1, 'Petee');` | JSON: `[1] {"after": {"id": 1, "name": "Petee"}}`
      Avro: `{"id":{"long":1}} {"after":{"office_dogs":{"id":{"long":1},"name":{"string":"Petee"}}}}` -`DELETE FROM office_dogs WHERE name = 'Petee'` | JSON: `[1] {"after": null}`
      Avro: `{"id":{"long":1}} {"after":null}` - -## Examples - -### Create a changefeed connected to Kafka - -{% include copy-clipboard.html %} -~~~ sql -> CREATE CHANGEFEED FOR TABLE name, name2, name3 - INTO 'kafka://host:port' - WITH updated, resolved; -~~~ -~~~ -+--------------------+ -| job_id | -+--------------------+ -| 360645287206223873 | -+--------------------+ -(1 row) -~~~ - -For more information on how to create a changefeed connected to Kafka, see [Change Data Capture](change-data-capture.html#create-a-changefeed-connected-to-kafka). - -### Create a changefeed connected to Kafka using Avro - -{% include copy-clipboard.html %} -~~~ sql -> CREATE CHANGEFEED FOR TABLE name, name2, name3 - INTO 'kafka://host:port' - WITH format = experimental_avro, confluent_schema_registry = ; -~~~ -~~~ -+--------------------+ -| job_id | -+--------------------+ -| 360645287206223873 | -+--------------------+ -(1 row) -~~~ - -For more information on how to create a changefeed that emits an [Avro](https://avro.apache.org/docs/1.8.2/spec.html) record, see [Change Data Capture](change-data-capture.html#create-a-changefeed-connected-to-kafka-using-avro). - -### Create a changefeed connected to a cloud storage sink - -{% include {{ page.version.version }}/misc/experimental-warning.md %} - -{% include copy-clipboard.html %} -~~~ sql -> CREATE CHANGEFEED FOR TABLE name, name2, name3 - INTO 'experimental-scheme://host?parameters' - WITH updated, resolved; -~~~ -~~~ -+--------------------+ -| job_id | -+--------------------+ -| 360645287206223873 | -+--------------------+ -(1 row) -~~~ - -For more information on how to create a changefeed connected to a cloud storage sink, see [Change Data Capture](change-data-capture.html#create-a-changefeed-connected-to-a-cloud-storage-sink). - -### Manage a changefeed - -Use the following SQL statements to pause, resume, and cancel a changefeed. - -{{site.data.alerts.callout_info}} -Changefeed-specific SQL statements (e.g., `CANCEL CHANGEFEED`) will be added in the future. -{{site.data.alerts.end}} - -#### Pause a changefeed - -{% include copy-clipboard.html %} -~~~ sql -> PAUSE JOB job_id; -~~~ - -For more information, see [`PAUSE JOB`](pause-job.html). - -#### Resume a paused changefeed - -{% include copy-clipboard.html %} -~~~ sql -> RESUME JOB job_id; -~~~ - -For more information, see [`RESUME JOB`](resume-job.html). - -#### Cancel a changefeed - -{% include copy-clipboard.html %} -~~~ sql -> CANCEL JOB job_id; -~~~ - -For more information, see [`CANCEL JOB`](cancel-job.html). - -### Start a new changefeed where another ended - -Find the [high-water timestamp](change-data-capture.html#monitor-a-changefeed) for the ended changefeed: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM crdb_internal.jobs WHERE job_id = ; -~~~ -~~~ - job_id | job_type | ... | high_water_timestamp | error | coordinator_id -+--------------------+------------+ ... +--------------------------------+-------+----------------+ - 383870400694353921 | CHANGEFEED | ... | 1537279405671006870.0000000000 | | 1 -(1 row) -~~~ - -Use the `high_water_timestamp` to start the new changefeed: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE CHANGEFEED FOR TABLE name, name2, name3 - INTO 'kafka//host:port' - WITH cursor = ''; -~~~ - -Note that because the cursor is provided, the initial scan is not performed. - -## See also - -- [Change Data Capture](change-data-capture.html) -- [Other SQL Statements](sql-statements.html) -- [Changefeed Dashboard](admin-ui-cdc-dashboard.html) diff --git a/src/current/v19.2/create-database.md b/src/current/v19.2/create-database.md deleted file mode 100644 index bd7598561ad..00000000000 --- a/src/current/v19.2/create-database.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -title: CREATE DATABASE -summary: The CREATE DATABASE statement creates a new CockroachDB database. -toc: true ---- - -The `CREATE DATABASE` [statement](sql-statements.html) creates a new CockroachDB database. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -Only members of the `admin` role can create new databases. By default, the `root` user belongs to the `admin` role. - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/create_database.html %} -
      - -## Parameters - -Parameter | Description -----------|------------ -`IF NOT EXISTS` | Create a new database only if a database of the same name does not already exist; if one does exist, do not return an error. -`name` | The name of the database to create, which [must be unique](#create-fails-name-already-in-use) and follow these [identifier rules](keywords-and-identifiers.html#identifiers). -`encoding` | The `CREATE DATABASE` statement accepts an optional `ENCODING` clause for compatibility with PostgreSQL, but `UTF-8` is the only supported encoding. The aliases `UTF8` and `UNICODE` are also accepted. Values should be enclosed in single quotes and are case-insensitive.

      Example: `CREATE DATABASE bank ENCODING = 'UTF-8'`. - -## Example - -### Create a database - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE bank; -~~~ - -{% include copy-clipboard.html %} -~~~ -> SHOW DATABASES; -~~~ - -~~~ -+---------------+ -| database_name | -+---------------+ -| bank | -| defaultdb | -| postgres | -| system | -+---------------+ -(4 rows) -~~~ - -### Create fails (name already in use) - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE bank; -~~~ - -~~~ -pq: database "bank" already exists -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE IF NOT EXISTS bank; -~~~ - -SQL does not generate an error, but instead responds `CREATE DATABASE` even though a new database wasn't created. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW DATABASES; -~~~ - -~~~ -+---------------+ -| database_name | -+---------------+ -| bank | -| defaultdb | -| postgres | -| system | -+---------------+ -(4 rows) -~~~ - -## See also - -- [`SHOW DATABASES`](show-databases.html) -- [`RENAME DATABASE`](rename-database.html) -- [`SET DATABASE`](set-vars.html) -- [`DROP DATABASE`](drop-database.html) -- [Other SQL Statements](sql-statements.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/create-index.md b/src/current/v19.2/create-index.md deleted file mode 100644 index f49060baf6e..00000000000 --- a/src/current/v19.2/create-index.md +++ /dev/null @@ -1,188 +0,0 @@ ---- -title: CREATE INDEX -summary: The CREATE INDEX statement creates an index for a table. Indexes improve your database's performance by helping SQL quickly locate data. -toc: true ---- - -The `CREATE INDEX` [statement](sql-statements.html) creates an index for a table. [Indexes](indexes.html) improve your database's performance by helping SQL locate data without having to look through every row of a table. - -The following types cannot be included in an index key, but can be stored (and used in a covered query) using the [`STORING` or `COVERING`](create-index.html#store-columns) clause: - -- [`JSONB`](jsonb.html) -- [`ARRAY`](array.html) -- The computed [`TUPLE`](scalar-expressions.html#tuple-constructor) type, even if it is constructed from indexed fields - -To create an index on the schemaless data in a [`JSONB`](jsonb.html) column, use an [inverted index](inverted-indexes.html). - -{{site.data.alerts.callout_info}} -Indexes are automatically created for a table's [`PRIMARY KEY`](primary-key.html) and [`UNIQUE`](unique.html) columns. When querying a table, CockroachDB uses the fastest index. For more information about that process, see [Index Selection in CockroachDB](https://www.cockroachlabs.com/blog/index-selection-cockroachdb-2/). -{{site.data.alerts.end}} - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the table. - -## Synopsis - -**Standard index:** - -
      {% include {{ page.version.version }}/sql/diagrams/create_index.html %}
      - -**Inverted index:** - -
      {% include {{ page.version.version }}/sql/diagrams/create_inverted_index.html %}
      - -## Parameters - - arameter | Description -----------|------------ -`UNIQUE` | Apply the [`UNIQUE` constraint](unique.html) to the indexed columns.

      This causes the system to check for existing duplicate values on index creation. It also applies the `UNIQUE` constraint at the table level, so the system checks for duplicate values when inserting or updating data. -`INVERTED` | Create an [inverted index](inverted-indexes.html) on the schemaless data in the specified [`JSONB`](jsonb.html) column.

      You can also use the PostgreSQL-compatible syntax `USING GIN`. For more details, see [Inverted Indexes](inverted-indexes.html#creation). -`IF NOT EXISTS` | Create a new index only if an index of the same name does not already exist; if one does exist, do not return an error. -`opt_index_name`
      `index_name` | The name of the index to create, which must be unique to its table and follow these [identifier rules](keywords-and-identifiers.html#identifiers).

      If you do not specify a name, CockroachDB uses the format `__key/idx`. `key` indicates the index applies the `UNIQUE` constraint; `idx` indicates it does not. Example: `accounts_balance_idx` -`table_name` | The name of the table you want to create the index on. -`USING name` | An optional clause for compatibility with third-party tools. Accepted values for `name` are `btree` and `gin`, with `btree` for a standard secondary index and `gin` as the PostgreSQL-compatible syntax for an [inverted index](#create-inverted-indexes) on schemaless data in a `JSONB` column. -`column_name` | The name of the column you want to index. -`ASC` or `DESC`| Sort the column in ascending (`ASC`) or descending (`DESC`) order in the index. How columns are sorted affects query results, particularly when using `LIMIT`.

      __Default:__ `ASC` -`STORING ...`| Store (but do not sort) each column whose name you include.

      For information on when to use `STORING`, see [Store Columns](#store-columns). Note that columns that are part of a table's [`PRIMARY KEY`](primary-key.html) cannot be specified as `STORING` columns in secondary indexes on the table.

      `COVERING` and `INCLUDE` are aliases for `STORING` and work identically. -`opt_interleave` | You can potentially optimize query performance by [interleaving indexes](interleave-in-parent.html), which changes how CockroachDB stores your data. -`opt_partition_by` | An [enterprise-only](enterprise-licensing.html) option that lets you [define index partitions at the row level](partitioning.html). - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Examples - -{% include {{page.version.version}}/sql/movr-statements.md %} - -### Create standard indexes - -To create the most efficient indexes, we recommend reviewing: - -- [Indexes: Best Practices](indexes.html#best-practices) -- [Index Selection in CockroachDB](https://www.cockroachlabs.com/blog/index-selection-cockroachdb-2/) - -#### Single-column indexes - -Single-column indexes sort the values of a single column. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX ON users (name); -~~~ - -Because each query can only use one index, single-column indexes are not typically as useful as multiple-column indexes. - -#### Multiple-column indexes - -Multiple-column indexes sort columns in the order you list them. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX ON users (name, city); -~~~ - -To create the most useful multiple-column indexes, we recommend reviewing our [best practices](indexes.html#indexing-columns). - -#### Unique indexes - -Unique indexes do not allow duplicate values among their columns. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE UNIQUE INDEX ON users (name, id); -~~~ - -This also applies the [`UNIQUE` constraint](unique.html) at the table level, similarly to [`ALTER TABLE`](alter-table.html). The above example is equivalent to: - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE users ADD CONSTRAINT users_name_id_key UNIQUE (name, id); -~~~ - -### Create inverted indexes - -[Inverted indexes](inverted-indexes.html) can be created on schemaless data in a [`JSONB`](jsonb.html) column. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INVERTED INDEX ON promo_codes (rules); -~~~ - -The above example is equivalent to the following PostgreSQL-compatible syntax: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX ON promo_codes USING GIN (rules); -~~~ - -### Store columns - -Storing a column improves the performance of queries that retrieve (but do not filter) its values. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX ON users (city) STORING (name); -~~~ - -However, to use stored columns, queries must filter another column in the same index. For example, SQL can retrieve `name` values from the above index only when a query's `WHERE` clause filters `city`. - -### Change column sort order - -To sort columns in descending order, you must explicitly set the option when creating the index. (Ascending order is the default.) - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX ON users (city DESC, name); -~~~ - -Note that how a column is ordered in the index will affect the ordering of the index keys, and may affect the efficiency of queries that include an `ORDER BY` clause. - -### Query specific indexes - -Normally, CockroachDB selects the index that it calculates will scan the fewest rows. However, you can override that selection and specify the name of the index you want to use. To find the name, use [`SHOW INDEX`](show-index.html). - -{% include copy-clipboard.html %} -~~~ sql -> SHOW INDEX FROM users; -~~~ - -~~~ - table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit -+------------+----------------+------------+--------------+-------------+-----------+---------+----------+ - users | primary | false | 1 | city | ASC | false | false - users | primary | false | 2 | id | ASC | false | false - users | users_name_idx | true | 1 | name | ASC | false | false - users | users_name_idx | true | 2 | city | ASC | false | true - users | users_name_idx | true | 3 | id | ASC | false | true -(5 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT name FROM users@users_name_idx WHERE city='new york'; -~~~ - -~~~ - name -+------------------+ - Catherine Nelson - Devin Jordan - James Hamilton - Judy White - Robert Murphy -(5 rows) -~~~ - -## See also - -- [Indexes](indexes.html) -- [`SHOW INDEX`](show-index.html) -- [`DROP INDEX`](drop-index.html) -- [`RENAME INDEX`](rename-index.html) -- [`SHOW JOBS`](show-jobs.html) -- [Other SQL Statements](sql-statements.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/create-role.md b/src/current/v19.2/create-role.md deleted file mode 100644 index 00d0361d4ed..00000000000 --- a/src/current/v19.2/create-role.md +++ /dev/null @@ -1,64 +0,0 @@ ---- -title: CREATE ROLE (Enterprise) -summary: The CREATE ROLE statement creates SQL roles, which are groups containing any number of roles and users as members. -toc: true ---- - -The `CREATE ROLE` [statement](sql-statements.html) creates SQL [roles](authorization.html#create-and-manage-roles), which are groups containing any number of roles and users as members. You can assign privileges to roles, and all members of the role (regardless of whether if they are direct or indirect members) will inherit the role's privileges. - -{{site.data.alerts.callout_info}}CREATE ROLE is an enterprise-only feature.{{site.data.alerts.end}} - - -## Considerations - -- Role names: - - Are case-insensitive - - Must start with either a letter or underscore - - Must contain only letters, numbers, or underscores - - Must be between 1 and 63 characters. -- After creating roles, you must [grant them privileges to databases and tables](grant.html). -- Roles and users can be members of roles. -- Roles and users share the same namespace and must be unique. -- All privileges of a role are inherited by all of its members. -- There is no limit to the number of members in a role. -- Roles cannot log in. They do not have a password and cannot use certificates. -- Membership loops are not allowed (direct: `A is a member of B is a member of A` or indirect: `A is a member of B is a member of C ... is a member of A`). - -## Required privileges - -Roles can only be created by superusers, i.e., members of the `admin` role. The `admin` role exists by default with `root` as the member. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/create_role.html %}
      - -## Parameters - -| Parameter | Description | -------------|-------------- -`name` | The name of the role you want to create. Role names are case-insensitive; must start with either a letter or underscore; must contain only letters, numbers, or underscores; and must be between 1 and 63 characters.

      Note that roles and [users](create-user.html) share the same namespace and must be unique. - -## Examples - -{% include copy-clipboard.html %} -~~~ sql -> CREATE ROLE dev_ops; -~~~ -~~~ -CREATE ROLE 1 -~~~ - -After creating roles, you can [add users to the role](grant-roles.html) and [grant the role privileges](grant.html). - -## See also - -- [Authorization](authorization.html) -- [`DROP ROLE` (Enterprise)](drop-role.html) -- [`GRANT `](grant.html) -- [`REVOKE `](revoke.html) -- [`GRANT `](grant-roles.html) -- [`REVOKE `](revoke-roles.html) -- [`SHOW ROLES`](show-roles.html) -- [`SHOW USERS`](show-users.html) -- [`SHOW GRANTS`](show-grants.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/create-security-certificates-custom-ca.md b/src/current/v19.2/create-security-certificates-custom-ca.md deleted file mode 100644 index 7ec7226e907..00000000000 --- a/src/current/v19.2/create-security-certificates-custom-ca.md +++ /dev/null @@ -1,137 +0,0 @@ ---- -title: Create Security Certificates using a Custom CA -summary: A secure CockroachDB cluster uses TLS for encrypted inter-node and client-node communication. -toc: true ---- - -To secure your CockroachDB cluster's inter-node and client-node communication, you need to provide a Certificate Authority (CA) certificate that has been used to sign keys and certificates (SSLs) for: - -- Nodes -- Clients -- Admin UI (optional) - -To create these certificates and keys, use the `cockroach cert` [commands](cockroach-commands.html) with the appropriate subcommands and flags, use [`openssl` commands](https://wiki.openssl.org/index.php/), or use a [custom CA](create-security-certificates-custom-ca.html) (for example, a public CA or your organizational CA). - - - -This document discusses the following advanced use cases for using security certificates with CockroachDB: - -Approach | Use case description --------------|------------ -[UI certificate and key](#accessing-the-admin-ui-for-a-secure-cluster) | When you want to access the Admin UI for a secure cluster and avoid clicking through a warning message to get to the UI. -[Split-node certificate](#split-node-certificates) | When your organizational CA requires you to have separate certificates for the node's incoming connections (from SQL and Admin UI clients, and from other CockroachDB nodes) and for outgoing connections to other CockroachDB nodes. -[Split-CA certificates](#split-ca-certificates) | When you have multiple CockroachDB clusters and need to restrict access to clients from accessing the other cluster. - -## Accessing the Admin UI for a secure cluster - -On [accessing the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui) for a secure cluster, your web browser will consider the CockroachDB-issued certificate invalid, because the browser hasn't been configured to trust the CA that issued the certificate. - -For secure clusters, you can avoid getting the warning message by using a certificate issued by a public CA whose certificates are trusted by browsers, in addition to the CockroachDB-created certificates: - -1. Request a certificate from a public CA (for example, [Let's Encrypt](https://letsencrypt.org/)). The certificate must have the IP addresses and DNS names used to reach the Admin UI listed in the `Subject Alternative Name` field. -2. Rename the certificate and key as `ui.crt` and `ui.key`. -3. Add the `ui.crt` and `ui.key` to the [certificate directory](cockroach-cert.html#certificate-directory). `ui.key` must not have group or world permissions (maximum permissions are 0700, or rwx------). You can disable this check by setting the environment variable `COCKROACH_SKIP_KEY_PERMISSION_CHECK=true`. -4. For nodes that are already running, load the `ui.crt` certificate without restarting the node by issuing a `SIGHUP` signal to the cockroach process: - {% include copy-clipboard.html %} - ~~~ shell - pkill -SIGHUP -x cockroach - ~~~ - The `SIGHUP` signal must be sent by the same user running the process (e.g., run with sudo if the cockroach process is running under user root). - -### Node key and certificates - -A node must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate issued by the public CA or your organizational CA. -`node.crt` | Server certificate created using the `cockroach cert` command.

      `node.crt` must have `CN=node` and the list of IP addresses and DNS names listed in `Subject Alternative Name` field.

      Must be signed by `ca.crt`. -`node.key` | Server key created using the `cockroach cert` command. -`ui.crt` | UI certificate signed by the public CA. `ui.crt` must have the IP addresses and DNS names used to reach the Admin UI listed in `Subject Alternative Name`. -`ui.key` | UI key corresponding to `ui.crt`. - -### Client key and certificates - -A client must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate issued by the public CA or your organizational CA. -`client..crt` | Client certificate for `` (e.g., `client.root.crt` for user `root`).

      Each `client..crt` must have `CN=` (for example, `CN=marc` for `client.marc.crt`)

      Must be signed by `ca.crt`. -`client..key` | Client key created using the `cockroach cert` command. - -## Split node certificates - -The node certificate discussed in the `cockroach cert` command [documentation](cockroach-cert.html) is multifunctional, which means the same certificate is presented for the node's incoming connections (from SQL and Admin UI clients, and from other CockroachDB nodes) and for outgoing connections to other CockroachDB nodes. To make the certificate multi-functional, the `node.crt` created using the `cockroach cert` command has `CN=node` and the list of IP addresses and DNS names listed in `Subject Alternative Name` field. This works if you are also using the CockroachDB CA created using the `cockroach cert` command. However, if you need to use an external public CA or your own organizational CA, the CA policy might not allow it to sign a server certificate containing a CN that is not an IP address or domain name. - -To get around this issue, you can split the node key and certificate into two: - -- `node.crt` and `node.key`: `node.crt` is used as the server certificate when a node receives incoming connections from clients and other nodes. All IP addresses and DNS names for the node must be listed in the `Subject Alternative Name` field. -- `client.node.crt` and `client.node.key`: `client.node.crt` is used as the client certificate when making connections to other nodes. `client.node.crt` must have `CN=node`. - -### Node key and certificates - -A node must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate issued by the public CA or your organizational CA. -`node.crt` | Server certificate used when a node receives incoming connections from clients and other nodes.

      All IP addresses and DNS names for the node must be listed in `Subject Alternative Name`.

      Must be signed by `ca.crt`. -`node.key` | Server key corresponding to `node.crt`. -`client.node.crt` | Client certificate when making connections to other nodes.

      Must have `CN=node`.

      Must be signed by `ca.crt`. -`client.node.key` | Client key corresponding to `client.node.crt`. - -Optionally, if you have a certificate issued by a public CA to securely access the Admin UI, you need to place the certificate and key (`ui.crt` and `ui.key` respectively) in the directory specified by the `--certs-dir` flag. - -### Client key and certificates - -A client must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate issued by the public CA or your organizational CA. -`client..crt` | Client certificate for `` (e.g., `client.root.crt` for user `root`).

      Each `client..crt` must have `CN=` (for example, `CN=marc` for `client.marc.crt`)

      Must be signed by `ca.crt`. -`client..key` | Client key corresponding to `client..crt`. - -## Split CA certificates - -{{site.data.alerts.callout_danger}} -We do not recommend you use split CA certificates unless your organizational security practices mandate you to do so. -{{site.data.alerts.end}} - -If you need to use separate CAs to sign node certificates and client certificates, then you need two CAs and their respective certificates and keys: `ca.crt` and `ca-client.crt`. - -### Node key and certificates - -A node must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate to verify node certificates. -`ca-client.crt` | CA certificate to verify client certificates. -`node.crt` | Server certificate used when a node receives incoming connections from clients and other nodes.

      All IP addresses and DNS names for the node must be listed in `Subject Alternative Name`.

      Must be signed by `ca.crt`. -`node.key` | Server key corresponding to `node.crt`. -`client.node.crt` | Client certificate when making connections to other nodes. This certificate must be signed using `ca-client.crt`

      Must have `CN=node`. -`client.node.key` | Client key corresponding to `client.node.crt`. - -Optionally, if you have a certificate issued by a public CA to securely access the Admin UI, you need to place the certificate and key (`ui.crt` and `ui.key` respectively) in the directory specified by the `--certs-dir` flag. - -### Client key and certificates - -A client must have the following files with file names as specified in the table: - -File name | File usage --------------|------------ -`ca.crt` | CA certificate. -`client..crt` | Client certificate for `` (e.g., `client.root.crt` for user `root`).

      Each `client..crt` must have `CN=` (for example, `CN=marc` for `client.marc.crt`).

      Must be signed by `ca-client.crt`. -`client..key` | Client key corresponding to `client..crt`. - -## See also - -- [Manual Deployment](manual-deployment.html): Learn about starting a multi-node secure cluster and accessing it from a client. -- [Start a Node](cockroach-start.html): Learn more about the flags you pass when adding a node to a secure cluster -- [Client Connection Parameters](connection-parameters.html) diff --git a/src/current/v19.2/create-security-certificates-openssl.md b/src/current/v19.2/create-security-certificates-openssl.md deleted file mode 100644 index a566a46adbd..00000000000 --- a/src/current/v19.2/create-security-certificates-openssl.md +++ /dev/null @@ -1,341 +0,0 @@ ---- -title: Create Security Certificates using Openssl -summary: A secure CockroachDB cluster uses TLS for encrypted inter-node and client-node communication. -toc: true ---- - -To secure your CockroachDB cluster's inter-node and client-node communication, you need to provide a Certificate Authority (CA) certificate that has been used to sign keys and certificates (SSLs) for: - -- Nodes -- Clients -- Admin UI (optional) - -To create these certificates and keys, use the `cockroach cert` [commands](cockroach-commands.html) with the appropriate subcommands and flags, use [`openssl` commands](https://wiki.openssl.org/index.php/), or use a [custom CA](create-security-certificates-custom-ca.html) (for example, a public CA or your organizational CA). - - - -## Subcommands - -Subcommand | Usage ------------|------ -[`openssl genrsa`](https://www.openssl.org/docs/manmaster/man1/genrsa.html) | Create an RSA private key. -[`openssl req`](https://www.openssl.org/docs/manmaster/man1/req.html) | Create CA certificate and CSRs (certificate signing requests). -[`openssl ca`](https://www.openssl.org/docs/manmaster/man1/ca.html) | Create node and client certificates using the CSRs. - -## Configuration files - -To use [`openssl req`](https://www.openssl.org/docs/manmaster/man1/req.html) and [`openssl ca`](https://www.openssl.org/docs/manmaster/man1/ca.html) subcommands, you need the following configuration files: - -File name pattern | File usage --------------|------------ -`ca.cnf` | CA configuration file -`node.cnf` | Server configuration file -`client.cnf` | Client configuration file - -## Certificate directory - -To create node and client certificates using the OpenSSL commands, you need access to a local copy of the CA certificate and key. We recommend creating all certificates (node, client, and CA certificates), and node and client keys in one place and then distributing them appropriately. Store the CA key somewhere safe and keep a backup; if you lose it, you will not be able to add new nodes or clients to your cluster. - -## Required keys and certificates - -Use the [`openssl genrsa`](https://www.openssl.org/docs/manmaster/man1/genrsa.html) and [`openssl req`](https://www.openssl.org/docs/manmaster/man1/req.html) subcommands to create all certificates, and node and client keys in a single directory, with the files named as follows: - -### Node key and certificates - -File name pattern | File usage --------------|------------ -`ca.crt` | CA certificate -`node.crt` | Server certificate -`node.key` | Key for server certificate - -### Client key and certificates - -File name pattern | File usage --------------|------------ -`ca.crt` | CA certificate. -`client..crt` | Client certificate for `` (for example: `client.root.crt` for user `root`). -`client..key` | Key for the client certificate. - -Note the following: - -- The CA key should not be uploaded to the nodes and clients, so it should be created in a separate directory. - -- Keys (files ending in `.key`) must not have group or world permissions (maximum permissions are 0700, or `rwx------`). This check can be disabled by setting the environment variable `COCKROACH_SKIP_KEY_PERMISSION_CHECK=true`. - -## Examples - -### Create the CA key and certificate pair - -1. Create two directories: - - {% include copy-clipboard.html %} - ~~~ shell - $ mkdir certs - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ mkdir my-safe-directory - ~~~ - - `certs`: Create your CA certificate and all node and client certificates and keys in this directory and then upload the relevant files to the nodes and clients. - - `my-safe-directory`: Create your CA key in this directory and then reference the key when generating node and client certificates. After that, keep the key safe and secret; do not upload it to your nodes or clients. - -2. Create the `ca.cnf` file and copy the following configuration into it. - - You can set the CA certificate expiration period using the `default_days` parameter. We recommend using the CockroachDB default value of the CA certificate expiration period, which is 3660 days. - - {% include copy-clipboard.html %} - ~~~ - # OpenSSL CA configuration file - [ ca ] - default_ca = CA_default - - [ CA_default ] - default_days = 3660 - database = index.txt - serial = serial.txt - default_md = sha256 - copy_extensions = copy - unique_subject = no - - # Used to create the CA certificate. - [ req ] - prompt=no - distinguished_name = distinguished_name - x509_extensions = extensions - - [ distinguished_name ] - organizationName = Cockroach - commonName = Cockroach CA - - [ extensions ] - keyUsage = critical,digitalSignature,nonRepudiation,keyEncipherment,keyCertSign - basicConstraints = critical,CA:true,pathlen:1 - - # Common policy for nodes and users. - [ signing_policy ] - organizationName = supplied - commonName = supplied - - # Used to sign node certificates. - [ signing_node_req ] - keyUsage = critical,digitalSignature,keyEncipherment - extendedKeyUsage = serverAuth,clientAuth - - # Used to sign client certificates. - [ signing_client_req ] - keyUsage = critical,digitalSignature,keyEncipherment - extendedKeyUsage = clientAuth - ~~~ - - {{site.data.alerts.callout_danger}}The keyUsage and extendedkeyUsage parameters are vital for CockroachDB functions. You can modify or omit other parameters as per your preferred OpenSSL configuration and you can add additional usages, but do not omit keyUsage and extendedkeyUsage parameters or remove the listed usages. {{site.data.alerts.end}} - -3. Create the CA key using the [`openssl genrsa`](https://www.openssl.org/docs/manmaster/man1/genrsa.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ openssl genrsa -out my-safe-directory/ca.key 2048 - ~~~ - {% include copy-clipboard.html %} - ~~~ shell - $ chmod 400 my-safe-directory/ca.key - ~~~ - -4. Create the CA certificate using the [`openssl req`](https://www.openssl.org/docs/manmaster/man1/req.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ openssl req \ - -new \ - -x509 \ - -config ca.cnf \ - -key my-safe-directory/ca.key \ - -out certs/ca.crt \ - -days 3660 \ - -batch - ~~~ - -5. Reset database and index files. - - {% include copy-clipboard.html %} - ~~~ shell - $ rm -f index.txt serial.txt - ~~~ - {% include copy-clipboard.html %} - ~~~ shell - $ touch index.txt - ~~~ - {% include copy-clipboard.html %} - ~~~ shell - $ echo '01' > serial.txt - ~~~ - -### Create the certificate and key pairs for nodes - -In the following steps, replace the placeholder text in the code with the actual username and node address. - -1. Create the `node.cnf` file for the first node and copy the following configuration into it: - - {% include copy-clipboard.html %} - ~~~ - # OpenSSL node configuration file - [ req ] - prompt=no - distinguished_name = distinguished_name - req_extensions = extensions - - [ distinguished_name ] - organizationName = Cockroach - # Required value for commonName, do not change. - commonName = node - - [ extensions ] - subjectAltName = DNS:,DNS:,IP: - ~~~ - - {{site.data.alerts.callout_danger}}The commonName and subjectAltName parameters are vital for CockroachDB functions. It is also required that commonName be set to node. You can modify or omit other parameters as per your preferred OpenSSL configuration, but do not omit the commonName and subjectAltName parameters. {{site.data.alerts.end}} - -2. Create the key for the first node using the [`openssl genrsa`](https://www.openssl.org/docs/manmaster/man1/genrsa.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ openssl genrsa -out certs/node.key 2048 - ~~~ - {% include copy-clipboard.html %} - ~~~ shell - $ chmod 400 certs/node.key - ~~~ - -3. Create the CSR for the first node using the [`openssl req`](https://www.openssl.org/docs/manmaster/man1/req.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ openssl req \ - -new \ - -config node.cnf \ - -key certs/node.key \ - -out node.csr \ - -batch - ~~~ - -4. Sign the node CSR to create the node certificate for the first node using the [`openssl ca`](https://www.openssl.org/docs/manmaster/man1/ca.html) command. - - You can set the node certificate expiration period using the `days` flag. We recommend using the CockroachDB default value of the node certificate expiration period, which is 1830 days. - - {% include copy-clipboard.html %} - ~~~ shell - $ openssl ca \ - -config ca.cnf \ - -keyfile my-safe-directory/ca.key \ - -cert certs/ca.crt \ - -policy signing_policy \ - -extensions signing_node_req \ - -out certs/node.crt \ - -outdir certs/ \ - -in node.csr \ - -days 1830 \ - -batch - ~~~ - -5. Upload certificates to the first node: - - {% include copy-clipboard.html %} - ~~~ shell - $ ssh @ "mkdir certs" - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ scp certs/ca.crt \ - certs/node.crt \ - certs/node.key \ - @:~/certs - ~~~ - -6. Delete the local copy of the first node's certificate and key: - - {% include copy-clipboard.html %} - ~~~ shell - $ rm certs/node.crt certs/node.key - ~~~ - - {{site.data.alerts.callout_info}}This is necessary because the certificates and keys for additional nodes will also be named node.crt and node.key.{{site.data.alerts.end}} - -7. Repeat steps 1 - 6 for each additional node. - -8. Remove the `.pem` files in the `certs` directory. These files are unnecessary duplicates of the `.crt` files that CockroachDB requires. - -### Create the certificate and key pair for a client - -In the following steps, replace the placeholder text in the code with the actual username. - -1. Create the `client.cnf` file for the first client and copy the following configuration into it: - - {% include copy-clipboard.html %} - ~~~ - [ req ] - prompt=no - distinguished_name = distinguished_name - - [ distinguished_name ] - organizationName = Cockroach - commonName = - ~~~ - - {{site.data.alerts.callout_info}}The commonName parameter is vital for CockroachDB functions. You can modify or omit other parameters as per your preferred OpenSSL configuration, but do not omit the commonName parameter. {{site.data.alerts.end}} - -2. Create the key for the first client using the [`openssl genrsa`](https://www.openssl.org/docs/manmaster/man1/genrsa.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ openssl genrsa -out certs/client..key 2048 - ~~~ - {% include copy-clipboard.html %} - ~~~ shell - $ chmod 400 certs/client..key - ~~~ - -3. Create the CSR for the first client using the [`openssl req`](https://www.openssl.org/docs/manmaster/man1/req.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ openssl req \ - -new \ - -config client.cnf \ - -key certs/client..key \ - -out client..csr \ - -batch - ~~~ - -4. Sign the client CSR to create the client certificate for the first client using the [`openssl ca`](https://www.openssl.org/docs/manmaster/man1/ca.html) command. You can set the client certificate expiration period using the `days` flag. We recommend using the CockroachDB default value of the client certificate expiration period, which is 1830 days. - - {% include copy-clipboard.html %} - ~~~ shell - $ openssl ca \ - -config ca.cnf \ - -keyfile my-safe-directory/ca.key \ - -cert certs/ca.crt \ - -policy signing_policy \ - -extensions signing_client_req \ - -out certs/client..crt \ - -outdir certs/ \ - -in client..csr \ - -days 1830 \ - -batch - ~~~ - -5. Upload certificates to the first client using your preferred method. - -6. Repeat steps 1 - 5 for each additional client. - -7. Remove the `.pem` files in the `certs` directory. These files are unnecessary duplicates of the `.crt` files that CockroachDB requires. - -## See also - -- [Manual Deployment](manual-deployment.html): Learn about starting a multi-node secure cluster and accessing it from a client. -- [Start a Node](cockroach-start.html): Learn more about the flags you pass when adding a node to a secure cluster -- [Client Connection Parameters](connection-parameters.html) diff --git a/src/current/v19.2/create-sequence.md b/src/current/v19.2/create-sequence.md deleted file mode 100644 index 02df9101fe2..00000000000 --- a/src/current/v19.2/create-sequence.md +++ /dev/null @@ -1,201 +0,0 @@ ---- -title: CREATE SEQUENCE -summary: -toc: true ---- - -The `CREATE SEQUENCE` [statement](sql-statements.html) creates a new sequence in a database. Use a sequence to auto-increment integers in a table. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Considerations - -- Using a sequence is slower than [auto-generating unique IDs with the `gen_random_uuid()`, `uuid_v4()` or `unique_rowid()` built-in functions](sql-faqs.html#how-do-i-auto-generate-unique-row-ids-in-cockroachdb). Incrementing a sequence requires a write to persistent storage, whereas auto-generating a unique ID does not. Therefore, use auto-generated unique IDs unless an incremental sequence is preferred or required. -- A column that uses a sequence can have a gap in the sequence values if a transaction advances the sequence and is then rolled back. Sequence updates are committed immediately and aren't rolled back along with their containing transaction. This is done to avoid blocking concurrent transactions that use the same sequence. - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the parent database. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/create_sequence.html %}
      - -## Parameters - - - - Parameter | Description ------------|------------ -`seq_name` | The name of the sequence to be created, which must be unique within its database and follow the [identifier rules](keywords-and-identifiers.html#identifiers). When the parent database is not set as the default, the name must be formatted as `database.seq_name`. -`INCREMENT` | The value by which the sequence is incremented. A negative number creates a descending sequence. A positive number creates an ascending sequence.

      **Default:** `1` -`MINVALUE` | The minimum value of the sequence. Default values apply if not specified or if you enter `NO MINVALUE`.

      **Default for ascending:** `1`

      **Default for descending:** `MININT` -`MAXVALUE` | The maximum value of the sequence. Default values apply if not specified or if you enter `NO MAXVALUE`.

      **Default for ascending:** `MAXINT`

      **Default for descending:** `-1` -`START` | The first value of the sequence.

      **Default for ascending:** `1`

      **Default for descending:** `-1` -`NO CYCLE` | Currently, all sequences are set to `NO CYCLE` and the sequence will not wrap. - - - -## Sequence functions - -We support the following [SQL sequence functions](functions-and-operators.html): - -- `nextval('seq_name')` -- `currval('seq_name')` -- `lastval()` -- `setval('seq_name', value, is_called)` - -## Examples - -### List all sequences - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.sequences; -~~~ -~~~ -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -| sequence_catalog | sequence_schema | sequence_name | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value | maximum_value | increment | cycle_option | -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -| def | db_2 | test_4 | INT | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO | -| def | test_db | customer_seq | INT | 64 | 2 | 0 | 101 | 1 | 9223372036854775807 | 2 | NO | -| def | test_db | desc_customer_list | INT | 64 | 2 | 0 | 1000 | -9223372036854775808 | -1 | -2 | NO | -| def | test_db | test_sequence3 | INT | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO | -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -(4 rows) -~~~ - -### Create a sequence with default settings - -In this example, we create a sequence with default settings. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE SEQUENCE customer_seq; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE customer_seq; -~~~ - -~~~ -+--------------+--------------------------------------------------------------------------+ -| table_name | create_statement | -+--------------+--------------------------------------------------------------------------+ -| customer_seq | CREATE SEQUENCE customer_seq MINVALUE 1 MAXVALUE 9223372036854775807 | -| | INCREMENT 1 START 1 | -+--------------+--------------------------------------------------------------------------+ -(1 row) -~~~ - -### Create a sequence with user-defined settings - -In this example, we create a sequence that starts at -1 and descends in increments of 2. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE SEQUENCE desc_customer_list START -1 INCREMENT -2; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE desc_customer_list; -~~~ - -~~~ -+--------------------+--------------------------------------------------------------------------+ -| table_name | create_statement | -+--------------------+--------------------------------------------------------------------------+ -| desc_customer_list | CREATE SEQUENCE desc_customer_list MINVALUE -9223372036854775808 | -| | MAXVALUE -1 INCREMENT -2 START -1 | -+--------------------+--------------------------------------------------------------------------+ -(1 row) -~~~ - -### Create a table with a sequence - -In this example, we create a table using the sequence we created in the first example as the table's primary key. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE customer_list ( - id INT PRIMARY KEY DEFAULT nextval('customer_seq'), - customer string, - address string - ); -~~~ - -Insert a few records to see the sequence. - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO customer_list (customer, address) - VALUES - ('Lauren', '123 Main Street'), - ('Jesse', '456 Broad Ave'), - ('Amruta', '9876 Green Parkway'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customer_list; -~~~ - -~~~ -+----+----------+--------------------+ -| id | customer | address | -+----+----------+--------------------+ -| 1 | Lauren | 123 Main Street | -| 2 | Jesse | 456 Broad Ave | -| 3 | Amruta | 9876 Green Parkway | -+----+----------+--------------------+ -~~~ - -### View the current value of a sequence - -To view the current value without incrementing the sequence, use: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customer_seq; -~~~ - -~~~ -+------------+---------+-----------+ -| last_value | log_cnt | is_called | -+------------+---------+-----------+ -| 3 | 0 | true | -+------------+---------+-----------+ -~~~ - -{{site.data.alerts.callout_info}}The log_cnt and is_called columns are returned only for PostgreSQL compatibility; they are not stored in the database.{{site.data.alerts.end}} - -If a value has been obtained from the sequence in the current session, you can also use the `currval('seq_name')` function to get that most recently obtained value: - -~~~ sql -> SELECT currval('customer_seq'); -~~~ - -~~~ -+---------+ -| currval | -+---------+ -| 3 | -+---------+ -~~~ - -## See also -- [`ALTER SEQUENCE`](alter-sequence.html) -- [`RENAME SEQUENCE`](rename-sequence.html) -- [`DROP SEQUENCE`](drop-sequence.html) -- [`SHOW CREATE`](show-create.html) -- [`SHOW SEQUENCES`](show-sequences.html) -- [Functions and Operators](functions-and-operators.html) -- [Other SQL Statements](sql-statements.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/create-statistics.md b/src/current/v19.2/create-statistics.md deleted file mode 100644 index 89855e9edf6..00000000000 --- a/src/current/v19.2/create-statistics.md +++ /dev/null @@ -1,164 +0,0 @@ ---- -title: CREATE STATISTICS -summary: Use the CREATE STATISTICS statement to generate table statistics for the cost-based optimizer to use. -toc: true ---- -Use the `CREATE STATISTICS` [statement](sql-statements.html) to generate table statistics for the [cost-based optimizer](cost-based-optimizer.html) to use. - -Once you [create a table](create-table.html) and load data into it (e.g., [`INSERT`](insert.html), [`IMPORT`](import.html)), table statistics can be generated. Table statistics help the cost-based optimizer determine the cardinality of the rows used in each query, which helps to predict more accurate costs. - -{{site.data.alerts.callout_info}} -[By default, CockroachDB automatically generates statistics](cost-based-optimizer.html#table-statistics) on all indexed columns, and up to 100 non-indexed columns. As a result, most users do not need to issue `CREATE STATISTICS` statements directly. -{{site.data.alerts.end}} - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/create_stats.html %} -
      - -## Required Privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the parent database. - -## Parameters - -| Parameter | Description | -|-----------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `statistics_name` | The name of the set of statistics you are creating. | -| `opt_stats_columns` | The name of the column(s) you want to create statistics for. | -| `create_stats_target` | The name of the table you want to create statistics for. | -| `opt_as_of_clause` | Used to create historical stats using the [`AS OF SYSTEM TIME`](as-of-system-time.html) clause. For instructions, see [Create statistics as of a given time](#create-statistics-as-of-a-given-time). | - -## Examples - -{% include {{page.version.version}}/sql/movr-statements.md %} - -### Create statistics on a single column - -{% include copy-clipboard.html %} -~~~ sql -> CREATE STATISTICS revenue_stats ON revenue FROM rides; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW STATISTICS FOR TABLE rides; -~~~ - -~~~ - statistics_name | column_names | created | row_count | distinct_count | null_count | histogram_id -+-----------------+-----------------+----------------------------------+-----------+----------------+------------+--------------------+ - __auto__ | {city} | 2020-08-26 17:24:25.334218+00:00 | 500 | 9 | 0 | 584555775053725697 - __auto__ | {vehicle_city} | 2020-08-26 17:24:25.334218+00:00 | 500 | 9 | 0 | 584555775060344833 - __auto__ | {id} | 2020-08-26 17:24:25.334218+00:00 | 500 | 500 | 0 | NULL - __auto__ | {rider_id} | 2020-08-26 17:24:25.334218+00:00 | 500 | 50 | 0 | NULL - __auto__ | {vehicle_id} | 2020-08-26 17:24:25.334218+00:00 | 500 | 15 | 0 | NULL - __auto__ | {start_address} | 2020-08-26 17:24:25.334218+00:00 | 500 | 500 | 0 | NULL - __auto__ | {end_address} | 2020-08-26 17:24:25.334218+00:00 | 500 | 500 | 0 | NULL - __auto__ | {start_time} | 2020-08-26 17:24:25.334218+00:00 | 500 | 30 | 0 | NULL - __auto__ | {end_time} | 2020-08-26 17:24:25.334218+00:00 | 500 | 367 | 0 | NULL - __auto__ | {revenue} | 2020-08-26 17:24:25.334218+00:00 | 500 | 100 | 0 | NULL - revenue_stats | {revenue} | 2020-08-26 17:24:34.494008+00:00 | 500 | 100 | 0 | 584555805068886017 -(11 rows) -~~~ - -Note that statistics are automatically collected for all columns in the `rides` table, making the `revenue_stats` statistics a duplicate of the statistics automatically collected on the `revenue` column. - -### Create statistics on a default set of columns - -The `CREATE STATISTICS` statement shown below automatically figures out which columns to get statistics on. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE STATISTICS users_stats FROM users; -~~~ - -This statement creates statistics identical to the statistics that CockroachDB creates automatically. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW STATISTICS FOR TABLE users; -~~~ - -~~~ - statistics_name | column_names | created | row_count | distinct_count | null_count | histogram_id -+-----------------+---------------+----------------------------------+-----------+----------------+------------+--------------------+ - __auto__ | {city} | 2020-08-26 17:24:25.305468+00:00 | 50 | 9 | 0 | 584555774958108673 - __auto__ | {id} | 2020-08-26 17:24:25.305468+00:00 | 50 | 50 | 0 | NULL - __auto__ | {name} | 2020-08-26 17:24:25.305468+00:00 | 50 | 49 | 0 | NULL - __auto__ | {address} | 2020-08-26 17:24:25.305468+00:00 | 50 | 50 | 0 | NULL - __auto__ | {credit_card} | 2020-08-26 17:24:25.305468+00:00 | 50 | 50 | 0 | NULL - users_stats | {city} | 2020-08-26 17:24:53.49405+00:00 | 50 | 9 | 0 | 584555867327430657 - users_stats | {id} | 2020-08-26 17:24:53.49405+00:00 | 50 | 50 | 0 | NULL - users_stats | {name} | 2020-08-26 17:24:53.49405+00:00 | 50 | 49 | 0 | NULL - users_stats | {address} | 2020-08-26 17:24:53.49405+00:00 | 50 | 50 | 0 | NULL - users_stats | {credit_card} | 2020-08-26 17:24:53.49405+00:00 | 50 | 50 | 0 | NULL -(10 rows) -~~~ - -### Create statistics as of a given time - -To create statistics as of a given time (in this example, 1 minute ago to avoid interfering with the production workload), run a statement like the following: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE STATISTICS vehicle_stats_1 FROM vehicles AS OF SYSTEM TIME '-1m'; -~~~ - -For more information about how the `AS OF SYSTEM TIME` clause works, including supported time formats, see [`AS OF SYSTEM TIME`](as-of-system-time.html). - -### Delete statistics - -{% include {{ page.version.version }}/misc/delete-statistics.md %} - -### View statistics jobs - -Every time the `CREATE STATISTICS` statement is executed, it kicks off a background job. This is true for queries issued by your application as well as queries issued by the [automatic stats feature](cost-based-optimizer.html#table-statistics). - -To view statistics jobs, there are two options: - -1. Use [`SHOW JOBS`](show-jobs.html) to see all statistics jobs that were created by user queries (i.e., someone entering `CREATE STATISTICS` at the SQL prompt or via application code): - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT * FROM [SHOW JOBS] WHERE job_type LIKE '%CREATE STATS%'; - ~~~ - - ~~~ - job_id | job_type | description | statement | user_name | status | running_status | created | started | finished | modified | fraction_completed | error | coordinator_id - +--------------------+--------------+--------------------------------------------------------------------------------------------------+-----------+-----------+-----------+----------------+----------------------------------+----------------------------------+----------------------------------+----------------------------------+--------------------+-------+----------------+ - 584555805032710145 | CREATE STATS | CREATE STATISTICS revenue_stats ON revenue FROM movr.public.rides | | root | succeeded | NULL | 2020-08-26 17:24:34.485089+00:00 | 2020-08-26 17:24:34.487231+00:00 | 2020-08-26 17:24:34.49702+00:00 | 2020-08-26 17:24:34.496442+00:00 | 1 | | 1 - 584555867287060481 | CREATE STATS | CREATE STATISTICS users_stats FROM movr.public.users | | root | succeeded | NULL | 2020-08-26 17:24:53.483605+00:00 | 2020-08-26 17:24:53.486025+00:00 | 2020-08-26 17:24:53.505254+00:00 | 2020-08-26 17:24:53.504697+00:00 | 1 | | 1 - 584555915664261121 | CREATE STATS | CREATE STATISTICS vehicle_stats_1 FROM movr.public.vehicles WITH OPTIONS AS OF SYSTEM TIME '-1m' | | root | succeeded | NULL | 2020-08-26 17:25:08.247163+00:00 | 2020-08-26 17:25:08.252334+00:00 | 2020-08-26 17:25:08.27947+00:00 | 2020-08-26 17:25:08.278204+00:00 | 1 | | 1 - (3 rows) - ~~~ - -2. Use `SHOW AUTOMATIC JOBS` to see statistics jobs that were created by the [automatic statistics feature](cost-based-optimizer.html#table-statistics): - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT * FROM [SHOW AUTOMATIC JOBS] WHERE job_type LIKE '%CREATE STATS%'; - ~~~ - - ~~~ - job_id | job_type | description | statement | user_name | status | running_status | created | started | finished | modified | fraction_completed | error | coordinator_id - +--------------------+-------------------+---------------------------------------------------------------------+-------------------------------------------------------------------------------------------+-----------+-----------+----------------+----------------------------------+----------------------------------+----------------------------------+----------------------------------+--------------------+-------+----------------+ - 584555774723129345 | AUTO CREATE STATS | Table statistics refresh for movr.public.promo_codes | CREATE STATISTICS __auto__ FROM [57] WITH OPTIONS THROTTLING 0.9 AS OF SYSTEM TIME '-30s' | root | succeeded | NULL | 2020-08-26 17:24:25.23534+00:00 | 2020-08-26 17:24:25.237261+00:00 | 2020-08-26 17:24:25.258822+00:00 | 2020-08-26 17:24:25.258199+00:00 | 1 | | 1 - 584555774808096769 | AUTO CREATE STATS | Table statistics refresh for movr.public.vehicles | CREATE STATISTICS __auto__ FROM [54] WITH OPTIONS THROTTLING 0.9 AS OF SYSTEM TIME '-30s' | root | succeeded | NULL | 2020-08-26 17:24:25.261267+00:00 | 2020-08-26 17:24:25.263309+00:00 | 2020-08-26 17:24:25.292766+00:00 | 2020-08-26 17:24:25.292114+00:00 | 1 | | 1 - 584555774921211905 | AUTO CREATE STATS | Table statistics refresh for movr.public.users | CREATE STATISTICS __auto__ FROM [53] WITH OPTIONS THROTTLING 0.9 AS OF SYSTEM TIME '-30s' | root | succeeded | NULL | 2020-08-26 17:24:25.295787+00:00 | 2020-08-26 17:24:25.297427+00:00 | 2020-08-26 17:24:25.31669+00:00 | 2020-08-26 17:24:25.315689+00:00 | 1 | | 1 - 584555775000444929 | AUTO CREATE STATS | Table statistics refresh for movr.public.rides | CREATE STATISTICS __auto__ FROM [55] WITH OPTIONS THROTTLING 0.9 AS OF SYSTEM TIME '-30s' | root | succeeded | NULL | 2020-08-26 17:24:25.31997+00:00 | 2020-08-26 17:24:25.322527+00:00 | 2020-08-26 17:24:25.35465+00:00 | 2020-08-26 17:24:25.353909+00:00 | 1 | | 1 - 584555775125815297 | AUTO CREATE STATS | Table statistics refresh for movr.public.user_promo_codes | CREATE STATISTICS __auto__ FROM [58] WITH OPTIONS THROTTLING 0.9 AS OF SYSTEM TIME '-30s' | root | succeeded | NULL | 2020-08-26 17:24:25.35823+00:00 | 2020-08-26 17:24:25.35987+00:00 | 2020-08-26 17:24:25.380727+00:00 | 2020-08-26 17:24:25.380128+00:00 | 1 | | 1 - 584555775206588417 | AUTO CREATE STATS | Table statistics refresh for movr.public.vehicle_location_histories | CREATE STATISTICS __auto__ FROM [56] WITH OPTIONS THROTTLING 0.9 AS OF SYSTEM TIME '-30s' | root | succeeded | NULL | 2020-08-26 17:24:25.382874+00:00 | 2020-08-26 17:24:25.384203+00:00 | 2020-08-26 17:24:25.405608+00:00 | 2020-08-26 17:24:25.404834+00:00 | 1 | | 1 - (6 rows) - ~~~ - -## See Also - -- [Cost-Based Optimizer](cost-based-optimizer.html) -- [`SHOW STATISTICS`](show-statistics.html) -- [`CREATE TABLE`](create-table.html) -- [`INSERT`](insert.html) -- [`IMPORT`](import.html) -- [`SHOW JOBS`](show-jobs.html) -- [SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/create-table-as.md b/src/current/v19.2/create-table-as.md deleted file mode 100644 index 7cbbd943373..00000000000 --- a/src/current/v19.2/create-table-as.md +++ /dev/null @@ -1,350 +0,0 @@ ---- -title: CREATE TABLE AS -summary: The CREATE TABLE AS statement persists the result of a query into the database for later reuse. -toc: true ---- - -The `CREATE TABLE ... AS` [statement](sql-statements.html) creates a new table from a [selection query](selection-queries.html). - - -## Intended use - -Tables created with `CREATE TABLE ... AS` are intended to persist the -result of a query for later reuse. - -This can be more efficient than a [view](create-view.html) when the -following two conditions are met: - -- The result of the query is used as-is multiple times. -- The copy needs not be kept up-to-date with the original table over time. - -When the results of a query are reused multiple times within a larger -query, a view is advisable instead. The query optimizer can "peek" -into the view and optimize the surrounding query using the primary key -and indices of the tables mentioned in the view query. - -A view is also advisable when the results must be up-to-date; a view -always retrieves the current data from the tables that the view query -mentions. - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the parent database. - -## Synopsis -
      - - -

      - -
      -{% include {{ page.version.version }}/sql/diagrams/create_table_as.html %} -
      - -
      - -
      - {% include {{ page.version.version }}/sql/diagrams/create_table_as.html %} -
      - -**create_as_col_qual_list ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/create_as_col_qual_list.html %} -
      - -**create_as_constraint_def ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/create_as_constraint_def.html %} -
      - -
      - -## Parameters - - - - Parameter | Description ------------|------------- - `IF NOT EXISTS` | Create a new table only if a table of the same name does not already exist in the database; if one does exist, do not return an error.

      Note that `IF NOT EXISTS` checks the table name only; it does not check if an existing table has the same columns, indexes, constraints, etc., of the new table. - `table_name` | The name of the table to create, which must be unique within its database and follow these [identifier rules](keywords-and-identifiers.html#identifiers). When the parent database is not set as the default, the name must be formatted as `database.name`.

      The [`UPSERT`](upsert.html) and [`INSERT ON CONFLICT`](insert.html) statements use a temporary table called `excluded` to handle uniqueness conflicts during execution. It's therefore not recommended to use the name `excluded` for any of your tables. - `column_name` | The name of the column you want to use instead of the name of the column from `select_stmt`. - `create_as_col_qual_list` | New in v19.2: An optional column definition, which may include [primary key constraints](primary-key.html) and [column family assignments](column-families.html). - `family_def` | New in v19.2: An optional [column family definition](column-families.html). Column family names must be unique within the table but can have the same name as columns, constraints, or indexes. - `create_as_constraint_def` | New in v19.2: An optional [primary key constraint](primary-key.html). - `select_stmt` | A [selection query](selection-queries.html) to provide the data. - -## Limitations - -Tables created with `CREATE TABLE ... AS` are not [interleaved](interleave-in-parent.html) with other tables. -The default rules for [column families](column-families.html) apply. - -The [primary key](primary-key.html) of tables created with `CREATE TABLE ... AS` is not automatically derived from the query results. You must specify new primary keys at table creation. For examples, see [Specify a primary key](create-table-as.html#specify-a-primary-key) and [Specify a primary key for partitioning](create-table-as.html#specify-a-primary-key-for-partitioning). Like for other tables, it is not possible to add or change the primary key after table creation. - -## Examples - -{% include {{page.version.version}}/sql/movr-statements.md %} - -### Create a table from a `SELECT` query - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users WHERE city = 'new york'; -~~~ -~~~ - id | city | name | address | credit_card -+--------------------------------------+----------+------------------+-----------------------------+-------------+ - 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228 - 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892 - 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636 - 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234 - 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487 - 19999999-9999-4a00-8000-000000000005 | new york | Nicole Mcmahon | 11540 Patton Extensions | 0303726947 -(6 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users_ny AS SELECT * FROM users WHERE city = 'new york'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users_ny; -~~~ -~~~ - id | city | name | address | credit_card -+--------------------------------------+----------+------------------+-----------------------------+-------------+ - 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228 - 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892 - 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636 - 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234 - 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487 - 19999999-9999-4a00-8000-000000000005 | new york | Nicole Mcmahon | 11540 Patton Extensions | 0303726947 -(6 rows) -~~~ - -### Change column names - -This statement creates a copy of an existing table but with changed column names: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users_ny_names (user_id, user_name) AS SELECT id, name FROM users WHERE city = 'new york'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users_ny_names; -~~~ -~~~ - user_id | user_name -+--------------------------------------+------------------+ - 00000000-0000-4000-8000-000000000000 | Robert Murphy - 051eb851-eb85-4ec0-8000-000000000001 | James Hamilton - 0a3d70a3-d70a-4d80-8000-000000000002 | Judy White - 0f5c28f5-c28f-4c00-8000-000000000003 | Devin Jordan - 147ae147-ae14-4b00-8000-000000000004 | Catherine Nelson - 19999999-9999-4a00-8000-000000000005 | Nicole Mcmahon -(6 rows) -~~~ - -### Create a table from a `VALUES` clause - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE drivers (id, city, name) AS VALUES (gen_random_uuid(), 'new york', 'Harry Potter'), (gen_random_uuid(), 'seattle', 'Evelyn Martin'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM drivers; -~~~ -~~~ - id | city | name -+--------------------------------------+----------+---------------+ - 146eebc4-c913-4678-8ea3-c5797d2b7f83 | new york | Harry Potter - 43cafd3b-2537-4fd8-a987-8138f88a22a4 | seattle | Evelyn Martin -(2 rows) -~~~ - -### Create a copy of an existing table - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users_ny_copy AS TABLE users_ny; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users_ny_copy; -~~~ -~~~ - id | city | name | address | credit_card -+--------------------------------------+----------+------------------+-----------------------------+-------------+ - 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228 - 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892 - 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636 - 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234 - 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487 - 19999999-9999-4a00-8000-000000000005 | new york | Nicole Mcmahon | 11540 Patton Extensions | 0303726947 -(6 rows) -~~~ - -When a table copy is created this way, the copy is not associated to -any primary key, secondary index, or constraint that was present on the -original table. - -### Specify a primary key - -New in v19.2: You can specify the [primary key](primary-key.html) of a new table created from a selection query: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users_ny_pk (id, city, name PRIMARY KEY) AS SELECT id, city, name FROM users WHERE city = 'new york'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users_ny_pk; -~~~ -~~~ - id | city | name -+--------------------------------------+----------+------------------+ - 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson - 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan - 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton - 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White - 19999999-9999-4a00-8000-000000000005 | new york | Nicole Mcmahon - 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy -(6 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE TABLE users_ny_pk; -~~~ -~~~ - table_name | create_statement -+----------------+--------------------------------------------------+ - users_ny_extra | CREATE TABLE users_ny_extra ( - | id UUID NULL, - | city VARCHAR NULL, - | name VARCHAR NOT NULL, - | CONSTRAINT "primary" PRIMARY KEY (name ASC), - | FAMILY "primary" (id, city, name) - | ) -(1 row) -~~~ - -### Define column families - -New in v19.2: You can define the [column families](column-families.html) of a new table created from a selection query: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users_ny_alt (id PRIMARY KEY FAMILY ids, name, city FAMILY locs, address, credit_card FAMILY payments) AS SELECT id, name, city, address, credit_card FROM users WHERE city = 'new york'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users_ny_alt; -~~~ -~~~ - id | name | city | address | credit_card -+--------------------------------------+------------------+----------+-----------------------------+-------------+ - 00000000-0000-4000-8000-000000000000 | Robert Murphy | new york | 99176 Anderson Mills | 8885705228 - 051eb851-eb85-4ec0-8000-000000000001 | James Hamilton | new york | 73488 Sydney Ports Suite 57 | 8340905892 - 0a3d70a3-d70a-4d80-8000-000000000002 | Judy White | new york | 18580 Rosario Ville Apt. 61 | 2597958636 - 0f5c28f5-c28f-4c00-8000-000000000003 | Devin Jordan | new york | 81127 Angela Ferry Apt. 8 | 5614075234 - 147ae147-ae14-4b00-8000-000000000004 | Catherine Nelson | new york | 1149 Lee Alley | 0792553487 - 19999999-9999-4a00-8000-000000000005 | Nicole Mcmahon | new york | 11540 Patton Extensions | 0303726947 -(6 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE TABLE users_ny_alt; -~~~ -~~~ - table_name | create_statement -+--------------+------------------------------------------------+ - users_ny_alt | CREATE TABLE users_ny_alt ( - | id UUID NOT NULL, - | name VARCHAR NULL, - | city VARCHAR NULL, - | address VARCHAR NULL, - | credit_card VARCHAR NULL, - | CONSTRAINT "primary" PRIMARY KEY (id ASC), - | FAMILY ids (id, name, address), - | FAMILY locs (city), - | FAMILY payments (credit_card) - | ) -(1 row) -~~~ - -### Specify a primary key for partitioning - -New in v19.2: If you are [partitioning](partitioning.html) a table based on a [primary key](primary-key.html), you must correctly define the primary key at table creation. It is not possible to add or change primary keys after table creation. To work around this limitation, you can create a new table from an existing one, with the correct primary keys specified in your `CREATE TABLE ... AS` statement. - -Suppose that you want to [geo-partition](demo-low-latency-multi-region-deployment.html) the `drivers` table that you created with the following statement: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE drivers (id, city, name) AS VALUES (gen_random_uuid(), 'new york', 'Harry Potter'), (gen_random_uuid(), 'seattle', 'Evelyn Martin'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE TABLE drivers; -~~~ -~~~ - table_name | create_statement -+------------+----------------------------------------------+ - drivers | CREATE TABLE drivers ( - | id UUID NULL, - | city STRING NULL, - | name STRING NULL, - | FAMILY "primary" (id, city, name, rowid) - | ) -(1 row) -~~~ - -In order for this table to be properly geo-partitioned with the other tables in the `movr` dataset, the table must have a composite primary key defined that includes the unique row identifier (`id`, in this case) and the row locality identifier (`city`). Use the following statement to create a new table with the correct composite primary key: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE drivers_pk (id, city, name, PRIMARY KEY (id, city)) AS SELECT id, city, name FROM drivers; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE TABLE drivers_pk; -~~~ -~~~ - table_name | create_statement -+------------+----------------------------------------------------------+ - drivers_pk | CREATE TABLE drivers_pk ( - | id UUID NOT NULL, - | city STRING NOT NULL, - | name STRING NULL, - | CONSTRAINT "primary" PRIMARY KEY (id ASC, city ASC), - | FAMILY "primary" (id, city, name) - | ) -(1 row) -~~~ - -## See also - -- [Selection Queries](selection-queries.html) -- [Simple `SELECT` Clause](select-clause.html) -- [`CREATE TABLE`](create-table.html) -- [`CREATE VIEW`](create-view.html) -- [`INSERT`](insert.html) -- [`DROP TABLE`](drop-table.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/create-table.md b/src/current/v19.2/create-table.md deleted file mode 100644 index b095bac104b..00000000000 --- a/src/current/v19.2/create-table.md +++ /dev/null @@ -1,524 +0,0 @@ ---- -title: CREATE TABLE -summary: The CREATE TABLE statement creates a new table in a database. -toc: true ---- - -The `CREATE TABLE` [statement](sql-statements.html) creates a new table in a database. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the parent database. - -## Synopsis - -
      - - -

      - -
      -{% include {{ page.version.version }}/sql/diagrams/create_table.html %} -
      - -
      - -
      - {% include {{ page.version.version }}/sql/diagrams/create_table.html %} -
      - -**column_def ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/column_def.html %} -
      - -**col_qualification ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/col_qualification.html %} -
      - -**index_def ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/index_def.html %} -
      - -**family_def ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/family_def.html %} -
      - -**table_constraint ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/table_constraint.html %} -
      - -**opt_interleave ::=** - -
      - {% include {{ page.version.version }}/sql/diagrams/opt_interleave.html %} -
      - -
      - -{{site.data.alerts.callout_success}}To create a table from the results of a SELECT statement, use CREATE TABLE AS. -{{site.data.alerts.end}} - -## Parameters - -Parameter | Description -----------|------------ -`IF NOT EXISTS` | Create a new table only if a table of the same name does not already exist in the database; if one does exist, do not return an error.

      Note that `IF NOT EXISTS` checks the table name only; it does not check if an existing table has the same columns, indexes, constraints, etc., of the new table. -`table_name` | The name of the table to create, which must be unique within its database and follow these [identifier rules](keywords-and-identifiers.html#identifiers). When the parent database is not set as the default, the name must be formatted as `database.name`.

      The [`UPSERT`](upsert.html) and [`INSERT ON CONFLICT`](insert.html) statements use a temporary table called `excluded` to handle uniqueness conflicts during execution. It's therefore not recommended to use the name `excluded` for any of your tables. -`column_def` | A comma-separated list of column definitions. Each column requires a [name/identifier](keywords-and-identifiers.html#identifiers) and [data type](data-types.html); optionally, a [column-level constraint](constraints.html) or other column qualification (e.g., [computed columns](computed-columns.html)) can be specified. Column names must be unique within the table but can have the same name as indexes or constraints.

      Any `PRIMARY KEY`, `UNIQUE`, and `CHECK` [constraints](constraints.html) defined at the column level are moved to the table-level as part of the table's creation. Use the [`SHOW CREATE`](show-create.html) statement to view them at the table level. -`index_def` | An optional, comma-separated list of [index definitions](indexes.html). For each index, the column(s) to index must be specified; optionally, a name can be specified. Index names must be unique within the table and follow these [identifier rules](keywords-and-identifiers.html#identifiers). See the [Create a Table with Secondary Indexes and Inverted Indexes](#create-a-table-with-secondary-and-inverted-indexes) example below.

      The [`CREATE INDEX`](create-index.html) statement can be used to create an index separate from table creation. -`family_def` | An optional, comma-separated list of [column family definitions](column-families.html). Column family names must be unique within the table but can have the same name as columns, constraints, or indexes.

      A column family is a group of columns that are stored as a single key-value pair in the underlying key-value store. CockroachDB automatically groups columns into families to ensure efficient storage and performance. However, there are cases when you may want to manually assign columns to families. For more details, see [Column Families](column-families.html). -`table_constraint` | An optional, comma-separated list of [table-level constraints](constraints.html). Constraint names must be unique within the table but can have the same name as columns, column families, or indexes. -`opt_interleave` | You can potentially optimize query performance by [interleaving tables](interleave-in-parent.html), which changes how CockroachDB stores your data. -`opt_partition_by` | An [enterprise-only](enterprise-licensing.html) option that lets you define table partitions at the row level. You can define table partitions by list or by range. See [Define Table Partitions](partitioning.html) for more information. - -## Table-level replication - -By default, tables are created in the default replication zone but can be placed into a specific replication zone. See [Create a Replication Zone for a Table](configure-replication-zones.html#create-a-replication-zone-for-a-table) for more information. - -## Row-level replication - -CockroachDB allows [enterprise users](enterprise-licensing.html) to [define table partitions](partitioning.html), thus providing row-level control of how and where the data is stored. See [Create a Replication Zone for a Table Partition](configure-replication-zones.html#create-a-replication-zone-for-a-partition) for more information. - -{{site.data.alerts.callout_info}}The primary key required for partitioning is different from the conventional primary key. To define the primary key for partitioning, prefix the unique identifier(s) in the primary key with all columns you want to partition and subpartition the table on, in the order in which you want to nest your subpartitions. See Partition using Primary Key for more details.{{site.data.alerts.end}} - -## Examples - -### Create a table - -In this example, we create the `users` table with a single [primary key](primary-key.html) column defined. In CockroachDB, every table requires a [primary key](primary-key.html). If one is not explicitly defined, a column called `rowid` of the type `INT` is added automatically as the primary key, with the `unique_rowid()` function used to ensure that new rows always default to unique `rowid` values. The primary key is automatically indexed. - -For performance recommendations on primary keys, see the [Primary Key Constraint](primary-key.html#performance-considerations) page and the [SQL Performance Best Practices](performance-best-practices-overview.html#use-multi-column-primary-keys) page. - -{{site.data.alerts.callout_info}}Strictly speaking, a primary key's unique index is not created; it is derived from the key(s) under which the data is stored, so it takes no additional space. However, it appears as a normal unique index when using commands like SHOW INDEX.{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users ( - id UUID PRIMARY KEY, - city STRING, - name STRING, - address STRING, - credit_card STRING, - dl STRING -); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM users; -~~~ - -~~~ - column_name | data_type | is_nullable | column_default | generation_expression | indices | is_hidden -+-------------+-----------+-------------+----------------+-----------------------+-----------+-----------+ - id | UUID | false | NULL | | {primary} | false - city | STRING | true | NULL | | {} | false - name | STRING | true | NULL | | {} | false - address | STRING | true | NULL | | {} | false - credit_card | STRING | true | NULL | | {} | false - dl | STRING | true | NULL | | {} | false -(6 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW INDEX FROM users; -~~~ - -~~~ - table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit -+------------+------------+------------+--------------+-------------+-----------+---------+----------+ - users | primary | false | 1 | id | ASC | false | false -(1 row) -~~~ - -### Create a table with secondary and inverted indexes - -In this example, we create secondary and inverted indexes during table creation. Secondary indexes allow efficient access to data with keys other than the primary key. [Inverted indexes](inverted-indexes.html) allow efficient access to the schemaless data in a [`JSONB`](jsonb.html) column. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE vehicles ( - id UUID NOT NULL, - city STRING NOT NULL, - type STRING, - owner_id UUID, - creation_time TIMESTAMP, - status STRING, - current_location STRING, - ext JSONB, - CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - INDEX vehicles_auto_index_fk_city_ref_users (city ASC, owner_id ASC), - INVERTED INDEX ix_vehicle_ext (ext), - FAMILY "primary" (id, city, type, owner_id, creation_time, status, current_location, ext) -); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW INDEX FROM vehicles; -~~~ - -~~~ - table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit -+------------+---------------------------------------+------------+--------------+-------------+-----------+---------+----------+ - vehicles | primary | false | 1 | city | ASC | false | false - vehicles | primary | false | 2 | id | ASC | false | false - vehicles | vehicles_auto_index_fk_city_ref_users | true | 1 | city | ASC | false | false - vehicles | vehicles_auto_index_fk_city_ref_users | true | 2 | owner_id | ASC | false | false - vehicles | vehicles_auto_index_fk_city_ref_users | true | 3 | id | ASC | false | true - vehicles | ix_vehicle_ext | true | 1 | ext | ASC | false | false - vehicles | ix_vehicle_ext | true | 2 | city | ASC | false | true - vehicles | ix_vehicle_ext | true | 3 | id | ASC | false | true -(8 rows) -~~~ - -We also have other resources on indexes: - -- Create indexes for existing tables using [`CREATE INDEX`](create-index.html). -- [Learn more about indexes](indexes.html). - -### Create a table with auto-generated unique row IDs - -{% include {{ page.version.version }}/faq/auto-generate-unique-ids.html %} - -### Create a table with a foreign key constraint - -[Foreign key constraints](foreign-key.html) guarantee a column uses only values that already exist in the column it references, which must be from another table. This constraint enforces referential integrity between the two tables. - -There are a [number of rules](foreign-key.html#rules-for-creating-foreign-keys) that govern foreign keys, but the two most important are: - -- Foreign key columns must be [indexed](indexes.html). If no index is defined in the `CREATE TABLE` statement using `INDEX`, `PRIMARY KEY`, or `UNIQUE`, a secondary index is automatically created on the foreign key columns. - -- Referenced columns must contain only unique values. This means the `REFERENCES` clause must use exactly the same columns as a [primary key](primary-key.html) or [unique](unique.html) constraint. - -You can include a [foreign key action](foreign-key.html#foreign-key-actions) to specify what happens when a column referenced by a foreign key constraint is updated or deleted. The default actions are `ON UPDATE NO ACTION` and `ON DELETE NO ACTION`. - -In this example, we use `ON DELETE CASCADE` (i.e., when row referenced by a foreign key constraint is deleted, all dependent rows are also deleted). - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users ( - id UUID PRIMARY KEY DEFAULT gen_random_uuid(), - city STRING, - name STRING, - address STRING, - credit_card STRING, - dl STRING UNIQUE CHECK (LENGTH(dl) < 8) -); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE vehicles ( - id UUID NOT NULL DEFAULT gen_random_uuid(), - city STRING NOT NULL, - type STRING, - owner_id UUID REFERENCES users(id) ON DELETE CASCADE, - creation_time TIMESTAMP, - status STRING, - current_location STRING, - ext JSONB, - CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - INDEX vehicles_auto_index_fk_city_ref_users (city ASC, owner_id ASC), - INVERTED INDEX ix_vehicle_ext (ext), - FAMILY "primary" (id, city, type, owner_id, creation_time, status, current_location, ext) -); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE TABLE vehicles; -~~~ - -~~~ - table_name | create_statement -+------------+-----------------------------------------------------------------------------------------------------+ - vehicles | CREATE TABLE vehicles ( - | id UUID NOT NULL DEFAULT gen_random_uuid(), - | city STRING NOT NULL, - | type STRING NULL, - | owner_id UUID NULL, - | creation_time TIMESTAMP NULL, - | status STRING NULL, - | current_location STRING NULL, - | ext JSONB NULL, - | CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - | INDEX vehicles_auto_index_fk_city_ref_users (city ASC, owner_id ASC), - | INVERTED INDEX ix_vehicle_ext (ext), - | CONSTRAINT fk_owner_id_ref_users FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE, - | INDEX vehicles_auto_index_fk_owner_id_ref_users (owner_id ASC), - | FAMILY "primary" (id, city, type, owner_id, creation_time, status, current_location, ext) - | ) -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO users (name, dl) VALUES ('Annika', 'ABC-123'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users; -~~~ - -~~~ - id | city | name | address | credit_card | dl -+--------------------------------------+------+--------+---------+-------------+---------+ - 26da1fce-59e1-4290-a786-9068242dd195 | NULL | Annika | NULL | NULL | ABC-123 -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO vehicles (city, owner_id) VALUES ('seattle', '26da1fce-59e1-4290-a786-9068242dd195'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM vehicles; -~~~ - -~~~ - id | city | type | owner_id | creation_time | status | current_location | ext -+--------------------------------------+---------+------+--------------------------------------+---------------+--------+------------------+------+ - fc6f7a8c-4ba9-42e1-9c37-7be3c906050c | seattle | NULL | 26da1fce-59e1-4290-a786-9068242dd195 | NULL | NULL | NULL | NULL -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM users WHERE id = '26da1fce-59e1-4290-a786-9068242dd195'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM vehicles; -~~~ -~~~ - id | city | type | owner_id | creation_time | status | current_location | ext -+----+------+------+----------+---------------+--------+------------------+-----+ -(0 rows) -~~~ - - -### Create a table with a check constraint - -In this example, we create the `users` table, but with some column [constraints](constraints.html). One column is the [primary key](primary-key.html), and another column is given a [unique constraint](unique.html) and a [check constraint](check.html) that limits the length of the string. Primary key columns and columns with unique constraints are automatically indexed. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users ( - id UUID PRIMARY KEY, - city STRING, - name STRING, - address STRING, - credit_card STRING, - dl STRING UNIQUE CHECK (LENGTH(dl) < 8) -); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM users; -~~~ - -~~~ - column_name | data_type | is_nullable | column_default | generation_expression | indices | is_hidden -+-------------+-----------+-------------+----------------+-----------------------+------------------------+-----------+ - id | UUID | false | NULL | | {primary,users_dl_key} | false - city | STRING | true | NULL | | {} | false - name | STRING | true | NULL | | {} | false - address | STRING | true | NULL | | {} | false - credit_card | STRING | true | NULL | | {} | false - dl | STRING | true | NULL | | {users_dl_key} | false -(6 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW INDEX FROM users; -~~~ - -~~~ - table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit -+------------+--------------+------------+--------------+-------------+-----------+---------+----------+ - users | primary | false | 1 | id | ASC | false | false - users | users_dl_key | false | 1 | dl | ASC | false | false - users | users_dl_key | false | 2 | id | ASC | false | true -(3 rows) -~~~ - -### Create a table that mirrors key-value storage - -{% include {{ page.version.version }}/faq/simulate-key-value-store.html %} - -### Create a table from a `SELECT` statement - -You can use the [`CREATE TABLE AS`](create-table-as.html) statement to create a new table from the results of a `SELECT` statement. For example, suppose you have a number of rows of user data in the `users` table, and you want to create a new table from the subset of users that are located in New York. - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users WHERE city = 'new york'; -~~~ - -~~~ - id | city | name | address | credit_card -+--------------------------------------+----------+------------------+-----------------------------+-------------+ - 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228 - 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892 - 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636 - 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234 - 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487 -(5 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE users_ny AS SELECT * FROM users WHERE city = 'new york'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM users_ny; -~~~ - -~~~ - id | city | name | address | credit_card -+--------------------------------------+----------+------------------+-----------------------------+-------------+ - 00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228 - 051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892 - 0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636 - 0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234 - 147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487 -(5 rows) -~~~ - -### Create a table with a computed column - -{% include {{ page.version.version }}/computed-columns/simple.md %} - -### Create a table with partitions - -{{site.data.alerts.callout_info}} -The primary key required for partitioning is different from the conventional primary key. To define the primary key for partitioning, prefix the unique identifier(s) in the primary key with all columns you want to partition and subpartition the table on, in the order in which you want to nest your subpartitions. See [Partition using Primary Key](partitioning.html#partition-using-primary-key) for more details. -{{site.data.alerts.end}} - -#### Create a table with partitions by list - -In this example, we create a table and [define partitions by list](partitioning.html#partition-by-list). - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE rides ( - id UUID NOT NULL, - city STRING NOT NULL, - vehicle_city STRING, - rider_id UUID, - vehicle_id UUID, - start_address STRING, - end_address STRING, - start_time TIMESTAMP, - end_time TIMESTAMP, - revenue DECIMAL(10,2), - CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - INDEX rides_auto_index_fk_city_ref_users (city ASC, rider_id ASC), - INDEX rides_auto_index_fk_vehicle_city_ref_vehicles (vehicle_city ASC, vehicle_id ASC), - FAMILY "primary" (id, city, vehicle_city, rider_id, vehicle_id, start_address, end_address, start_time, end_time, revenue), - CONSTRAINT check_vehicle_city_city CHECK (vehicle_city = city)) - PARTITION BY LIST (city) - (PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle')); -~~~ - -#### Create a table with partitions by range - -In this example, we create a table and [define partitions by range](partitioning.html#partition-by-range). - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE rides ( - id UUID NOT NULL, - city STRING NOT NULL, - vehicle_city STRING, - rider_id UUID, - vehicle_id UUID, - start_address STRING, - end_address STRING, - start_time TIMESTAMP, - end_time TIMESTAMP, - ride_length INTERVAL as (start_time - end_time) STORED, - revenue DECIMAL(10,2), - CONSTRAINT "primary" PRIMARY KEY (ride_length ASC, city ASC, id ASC), - INDEX rides_auto_index_fk_city_ref_users (city ASC, rider_id ASC), - INDEX rides_auto_index_fk_vehicle_city_ref_vehicles (vehicle_city ASC, vehicle_id ASC), - FAMILY "primary" (id, city, vehicle_city, rider_id, vehicle_id, start_address, end_address, start_time, end_time, revenue), - CONSTRAINT check_vehicle_city_city CHECK (vehicle_city = city)) - PARTITION BY RANGE (ride_length) - (PARTITION short_rides VALUES FROM ('0 seconds') TO ('30 minutes'), - PARTITION long_rides VALUES FROM ('30 minutes') TO (MAXVALUE)); -~~~ - -### Show the definition of a table - -To show the definition of a table, use the [`SHOW CREATE`](show-create.html) statement. The contents of the `create_statement` column in the response is a string with embedded line breaks that, when echoed, produces formatted output. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE rides; -~~~ - -~~~ - table_name | create_statement -+------------+----------------------------------------------------------------------------------------------------------------------------------------------+ - rides | CREATE TABLE rides ( - | id UUID NOT NULL, - | city STRING NOT NULL, - | vehicle_city STRING NULL, - | rider_id UUID NULL, - | vehicle_id UUID NULL, - | start_address STRING NULL, - | end_address STRING NULL, - | start_time TIMESTAMP NULL, - | end_time TIMESTAMP NULL, - | ride_length INTERVAL NOT NULL AS (start_time - end_time) STORED, - | revenue DECIMAL(10,2) NULL, - | CONSTRAINT "primary" PRIMARY KEY (ride_length ASC, city ASC, id ASC), - | INDEX rides_auto_index_fk_city_ref_users (city ASC, rider_id ASC), - | INDEX rides_auto_index_fk_vehicle_city_ref_vehicles (vehicle_city ASC, vehicle_id ASC), - | FAMILY "primary" (id, city, vehicle_city, rider_id, vehicle_id, start_address, end_address, start_time, end_time, revenue, ride_length), - | CONSTRAINT check_vehicle_city_city CHECK (vehicle_city = city) - | ) PARTITION BY RANGE (ride_length) ( - | PARTITION short_rides VALUES FROM ('00:00:00') TO ('00:30:00'), - | PARTITION long_rides VALUES FROM ('00:30:00') TO (MAXVALUE) - | ) -(1 row) -~~~ - -## See also - -- [`INSERT`](insert.html) -- [`ALTER TABLE`](alter-table.html) -- [`DELETE`](delete.html) -- [`DROP TABLE`](drop-table.html) -- [`RENAME TABLE`](rename-table.html) -- [`SHOW TABLES`](show-tables.html) -- [`SHOW COLUMNS`](show-columns.html) -- [Column Families](column-families.html) -- [Table-Level Replication Zones](configure-replication-zones.html#create-a-replication-zone-for-a-table) -- [Define Table Partitions](partitioning.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/create-user.md b/src/current/v19.2/create-user.md deleted file mode 100644 index dc2e1c8b989..00000000000 --- a/src/current/v19.2/create-user.md +++ /dev/null @@ -1,132 +0,0 @@ ---- -title: CREATE USER -summary: The CREATE USER statement creates SQL users, which let you control privileges on your databases and tables. -toc: true ---- - -The `CREATE USER` [statement](sql-statements.html) creates SQL users, which let you control [privileges](authorization.html#assign-privileges) on your databases and tables. - -## Considerations - -- Usernames: - - Are case-insensitive - - Must start with a letter, number, or underscore - - Must contain only letters, numbers, or underscores - - Must be between 1 and 63 characters. -- After creating users, you must [grant them privileges to databases and tables](grant.html). -- All users belong to the `public` role, to which you can [grant](grant.html) and [revoke](revoke.html) privileges. -- On secure clusters, you must [create client certificates for users](cockroach-cert.html#create-the-certificate-and-key-pair-for-a-client) and users must [authenticate their access to the cluster](#user-authentication). - -## Required privileges - -The user must have the `INSERT` and `UPDATE` [privileges](authorization.html#assign-privileges) on the `system.users` table. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/create_user.html %}
      - -## Parameters - - - - Parameter | Description ------------|------------- -`user_name` | The name of the user you want to create.

      Usernames are case-insensitive; must start with a letter, number, or underscore; must contain only letters, numbers, or underscores; and must be between 1 and 63 characters. -`password` | Let the user [authenticate their access to a secure cluster](#user-authentication) using this password. Passwords must be entered as [string](string.html) values surrounded by single quotes (`'`).

      Password creation is supported only in secure clusters for non-`root` users. The `root` user must authenticate with a client certificate and key. - -## User authentication - -Secure clusters require users to authenticate their access to databases and tables. CockroachDB offers two methods for this: - -- [Client certificate and key authentication](#secure-clusters-with-client-certificates), which is available to all users. To ensure the highest level of security, we recommend only using client certificate and key authentication. - -- [Password authentication](#secure-clusters-with-passwords), which is available to non-`root` users who you've created passwords for. To create a user with a password, use the `WITH PASSWORD` clause of `CREATE USER`. To add a password to an existing user, use the [`ALTER USER`](alter-user.html) statement. - - Users can use passwords to authenticate without supplying client certificates and keys; however, we recommend using certificate-based authentication whenever possible. - - Password creation is supported only in secure clusters. - -## Examples - -### Create a user - -Usernames are case-insensitive; must start with a letter, number, or underscore; must contain only letters, numbers, or underscores; and must be between 1 and 63 characters. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE USER jpointsman; -~~~ - -After creating users, you must: - -- [Grant them privileges to databases](grant.html). -- For secure clusters, you must also [create their client certificates](cockroach-cert.html#create-the-certificate-and-key-pair-for-a-client). - -### Create a user with a password - -{% include copy-clipboard.html %} -~~~ sql -> CREATE USER jpointsman WITH PASSWORD 'Q7gc8rEdS'; -~~~ - -Password creation is supported only in secure clusters for non-`root` users. The `root` user must authenticate with a client certificate and key. - -### Manage users - -After creating a user, you can use the [`ALTER USER`](alter-user.html) statement to add or change the user's password and the [`DROP USER`](drop-user.html) statement to the remove users. - -### Authenticate as a specific user - -
      - - -
      -

      - -
      - -#### Secure clusters with client certificates - -All users can authenticate their access to a secure cluster using [a client certificate](cockroach-cert.html#create-the-certificate-and-key-pair-for-a-client) issued to their username. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --user=jpointsman -~~~ - -#### Secure clusters with passwords - -[Users with passwords](#create-a-user) can authenticate their access by entering their password at the command prompt instead of using their client certificate and key. - -If we cannot find client certificate and key files matching the user, we fall back on password authentication. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --user=jpointsman -~~~ - -
      - -
      - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --user=jpointsman -~~~ - -
      - -## See also - -- [Authorization](authorization.html) -- [`ALTER USER`](alter-user.html) -- [`DROP USER`](drop-user.html) -- [`SHOW USERS`](show-users.html) -- [`GRANT`](grant.html) -- [`SHOW GRANTS`](show-grants.html) -- [Create Security Certificates](cockroach-cert.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/create-view.md b/src/current/v19.2/create-view.md deleted file mode 100644 index 7bf8b272ca4..00000000000 --- a/src/current/v19.2/create-view.md +++ /dev/null @@ -1,112 +0,0 @@ ---- -title: CREATE VIEW -summary: The CREATE VIEW statement creates a . -toc: true ---- - -The `CREATE VIEW` statement creates a new [view](views.html), which is a stored query represented as a virtual table. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the parent database and the `SELECT` privilege on any table(s) referenced by the view. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/create_view.html %}
      - -## Parameters - -Parameter | Description -----------|------------ -`view_name` | The name of the view to create, which must be unique within its database and follow these [identifier rules](keywords-and-identifiers.html#identifiers). When the parent database is not set as the default, the name must be formatted as `database.name`. -`name_list` | An optional, comma-separated list of column names for the view. If specified, these names will be used in the response instead of the columns specified in `AS select_stmt`. -`AS select_stmt` | The [selection query](selection-queries.html) to execute when the view is requested.

      Note that it is not currently possible to use `*` to select all columns from a referenced table or view; instead, you must specify specific columns. - -## Example - -{{site.data.alerts.callout_success}}This example highlights one key benefit to using views: simplifying complex queries. For additional benefits and examples, see Views.{{site.data.alerts.end}} - -Let's say you're using our [sample `startrek` database](cockroach-gen.html#generate-example-data), which contains two tables, `episodes` and `quotes`. There's a foreign key constraint between the `episodes.id` column and the `quotes.episode` column. To count the number of famous quotes per season, you could run the following join: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT startrek.episodes.season, count(*) - FROM startrek.quotes - JOIN startrek.episodes - ON startrek.quotes.episode = startrek.episodes.id - GROUP BY startrek.episodes.season; -~~~ - -~~~ -+--------+----------+ -| season | count(*) | -+--------+----------+ -| 2 | 76 | -| 3 | 46 | -| 1 | 78 | -+--------+----------+ -(3 rows) -~~~ - -Alternatively, to make it much easier to run this complex query, you could create a view: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE VIEW startrek.quotes_per_season (season, quotes) - AS SELECT startrek.episodes.season, count(*) - FROM startrek.quotes - JOIN startrek.episodes - ON startrek.quotes.episode = startrek.episodes.id - GROUP BY startrek.episodes.season; -~~~ - -~~~ -CREATE VIEW -~~~ - -The view is then represented as a virtual table alongside other tables in the database: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM startrek; -~~~ - -~~~ -+-------------------+ -| table_name | -+-------------------+ -| episodes | -| quotes | -| quotes_per_season | -+-------------------+ -(4 rows) -~~~ - -Executing the query is as easy as `SELECT`ing from the view, as you would from a standard table: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM startrek.quotes_per_season; -~~~ - -~~~ -+--------+--------+ -| season | quotes | -+--------+--------+ -| 2 | 76 | -| 3 | 46 | -| 1 | 78 | -+--------+--------+ -(3 rows) -~~~ - -## See also - -- [Selection Queries](selection-queries.html) -- [Views](views.html) -- [`SHOW CREATE`](show-create.html) -- [`ALTER VIEW`](alter-view.html) -- [`DROP VIEW`](drop-view.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/data-types.md b/src/current/v19.2/data-types.md deleted file mode 100644 index 68a4edab788..00000000000 --- a/src/current/v19.2/data-types.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -title: Data Types -summary: Learn about the data types supported by CockroachDB. -toc: true ---- - -## Supported types - -CockroachDB supports the following data types. Click a type for more details. - -Type | Description | Example | [Vectorized Execution](vectorized-execution.html) ------|-------------|---------|---------- -[`ARRAY`](array.html) | A 1-dimensional, 1-indexed, homogeneous array of any non-array data type. | `{"sky","road","car"}` | Not supported -[`BIT`](bit.html) | A string of binary digits (bits). | `B'10010101'` | Not supported -[`BOOL`](bool.html) | A Boolean value. | `true` | Supported -[`BYTES`](bytes.html) | A string of binary characters. | `b'\141\061\142\062\143\063'` | Supported -[`COLLATE`](collate.html) | The `COLLATE` feature lets you sort [`STRING`](string.html) values according to language- and country-specific rules, known as collations. | `'a1b2c3' COLLATE en` | Not supported -[`DATE`](date.html) | A date. | `DATE '2016-01-25'` | Supported -[`DECIMAL`](decimal.html) | An exact, fixed-point number. | `1.2345` | Partially supported -[`FLOAT`](float.html) | A 64-bit, inexact, floating-point number. | `1.2345` | Supported -[`INET`](inet.html) | An IPv4 or IPv6 address. | `192.168.0.1` | Not supported -[`INT`](int.html) | A signed integer, up to 64 bits. | `12345` | Supported -[`INTERVAL`](interval.html) | A span of time. | `INTERVAL '2h30m30s'` | Not supported -[`JSONB`](jsonb.html) | JSON (JavaScript Object Notation) data. | `'{"first_name": "Lola", "last_name": "Dog", "location": "NYC", "online" : true, "friends" : 547}'` | Not supported -[`SERIAL`](serial.html) | A pseudo-type that combines an [integer type](int.html) with a [`DEFAULT` expression](default-value.html). | `148591304110702593` | Not supported -[`STRING`](string.html) | A string of Unicode characters. | `'a1b2c3'` | Supported -[`TIME`](time.html) | A time of day in UTC. | `TIME '01:23:45.123456'` | Not supported -[`TIMESTAMP`
      `TIMESTAMPTZ`](timestamp.html) | A date and time pairing in UTC. | `TIMESTAMP '2016-01-25 10:10:10'`
      `TIMESTAMPTZ '2016-01-25 10:10:10-05:00'` | `TIMESTAMP` supported
      `TIMESTAMPTZ` not supported -[`UUID`](uuid.html) | A 128-bit hexadecimal value. | `7f9c24e8-3b12-4fef-91e0-56a2d5a246ec` | Supported - -## Data type conversions and casts - -CockroachDB supports explicit type conversions using the following methods: - -- ` 'string literal'`, to convert from the literal representation of a value to a value of that type. For example: - `DATE '2008-12-21'`, `INT '123'`, or `BOOL 'true'`. - -- `::`, or its equivalent longer form `CAST( AS )`, which converts an arbitrary expression of one built-in type to another (this is also known as type coercion or "casting"). For example: - `NOW()::DECIMAL`, `VARIANCE(a+2)::INT`. - - {{site.data.alerts.callout_success}} - To create constant values, consider using a - type annotation - instead of a cast, as it provides more predictable results. - {{site.data.alerts.end}} - -- Other [built-in conversion functions](functions-and-operators.html) when the type is not a SQL type, for example `from_ip()`, `to_ip()` to convert IP addresses between `STRING` and `BYTES` values. - - -You can find each data type's supported conversion and casting on its -respective page in its section **Supported casting & conversion**. diff --git a/src/current/v19.2/date.md b/src/current/v19.2/date.md deleted file mode 100644 index 0e8d239b5f3..00000000000 --- a/src/current/v19.2/date.md +++ /dev/null @@ -1,95 +0,0 @@ ---- -title: DATE -summary: CockroachDB's DATE data type stores a year, month, and day. -toc: true ---- - -The `DATE` [data type](data-types.html) stores a year, month, and day. - -## Syntax - -A constant value of type `DATE` can be expressed using an -[interpreted literal](sql-constants.html#interpreted-literals), or a -string literal -[annotated with](scalar-expressions.html#explicitly-typed-expressions) -type `DATE` or -[coerced to](scalar-expressions.html#explicit-type-coercions) type -`DATE`. - -The string format for dates is `YYYY-MM-DD`. For example: `DATE '2016-12-23'`. - -CockroachDB also supports using uninterpreted -[string literals](sql-constants.html#string-literals) in contexts -where a `DATE` value is otherwise expected. - -{{site.data.alerts.callout_info}} -New in v19.2: `DATE` values in CockroachDB are fully [PostgreSQL-compatible](https://www.postgresql.org/docs/current/datatype-datetime.html), including support for special values (e.g., `+/- infinity`). Existing dates outside of the PostgreSQL date range (`4714-11-24 BC` to `5874897-12-31`) are converted to `+/- infinity` dates. -{{site.data.alerts.end}} - -## Size - -A `DATE` column supports values up to 16 bytes in width, but the total storage size is likely to be larger due to CockroachDB metadata. - -## Examples - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE dates (a DATE PRIMARY KEY, b INT); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM dates; -~~~ - -~~~ -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -| column_name | data_type | is_nullable | column_default | generation_expression | indices | -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -| a | DATE | false | NULL | | {"primary"} | -| b | INT | true | NULL | | {} | -+-------------+-----------+-------------+----------------+-----------------------+-------------+ -(2 rows) -~~~ - -Explicitly typed `DATE` literal: -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO dates VALUES (DATE '2016-03-26', 12345); -~~~ - -String literal implicitly typed as `DATE`: -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO dates VALUES ('2016-03-27', 12345); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM dates; -~~~ - -~~~ -+---------------------------+-------+ -| a | b | -+---------------------------+-------+ -| 2016-03-26 00:00:00+00:00 | 12345 | -| 2016-03-27 00:00:00+00:00 | 12345 | -+---------------------------+-------+ -~~~ - -## Supported casting and conversion - -`DATE` values can be [cast](data-types.html#data-type-conversions-and-casts) to any of the following data types: - -Type | Details ------|-------- -`DECIMAL` | Converts to number of days since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice. -`FLOAT` | Converts to number of days since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice. -`TIMESTAMP` | Sets the time to 00:00 (midnight) in the resulting timestamp. -`INT` | Converts to number of days since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice. -`STRING` | –– - -## See also - -[Data Types](data-types.html) diff --git a/src/current/v19.2/dbeaver.md b/src/current/v19.2/dbeaver.md deleted file mode 100644 index 348ec73552b..00000000000 --- a/src/current/v19.2/dbeaver.md +++ /dev/null @@ -1,90 +0,0 @@ ---- -title: DBeaver -summary: The DBeaver database tool completely integrates with CockroachDB to provide a GUI for managing your database. -toc: true ---- - -The [DBeaver database tool][dbeaver] is a tool that completely integrates with CockroachDB to provide a GUI for managing your database. - -According to the [DBeaver website][dbeaver]: - -> DBeaver is a cross-platform Database GUI tool for developers, SQL programmers, database administrators, and analysts. - -In this tutorial, you'll work through the process of using DBeaver with a secure CockroachDB cluster. - -{{site.data.alerts.callout_success}} -For more information about using DBeaver, see the [DBeaver documentation](https://dbeaver.io/docs/). - -If you run into problems, please file an issue on the [DBeaver issue tracker](https://github.com/dbeaver/dbeaver/issues). -{{site.data.alerts.end}} - -## Before You Begin - -To work through this tutorial, take the following steps: - -- [Install CockroachDB](install-cockroachdb.html) and [start a secure cluster](secure-a-cluster.html). -- Download a copy of [DBeaver](https://dbeaver.io/download/) version 5.2.3 or greater. - -## Step 1. Start DBeaver and connect to CockroachDB - -Start DBeaver, and select **Database > New Connection** from the menu. In the dialog that appears, select **CockroachDB** from the list. - -DBeaver - Select CockroachDB - -## Step 2. Update the connection settings - -On the **Create new connection** dialog that appears, click **Network settings**. - -DBeaver - CockroachDB connection settings - -From the network settings, click the **SSL** tab. It will look like the screenshot below. - -DBeaver - SSL tab - -Check the **Use SSL** checkbox as shown, and fill in the text areas as follows: - -- **Root certificate**: Use the `ca.crt` file you generated for your secure cluster. -- **SSL certificate**: Use a client certificate generated from your cluster's root certificate. For the root user, this will be named `client.root.crt`. For additional security, you may want to create a new database user and client certificate just for use with DBeaver. -- **SSL certificate key**: Because DBeaver is a Java application, you will need to transform your key file to the `*.pk8` format using an [OpenSSL command](https://wiki.openssl.org/index.php/Command_Line_Utilities#pkcs8_.2F_pkcs5) like the one shown below. Once you have created the file, enter its location here. In this example, the filename is `client.root.pk8`. - {% include copy-clipboard.html %} - ~~~ console - $ openssl pkcs8 -topk8 -inform PEM -outform DER -in client.root.key -out client.root.pk8 -nocrypt - ~~~ - -Select **require** from the **SSL mode** dropdown. There is no need to set the **SSL Factory**, you can let DBeaver use the default. - -## Step 3. Test the connection settings - -Click **Test Connection ...**. If everything worked, you will see a **Success** dialog like the one shown below. - -DBeaver - connection success dialog - -## Step 4. Start using DBeaver - -Click **Finish** to get started using DBeaver with CockroachDB. - -DBeaver - CockroachDB with the movr database - -For more information about using DBeaver, see the [DBeaver documentation](https://dbeaver.io/docs/). - -## Report Issues with DBeaver & CockroachDB - -If you run into problems, please file an issue on the [DBeaver issue tracker](https://github.com/dbeaver/dbeaver/issues), including the following details about the environment where you encountered the issue: - -- CockroachDB version ([`cockroach version`](cockroach-version.html)) -- DBeaver version -- Operating system -- Steps to reproduce the behavior -- If possible, a trace of the SQL statements sent to CockroachDB while the error is being reproduced using [SQL query logging](query-behavior-troubleshooting.html#sql-logging). - -## See Also - -+ [DBeaver documentation](https://dbeaver.io/docs/) -+ [DBeaver issue tracker](https://github.com/dbeaver/dbeaver/issues) -+ [Client connection parameters](connection-parameters.html) -+ [Third-Party Database Tools](third-party-database-tools.html) -+ [Learn CockroachDB SQL](learn-cockroachdb-sql.html) - - - -[dbeaver]: https://dbeaver.io diff --git a/src/current/v19.2/debug-and-error-logs.md b/src/current/v19.2/debug-and-error-logs.md deleted file mode 100644 index 7a7d28192c2..00000000000 --- a/src/current/v19.2/debug-and-error-logs.md +++ /dev/null @@ -1,101 +0,0 @@ ---- -title: Understand Debug & Error Logs -summary: CockroachDB logs include details about certain node-level and range-level events, such as errors. -toc: true ---- - -If you need to [troubleshoot](troubleshooting-overview.html) issues with your cluster, you can check a node's logs, which include details about certain node-level and range-level events, such as errors. For example, if CockroachDB crashes, it normally logs a stack trace to what caused the problem. - -## Details - -When a node processes a [`cockroach` command](cockroach-commands.html), it produces a stream of messages about the command's activities. Each message's body describes the activity, and its envelope contains metadata such as the message's severity level. - -As a command generates messages, CockroachDB uses the [command](#commands)'s [logging flags](#flags) and the message's [severity level](#severity-levels) to determine the appropriate [location](#output-locations) for it. - -Each node's logs detail only the internal activity of that node without visibility into the behavior of other nodes in the cluster. When troubleshooting, this means that you must identify the node where the problem occurred or [collect the logs from all active nodes in your cluster](cockroach-debug-zip.html). - -### Commands - -All [`cockroach` commands](cockroach-commands.html) support logging. However, it's important to note: - -- `cockroach start` generates most messages related to the operation of your cluster. -- Other commands do generate messages, but they're typically only interesting in troubleshooting scenarios. - -### Severity levels - -CockroachDB identifies each message with a severity level, letting operators know if they need to intercede: - -1. `INFO` *(lowest severity; no action necessary)* -2. `WARNING` -3. `ERROR` -4. `FATAL` *(highest severity; requires operator attention)* - -**Default behavior by severity level** - -Command | `INFO` messages | `WARNING` and above messages ---------|--------|-------------------- -[`cockroach start`](cockroach-start.html) | Write to file | Write to file -[All other commands](cockroach-commands.html) | Discard | Print to `stderr` - -### Output locations - -Based on the command's flags and the message's [severity level](#severity-levels), CockroachDB does one of the following: - -- [Writes the message to a file](#write-to-file) -- [Prints it to `stderr`](#print-to-stderr) -- [Discards the message entirely](#discard-message) - -#### Write to file - -CockroachDB can write messages to log files. The files are named using the following format: - -~~~ -cockroach.[host].[user].[start timestamp in UTC].[process ID].log -~~~ - -For example: - -~~~ -cockroach.richards-mbp.rloveland.2018-03-15T15_24_10Z.024338.log -~~~ - -{{site.data.alerts.callout_info}}All log file timestamps are in UTC because CockroachDB is designed to be deployed in a distributed cluster. Nodes may be located in different time zones, and using UTC makes it easy to correlate log messages from those nodes no matter where they are located.{{site.data.alerts.end}} - -Property | `cockroach start` | All other commands ----------|-------------------|------------------- -Enabled by | Default1 | Explicit `--log-dir` flag -Default File Destination | `[first `[`store`](cockroach-start.html#store)` dir]/logs` | *N/A* -Change File Destination | `--log-dir=[destination]` | `--log-dir=[destination]` -Default Severity Level Threshold | `INFO` | *N/A* -Change Severity Threshold | `--log-file-verbosity=[severity level]` | `--log-file-verbosity=[severity level]` -Disabled by | `--log-dir=`1 | Default - -{{site.data.alerts.callout_info}}1 If the cockroach process does not have access to on-disk storage, cockroach start does not write messages to log files; instead it prints all messages to stderr.{{site.data.alerts.end}} - -#### Print to `stderr` - -CockroachDB can print messages to `stderr`, which normally prints them to the machine's terminal but does not store them. - -Property | `cockroach start` | All other commands ----------|-------------------|------------------- -Enabled by | Explicit `--logtostderr` flag2 | Default -Default Severity Level Threshold | *N/A* | `WARNING` -Change Severity Threshold | `--logtostderr=[severity level]` | `--logtostderr=[severity level]` -Disabled by | Default2 | `--logtostderr=NONE` - -{{site.data.alerts.callout_info}}2 cockroach start does not print any messages to stderr unless the cockroach process does not have access to on-disk storage, in which case it defaults to --logtostderr=INFO and prints all messages to stderr.{{site.data.alerts.end}} - -#### Discard message - -Messages with severity levels below the `--logtostderr` and `--log-file-verbosity` flag's values are neither written to files nor printed to `stderr`, so they are discarded. - -By default, commands besides `cockroach start` discard messages with the `INFO` [severity level](#severity-levels). - -## Flags - -{% include {{ page.version.version }}/misc/logging-flags.md %} - -## See also - -- [Troubleshooting Overview](troubleshooting-overview.html) -- [Support Resources](support-resources.html) diff --git a/src/current/v19.2/decimal.md b/src/current/v19.2/decimal.md deleted file mode 100644 index 95b7d54f524..00000000000 --- a/src/current/v19.2/decimal.md +++ /dev/null @@ -1,118 +0,0 @@ ---- -title: DECIMAL -summary: The DECIMAL data type stores exact, fixed-point numbers. -toc: true ---- - -The `DECIMAL` [data type](data-types.html) stores exact, fixed-point numbers. This type is used when it is important to preserve exact precision, for example, with monetary data. - -{{site.data.alerts.callout_info}} -The [vectorized execution](vectorized-execution.html) engine does not support the serialization of `DECIMAL` data types in CockroachDB v19.2. As such, vectorized execution is not supported for `DECIMAL` data types that must be sent over a network. -{{site.data.alerts.end}} - -## Aliases - -In CockroachDB, the following are aliases for `DECIMAL`: - -- `DEC` -- `NUMERIC` - -## Precision and scale - -To limit a decimal column, use `DECIMAL(precision, scale)`, where `precision` is the **maximum** count of digits both to the left and right of the decimal point and `scale` is the **exact** count of digits to the right of the decimal point. The `precision` must not be smaller than the `scale`. Also note that using `DECIMAL(precision)` is equivalent to `DECIMAL(precision, 0)`. - -When inserting a decimal value: - -- If digits to the right of the decimal point exceed the column's `scale`, CockroachDB rounds to the scale. -- If digits to the right of the decimal point are fewer than the column's `scale`, CockroachDB pads to the scale with `0`s. -- If digits to the left and right of the decimal point exceed the column's `precision`, CockroachDB gives an error. -- If the column's `precision` and `scale` are identical, the inserted value must round to less than 1. - -## Syntax - -A constant value of type `DECIMAL` can be entered as a [numeric literal](sql-constants.html#numeric-literals). -For example: `1.414` or `-1234`. - -The special IEEE754 values for positive infinity, negative infinity -and [NaN (Not-a-Number)](https://en.wikipedia.org/wiki/NaN) cannot be -entered using numeric literals directly and must be converted using an -[interpreted literal](sql-constants.html#interpreted-literals) or an -[explicit conversion](scalar-expressions.html#explicit-type-coercions) -from a string literal instead. - -The following values are recognized: - - Syntax | Value -----------------------------------------|------------------------------------------------ - `inf`, `infinity`, `+inf`, `+infinity` | +∞ - `-inf`, `-infinity` | -∞ - `nan` | [NaN (Not-a-Number)](https://en.wikipedia.org/wiki/NaN) - -For example: - -- `DECIMAL '+Inf'` -- `'-Inf'::DECIMAL` -- `CAST('NaN' AS DECIMAL)` - -## Size - -The size of a `DECIMAL` value is variable, starting at 9 bytes. It's recommended to keep values under 64 kilobytes to ensure performance. Above that threshold, [write amplification](https://en.wikipedia.org/wiki/Write_amplification) and other considerations may cause significant performance degradation. - -## Examples - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE decimals (a DECIMAL PRIMARY KEY, b DECIMAL(10,5), c NUMERIC); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM decimals; -~~~ - -~~~ -+-------------+---------------+-------------+----------------+-----------------------+-------------+ -| column_name | data_type | is_nullable | column_default | generation_expression | indices | -+-------------+---------------+-------------+----------------+-----------------------+-------------+ -| a | DECIMAL | false | NULL | | {"primary"} | -| b | DECIMAL(10,5) | true | NULL | | {} | -| c | DECIMAL | true | NULL | | {} | -+-------------+---------------+-------------+----------------+-----------------------+-------------+ -(3 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO decimals VALUES (1.01234567890123456789, 1.01234567890123456789, 1.01234567890123456789); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM decimals; -~~~ - -~~~ -+------------------------+---------+-----------------------+ -| a | b | c | -+------------------------+---------+-----------------------+ -| 1.01234567890123456789 | 1.01235 | 1.0123456789012346789 | -+------------------------+---------+-----------------------+ -# The value in "a" matches what was inserted exactly. -# The value in "b" has been rounded to the column's scale. -# The value in "c" is handled like "a" because NUMERIC is an alias. -~~~ - -## Supported casting and conversion - -`DECIMAL` values can be [cast](data-types.html#data-type-conversions-and-casts) to any of the following data types: - -Type | Details ------|-------- -`INT` | Truncates decimal precision -`FLOAT` | Loses precision and may round up to +/- infinity if the value is too large in magnitude, or to +/-0 if the value is too small in magnitude -`BOOL` | **0** converts to `false`; all other values convert to `true` -`STRING` | –– - -## See also - -[Data Types](data-types.html) diff --git a/src/current/v19.2/default-value.md b/src/current/v19.2/default-value.md deleted file mode 100644 index 2535ab4265e..00000000000 --- a/src/current/v19.2/default-value.md +++ /dev/null @@ -1,81 +0,0 @@ ---- -title: Default Value Constraint -summary: The Default Value constraint specifies a value to populate a column with if none is provided. -toc: true ---- - -The `DEFAULT` value [constraint](constraints.html) specifies a value to write into the constrained column if one is not defined in an `INSERT` statement. The value may be either a hard-coded literal or an expression that is evaluated at the time the row is created. - - -## Details - -- The [data type](data-types.html) of the Default Value must be the same as the data type of the column. -- The `DEFAULT` value constraint only applies if the column does not have a value specified in the [`INSERT`](insert.html) statement. You can still insert a *NULL* into an optional (nullable) column by explicitly inserting *NULL*. For example, `INSERT INTO foo VALUES (1, NULL);`. - -## Syntax - -You can only apply the `DEFAULT` value constraint to individual columns. - -{{site.data.alerts.callout_info}} -You can also add the `DEFAULT` value constraint to an existing table through [`ALTER COLUMN`](alter-column.html#set-or-change-a-default-value). -{{site.data.alerts.end}} - -
      {% include {{ page.version.version }}/sql/diagrams/default_value_column_level.html %}
      - - Parameter | Description ------------|------------- - `table_name` | The name of the table you're creating. - `column_name` | The name of the constrained column. - `column_type` | The constrained column's [data type](data-types.html). - `default_value` | The value you want to insert by default, which must evaluate to the same [data type](data-types.html) as the `column_type`. - `column_constraints` | Any other column-level [constraints](constraints.html) you want to apply to this column. - `column_def` | Definitions for any other columns in the table. - `table_constraints` | Any table-level [constraints](constraints.html) you want to apply. - -## Example - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE inventories ( - product_id INT, - warehouse_id INT, - quantity_on_hand INT DEFAULT 100, - PRIMARY KEY (product_id, warehouse_id) - ); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO inventories (product_id, warehouse_id) VALUES (1,20); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO inventories (product_id, warehouse_id, quantity_on_hand) VALUES (2,30, NULL); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM inventories; -~~~ -~~~ -+------------+--------------+------------------+ -| product_id | warehouse_id | quantity_on_hand | -+------------+--------------+------------------+ -| 1 | 20 | 100 | -| 2 | 30 | NULL | -+------------+--------------+------------------+ -~~~ - -If the `DEFAULT` value constraint is not specified and an explicit value is not given, a value of *NULL* is assigned to the column. - -## See also - -- [Constraints](constraints.html) -- [`ALTER COLUMN`](alter-column.html) -- [`CHECK` constraint](check.html) -- [`REFERENCES` constraint (Foreign Key)](foreign-key.html) -- [`NOT NULL` constraint](not-null.html) -- [`PRIMARY KEY` constraint](primary-key.html) -- [`UNIQUE` constraint](unique.html) -- [`SHOW CONSTRAINTS`](show-constraints.html) diff --git a/src/current/v19.2/delete-data.md b/src/current/v19.2/delete-data.md deleted file mode 100644 index ab5b342666d..00000000000 --- a/src/current/v19.2/delete-data.md +++ /dev/null @@ -1,157 +0,0 @@ ---- -title: Delete Data -summary: How to delete data from CockroachDB during application development -toc: true ---- - -This page has instructions for deleting data from CockroachDB (using the [`DELETE`](delete.html) statement) using various programming languages. - -## Before you begin - -Make sure you have already: - -- Set up a [local cluster](secure-a-cluster.html). -- [Installed a Postgres client](install-client-drivers.html). -- [Connected to the database](connect-to-the-database.html). -- [Inserted data](insert-data.html) that you now want to delete. - -{% include {{page.version.version}}/app/retry-errors.md %} - -## Delete a single row - -
      - - - - -
      - -
      - -{% include copy-clipboard.html %} -~~~ sql -DELETE from accounts WHERE id = 1; -~~~ - -For more information about how to use the built-in SQL client, see the [`cockroach sql`](cockroach-sql.html) reference docs. - -
      - -
      - -{% include copy-clipboard.html %} -~~~ go -// 'db' is an open database connection - -if _, err := db.Exec("DELETE FROM accounts WHERE id = 1"); err != nil { - return err -} -~~~ - -{% include {{page.version.version}}/app/for-a-complete-example-go.md %} - -
      - -
      - -{% include copy-clipboard.html %} -~~~ java -// ds is an org.postgresql.ds.PGSimpleDataSource - -try (Connection connection = ds.getConnection()) { - connection.createStatement().executeUpdate("DELETE FROM accounts WHERE id = 1"); - -} catch (SQLException e) { - System.out.printf("sql state = [%s]\ncause = [%s]\nmessage = [%s]\n", - e.getSQLState(), e.getCause(), e.getMessage()); -} -~~~ - -{% include {{page.version.version}}/app/for-a-complete-example-java.md %} - -
      - -
      - -{% include copy-clipboard.html %} -~~~ python -# conn is a psycopg2 connection - -with conn.cursor() as cur: - cur.execute("DELETE FROM accounts WHERE id = 1", -conn.commit() -~~~ - -{% include {{page.version.version}}/app/for-a-complete-example-python.md %} - -
      - -## Delete multiple rows - -You can delete multiple rows from a table in several ways: - -- Using a `WHERE` clause to limit the number of rows based on one or more predicates: - - {% include copy-clipboard.html %} - ~~~ sql - DELETE FROM student_loan_accounts WHERE loan_amount < 30000; - ~~~ - -- Using a `WHERE` clause to specify multiple records by a specific column's value (in this case, `id`): - - {% include copy-clipboard.html %} - ~~~ sql - DELETE FROM accounts WHERE id IN (1, 2, 3, 4, 5); - ~~~ - -- Using [`TRUNCATE`](truncate.html) instead of [`DELETE`](delete.html) to delete all of the rows from a table, as recommended in our [performance best practices](performance-best-practices-overview.html#use-truncate-instead-of-delete-to-delete-all-rows-in-a-table). - -{{site.data.alerts.callout_success}} -We recommend [deleting large amounts of data in batches](delete.html#batch-deletes). -{{site.data.alerts.end}} - -{{site.data.alerts.callout_info}} -Before deleting large amounts of data, see [Performance considerations](#performance-considerations). -{{site.data.alerts.end}} - -## Performance considerations - -Because of the way CockroachDB works under the hood, deleting data from the database does not immediately reduce disk usage. Instead, records are marked as "deleted" and processed asynchronously by a background garbage collection process. This process runs every 25 hours by default to allow sufficient time for running [backups](backup-and-restore.html) and running [time travel queries using `AS OF SYSTEM TIME`](as-of-system-time.html). The garbage collection interval is controlled by the [`gc.ttlseconds`](configure-replication-zones.html#replication-zone-variables) setting. - -The practical implications of the above are: - -- Deleting data will not immediately decrease disk usage. -- If you issue multiple [`DELETE`](delete.html) statements in sequence that each delete large amounts of data, each subsequent `DELETE` statement will run more slowly. For details, see [Preserving `DELETE` performance over time](delete.html#preserving-delete-performance-over-time). -- To delete all of the rows in a table, [it's faster to use `TRUNCATE` instead of `DELETE`](performance-best-practices-overview.html#use-truncate-instead-of-delete-to-delete-all-rows-in-a-table). - -For more information about how the storage layer of CockroachDB works, see the [storage layer reference documentation](architecture/storage-layer.html). - -## See also - -Reference information related to this task: - -- [`DELETE`](delete.html) -- [Disk space usage after deletes](delete.html#disk-space-usage-after-deletes) -- [`TRUNCATE`](truncate.html) -- [`DROP TABLE`](drop-table.html) -- [Understanding and Avoiding Transaction Contention](performance-best-practices-overview.html#understanding-and-avoiding-transaction-contention) - -Other common tasks: - -- [Connect to the Database](connect-to-the-database.html) -- [Insert Data](insert-data.html) -- [Query Data](query-data.html) -- [Update Data](update-data.html) -- [Run Multi-Statement Transactions](run-multi-statement-transactions.html) -- [Error Handling and Troubleshooting](error-handling-and-troubleshooting.html) -- [Make Queries Fast][fast] -- [Hello World Example apps](hello-world-example-apps.html) - - - -[selection]: selection-queries.html -[manual]: manual-deployment.html -[orchestrated]: orchestration.html -[fast]: make-queries-fast.html -[paginate]: selection-queries.html#paginate-through-limited-results -[joins]: joins.html diff --git a/src/current/v19.2/delete.md b/src/current/v19.2/delete.md deleted file mode 100644 index 1bb97fbb452..00000000000 --- a/src/current/v19.2/delete.md +++ /dev/null @@ -1,431 +0,0 @@ ---- -title: DELETE -summary: The DELETE statement deletes one or more rows from a table. -toc: true ---- - -The `DELETE` [statement](sql-statements.html) deletes rows from a table. - -{{site.data.alerts.callout_danger}}If you delete a row that is referenced by a foreign key constraint and has an ON DELETE action, all of the dependent rows will also be deleted or updated.{{site.data.alerts.end}} - -{{site.data.alerts.callout_info}}To delete columns, see DROP COLUMN.{{site.data.alerts.end}} - -## Performance best practices - -- To delete all rows in a table, use [`TRUNCATE`](truncate.html) instead of `DELETE`. Unless your table is small (less than 1000 rows), `TRUNCATE` will be more performant. - -- To delete a large number of rows, we recommend iteratively deleting batches of rows until all of the unwanted rows are deleted. For more information, see [batch deletes](#batch-deletes). - -## Required privileges - -The user must have the `DELETE` and `SELECT` [privileges](authorization.html#assign-privileges) on the table. - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/delete.html %} -
      - -## Parameters - - - - Parameter | Description ------------|------------- - `common_table_expr` | See [Common Table Expressions](common-table-expressions.html). - `table_name` | The name of the table that contains the rows you want to update. - `AS table_alias_name` | An alias for the table name. When an alias is provided, it completely hides the actual table name. -`WHERE a_expr`| `a_expr` must be an expression that returns Boolean values using columns (e.g., ` = `). Delete rows that return `TRUE`.

      __Without a `WHERE` clause in your statement, `DELETE` removes all rows from the table. To delete all rows in a table, we recommend using [`TRUNCATE`](truncate.html) instead of `DELETE`.__ - `sort_clause` | An `ORDER BY` clause.

      See [Ordering of rows in DML statements](query-order.html#ordering-rows-in-dml-statements) for more details. - `limit_clause` | A `LIMIT` clause. See [Limiting Query Results](limit-offset.html) for more details. - `RETURNING target_list` | Return values based on rows deleted, where `target_list` can be specific column names from the table, `*` for all columns, or computations using [scalar expressions](scalar-expressions.html).

      To return nothing in the response, not even the number of rows updated, use `RETURNING NOTHING`. - -## Success responses - -Successful `DELETE` statements return one of the following: - - Response | Description ------------|------------- -`DELETE` _`int`_ | _int_ rows were deleted.

      `DELETE` statements that do not delete any rows respond with `DELETE 0`. When `RETURNING NOTHING` is used, this information is not included in the response. -Retrieved table | Including the `RETURNING` clause retrieves the deleted rows, using the columns identified by the clause's parameters.

      [See an example.](#return-deleted-rows) - -## Disk space usage after deletes - -Deleting a row does not immediately free up the disk space. This is -due to the fact that CockroachDB retains [the ability to query tables -historically](https://www.cockroachlabs.com/blog/time-travel-queries-select-witty_subtitle-the_future/). - -If disk usage is a concern, the solution is to -[reduce the garbage collection time-to-live](configure-replication-zones.html) (TTL) for -the zone by setting `gc.ttlseconds` to a lower value, which will cause -garbage collection to clean up deleted objects (rows, tables) more -frequently. - -## Select performance on deleted rows - -Queries that scan across tables that have lots of deleted rows will -have to scan over deletions that have not yet been garbage -collected. Certain database usage patterns that frequently scan over -and delete lots of rows will want to reduce the -[garbage collection time-to-live](configure-replication-zones.html) values to clean up -deleted rows more frequently. - -## Sorting the output of deletes - -{% include {{page.version.version}}/misc/sorting-delete-output.md %} - -For more information about ordering query results in general, see -[Ordering Query Results](query-order.html) and [Ordering of rows in -DML statements](query-order.html#ordering-rows-in-dml-statements). - -## Force index selection for deletes - -New in v19.2: By using the explicit index annotation (also known as "index hinting"), you can override [CockroachDB's index selection](https://www.cockroachlabs.com/blog/index-selection-cockroachdb-2/) and use a specific [index](indexes.html) for deleting rows of a named table. - -{{site.data.alerts.callout_info}} -Index selection can impact [performance](performance-best-practices-overview.html), but does not change the result of a query. -{{site.data.alerts.end}} - -The syntax to force a specific index for a delete is: - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM table@my_idx; -~~~ - -This is equivalent to the longer expression: - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM table@{FORCE_INDEX=my_idx}; -~~~ - -To view how the index hint modifies the query plan that CockroachDB follows for deleting rows, use an [`EXPLAIN`](explain.html#opt-option) statement. To see all indexes available on a table, use [`SHOW INDEXES`](show-index.html). - -For examples, see [Delete with index hints](#delete-with-index-hints). - -## Batch deletes - -To delete a large number of rows (i.e., tens of thousands of rows or more), we recommend iteratively deleting subsets of the rows that you want to delete, until all of the unwanted rows have been deleted. You can write a script to do this, or you can write a loop into your application. - -In the sections below, we provide guidance on batch deleting with the `DELETE` query filter [on an indexed column](#batch-delete-on-an-indexed-column) and [on a non-indexed column](#batch-delete-on-a-non-indexed-column). Filtering on an indexed column is both simpler to implement and more efficient, but adding an index to a table can slow down insertions to the table and may cause bottlenecks. Queries that filter on a non-indexed column must perform at least one full-table scan, a process that takes time proportional to the size of the entire table. - -{{site.data.alerts.callout_danger}} -Exercise caution when batch deleting rows from tables with foreign key constraints and explicit [`ON DELETE` foreign key actions](foreign-key.html#foreign-key-actions). To preserve `DELETE` performance on tables with foreign key actions, we recommend using smaller batch sizes, as additional rows updated or deleted due to `ON DELETE` actions can make batch loops significantly slower. -{{site.data.alerts.end}} - -### Batch delete on an indexed column - -For high-performance batch deletes, we recommending filtering the `DELETE` query on an [indexed column](indexes.html). - -{{site.data.alerts.callout_info}} -Having an indexed filtering column can make delete operations faster, but it might lead to bottlenecks in execution, especially if the filtering column is a [timestamp](timestamp.html). -{{site.data.alerts.end}} - -Each iteration of a batch-delete loop should execute a transaction containing a single `DELETE` query. When writing this `DELETE` query: - -- Use a `WHERE` clause to filter on a column that identifies the unwanted rows. If the filtering column is not the primary key, the column should have [a secondary index](indexes.html). Note that if the filtering column is not already indexed, it is not beneficial to add an index just to speed up batch deletes. Instead, consider [batch deleting on non-indexed columns](#batch-delete-on-a-non-indexed-column). -- To ensure that rows are efficiently scanned in the `DELETE` query, add an [`ORDER BY`](query-order.html) clause on the filtering column. -- Use a [`LIMIT`](limit-offset.html) clause to limit the number of rows to the desired batch size. To determine the optimal batch size, try out different batch sizes (1,000 rows, 10,000 rows, 100,000 rows, etc.) and monitor the change in performance. -- Add a `RETURNING` clause to the end of the query that returns the filtering column values of the deleted rows. Then, using the values of the deleted rows, update the filter to match only the subset of remaining rows to delete. This narrows each query's scan to the fewest rows possible, and [preserves the performance of the deletes over time](#preserving-delete-performance-over-time). This pattern assumes that no new rows are generated that match on the `DELETE` filter during the time that it takes to perform the delete. - -For example, suppose that you want to delete all rows in the [`tpcc`](cockroach-workload.html#tpcc-workload) `new_order` table where `no_w_id` is less than `5`, in batches of 5,000 rows. To do this, you can write a script that loops over batches of 5,000 rows, following the `DELETE` query guidance provided above. Note that in this case, `no_w_id` is the first column in the primary index, and, as a result, you do not need to create a secondary index on the column. - -In Python, the script would look similar to the following: - -{% include copy-clipboard.html %} -~~~ python -#!/usr/bin/env python3 - -import psycopg2 -import psycopg2.sql -import os - -conn = psycopg2.connect(os.environ.get('DB_URI')) -filter = 4 -lastrow = None - -while True: - with conn: - with conn.cursor() as cur: - if lastrow: - filter = lastrow[0] - query = psycopg2.sql.SQL("DELETE FROM new_order WHERE no_w_id <= %s ORDER BY no_w_id DESC LIMIT 5000 RETURNING no_w_id") - cur.execute(query, (filter,)) - print(cur.statusmessage) - if cur.rowcount == 0: - break - lastrow = cur.fetchone() - -conn.close() -~~~ - -This script iteratively deletes rows in batches of 5,000, until all of the rows where `no_w_id <= 4` are deleted. Note that at each iteration, the filter is updated to match a narrower subset of rows. - -### Batch delete on a non-indexed column - -If you cannot index the column that identifies the unwanted rows, we recommend defining the batch loop to execute separate read and write operations at each iteration: - -1. Execute a [`SELECT` query](selection-queries.html) that returns the primary key values for the rows that you want to delete. When writing the `SELECT` query: - - Use a `WHERE` clause that filters on the column identifying the rows. - - Add an [`AS OF SYSTEM TIME` clause](as-of-system-time.html) to the end of the selection subquery, or run the selection query in a separate, read-only transaction with [`SET TRANSACTION AS OF SYSTEM TIME`](as-of-system-time.html#using-as-of-system-time-in-transactions). This helps to reduce [transaction contention](transactions.html#transaction-contention). - - Use a [`LIMIT`](limit-offset.html) clause to limit the number of rows queried to a subset of the rows that you want to delete. To determine the optimal `SELECT` batch size, try out different sizes (10,000 rows, 100,000 rows, 1,000,000 rows, etc.), and monitor the change in performance. Note that this `SELECT` batch size can be much larger than the batch size of rows that are deleted in the subsequent `DELETE` query. - - To ensure that rows are efficiently scanned in the subsequent `DELETE` query, include an [`ORDER BY`](query-order.html) clause on the primary key. - -2. Write a nested `DELETE` loop over the primary key values returned by the `SELECT` query, in batches smaller than the initial `SELECT` batch size. To determine the optimal `DELETE` batch size, try out different sizes (1,000 rows, 10,000 rows, 100,000 rows, etc.), and monitor the change in performance. Where possible, we recommend executing each `DELETE` in a separate transaction. - -For example, suppose that you want to delete all rows in the [`tpcc`](cockroach-workload.html#tpcc-workload) `history` table that are older than a month. You can create a script that loops over the data and deletes unwanted rows in batches, following the query guidance provided above. - -In Python, the script would look similar to the following: - -{% include copy-clipboard.html %} -~~~ python -#!/usr/bin/env python3 - -import psycopg2 -import os -import time - -conn = psycopg2.connect(os.environ.get('DB_URI')) - -while True: - with conn: - with conn.cursor() as cur: - cur.execute("SET TRANSACTION AS OF SYSTEM TIME '-5s'") - cur.execute("SELECT h_w_id, rowid FROM history WHERE h_date < current_date() - INTERVAL '1 MONTH' ORDER BY h_w_id, rowid LIMIT 20000") - pkvals = list(cur) - if not pkvals: - return - while pkvals: - batch = pkvals[:5000] - pkvals = pkvals[5000:] - with conn: - with conn.cursor() as cur: - cur.execute("DELETE FROM history WHERE (h_w_id, rowid) = ANY %s", (batch,)) - print(cur.statusmessage) - del batch - del pkvals - time.sleep(5) - -conn.close() -~~~ - -At each iteration, the selection query returns the primary key values of up to 20,000 rows of matching historical data from 5 seconds in the past, in a read-only transaction. Then, a nested loop iterates over the returned primary key values in smaller batches of 5,000 rows. At each iteration of the nested `DELETE` loop, a batch of rows is deleted. After the nested `DELETE` loop deletes all of the rows from the initial selection query, a time delay ensures that the next selection query reads historical data from the table after the last iteration's `DELETE` final delete. - -### Batch-delete "expired" data - -CockroachDB does not support Time to Live (TTL) on table rows. To delete "expired" rows, we recommend automating a batch delete process using a job scheduler like `cron`. - -For example, suppose that every morning you want to delete all rows in the [`tpcc`](cockroach-workload.html#tpcc-workload) `history` table that are older than a month. To do this, you could use the example Python script that [batch deletes on the non-indexed `h_date` column](#batch-delete-on-a-non-indexed-column). - -To run the script with a daily `cron` job: - -1. Make the file executable: - {% include copy-clipboard.html %} - ~~~ shell - $ chmod +x cleanup.py - ~~~ - -2. Create a new `cron` job: - {% include copy-clipboard.html %} - ~~~ shell - $ crontab -e - ~~~ - - {% include copy-clipboard.html %} - ~~~ txt - 30 10 * * * DB_URI='cockroachdb://user@host:26257/bank' cleanup.py >> ~/cron.log 2>&1 - ~~~ - -Saving the `cron` file will install a new job that runs the `cleanup.py` file every morning at 10:30 A.M., writing the results to the `cron.log` file. - -### Preserving `DELETE` performance over time - -CockroachDB relies on [multi-version concurrency control (MVCC)](architecture/storage-layer.html#mvcc) to process concurrent requests while guaranteeing [strong consistency](frequently-asked-questions.html#how-is-cockroachdb-strongly-consistent). As such, when you delete a row, it is not immediately removed from disk. The MVCC values for the row will remain until the garbage collection period defined by the [`gc.ttlseconds`](configure-replication-zones.html#gc-ttlseconds) variable in the applicable [zone configuration](show-zone-configurations.html) has passed. By default, this period is 25 hours. - -This means that with the default settings, each iteration of your `DELETE` statement must scan over all of the rows previously marked for deletion within the last 25 hours. If you try to delete 10,000 rows 10 times within the same 25 hour period, the 10th command will have to scan over the 90,000 rows previously marked for deletion. - -To preserve performance over iterative `DELETE` queries, we recommend taking one of the following approaches: - -- At each iteration, update the `WHERE` clause to filter only the rows that have not yet been marked for deletion. For an example, see [Batch-delete on an indexed filter](#batch-delete-on-an-indexed-column) above. -- At each iteration, first use a `SELECT` statement to return primary key values on rows that are not yet deleted. Rows marked for deletion will not be returned. Then, use a nested `DELETE` loop over a smaller batch size, filtering on the primary key values. For an example, see [Batch delete on a non-indexed column](#batch-delete-on-a-non-indexed-column) above. -- To iteratively delete rows in constant time, using a simple `DELETE` loop, you can [alter your zone configuration](configure-replication-zones.html#overview) and change `gc.ttlseconds` to a low value like 5 minutes (i.e., `300`), and then run your `DELETE` statement once per GC interval. - -## Examples - -{% include {{page.version.version}}/sql/movr-statements.md %} - -### Delete rows using Primary Key/unique columns - -Using columns with the [Primary Key](primary-key.html) or [Unique](unique.html) constraints to delete rows ensures your statement is unambiguous—no two rows contain the same column value, so it's less likely to delete data unintentionally. - -In this example, `code` is our primary key and we want to delete the row where the code equals "about_stuff_city". Because we're positive no other rows have that value in the `code` column, there's no risk of accidentally removing another row. - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM promo_codes WHERE code = 'about_stuff_city'; -~~~ -~~~ -DELETE 1 -~~~ - -### Delete rows using non-unique columns - -Deleting rows using non-unique columns removes _every_ row that returns `TRUE` for the `WHERE` clause's `a_expr`. This can easily result in deleting data you didn't intend to. - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM promo_codes WHERE creation_time > '2019-01-30 00:00:00+00:00'; -~~~ -~~~ -DELETE 4 -~~~ - -The example statement deleted four rows, which might be unexpected. - -### Return deleted rows - -To see which rows your statement deleted, include the `RETURNING` clause to retrieve them using the columns you specify. - -#### Use all columns - -By specifying `*`, you retrieve all columns of the delete rows. - -#### Use specific columns - -To retrieve specific columns, name them in the `RETURNING` clause. - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM promo_codes WHERE creation_time > '2019-01-29 00:00:00+00:00' RETURNING code, rules; -~~~ -~~~ - code | rules -+------------------------+----------------------------------------------+ - box_investment_stuff | {"type": "percent_discount", "value": "10%"} - energy_newspaper_field | {"type": "percent_discount", "value": "10%"} - simple_guy_theory | {"type": "percent_discount", "value": "10%"} - study_piece_war | {"type": "percent_discount", "value": "10%"} - tv_this_list | {"type": "percent_discount", "value": "10%"} -(5 rows) -~~~ - -#### Change column labels - -When `RETURNING` specific columns, you can change their labels using `AS`. - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM promo_codes WHERE creation_time > '2019-01-28 00:00:00+00:00' RETURNING code, rules AS discount; -~~~ -~~~ - code | discount -+---------------------+----------------------------------------------+ - chair_company_state | {"type": "percent_discount", "value": "10%"} - view_reveal_radio | {"type": "percent_discount", "value": "10%"} -(2 rows) -~~~ - -#### Sort and return deleted rows - -To sort and return deleted rows, use a statement like the following: - -{% include copy-clipboard.html %} -~~~ sql -> WITH a AS (DELETE FROM promo_codes WHERE creation_time > '2019-01-27 00:00:00+00:00' RETURNING *) - SELECT * FROM a ORDER BY expiration_time; -~~~ - -~~~ - code | description | creation_time | expiration_time | rules -+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------+---------------------------+----------------------------------------------+ - often_thing_hair | Society right wish face see if pull. Great generation social bar read budget wonder natural. Somebody dark field economic material. Nature nature paper law worry common. Serious activity hospital wide none. | 2019-01-27 03:04:05+00:00 | 2019-01-29 03:04:05+00:00 | {"type": "percent_discount", "value": "10%"} - step_though_military | Director middle summer most create any. | 2019-01-27 03:04:05+00:00 | 2019-01-29 03:04:05+00:00 | {"type": "percent_discount", "value": "10%"} - own_whose_economy | Social participant order this. Guy toward nor indeed police player inside nor. Model education voice several college art on. Start listen their maybe. | 2019-01-27 03:04:05+00:00 | 2019-01-30 03:04:05+00:00 | {"type": "percent_discount", "value": "10%"} - crime_experience_certainly | Prepare right teacher mouth student. Trouble condition weight during scene something stand. | 2019-01-27 03:04:05+00:00 | 2019-01-31 03:04:05+00:00 | {"type": "percent_discount", "value": "10%"} - policy_its_wife | Player either she something good minute or. Nearly policy player receive. Somebody mean book store fire realize. | 2019-01-27 03:04:05+00:00 | 2019-01-31 03:04:05+00:00 | {"type": "percent_discount", "value": "10%"} -(5 rows) -~~~ - -### Delete with index hints - -Suppose you create a multi-column index on the `users` table with the `name` and `city` columns. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX ON users (name, city); -~~~ - -Now suppose you want to delete the two users named "Jon Snow". You can use the [`EXPLAIN (OPT)`](explain.html#opt-option) command to see how the [cost-based optimizer](cost-based-optimizer.html) decides to perform the delete: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (OPT) DELETE FROM users WHERE name='Jon Snow'; -~~~ - -~~~ - text -+---------------------------------------------------------------------+ - delete users - └── scan users@users_name_city_idx - └── constraint: /8/7/6: [/'Jon Snow' - /'Jon Snow'] -(3 rows) -~~~ - -The output of the `EXPLAIN` statement shows that the optimizer scans the newly-created `users_name_city_idx` index when performing the delete. This makes sense, as you are performing a delete based on the `name` column. - -Now suppose that instead you want to perform a delete, but using the `id` column instead. - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (OPT) DELETE FROM users WHERE id IN ('70a3d70a-3d70-4400-8000-000000000016', '3d70a3d7-0a3d-4000-8000-00000000000c'); -~~~ - -~~~ - text -+-------------------------------------------------------------------------------------------------------+ - delete users - └── select - ├── scan users@users_name_city_idx - └── filters - └── id IN ('3d70a3d7-0a3d-4000-8000-00000000000c', '70a3d70a-3d70-4400-8000-000000000016') -(5 rows) -~~~ - -The optimizer still scans the newly-created `users_name_city_idx` index when performing the delete. Although scanning the table on this index could still be the most efficient, you may want to assess the performance difference between using `users_name_city_idx` and an index on the `id` column, as you are performing a delete with a filter on the `id` column. - -If you provide an index hint (i.e., force the index selection) to use the primary index on the column instead, the CockroachDB will scan the users table using the primary index, on `city`, and `id`. - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (OPT) DELETE FROM users@primary WHERE id IN ('70a3d70a-3d70-4400-8000-000000000016', '3d70a3d7-0a3d-4000-8000-00000000000c'); -~~~ - -~~~ - text -+-------------------------------------------------------------------------------------------------------+ - delete users - └── select - ├── scan users - │ └── flags: force-index=primary - └── filters - └── id IN ('3d70a3d7-0a3d-4000-8000-00000000000c', '70a3d70a-3d70-4400-8000-000000000016') -(6 rows) -~~~ - -## See also - -- [`INSERT`](insert.html) -- [`UPDATE`](update.html) -- [`UPSERT`](upsert.html) -- [`TRUNCATE`](truncate.html) -- [`ALTER TABLE`](alter-table.html) -- [`DROP TABLE`](drop-table.html) -- [`DROP DATABASE`](drop-database.html) -- [Other SQL Statements](sql-statements.html) -- [Limiting Query Results](limit-offset.html) - diff --git a/src/current/v19.2/demo-automatic-cloud-migration.md b/src/current/v19.2/demo-automatic-cloud-migration.md deleted file mode 100644 index 30b03fb501f..00000000000 --- a/src/current/v19.2/demo-automatic-cloud-migration.md +++ /dev/null @@ -1,274 +0,0 @@ ---- -title: Cross-Cloud Migration -summary: Use a local cluster to simulate migrating from one cloud platform to another. -toc: true ---- - -CockroachDB's flexible [replication controls](configure-replication-zones.html) make it trivially easy to run a single CockroachDB cluster across cloud platforms and to migrate data from one cloud to another without any service interruption. This page walks you through a local simulation of the process. - -## Watch the demo - -{% include_cached youtube.html video_id="cCJkgZy6s2Q" %} - -## Step 1. Install prerequisites - -In this tutorial, you'll use CockroachDB, its built-in `ycsb` workload, and the HAProxy load balancer. Before you begin, make sure these applications are installed: - -- Install the latest version of [CockroachDB](install-cockroachdb.html). -- Install [HAProxy](http://www.haproxy.org/). If you're on a Mac and using Homebrew, use `brew install haproxy`. - -Also, to keep track of the data files and logs for your cluster, you may want to create a new directory (e.g., `mkdir cloud-migration`) and start all your nodes in that directory. - -## Step 2. Start a 3-node cluster on "cloud 1" - -If you've already [started a local cluster](start-a-local-cluster.html), the commands for starting nodes should be familiar to you. The new flag to note is [`--locality`](configure-replication-zones.html#descriptive-attributes-assigned-to-nodes), which accepts key-value pairs that describe the topography of a node. In this case, you're using the flag to specify that the first 3 nodes are running on cloud 1. - -In a new terminal, start node 1 on cloud 1: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---locality=cloud=1 \ ---store=cloud1node1 \ ---listen-addr=localhost:26257 \ ---http-addr=localhost:8080 \ ---cache=100MB \ ---join=localhost:26257,localhost:26258,localhost:26259 -~~~~ - -In a new terminal, start node 2 on cloud 1: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---locality=cloud=1 \ ---store=cloud1node2 \ ---listen-addr=localhost:26258 \ ---http-addr=localhost:8081 \ ---cache=100MB \ ---join=localhost:26257,localhost:26258,localhost:26259 -~~~ - -In a new terminal, start node 3 on cloud 1: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---locality=cloud=1 \ ---store=cloud1node3 \ ---listen-addr=localhost:26259 \ ---http-addr=localhost:8082 \ ---cache=100MB \ ---join=localhost:26257,localhost:26258,localhost:26259 -~~~ - -## Step 3. Initialize the cluster - -In a new terminal, use the [`cockroach init`](cockroach-init.html) command to perform a one-time initialization of the cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init \ ---insecure \ ---host=localhost:26257 -~~~ - -## Step 4. Set up HAProxy load balancing - -You're now running 3 nodes in a simulated cloud. Each of these nodes is an equally suitable SQL gateway to your cluster, but to ensure an even balancing of client requests across these nodes, you can use a TCP load balancer. Let's use the open-source [HAProxy](http://www.haproxy.org/) load balancer that you installed earlier. - -In a new terminal, run the [`cockroach gen haproxy`](cockroach-gen.html) command, specifying the port of any node: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen haproxy \ ---insecure \ ---host=localhost:26257 -~~~ - -This command generates an `haproxy.cfg` file automatically configured to work with the 3 nodes of your running cluster. In the file, change `bind :26257` to `bind :26000`. This changes the port on which HAProxy accepts requests to a port that is not already in use by a node and that will not be used by the nodes you'll add later. - -~~~ -global - maxconn 4096 - -defaults - mode tcp - # Timeout values should be configured for your specific use. - # See: https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#4-timeout%20connect - timeout connect 10s - timeout client 1m - timeout server 1m - # TCP keep-alive on client side. Server already enables them. - option clitcpka - -listen psql - bind :26000 - mode tcp - balance roundrobin - option httpchk GET /health?ready=1 - server cockroach1 localhost:26257 check port 8080 - server cockroach2 localhost:26258 check port 8081 - server cockroach3 localhost:26259 check port 8082 -~~~ - -Start HAProxy, with the `-f` flag pointing to the `haproxy.cfg` file: - -{% include copy-clipboard.html %} -~~~ shell -$ haproxy -f haproxy.cfg -~~~ - -## Step 5. Run a sample workload - -Now that you have a load balancer running in front of your cluster, lets use the YCSB workload built into CockroachDB to simulate multiple client connections, each performing mixed read/write workloads. - -1. In a new terminal, load the initial `ycsb` schema and data, pointing it at HAProxy's port: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init ycsb \ - 'postgresql://root@localhost:26000?sslmode=disable' - ~~~ - -2. Run the `ycsb` workload, pointing it at HAProxy's port: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run ycsb \ - --duration=20m \ - --concurrency=10 \ - --max-rate=1000 - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - - This command initiates 10 concurrent client workloads for 20 minutes, but limits the total load to 1000 operations per second (since you're running everything on a single machine). - - You'll soon see per-operation statistics print to standard output every second: - - ~~~ - _elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms) - 1s 0 9258.1 9666.6 0.7 1.3 2.0 8.9 read - 1s 0 470.1 490.9 1.7 2.9 4.1 5.0 update - 2s 0 10244.6 9955.6 0.7 1.2 2.0 6.6 read - 2s 0 559.0 525.0 1.6 3.1 6.0 7.3 update - 3s 0 9870.8 9927.4 0.7 1.4 2.4 10.0 read - 3s 0 500.0 516.6 1.6 4.2 7.9 15.2 update - 4s 0 9847.2 9907.3 0.7 1.4 2.4 23.1 read - 4s 0 506.8 514.2 1.6 3.7 7.6 17.8 update - 5s 0 10084.4 9942.6 0.7 1.3 2.1 7.1 read - 5s 0 537.2 518.8 1.5 3.5 10.0 15.2 update - ... - ~~~ - -## Step 6. Watch data balance across all 3 nodes - -Now open the Admin UI at `http://localhost:8080` and click **Metrics** in the left-hand navigation bar. The **Overview** dashboard is displayed. Hover over the **SQL Queries** graph at the top. After a minute or so, you'll see that the load generator is executing approximately 95% reads and 5% writes across all nodes: - -CockroachDB Admin UI - -Scroll down a bit and hover over the **Replicas per Node** graph. Because CockroachDB replicates each piece of data 3 times by default, the replica count on each of your 3 nodes should be identical: - -CockroachDB Admin UI - -## Step 7. Add 3 nodes on "cloud 2" - -At this point, you're running three nodes on cloud 1. But what if you'd like to start experimenting with resources provided by another cloud vendor? Let's try that by adding three more nodes to a new cloud platform. Again, the flag to note is [`--locality`](configure-replication-zones.html#descriptive-attributes-assigned-to-nodes), which you're using to specify that these next 3 nodes are running on cloud 2. - -In a new terminal, start node 4 on cloud 2: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---locality=cloud=2 \ ---store=cloud2node4 \ ---listen-addr=localhost:26260 \ ---http-addr=localhost:8083 \ ---cache=100MB \ ---join=localhost:26257,localhost:26258,localhost:26259 -~~~ - -In a new terminal, start node 5 on cloud 2: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---locality=cloud=2 \ ---store=cloud2node5 \ ---advertise-addr=localhost:26261 \ ---http-addr=localhost:8084 \ ---cache=100MB \ ---join=localhost:26257,localhost:26258,localhost:26259 -~~~ - -In a new terminal, start node 6 on cloud 2: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---locality=cloud=2 \ ---store=cloud2node6 \ ---advertise-addr=localhost:26262 \ ---http-addr=localhost:8085 \ ---cache=100MB \ ---join=localhost:26257,localhost:26258,localhost:26259 -~~~ - -## Step 8. Watch data balance across all 6 nodes - -Back on the **Overview** dashboard in Admin UI, hover over the **Replicas per Node** graph again. Because you used [`--locality`](configure-replication-zones.html#descriptive-attributes-assigned-to-nodes) to specify that nodes are running on 2 clouds, you'll see an approximately even number of replicas on each node, indicating that CockroachDB has automatically rebalanced replicas across both simulated clouds: - -CockroachDB Admin UI - -Note that it takes a few minutes for the Admin UI to show accurate per-node replica counts on hover. This is why the new nodes in the screenshot above show 0 replicas. However, the graph lines are accurate, and you can click **View node list** in the **Summary** area for accurate per-node replica counts as well. - -## Step 9. Migrate all data to "cloud 2" - -So your cluster is replicating across two simulated clouds. But let's say that after experimentation, you're happy with cloud vendor 2, and you decide that you'd like to move everything there. Can you do that without interruption to your live client traffic? Yes, and it's as simple as running a single command to add a [hard constraint](configure-replication-zones.html#replication-constraints) that all replicas must be on nodes with `--locality=cloud=2`. - -In a new terminal, [edit the default replication zone](configure-zone.html): - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --execute="ALTER RANGE default CONFIGURE ZONE USING constraints='[+cloud=2]';" --insecure --host=localhost:26257 -~~~ - -## Step 10. Verify the data migration - -Back on the **Overview** dashboard in the Admin UI, hover over the **Replicas per Node** graph again. Very soon, you'll see the replica count double on nodes 4, 5, and 6 and drop to 0 on nodes 1, 2, and 3: - -CockroachDB Admin UI - -This indicates that all data has been migrated from cloud 1 to cloud 2. In a real cloud migration scenario, at this point you would update the load balancer to point to the nodes on cloud 2 and then stop the nodes on cloud 1. But for the purpose of this local simulation, there's no need to do that. - -## Step 11. Stop the cluster - -Once you're done with your cluster, stop YCSB by switching into its terminal and pressing **CTRL-C**. Then do the same for HAProxy and each CockroachDB node. - -{{site.data.alerts.callout_success}}For the last node, the shutdown process will take longer (about a minute) and will eventually force stop the node. This is because, with only 1 node still online, a majority of replicas are no longer available (2 of 3), and so the cluster is not operational. To speed up the process, press CTRL-C a second time.{{site.data.alerts.end}} - -If you do not plan to restart the cluster, you may want to remove the nodes' data stores and the HAProxy config file: - -{% include copy-clipboard.html %} -~~~ shell -$ rm -rf cloud1node1 cloud1node2 cloud1node3 cloud2node4 cloud2node5 cloud2node6 haproxy.cfg -~~~ - -## What's next? - -Explore other core CockroachDB benefits and features: - -{% include {{ page.version.version }}/misc/explore-benefits-see-also.md %} - -You may also want to learn other ways to control the location and number of replicas in a cluster: - -- [Even Replication Across Datacenters](configure-replication-zones.html#even-replication-across-datacenters) -- [Multiple Applications Writing to Different Databases](configure-replication-zones.html#multiple-applications-writing-to-different-databases) -- [Stricter Replication for a Table and Its Indexes](configure-replication-zones.html#stricter-replication-for-a-table-and-its-secondary-indexes) -- [Tweaking the Replication of System Ranges](configure-replication-zones.html#tweaking-the-replication-of-system-ranges) diff --git a/src/current/v19.2/demo-fault-tolerance-and-recovery.md b/src/current/v19.2/demo-fault-tolerance-and-recovery.md deleted file mode 100644 index 3c16957e3c1..00000000000 --- a/src/current/v19.2/demo-fault-tolerance-and-recovery.md +++ /dev/null @@ -1,401 +0,0 @@ ---- -title: Fault Tolerance & Recovery -summary: Use a local cluster to explore how CockroachDB remains available during, and recovers after, failure. -toc: true ---- - -This page walks you through a simple demonstration of how CockroachDB remains available during, and recovers after, failure. Starting with a 6-node local cluster with the default 3-way replication, you'll run a sample workload, stop a node to simulate failure, and see how the cluster continues uninterrupted. You'll then leave that node offline for long enough to watch the cluster repair itself by re-replicating missing data to other nodes. You'll then prepare the cluster for 2 simultaneous node failures by increasing to 5-way replication, then take two nodes offline at the same time, and again see how the cluster continues uninterrupted. - -## Before you begin - -Make sure you have already [installed CockroachDB](install-cockroachdb.html). - -## Step 1. Start a 6-node cluster - -1. Use the [`cockroach start`](cockroach-start.html) command to start 6 nodes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=fault-node1 \ - --listen-addr=localhost:26257 \ - --http-addr=localhost:8080 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=fault-node2 \ - --listen-addr=localhost:26258 \ - --http-addr=localhost:8081 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=fault-node3 \ - --listen-addr=localhost:26259 \ - --http-addr=localhost:8082 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=fault-node4 \ - --listen-addr=localhost:26260 \ - --http-addr=localhost:8083 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=fault-node5 \ - --listen-addr=localhost:26261 \ - --http-addr=localhost:8084 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=fault-node6 \ - --listen-addr=localhost:26262 \ - --http-addr=localhost:8085 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - -2. Use the [`cockroach init`](cockroach-init.html) command to perform a one-time initialization of the cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach init \ - --insecure \ - --host=localhost:26257 - ~~~ - -## Step 2. Set up load balancing - -In this module, you'll run a sample workload to simulate multiple client connections. Each node is an equally suitable SQL gateway for the load, but it's always recommended to [spread requests evenly across nodes](recommended-production-settings.html#load-balancing). You'll use the open-source [HAProxy](http://www.haproxy.org/) load balancer to do that here. - -1. Install HAProxy. - -
      - - -
      -

      - -
      - If you're on a Mac and use Homebrew, run: - {% include copy-clipboard.html %} - ~~~ shell - $ brew install haproxy - ~~~ -
      - -
      - If you're using Linux and use apt-get, run: - {% include copy-clipboard.html %} - ~~~ shell - $ sudo apt-get install haproxy - ~~~ -
      - -2. Run the [`cockroach gen haproxy`](cockroach-gen.html) command, specifying the port of any node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach gen haproxy \ - --insecure \ - --host=localhost \ - --port=26257 - ~~~ - - This command generates an `haproxy.cfg` file automatically configured to work with the nodes of your running cluster. - -3. In `haproxy.cfg`, change `bind :26257` to `bind :26000`. This changes the port on which HAProxy accepts requests to a port that is not already in use by a node. - - {% include copy-clipboard.html %} - ~~~ shell - sed -i.saved 's/^ bind :26257/ bind :26000/' haproxy.cfg - ~~~ - -4. Start HAProxy, with the `-f` flag pointing to the `haproxy.cfg` file: - - {% include copy-clipboard.html %} - ~~~ shell - $ haproxy -f haproxy.cfg & - ~~~ - -## Step 3. Run a sample workload - -Now that you have a load balancer running in front of your cluster, use the [`cockroach workload`](cockroach-workload.html) command to run CockroachDB's built-in version of the YCSB benchmark, simulating multiple client connections, each performing mixed read/write operations. - -1. Load the initial `ycsb` schema and data, pointing it at HAProxy's port: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init ycsb --splits=50 \ - 'postgresql://root@localhost:26000?sslmode=disable' - ~~~ - - The `--splits` flag tells the workload to manually split ranges a number of times. This is not something you'd normally do, but for the purpose of this tutorial, it makes it easier to visualize the movement of data in the cluster. - -2. Run the `ycsb` workload, pointing it at HAProxy's port: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run ycsb \ - --duration=20m \ - --concurrency=3 \ - --max-rate=1000 \ - --tolerate-errors \ - 'postgresql://root@localhost:26000?sslmode=disable' - ~~~ - - This command initiates 3 concurrent client workloads for 20 minutes, but limits the total load to 1000 operations per second (since you're running everything on a single machine). - - You'll see per-operation statistics print to standard output every second: - - ~~~ - _elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms) - 1.0s 0 902.8 930.9 1.1 2.1 4.1 62.9 read - 1.0s 0 46.5 48.0 3.4 4.7 6.0 6.0 update - 2.0s 0 923.3 926.9 1.0 2.9 5.5 8.9 read - 2.0s 0 38.0 43.0 3.0 6.0 7.6 7.6 update - 3.0s 0 901.1 918.3 1.1 2.5 5.0 6.6 read - 3.0s 0 55.0 47.0 3.4 7.9 9.4 11.5 update - 4.0s 0 948.9 926.0 1.0 1.6 2.6 5.0 read - 4.0s 0 46.0 46.7 3.1 5.2 16.8 16.8 update - 5.0s 0 932.0 927.2 1.1 1.8 2.9 13.6 read - 5.0s 0 56.0 48.6 3.0 4.2 5.2 5.5 update - ... - ~~~ - - After the specified duration (20 minutes in this case), the workload will stop and you'll see totals printed to standard output. - -## Step 4. Check the workload - -Initially, the workload creates a new database called `ycsb`, creates a `usertable` table in that database, and inserts a bunch of rows into the table. Soon, the load generator starts executing approximately 95% reads and 5% writes. - -1. Go to the Admin UI at http://localhost:8080. - -2. To check the SQL queries getting executed, click **Metrics** on the left, and hover over the **SQL Queries** graph at the top: - - CockroachDB Admin UI - -3. To check the client connections from the load generator, select the **SQL** dashboard and hover over the **SQL Connections** graph: - - CockroachDB Admin UI - - You'll notice 3 client connections from the load generator. If you want to check that HAProxy balanced each connection to a different node, you can change the **Graph** dropdown from **Cluster** to specific nodes. - -4. To see more details about the `ycsb` database and `usertable` table, click **Databases** in the upper left and then scroll down until you see **ycsb**: - - CockroachDB Admin UI - - You can also view the schema of the `usertable` by clicking the table name: - - CockroachDB Admin UI - -5. By default, CockroachDB replicates all data 3 times and balances it across all nodes. To see this balance, click **Overview** and check the replica count across all nodes: - - CockroachDB Admin UI - -## Step 5. Simulate a single node failure - -When a node fails, the cluster waits for the node to remain offline for 5 minutes by default before considering it dead, at which point the cluster automatically repairs itself by re-replicating any of the replicas on the down nodes to other available nodes. - -1. In a new terminal, [edit the default replication zone](configure-replication-zones.html) to reduce the amount of time the cluster waits before considering a node dead to the minimum allowed of 1 minute and 15 seconds: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql \ - --insecure \ - --host=localhost:26000 \ - --execute="SET CLUSTER SETTING server.time_until_store_dead = '1m15s';" - ~~~ - -2. Then use the [`cockroach quit`](cockroach-quit.html) command to stop a node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit \ - --insecure \ - --host=localhost:26261 - ~~~ - -## Step 6. Check load continuity and cluster health - -Go back to the Admin UI, click **Metrics** on the left, and verify that the cluster as a whole continues serving data, despite one of the nodes being unavailable and marked as **Suspect**: - -CockroachDB Admin UI - -This shows that when all ranges are replicated 3 times (the default), the cluster can tolerate a single node failure because the surviving nodes have a majority of each range's replicas (2/3). - -## Step 7. Watch the cluster repair itself - -Click **Overview** on the left: - -CockroachDB Admin UI - -Because you reduced the time it takes for the cluster to consider the down node dead, after 1 minute or so, the cluster will consider the down node "dead", and you'll see the replica count on the remaining nodes increase and the number of under-replicated ranges decrease to 0. This shows the cluster repairing itself by re-replicating missing replicas. - -## Step 8. Prepare for two simultaneous node failures - -At this point, the cluster has recovered and is ready to handle another failure. However, the cluster cannot handle two _near-simultaneous_ failures in this configuration. Failures are "near-simultaneous" if they are closer together than the `server.time_until_store_dead` [cluster setting](cluster-settings.html) plus the time taken for the number of replicas on the dead node to drop to zero. If two failures occurred in this configuration, some ranges would become unavailable until one of the nodes recovers. - -To be able to tolerate 2 of 5 nodes failing simultaneously without any service interruption, ranges must be replicated 5 times. - -1. Restart the dead node, using the same command you used to start the node initially: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=fault-node5 \ - --listen-addr=localhost:26261 \ - --http-addr=localhost:8084 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - -2. Use the [`ALTER RANGE ... CONFIGURE ZONE`](configure-zone.html) command to change the cluster's `default` replication factor to 5: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --execute="ALTER RANGE default CONFIGURE ZONE USING num_replicas=5;" --insecure --host=localhost:26000 - ~~~ - -3. Back in the Admin UI **Overview** dashboard, watch the replica count increases and even out across all 6 nodes: - - CockroachDB Admin UI - - This shows the cluster up-replicating so that each range has 5 replicas, one on each node. - -## Step 9. Simulate two simultaneous node failures - -Use the [`cockroach quit`](cockroach-quit.html) command to stop two nodes: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach quit --insecure --host=localhost:26260 -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach quit --insecure --host=localhost:26261 -~~~ - -## Step 10. Check load continuity and cluster health - -1. Like before, go to the Admin UI, click **Metrics** on the left, and verify that the cluster as a whole continues serving data, despite 2 nodes being offline: - - CockroachDB Admin UI - - This shows that when all ranges are replicated 5 times, the cluster can tolerate 2 simultaneous node outages because the surviving nodes have a majority of each range's replicas (3/5). - -2. To verify this further, use the `cockroach sql` command to count the number of rows in the `ycsb.usertable` table and verify that it is still serving reads: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql \ - --insecure \ - --host=localhost:26257 \ - --execute="SELECT count(*) FROM ycsb.usertable;" - ~~~ - - ~~~ - count - +-------+ - 10000 - (1 row) - ~~~ - - And writes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql \ - --insecure \ - --host=localhost:26257 \ - --execute="INSERT INTO ycsb.usertable VALUES ('asdf', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);" - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql \ - --insecure \ - --host=localhost:26257 \ - --execute="SELECT count(*) FROM ycsb.usertable;" - ~~~ - - ~~~ - count - +-------+ - 10001 - (1 row) - ~~~ - -## Step 11. Clean up - -1. In the terminal where the YCSB workload is running, press CTRL + c. - -2. Terminate HAProxy: - - {% include copy-clipboard.html %} - ~~~ shell - $ pkill haproxy - ~~~ - -3. Use the [`cockroach quit`](cockroach-quit.html) command to shut down the remaining 4 nodes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26257 - ~~~ - - {{site.data.alerts.callout_info}} - For the final 2 nodes, the shutdown process will take longer (about a minute each) and will eventually force the nodes to stop. This is because, with only 2 of 5 nodes left, a majority of replicas are not available, and so the cluster is no longer operational. - {{site.data.alerts.end}} - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26258 - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26259 - ~~~ - -4. To restart the cluster at a later time, run the same `cockroach start` commands as earlier from the directory containing the nodes' data stores. - - If you do not plan to restart the cluster, you may want to remove the nodes' data stores and the HAProxy config files: - - {% include copy-clipboard.html %} - ~~~ shell - $ rm -rf fault-node1 fault-node2 fault-node3 fault-node4 fault-node5 fault-node6 haproxy.cfg haproxy.cfg.saved - ~~~ - -## What's next? - -Explore other core CockroachDB benefits and features: - -{% include {{ page.version.version }}/misc/explore-benefits-see-also.md %} diff --git a/src/current/v19.2/demo-follow-the-workload.md b/src/current/v19.2/demo-follow-the-workload.md deleted file mode 100644 index b57cc5fa50b..00000000000 --- a/src/current/v19.2/demo-follow-the-workload.md +++ /dev/null @@ -1,286 +0,0 @@ ---- -title: Follow-the-Workload -summary: CockroachDB can dynamically optimize read latency for the location from which most of the workload is originating. -toc: true ---- - -"Follow-the-workload" refers to CockroachDB's ability to dynamically optimize read latency for the location from which most of the workload is originating. This page explains how "follow-the-workload" works and walks you through a simple demonstration using a local cluster. - -{{site.data.alerts.callout_info}} -In practice, follow-the-workload is most useful when a CockroachDB cluster is running across multiple regions, with high latency between them, but other patterns are often preferable. For more details, see [Multi-Region Topology Patterns](topology-patterns.html#multi-region-patterns). -{{site.data.alerts.end}} - -## Before you begin - -### Basic concepts - -To understand how "follow-the-workload" works, it's important to start with some basic concepts: - -Concept | Description ---------|------------ -**Range** | CockroachDB stores all user data (tables, indexes, etc.) and almost all system data in a giant sorted map of key-value pairs. This keyspace is divided into "ranges", contiguous chunks of the keyspace, so that every key can always be found in a single range. -**Replica** | CockroachDB replicates each range (3 times by default) and stores each replica on a different node. -**Leaseholder** | For each range, one of the replicas holds the "range lease". This replica, referred to as the "leaseholder", is the one that receives and coordinates all read and write requests for the range.

      Unlike writes, read requests access the leaseholder and send the results to the client without needing to coordinate with any of the other range replicas. This reduces the network round trips involved and is possible because the leaseholder is guaranteed to be up-to-date due to the fact that all write requests also go to the leaseholder. - -### How it works - -"Follow-the-workload" is based on the way **range leases** handle read requests. Read requests bypass the Raft consensus protocol, accessing the range replica that holds the range lease (the leaseholder) and sending the results to the client without needing to coordinate with any of the other range replicas. Bypassing Raft, and the network round trips involved, is possible because the leaseholder is guaranteed to be up-to-date due to the fact that all write requests also go to the leaseholder. - -This increases the speed of reads, but it doesn't guarantee that the range lease will be anywhere close to the origin of requests. If requests are coming from the US West, for example, and the relevant range lease is on a node in the US East, the requests would likely enter a gateway node in the US West and then get routed to the node with the range lease in the US East. - -However, you can cause the cluster to actively move range leases for even better read performance by starting each node with the [`--locality`](cockroach-start.html#locality) flag. With this flag specified, the cluster knows about the location of each node, so when there's high latency between nodes, the cluster will move active range leases to a node closer to the origin of the majority of the workload. This is especially helpful for applications with workloads that move around throughout the day (e.g., most of the traffic is in the US East in the morning and in the US West in the evening). - -{{site.data.alerts.callout_success}} -To enable "follow-the-workload", you just need to start each node of the cluster with the `--locality` flag, as shown in the tutorial below. No additional user action is required. -{{site.data.alerts.end}} - -### Example - -In this example, let's imagine that lots of read requests are going to node 1, and that the requests are for data in range 3. Because range 3's lease is on node 3, the requests are routed to node 3, which returns the results to node 1. Node 1 then responds to the clients. - -Follow-the-workload example - -However, if the nodes were started with the [`--locality`](cockroach-start.html#locality) flag, after a short while, the cluster would move range 3's lease to node 1, which is closer to the origin of the workload, thus reducing the network round trips and increasing the speed of reads. - -Follow-the-workload example - -## Step 1. Install prerequisites - -In this tutorial, you'll use CockroachDB, the `comcast` network tool to simulate network latency on your local workstation, and the `tpcc` workload built into CockroachDB to simulate client workloads. Before you begin, make sure these applications are installed: - -- Install the latest version of [CockroachDB](install-cockroachdb.html). -- Install [Go](https://golang.org/doc/install) version 1.9 or higher. If you're on a Mac and using Homebrew, use `brew install go`. You can check your local version by running `go version`. -- Install the [`comcast`](https://github.com/tylertreat/comcast) network simulation tool: `go get github.com/tylertreat/comcast` - -Also, to keep track of the data files and logs for your cluster, you may want to create a new directory (e.g., `mkdir follow-workload`) and start all your nodes in that directory. - -## Step 2. Start the cluster - -Use the [`cockroach start`](cockroach-start.html) command to start 3 nodes on your local workstation, using the [`--locality`](cockroach-start.html#locality) flag to pretend that each node is in a different region of the US. - -1. Start a node in the "US West": - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --locality=region=us-west \ - --store=follow1 \ - --listen-addr=localhost:26257 \ - --http-addr=localhost:8080 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - -2. Start a node in the "US Midwest": - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --locality=region=us-midwest \ - --store=follow2 \ - --listen-addr=localhost:26258 \ - --http-addr=localhost:8081 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - -3. Start a node in the "US East": - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --locality=region=us-east \ - --store=follow3 \ - --listen-addr=localhost:26259 \ - --http-addr=localhost:8082 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - -4. Use the [`cockroach init`](cockroach-init.html) command to perform a one-time initialization of the cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach init \ - --insecure \ - --host=localhost:26257 - ~~~ - -## Step 3. Simulate network latency - -"Follow-the-workload" only kicks in when there's high latency between the nodes of the CockroachDB cluster. In this tutorial, you'll run 3 nodes on your local workstation, with each node pretending to be in a different region of the US. To simulate latency between the nodes, use the `comcast` tool that you installed earlier. - -1. Start `comcast` as follows: - - {% include copy-clipboard.html %} - ~~~ shell - $ comcast --device lo0 --latency 100 - ~~~ - - For the `--device` flag, use `lo0` if you're on Mac or `lo` if you're on Linux. If neither works, run the `ifconfig` command and find the interface responsible for `127.0.0.1` in the output. - - This command causes a 100 millisecond delay for all requests on the loopback interface of your local workstation. It will only affect connections from the machine to itself, not to/from the Internet. - -2. To verify the delay between nodes, check the **Network Latency** page of the Admin UI: - - CockroachDB Admin UI - -## Step 4. Simulate traffic in the US East - -Now that the cluster is live, use CockroachDB's [built-in version the `tpcc` benchmark](cockroach-workload.html) to simulate multiple client connections to the node in the "US East". - -1. Load the initial schema and data, pointing it at port `26259`, which is the port of the node with the `us-east` locality: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init tpcc \ - 'postgresql://root@localhost:26259?sslmode=disable' - ~~~ - -2. Let the workload run to completion. - -## Step 5. Check the location of the range lease - -The load generator created a `tpcc` database with several tables that map to underlying key-value ranges. Verify that the range lease for the `customer` table moved to the node in the "US East" as follows. - -1. Run the [`cockroach node status`](cockroach-node.html) command against any node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach node status --insecure --host=localhost:26259 - ~~~ - - ~~~ - id | address | sql_address | build | started_at | updated_at | locality | is_available | is_live - +----+-----------------+-----------------+-----------------------------------------+----------------------------------+----------------------------------+-------------------+--------------+---------+ - 1 | localhost:26257 | localhost:26257 | v19.2.0-alpha.20190606-2491-gfe735c9a97 | 2019-09-28 03:14:20.566372+00:00 | 2019-09-28 03:18:41.866604+00:00 | region=us-west | true | true - 2 | localhost:26259 | localhost:26259 | v19.2.0-alpha.20190606-2491-gfe735c9a97 | 2019-09-28 03:14:21.353188+00:00 | 2019-09-28 03:18:38.165272+00:00 | region=us-east | true | true - 3 | localhost:26258 | localhost:26258 | v19.2.0-alpha.20190606-2491-gfe735c9a97 | 2019-09-28 03:14:21.862969+00:00 | 2019-09-28 03:18:38.577831+00:00 | region=us-midwest | true | true - (3 rows) - ~~~ - -2. In the response, note the ID of the node running on port `26259` (in this case, node 2). - -3. Connect the [built-in SQL shell](cockroach-sql.html) to any node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure --host=localhost:26259 - ~~~ - -4. Check where the range lease is for the `tpcc.customer` table: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW RANGES FROM TABLE tpcc.customer; - ~~~ - - ~~~ - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-----------------------+----------+---------------------------------------------------+ - NULL | NULL | 28 | 19.194141 | 2 | region=us-east | {1,2,3} | {region=us-west,region=us-east,region=us-midwest} - (1 row) - ~~~ - - `lease_holder` and `replicas` indicate the node IDs. As you can see, the lease for the range holding the `customer` table's data is on node 2, which is the same ID as the node on port `26259`. - -5. Exit the SQL shell: - - {% include copy-clipboard.html %} - ~~~ sql - > \q - ~~~ - -## Step 6. Simulate traffic in the US West - -1. Run the [`cockroach workload run tpcc`](cockroach-workload.html) command to generate more load, this time pointing it at port `26257`, which is the port of the node with the `us-west` locality: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload run tpcc \ - --duration=5m \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - - You'll see per-operation statistics print to standard output every second. - -2. Let the workload run to completion. This is necessary since the system will still "remember" the earlier requests to the other locality. - - {{site.data.alerts.callout_info}} - The latency numbers printed by the workload will be over 200 milliseconds because the 100 millisecond delay in each direction (200ms round-trip) caused by the `comcast` tool also applies to the traffic going from the `tpcc` process to the `cockroach` process. If you were to set up more advanced rules that excluded the `tpcc` process's traffic or to run this on a real network with real network delay, these numbers would be down in the single-digit milliseconds. - {{site.data.alerts.end}} - -## Step 7. Check the location of the range lease - -Verify that the range lease for the `customer` table moved to the node in the "US West" as follows. - -1. Connect the [built-in SQL shell](cockroach-sql.html) to any node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure --host=localhost:26257 - ~~~ - -2. Check where the range lease is for the `tpcc.customer` table: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW RANGES FROM TABLE tpcc.customer; - ~~~ - - ~~~ - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-----------------------+----------+---------------------------------------------------+ - NULL | NULL | 28 | 19.194141 | 1 | region=us-west | {1,2,3} | {region=us-west,region=us-east,region=us-midwest} - (1 row) - ~~~ - - As you can see, the lease for the range holding the `customer` table's data is now on node 1, which is the same ID as the node on port `26257`. - -## Step 8. Clean up - -1. Once you're done with this tutorial, you will not want a 100 millisecond delay for all requests on your local workstation, so stop the `comcast` tool: - - {% include copy-clipboard.html %} - ~~~ shell - $ comcast --device lo0 --stop - ~~~ - -2. Use the [`cockroach quit`](cockroach-quit.html) command to gracefully shut down each node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26257 - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26258 - ~~~ - - {{site.data.alerts.callout_info}} - For the last node, the shutdown process will take longer (about a minute each) and will eventually force the node to stop. This is because, with only 1 of 3 nodes left, a majority of replicas are not available, and so the cluster is no longer operational. - {{site.data.alerts.end}} - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26259 - ~~~ - -3. To restart the cluster at a later time, run the same `cockroach start` commands as earlier from the directory containing the nodes' data stores. - - If you do not plan to restart the cluster, you may want to remove the nodes' data stores: - - {% include copy-clipboard.html %} - ~~~ shell - $ rm -rf follow1 follow2 follow3 - ~~~ - -## What's next? - -Explore other core CockroachDB benefits and features: - -{% include {{ page.version.version }}/misc/explore-benefits-see-also.md %} diff --git a/src/current/v19.2/demo-json-support.md b/src/current/v19.2/demo-json-support.md deleted file mode 100644 index 1c177cbdd7b..00000000000 --- a/src/current/v19.2/demo-json-support.md +++ /dev/null @@ -1,271 +0,0 @@ ---- -title: JSON Support -summary: Use a local cluster to explore how CockroachDB can store and query unstructured JSONB data. -toc: true ---- - -This page walks you through a simple demonstration of how CockroachDB can store and query unstructured [`JSONB`](jsonb.html) data from a third-party API, as well as how an [inverted index](inverted-indexes.html) can optimize your queries. - -## Step 1. Install prerequisites - -
      - - -
      - -
      -- Install the latest version of [CockroachDB](install-cockroachdb.html). -- Install the latest version of [Go](https://golang.org/dl/): `brew install go` -- Install the [PostgreSQL driver](https://github.com/lib/pq): `go get github.com/lib/pq` -
      - -
      -- Install the latest version of [CockroachDB](install-cockroachdb.html). -- Install the [Python psycopg2 driver](http://initd.org/psycopg/docs/install.html): `pip install psycopg2` -- Install the [Python Requests library](https://requests.readthedocs.io/en/latest/): `pip install requests` -
      - -## Step 2. Start a single-node cluster - -For the purpose of this tutorial, you need only one CockroachDB node running in insecure mode, so use the [`cockroach start-single-node`](cockroach-start-single-node.html) command: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start-single-node \ ---insecure \ ---store=json-test \ ---listen-addr=localhost:26257 \ ---http-addr=localhost:8080 \ ---background -~~~ - -## Step 3. Create a user - -Open the [built-in SQL shell](cockroach-sql.html) as the `root` user and create a new user, `maxroach`: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --host=localhost:26257 -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> CREATE USER maxroach; -~~~ - -## Step 4. Create a database and grant privileges - -Next, create a database called `jsonb_test`: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE DATABASE jsonb_test; -~~~ - -Set the database as the default: - -{% include copy-clipboard.html %} -~~~ sql -> SET DATABASE = jsonb_test; -~~~ - -Then [grant privileges](grant.html) to the `maxroach` user: - -{% include copy-clipboard.html %} -~~~ sql -> GRANT ALL ON DATABASE jsonb_test TO maxroach; -~~~ - -## Step 5. Create a table - -Still in the SQL shell, create a table called `programming`: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE programming ( - id UUID DEFAULT uuid_v4()::UUID PRIMARY KEY, - posts JSONB - ); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE TABLE programming; -~~~ - -~~~ - table_name | create_statement -+-------------+------------------------------------------------+ - programming | CREATE TABLE programming ( - | id UUID NOT NULL DEFAULT uuid_v4()::UUID, - | posts JSONB NULL, - | CONSTRAINT "primary" PRIMARY KEY (id ASC), - | FAMILY "primary" (id, posts) - | ) -(1 row) -~~~ - -## Step 6. Run the code - -Now that you have a database, user, and a table, let's run code to insert rows into the table. - -
      - - -
      - -
      -The code queries the [Reddit API](https://www.reddit.com/dev/api/) for posts in [/r/programming](https://www.reddit.com/r/programming/). The Reddit API only returns 25 results per page; however, each page returns an `"after"` string that tells you how to get the next page. Therefore, the program does the following in a loop: - -1. Makes a request to the API. -2. Inserts the results into the table and grabs the `"after"` string. -3. Uses the new `"after"` string as the basis for the next request. - -Download the json-sample.go file, or create the file yourself and copy the code into it: - -{% include copy-clipboard.html %} -~~~ go -{% include {{ page.version.version }}/json/json-sample.go %} -~~~ - -In a new terminal window, navigate to your sample code file and run it: - -{% include copy-clipboard.html %} -~~~ shell -$ go run json-sample.go -~~~ -
      - -
      -The code queries the [Reddit API](https://www.reddit.com/dev/api/) for posts in [/r/programming](https://www.reddit.com/r/programming/). The Reddit API only returns 25 results per page; however, each page returns an `"after"` string that tells you how to get the next page. Therefore, the program does the following in a loop: - -1. Makes a request to the API. -2. Grabs the `"after"` string. -3. Inserts the results into the table. -4. Uses the new `"after"` string as the basis for the next request. - -Download the json-sample.py file, or create the file yourself and copy the code into it: - -{% include copy-clipboard.html %} -~~~ python -{% include {{ page.version.version }}/json/json-sample.py %} -~~~ - -In a new terminal window, navigate to your sample code file and run it: - -{% include copy-clipboard.html %} -~~~ shell -$ python json-sample.py -~~~ -
      - -The program will take awhile to finish, but you can start querying the data right away. - -## Step 7. Query the data - -Back in the terminal where the SQL shell is running, verify that rows of data are being inserted into your table: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT count(*) FROM programming; -~~~ - -~~~ - count -+-------+ - 675 -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT count(*) FROM programming; -~~~ - -~~~ - count -+-------+ - 825 -(1 row) -~~~ - -Now, retrieve all the current entries where the link is pointing to somewhere on GitHub: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT id FROM programming \ -WHERE posts @> '{"data": {"domain": "github.com"}}'; -~~~ - -~~~ - id -+--------------------------------------+ - 05348629-d8f1-4c90-99cc-11e8ab313edb - 059a1562-0054-49ff-adc7-aec82c6f74fb - 1b5ea86d-c892-43ba-b40a-c63761aff3ea - 25ac5bfe-44e2-4c6a-892c-959f859ee4e7 - 2ab49796-3e55-4a33-8a83-9decef9fbccc - 2df2e3ac-757b-4689-844d-935876df75e9 - 4506e0b8-a572-499c-a9c1-2a5075a021f8 - 5209ce99-2253-4490-bceb-fd881ff6d962 - 56cf90cd-43a9-49e9-a078-3e28c115232f - 57f287a3-d396-460a-a649-9fa41c4315e4 - ... -(90 rows) - -Time: 103.748ms -~~~ - -{{site.data.alerts.callout_info}} -Since you are querying live data, your results for this and the following steps may vary from the results documented in this tutorial. -{{site.data.alerts.end}} - -## Step 8. Create an inverted index to optimize performance - -The query in the previous step took 103.748ms. To optimize the performance of queries that filter on the `JSONB` column, let's create an [inverted index](inverted-indexes.html) on the column: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INVERTED INDEX ON programming(posts); -~~~ - -## Step 9. Run the query again - -Now that there is an inverted index, the same query will run much faster: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT id FROM programming \ -WHERE posts @> '{"data": {"domain": "github.com"}}'; -~~~ -~~~ -(109 rows) - -Time: 6.862ms -~~~ - -Instead of 103.748ms, the query now takes 6.862ms. - -## Step 10. Clean up - -If the program is still running, press CTRL + c to stop it and then use [`cockroach quit`](cockroach-quit.html) to stop the single-node CockroachDB cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach quit --insecure --host=localhost:26257 -~~~ - -If you do not plan to restart the cluster, remove the node's data store: - -{% include copy-clipboard.html %} -~~~ shell -$ rm -rf json-test -~~~ - -## What's next? - -Explore other core CockroachDB benefits and features: - -{% include {{ page.version.version }}/misc/explore-benefits-see-also.md %} - -You may also want to learn more about the [`JSONB`](jsonb.html) data type and [inverted indexes](inverted-indexes.html). diff --git a/src/current/v19.2/demo-low-latency-multi-region-deployment.md b/src/current/v19.2/demo-low-latency-multi-region-deployment.md deleted file mode 100644 index ef2fb9aa63e..00000000000 --- a/src/current/v19.2/demo-low-latency-multi-region-deployment.md +++ /dev/null @@ -1,1151 +0,0 @@ ---- -title: Low Latency Reads and Writes in a Multi-Region Cluster -summary: Use data topologies to get low-latency reads and writes in a multi-region CockroachDB cluster. -toc: true -toc_not_nested: true -key: demo-geo-partitioning.html ---- - -In CockroachDB, data is replicated and distributed across the nodes of a cluster for consistency and resiliency, and [read and write requests are automatically routed between nodes](architecture/reads-and-writes-overview.html) as appropriate. In a single-region cluster, this behavior doesn't affect performance because network latency between nodes is sub-millisecond. In a cluster spread across multiple geographic regions, however, the distribution of data becomes a key performance bottleneck, and for that reason, it is important to think about the latency requirements of each table and then use the appropriate [data topologies](topology-patterns.html) to locate data for optimal performance. - -This tutorial walks you through the process of deploying a 9-node CockroachDB cluster across 3 US regions, 3 AZs per region, with a fictional vehicle-sharing application called [MovR](movr.html) running concurrently in each region. Initially, you'll see the effect of network latency when requests must move back and forth across the US. Then you'll use two important multi-region data topologies, [Geo-Partitioned Replicas](topology-geo-partitioned-replicas.html) and [Duplicate Indexes](topology-duplicate-indexes.html), to remove this bottleneck and dramatically lower latency, with the majority of reads and writes executing in 2 milliseconds or less. Finally, you'll experience the cluster's resiliency to AZ-level failure. - -## See it in action - -### Watch a demo - -Watch [this webinar recording](https://www.cockroachlabs.com/webinars/implementation-topologies-for-distributed-sql/) to see a demonstration of the concepts and features in this tutorial. - -### Read a case study - -Read about how an [electronic lock manufacturer](https://www.cockroachlabs.com/case-studies/european-electronic-lock-manufacturer-modernizes-iam-system-with-managed-cockroachdb/) and [multi-national bank](https://www.cockroachlabs.com/case-studies/top-five-multinational-bank-modernizes-its-european-core-banking-services-migrating-from-oracle-to-cockroachdb/) are using the Geo-Partitioned Replicas topology in production for improved performance and regulatory compliance. - -## Before you begin - -- [Request a trial license](#request-a-trial-license) -- [Review important concepts](#review-important-concepts) -- [Review the cluster setup](#review-the-cluster-setup) -- [Review the MovR application](#review-the-movr-application) - -### Request a trial license - -Some CockroachDB features used in this tutorial require an enterprise license, so [request a 30-day trial license](https://www.cockroachlabs.com/get-cockroachdb/enterprise/) before you get started. - -You should receive your trial license via email within a few minutes. You'll enable your license once your cluster is up-and-running. - -### Review important concepts - -To understand performance in a geographically distributed CockroachDB cluster, it's important to first review [how reads and writes work in CockroachDB](architecture/reads-and-writes-overview.html). - -### Review the cluster setup - -You'll deploy a 9-node CockroachDB cluster across 3 GCE regions, with each node on a VM in a distinct availability zone for optimal resiliency: - -Multi-region hardware setup - -A few notes: - -- For each CockroachDB node, you'll use the [`n1-standard-4`](https://cloud.google.com/compute/docs/machine-types#standard_machine_types) machine type (4 vCPUs, 15 GB memory) with the Ubuntu 16.04 OS image and a [local SSD](https://cloud.google.com/compute/docs/disks/local-ssd#create_local_ssd) disk. -- You'll start each node with the [`--locality` flag](cockroach-start.html#locality) describing the node's region and availability zone. Initially, this locality information will lead CockroachDB to evenly distribute data across the 3 regions. Then, it will be used to apply data topologies for lower latency. -- There will be an extra VM in each region for an instance of the MovR application and the open-source HAProxy load balancer. The application in each region will be pointed at the local load balancer, which will direct connections only to the CockroachDB nodes in the same region. - -### Review the MovR application - -For your application, you'll use our open-source, fictional, peer-to-peer vehicle-sharing app, [MovR](movr.html). You'll run 3 instances of MovR, one in each US region, with each instance representing users in a specific city: New York, Chicago, or Seattle. - -#### The schema - -{% include {{ page.version.version }}/misc/movr-schema.md %} - -All of the tables except `promo_codes` have a multi-column primary key of `city` and `id`, with `city` being the first in the key. As such, the rows in these tables are geographically specific and ordered by geography. These tables are read and updated very frequently, and so to keep read and write latency low, you'll use the [Geo-Partitioned Replicas](topology-geo-partitioned-replicas.html) topology for these tables. - -In contrast, the data in the `promo_codes` table is not tied to geography, and the data is read frequently but rarely updated. This type of table is often referred to as a "reference table" or "lookup table". In this case, you'll use the [Duplicate Indexes](topology-duplicate-indexes.html) topology to keep just read latency very low, since that's primary. - -#### The workflow - -{% include {{ page.version.version }}/misc/movr-workflow.md %} - -## Step 1. Set up the environment - -- [Configure your network](#configure-your-network) -- [Provision VMs](#provision-vms) - -### Configure your network - -{% include {{ page.version.version }}/performance/configure-network.md %} - -### Provision VMs - -You need 9 VMs across 3 GCE regions, 3 per region with each VM in a distinct availability zone. You also need 3 extra VMs, 1 per region, for a region-specific version of MovR and the HAProxy load balancer. - -1. [Create 9 VMs](https://cloud.google.com/compute/docs/instances/create-start-instance) for CockroachDB nodes. - - When creating each VM: - - Use the [`n1-standard-4`](https://cloud.google.com/compute/docs/machine-types#standard_machine_types) machine type (4 vCPUs, 15 GB memory) and the Ubuntu 16.04 OS image. - - Select one of the following [region and availability zone](https://cloud.google.com/compute/docs/regions-zones/) configurations. Be sure to use each region/availability combination only once. - - VM | Region | Availability Zone - ---|--------|------------------ - 1 | `us-east1` | `us-east1-b` - 2 | `us-east1` | `us-east1-c` - 3 | `us-east1` | `us-east1-d` - 4 | `us-central1` | `us-central1-a` - 5 | `us-central1` | `us-central1-b` - 6 | `us-central1` | `us-central1-c` - 7 | `us-west1` | `us-west1-a` - 8 | `us-west1` | `us-west1-b` - 9 | `us-west1` | `us-west1-c` - - [Create and mount a local SSD](https://cloud.google.com/compute/docs/disks/local-ssd#create_local_ssd). - - To apply the Admin UI firewall rule you created earlier, click **Management, disk, networking, SSH keys**, select the **Networking** tab, and then enter `cockroachdb` in the **Network tags** field. - -2. [Create 3 VMs](https://cloud.google.com/compute/docs/instances/create-start-instance) for the region-specific versions of MovR and HAProxy, one in each of the regions mentioned above, using same machine types and OS image as mentioned above. - -3. Note the internal IP address of each VM. You'll need these addresses when starting the CockroachDB nodes, configuring HAProxy, and running the MovR application. - -## Step 2. Start CockroachDB - -Now that you have VMs in place, start your CockroachDB cluster across the three US regions. - -- [Start nodes in US East](#start-nodes-in-us-east) -- [Start nodes in US Central](#start-nodes-in-us-central) -- [Start nodes in US West](#start-nodes-in-us-west) -- [Initialize the cluster](#initialize-the-cluster) - -### Start nodes in US East - -1. SSH to the first VM in the US East region where you want to run a CockroachDB node. - -2. Download the [CockroachDB archive](https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz) for Linux, extract the binary, and copy it into the `PATH`: - - {% include copy-clipboard.html %} - ~~~ shell - $ curl https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz \ - | tar -xz - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo cp -i cockroach-{{ page.release_info.version }}.linux-amd64/cockroach /usr/local/bin/ - ~~~ - -3. Run the [`cockroach start`](cockroach-start.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --advertise-addr= \ - --join=:26257,:26257,:26257 \ - --locality=cloud=gce,region=us-east1,zone= \ - --cache=.25 \ - --max-sql-memory=.25 \ - --background - ~~~ - -4. Repeat steps 1 - 3 for the other two CockroachDB nodes in the region. Each time, be sure to: - - Adjust the `--advertise-addr` flag. - - Use the appropriate availability zone of the VM in the `zone` portion of the `--locality` flag. - -### Start nodes in US Central - -1. SSH to the first VM in the US Central region where you want to run a CockroachDB node. - -2. Download the [CockroachDB archive](https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz) for Linux, extract the binary, and copy it into the `PATH`: - - {% include copy-clipboard.html %} - ~~~ shell - $ curl https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz \ - | tar -xz - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo cp -i cockroach-{{ page.release_info.version }}.linux-amd64/cockroach /usr/local/bin/ - ~~~ - -3. Run the [`cockroach start`](cockroach-start.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --advertise-addr= \ - --join=:26257,:26257,:26257 \ - --locality=cloud=gce,region=us-central1,zone= \ - --cache=.25 \ - --max-sql-memory=.25 \ - --background - ~~~ - -4. Repeat steps 1 - 3 for the other two CockroachDB nodes in the region. Each time, be sure to: - - Adjust the `--advertise-addr` flag. - - Use the appropriate availability zone of the VM in the `zone` portion of the `--locality` flag. - -### Start nodes in US West - -1. SSH to the first VM in the US West region where you want to run a CockroachDB node. - -2. Download the [CockroachDB archive](https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz) for Linux, extract the binary, and copy it into the `PATH`: - - {% include copy-clipboard.html %} - ~~~ shell - $ curl https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz \ - | tar -xz - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo cp -i cockroach-{{ page.release_info.version }}.linux-amd64/cockroach /usr/local/bin/ - ~~~ - -3. Run the [`cockroach start`](cockroach-start.html) command: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --advertise-addr= \ - --join=:26257,:26257,:26257 \ - --locality=cloud=gce,region=us-west1,zone= \ - --cache=.25 \ - --max-sql-memory=.25 \ - --background - ~~~ - -4. Repeat steps 1 - 3 for the other two CockroachDB nodes in the region. Each time, be sure to: - - Adjust the `--advertise-addr` flag. - - Use the appropriate availability zone of the VM in the `zone` portion of the `--locality` flag. - -### Initialize the cluster - -On any of the VMs, run the one-time [`cockroach init`](cockroach-init.html) command to join the first nodes into a cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init --insecure --host=
      -~~~ - -## Step 3. Start MovR - -- [Set up the client VMs](#set-up-the-client-vms) -- [Configure the cluster for MovR](#configure-the-cluster-for-movr) -- [Start MovR in US East](#start-movr-in-us-east) -- [Start MovR in US Central](#start-movr-in-us-central) -- [Start MovR in US West](#start-movr-in-us-west) - -### Set up the client VMs - -Next, install Docker and HAProxy on each client VM. Docker is required so you can later run MovR from a Docker image, and HAProxy will serve as the region-specific load balancer for MovR in each region. - -1. SSH to the VM in the US East region where you want to run MovR and HAProxy. - -2. Install Docker: - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo apt-get update && \ - sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common && \ - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && \ - sudo apt-key fingerprint 0EBFCD88 && \ - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \ - sudo apt-get update && \ - sudo apt-get install -y docker-ce docker-ce-cli containerd.io - ~~~ - - If you get an error, run one command at a time or follow the [official Docker instructions](https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-using-the-repository). - -3. Install HAProxy: - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo apt-get update - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo apt-get install haproxy - ~~~ - -4. Download the [CockroachDB archive](https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz) for Linux, extract the binary, and copy it into the `PATH`: - - {% include copy-clipboard.html %} - ~~~ shell - $ curl https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz \ - | tar -xz - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo cp -i cockroach-{{ page.release_info.version }}.linux-amd64/cockroach /usr/local/bin/ - ~~~ - - The `cockroach` binary needs to be on these VMs so you can run some client commands built into the binary, such as the command in the next step and the command for starting the built-in SQL shell. - -5. Run the [`cockroach gen haproxy`](cockroach-gen.html) command to generate an HAProxy config file, specifying the address of any CockroachDB node and the `--locality` of nodes in the US East region: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach gen haproxy \ - --insecure \ - --host=
      \ - --locality=region=us-east1 - ~~~ - - The generated configuration file is called `haproxy.cfg` and looks as follows, with the `server` addresses pre-populated with just the nodes in US East based on the `--locality` flag used: - - ~~~ - global - maxconn 4096 - - defaults - mode tcp - # Timeout values should be configured for your specific use. - # See: https://cbonte.github.io/haproxy-dconv/1.8/configuration.html#4-timeout%20connect - timeout connect 10s - timeout client 1m - timeout server 1m - # TCP keep-alive on client side. Server already enables them. - option clitcpka - - listen psql - bind :26257 - mode tcp - balance roundrobin - option httpchk GET /health?ready=1 - server cockroach1 :26257 check port 8080 - server cockroach2 :26257 check port 8080 - server cockroach3 :26257 check port 8080 - ~~~ - -6. Start HAProxy, with the `-f` flag pointing to the `haproxy.cfg` file: - - {% include copy-clipboard.html %} - ~~~ shell - $ haproxy -f haproxy.cfg & - ~~~ - -7. Repeat the steps above for the client VMs in the other two regions. For each region, be sure to adjust the `--locality` flag when running the `cockroach gen haproxy` command. - -### Configure the cluster for MovR - -Before you can run MovR against the cluster, you must create a `movr` database and enable an enterprise license. - -1. SSH to the client VM in the US East region. - -2. Use the [`cockroach sql`](cockroach-sql.html) command to start the built-in SQL shell, specifying the address of the HAProxy load balancer in the region: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure --host=
      - ~~~ - -3. In the SQL shell, create the `movr` database: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE DATABASE movr; - ~~~ - -4. Enable the trial license you requested earlier: - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING cluster.organization = ''; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SET CLUSTER SETTING enterprise.license = ''; - ~~~ - -5. Set the longitude and latitude of the regions where you are running CockroachDB nodes: - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT into system.locations VALUES - ('region', 'us-east1', 33.836082, -81.163727), - ('region', 'us-central1', 42.032974, -93.581543), - ('region', 'us-west1', 43.804133, -120.554201); - ~~~ - - Inserting these coordinates enables you to visualize your cluster on the [**Node Map**](enable-node-map.html) feature of the Admin UI. - -6. Exit the SQL shell: - - {% include copy-clipboard.html %} - ~~~ sql - \q - ~~~ - -### Start MovR in US East - -{{site.data.alerts.callout_info}} -Be sure to use the exact version of MovR specified in the commands: `movr:19.09.2`. This tutorial relies on the SQL schema in this specific version. -{{site.data.alerts.end}} - -1. Still on the client VM in the US East region, load the MovR schema and initial data for the cities of New York, Chicago, and Seattle, pointing at the address of the US East load balancer: - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo docker run -it --rm cockroachdb/movr:19.09.2 \ - --app-name "movr-load" \ - --url "postgres://root@
      :26257/movr?sslmode=disable" \ - load \ - --num-users 100 \ - --num-rides 100 \ - --num-vehicles 10 \ - --city-pair us_east:"new york" \ - --city-pair central:chicago \ - --city-pair us_west:seattle - ~~~ - - After the Docker image downloads, you'll see data being generated for the specified cities: - - ~~~ - ... - [INFO] (MainThread) initializing tables - [INFO] (MainThread) loading cities ['new york', 'chicago', 'seattle'] - [INFO] (MainThread) loading movr data with ~100 users, ~10 vehicles, and ~100 rides - [INFO] (MainThread) Only using 3 of 5 requested threads, since we only create at most one thread per city - [INFO] (Thread-1 ) Generating user data for new york... - [INFO] (Thread-2 ) Generating user data for chicago... - [INFO] (Thread-3 ) Generating user data for seattle... - [INFO] (Thread-2 ) Generating vehicle data for chicago... - [INFO] (Thread-3 ) Generating vehicle data for seattle... - [INFO] (Thread-1 ) Generating vehicle data for new york... - [INFO] (Thread-2 ) Generating ride data for chicago... - [INFO] (Thread-3 ) Generating ride data for seattle... - [INFO] (Thread-1 ) Generating ride data for new york... - [INFO] (Thread-2 ) populated chicago in 9.173931 seconds - [INFO] (Thread-3 ) populated seattle in 9.257723 seconds - [INFO] (Thread-1 ) populated new york in 9.386243 seconds - [INFO] (MainThread) populated 3 cities in 20.587325 seconds - [INFO] (MainThread) - 4.954505 users/second - [INFO] (MainThread) - 4.954505 rides/second - [INFO] (MainThread) - 0.582883 vehicles/second - ~~~ - -2. Start MovR in the US East region, representing users in New York. Be sure to point at the address of the US East load balancer: - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo docker run -it --rm cockroachdb/movr:19.09.2 \ - --app-name "movr-east" \ - --url "postgres://root@
      :26257/movr?sslmode=disable" \ - --num-threads=15 \ - run \ - --city="new york" - ~~~ - -### Start MovR in US Central - -1. SSH to the client VM in the US Central region. - -2. Start MovR in the US Central region, representing users in Chicago. Be sure to point at the address of the US Central load balancer: - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo docker run -it --rm cockroachdb/movr:19.09.2 \ - --app-name "movr-central" \ - --url "postgres://root@
      :26257/movr?sslmode=disable" \ - --num-threads=15 \ - run \ - --city="chicago" - ~~~ - -### Start MovR in US West - -1. SSH to the client VM in the US West region. - -2. Start MovR in the US West region, representing users in Seattle. Be sure to point at the address of the US West load balancer: - - {% include copy-clipboard.html %} - ~~~ shell - $ sudo docker run -it --rm cockroachdb/movr:19.09.2 \ - --app-name "movr-west" \ - --url "postgres://root@
      :26257/movr?sslmode=disable" \ - --num-threads=15 \ - run \ - --city="seattle" - ~~~ - -## Step 4. Access the Admin UI - -Now that you've deployed and configured your cluster, take a look at it in the Admin UI: - -1. Open a browser and go to `http://:8080`. - -2. On the **Cluster Overview** page, select **View: Node Map** to access the [Node Map](enable-node-map.html), which visualizes your CockroachDB cluster on a map of the US: - - Geo-partitioning node map - -3. Drill down one level to see your nodes across 3 regions: - - Geo-partitioning node map - -4. Drill into a region to see that each node is in a distinct availability zone: - - Geo-partitioning node map - -## Step 5. Check latency - -Use the Admin UI to see the effect of network latency before applying multi-region data topologies. - -1. Still in the Admin UI, click **Metrics** on the left and hover over the **Service Latency: SQL, 99th percentile** timeseries graph: - - Geo-partitioning SQL latency - - For each node, you'll see that the max latency of 99% of queries is in the 100s of milliseconds. To understand why SQL latency is so high, it's important to first look at how long it takes requests to physically travel between the nodes in your cluster. - -2. Click the gear icon in the lower left of the Admin UI and then click **Network Latency**: - - Geo-partitioning network latency - - The **Network Diagnostics** page shows the round-trip latency between any two nodes in your cluster. Here's a node/region mapping: - - Nodes | Region - ------|------- - 1 - 3 | `us-east1` - 4 - 6 | `us-central1` - 7 - 9 | `us-west1` - - As you can see, within a single region, round-trip latency is sub-millisecond. For example, between nodes 5 and 6 in the `us-central1` region, round-trip latency is 0.56ms. However, between nodes in different regions, round-trip latency is significantly higher. For example, between node 2 in `us-east1` and node 7 in `us-west`, round-trip latency is 66.43ms. - -## Step 6. Check replica distribution - -With network latency in mind, now use the built-in SQL shell to check the distribution of replicas. This will help us understand how SQL queries are moving between the nodes of the cluster and, thus, incurring latency. - -1. SSH to the client VM in any region. - -2. Use the [`cockroach sql`](cockroach-sql.html) command to start the built-in SQL shell, specifying the address of the HAProxy load balancer in the region: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure --database=movr --host=
      - ~~~ - -3. In the SQL shell, use the [`SHOW RANGES`](show-ranges.html) statement to view the location of replicas for the tables and their secondary indexes: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW RANGES FROM TABLE users; - SHOW RANGES FROM TABLE vehicles; - SHOW RANGES FROM INDEX vehicles_auto_index_fk_city_ref_users; - SHOW RANGES FROM TABLE rides; - SHOW RANGES FROM INDEX rides_auto_index_fk_city_ref_users; - SHOW RANGES FROM INDEX rides_auto_index_fk_vehicle_city_ref_vehicles; - SHOW RANGES FROM TABLE user_promo_codes; - SHOW RANGES FROM TABLE vehicle_location_histories; - ~~~ - - ~~~ - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 85 | 0.158604 | 1 | cloud=gce,region=us-east1,zone=us-east1-b | {1,6,8} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-b"} - (1 row) - - Time: 750.045316ms - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 37 | 0.298143 | 3 | cloud=gce,region=us-east1,zone=us-east1-d | {3,6,7} | {"cloud=gce,region=us-east1,zone=us-east1-d","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-a"} - (1 row) - - Time: 473.718371ms - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 37 | 0.302683 | 3 | cloud=gce,region=us-east1,zone=us-east1-d | {3,6,7} | {"cloud=gce,region=us-east1,zone=us-east1-d","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-a"} - (1 row) - - Time: 2.556900719s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 39 | 0.563349 | 7 | cloud=gce,region=us-west1,zone=us-west1-a | {3,6,7} | {"cloud=gce,region=us-east1,zone=us-east1-d","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-a"} - (1 row) - - Time: 673.337559ms - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 39 | 0.571556 | 7 | cloud=gce,region=us-west1,zone=us-west1-a | {3,6,7} | {"cloud=gce,region=us-east1,zone=us-east1-d","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-a"} - (1 row) - - Time: 3.184113514s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 39 | 0.57792 | 7 | cloud=gce,region=us-west1,zone=us-west1-a | {3,6,7} | {"cloud=gce,region=us-east1,zone=us-east1-d","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-a"} - (1 row) - - Time: 2.812128768s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 38 | 0.054887 | 9 | cloud=gce,region=us-west1,zone=us-west1-c | {2,6,9} | {"cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-c"} - (1 row) - - Time: 896.010317ms - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 86 | 2.197216 | 6 | cloud=gce,region=us-central1,zone=us-central1-c | {2,6,7} | {"cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-a"} - (1 row) - - Time: 708.643369ms - ~~~ - - Here's the node/region mapping again: - - Nodes | Region - ------|------- - 1 - 3 | `us-east1` - 4 - 6 | `us-central1` - 7 - 9 | `us-west1` - - You'll see that most tables and indexes map to a single range, and each range has 3 replicas spread across regions with one replica identified as `lease_holder`. - - Thinking back to [how reads and writes work in CockroachDB](architecture/reads-and-writes-overview.html), this tells you that many reads are leaving their region to reach the relevant leaseholder replica, and all writes are spanning regions to achieve Raft consensus. This explains the currently high latencies. - - For example, based on the output above, the replicas for the `users` table are on nodes 1, 6, and 8, with the leaseholder on node 1. This means that when a user in Seattle registers for the MovR service: - - 1. A request to write a row to the `users` table goes through the load balancer in US west to a gateway node in US west. - 2. The request is routed to the leaseholder on node 1 in US east. - 3. The leaseholder waits for consensus from a replica in US central or US west. - 4. The leaseholder returns acknowledgement to the gateway node in the US west. - 5. The gateway node responds to the client. - -## Step 7. Apply data topologies - -- [Partition geo-specific tables](#partition-geo-specific-tables) -- [Duplicate the reference table](#duplicate-the-reference-table) - -### Partition geo-specific tables - -As mentioned earlier, all of the tables except `promo_codes` are geographically specific, ordered by `city`, and read and updated very frequently. For these tables, the most effective way to prevent the high latency resulting from cross-region operations is to apply the [Geo-Partitioned Replicas](topology-geo-partitioned-replicas.html) data topology. In practice, you will tell CockroachDB to partition these tables and their secondary indexes by `city`, each partition becoming its own range of 3 replicas. You will then tell CockroachDB to pin each partition (all of its replicas) to the relevant region. This means that reads and writes on these tables will always have access to the relevant replicas in each region and, therefore, will have low, intra-region latencies. - -1. Back in the SQL shell on one of your client VMs, use [`ALTER TABLE/INDEX ... PARTITION BY`](partition-by.html) statements to define partitions by `city` for the geo-specific tables and their secondary indexes: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE users PARTITION BY LIST (city) ( - PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle') - ); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE vehicles PARTITION BY LIST (city) ( - PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle') - ); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER INDEX vehicles_auto_index_fk_city_ref_users PARTITION BY LIST (city) ( - PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle') - ); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE rides PARTITION BY LIST (city) ( - PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle') - ); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER INDEX rides_auto_index_fk_city_ref_users PARTITION BY LIST (city) ( - PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle') - ); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER INDEX rides_auto_index_fk_vehicle_city_ref_vehicles PARTITION BY LIST (vehicle_city) ( - PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle') - ); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE user_promo_codes PARTITION BY LIST (city) ( - PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle') - ); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE vehicle_location_histories PARTITION BY LIST (city) ( - PARTITION new_york VALUES IN ('new york'), - PARTITION chicago VALUES IN ('chicago'), - PARTITION seattle VALUES IN ('seattle') - ); - ~~~ - -2. Use the [`SHOW CREATE TABLE`](show-create.html) statement to review the partition definition for one of the geo-specific tables: - - {{site.data.alerts.callout_success}} - New in v19.2: The warning at the bottom tells you that partitions are not yet applied because corresponding replication zones still need to be created. - {{site.data.alerts.end}} - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW CREATE TABLE vehicles; - ~~~ - - ~~~ - table_name | create_statement - +------------+-----------------------------------------------------------------------------------------------------+ - vehicles | CREATE TABLE vehicles ( - | id UUID NOT NULL, - | city VARCHAR NOT NULL, - | type VARCHAR NULL, - | owner_id UUID NULL, - | creation_time TIMESTAMP NULL, - | status VARCHAR NULL, - | current_location VARCHAR NULL, - | ext JSONB NULL, - | CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - | CONSTRAINT fk_city_ref_users FOREIGN KEY (city, owner_id) REFERENCES users(city, id), - | INDEX vehicles_auto_index_fk_city_ref_users (city ASC, owner_id ASC) PARTITION BY LIST (city) ( - | PARTITION new_york VALUES IN (('new york')), - | PARTITION chicago VALUES IN (('chicago')), - | PARTITION seattle VALUES IN (('seattle')) - | ), - | FAMILY "primary" (id, city, type, owner_id, creation_time, status, current_location, ext) - | ) PARTITION BY LIST (city) ( - | PARTITION new_york VALUES IN (('new york')), - | PARTITION chicago VALUES IN (('chicago')), - | PARTITION seattle VALUES IN (('seattle')) - | ) - | -- Warning: Partitioned table with no zone configurations. - (1 row) - ~~~ - -3. Use [`ALTER PARTITION ... CONFIGURE ZONE`](configure-zone.html) statements to create replication zones that pin each partition to nodes in the relevant region, using the localities specified when nodes were started: - - {{site.data.alerts.callout_success}} - New in v19.2: The `
      @*` syntax lets you create zone configurations for all identically named partitions of a table, saving you multiple steps. - {{site.data.alerts.end}} - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER PARTITION new_york OF INDEX movr.users@* - CONFIGURE ZONE USING constraints='[+region=us-east1]'; - ALTER PARTITION chicago OF INDEX movr.users@* - CONFIGURE ZONE USING constraints='[+region=us-central1]'; - ALTER PARTITION seattle OF INDEX movr.users@* - CONFIGURE ZONE USING constraints='[+region=us-west1]'; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER PARTITION new_york OF INDEX movr.vehicles@* - CONFIGURE ZONE USING constraints='[+region=us-east1]'; - ALTER PARTITION chicago OF INDEX movr.vehicles@* - CONFIGURE ZONE USING constraints='[+region=us-central1]'; - ALTER PARTITION seattle OF INDEX movr.vehicles@* - CONFIGURE ZONE USING constraints='[+region=us-west1]'; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER PARTITION new_york OF INDEX movr.rides@* - CONFIGURE ZONE USING constraints='[+region=us-east1]'; - ALTER PARTITION chicago OF INDEX movr.rides@* - CONFIGURE ZONE USING constraints='[+region=us-central1]'; - ALTER PARTITION seattle OF INDEX movr.rides@* - CONFIGURE ZONE USING constraints='[+region=us-west1]'; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER PARTITION new_york OF INDEX movr.user_promo_codes@* - CONFIGURE ZONE USING constraints='[+region=us-east1]'; - ALTER PARTITION chicago OF INDEX movr.user_promo_codes@* - CONFIGURE ZONE USING constraints='[+region=us-central1]'; - ALTER PARTITION seattle OF INDEX movr.user_promo_codes@* - CONFIGURE ZONE USING constraints='[+region=us-west1]'; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER PARTITION new_york OF INDEX movr.vehicle_location_histories@* - CONFIGURE ZONE USING constraints='[+region=us-east1]'; - ALTER PARTITION chicago OF INDEX movr.vehicle_location_histories@* - CONFIGURE ZONE USING constraints='[+region=us-central1]'; - ALTER PARTITION seattle OF INDEX movr.vehicle_location_histories@* - CONFIGURE ZONE USING constraints='[+region=us-west1]'; - ~~~ - -3. At this point, you can use the [`SHOW CREATE TABLE`](show-create.html) statement to confirm that partitions are in effect: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW CREATE TABLE vehicles; - ~~~ - - ~~~ - table_name | create_statement - +------------+-------------------------------------------------------------------------------------------------------------------+ - vehicles | CREATE TABLE vehicles ( - | id UUID NOT NULL, - | city VARCHAR NOT NULL, - | type VARCHAR NULL, - | owner_id UUID NULL, - | creation_time TIMESTAMP NULL, - | status VARCHAR NULL, - | current_location VARCHAR NULL, - | ext JSONB NULL, - | CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - | CONSTRAINT fk_city_ref_users FOREIGN KEY (city, owner_id) REFERENCES users(city, id), - | INDEX vehicles_auto_index_fk_city_ref_users (city ASC, owner_id ASC) PARTITION BY LIST (city) ( - | PARTITION new_york VALUES IN (('new york')), - | PARTITION chicago VALUES IN (('chicago')), - | PARTITION seattle VALUES IN (('seattle')) - | ), - | FAMILY "primary" (id, city, type, owner_id, creation_time, status, current_location, ext) - | ) PARTITION BY LIST (city) ( - | PARTITION new_york VALUES IN (('new york')), - | PARTITION chicago VALUES IN (('chicago')), - | PARTITION seattle VALUES IN (('seattle')) - | ); - | ALTER PARTITION chicago OF INDEX movr.public.vehicles@primary CONFIGURE ZONE USING - | constraints = '[+region=us-central1]'; - | ALTER PARTITION new_york OF INDEX movr.public.vehicles@primary CONFIGURE ZONE USING - | constraints = '[+region=us-east1]'; - | ALTER PARTITION seattle OF INDEX movr.public.vehicles@primary CONFIGURE ZONE USING - | constraints = '[+region=us-west1]'; - | ALTER PARTITION chicago OF INDEX movr.public.vehicles@vehicles_auto_index_fk_city_ref_users CONFIGURE ZONE USING - | constraints = '[+region=us-central1]'; - | ALTER PARTITION new_york OF INDEX movr.public.vehicles@vehicles_auto_index_fk_city_ref_users CONFIGURE ZONE USING - | constraints = '[+region=us-east1]'; - | ALTER PARTITION seattle OF INDEX movr.public.vehicles@vehicles_auto_index_fk_city_ref_users CONFIGURE ZONE USING - | constraints = '[+region=us-west1]' - (1 row) - ~~~ - - In contrast to the last time you ran this statement, you can now see the commands for re-creating the replication zone for each partition of the `vehicles` table and its secondary index. - - New in v19.2: The [`SHOW PARTITIONS`](show-partitions.html) statement is another way to confirm that partitions are in effect: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW PARTITIONS FROM TABLE vehicles; - ~~~ - - ~~~ - database_name | table_name | partition_name | parent_partition | column_names | index_name | partition_value | zone_config | full_zone_config - +---------------+------------+----------------+------------------+--------------+------------------------------------------------+-----------------+---------------------------------------+----------------------------------------+ - movr | vehicles | new_york | NULL | city | vehicles@primary | ('new york') | constraints = '[+region=us-east1]' | range_min_bytes = 16777216, - | | | | | | | | range_max_bytes = 67108864, - | | | | | | | | gc.ttlseconds = 90000, - | | | | | | | | num_replicas = 3, - | | | | | | | | constraints = '[+region=us-east1]', - | | | | | | | | lease_preferences = '[]' - movr | vehicles | new_york | NULL | city | vehicles@vehicles_auto_index_fk_city_ref_users | ('new york') | constraints = '[+region=us-east1]' | range_min_bytes = 16777216, - | | | | | | | | range_max_bytes = 67108864, - | | | | | | | | gc.ttlseconds = 90000, - | | | | | | | | num_replicas = 3, - | | | | | | | | constraints = '[+region=us-east1]', - | | | | | | | | lease_preferences = '[]' - movr | vehicles | chicago | NULL | city | vehicles@primary | ('chicago') | constraints = '[+region=us-central1]' | range_min_bytes = 16777216, - | | | | | | | | range_max_bytes = 67108864, - | | | | | | | | gc.ttlseconds = 90000, - | | | | | | | | num_replicas = 3, - | | | | | | | | constraints = '[+region=us-central1]', - | | | | | | | | lease_preferences = '[]' - movr | vehicles | chicago | NULL | city | vehicles@vehicles_auto_index_fk_city_ref_users | ('chicago') | constraints = '[+region=us-central1]' | range_min_bytes = 16777216, - | | | | | | | | range_max_bytes = 67108864, - | | | | | | | | gc.ttlseconds = 90000, - | | | | | | | | num_replicas = 3, - | | | | | | | | constraints = '[+region=us-central1]', - | | | | | | | | lease_preferences = '[]' - movr | vehicles | seattle | NULL | city | vehicles@primary | ('seattle') | constraints = '[+region=us-west1]' | range_min_bytes = 16777216, - | | | | | | | | range_max_bytes = 67108864, - | | | | | | | | gc.ttlseconds = 90000, - | | | | | | | | num_replicas = 3, - | | | | | | | | constraints = '[+region=us-west1]', - | | | | | | | | lease_preferences = '[]' - movr | vehicles | seattle | NULL | city | vehicles@vehicles_auto_index_fk_city_ref_users | ('seattle') | constraints = '[+region=us-west1]' | range_min_bytes = 16777216, - | | | | | | | | range_max_bytes = 67108864, - | | | | | | | | gc.ttlseconds = 90000, - | | | | | | | | num_replicas = 3, - | | | | | | | | constraints = '[+region=us-west1]', - | | | | | | | | lease_preferences = '[]' - (6 rows) - ~~~ - - {% include {{page.version.version}}/sql/crdb-internal-partitions.md %} - -### Duplicate the reference table - -In contrast to the other tables, the `promo_codes` table is not tied to geography, and its data is read frequently but rarely updated. This type of table is often referred to as a "reference table" or "lookup table". For this table, you'll keep read latency low by applying the [Duplicate Indexes](topology-duplicate-indexes.html) data topology. In practice, you will put the leaseholder for the table itself (also called the primary index) in one region, create two secondary indexes on the table, and tell CockroachDB to put the leaseholder for each secondary index in one of the other regions. CockroachDB's [cost-based optimizer](cost-based-optimizer.html) will then make sure that reads from `promo_codes` access the local leaseholder (either for the table itself or for one of the secondary indexes). Writes, however, will still leave the region to get consensus for the table and its secondary indexes, but writes are so rare that this will not impact overall performance. - -1. Create two indexes on the `promo_codes` table, and make them complete copies of the primary index: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE INDEX promo_codes_idx_east ON promo_codes (code) - STORING (description, creation_time, expiration_time, rules); - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE INDEX promo_codes_idx_west ON promo_codes (code) - STORING (description, creation_time, expiration_time, rules); - ~~~ - -2. Use [`ALTER TABLE/INDEX ... CONFIGURE ZONE`](configure-zone.html) statements to create replication zones for the primary index and each secondary index, in each case setting a leaseholder preference telling CockroachDB to put the leaseholder for the index in a distinct region: - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER TABLE promo_codes - CONFIGURE ZONE USING - num_replicas = 3, - constraints = '{"+region=us-central1": 1}', - lease_preferences = '[[+region=us-central1]]'; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER INDEX promo_codes@promo_codes_idx_east - CONFIGURE ZONE USING - num_replicas = 3, - constraints = '{"+region=us-east1": 1}', - lease_preferences = '[[+region=us-east1]]'; - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > ALTER INDEX promo_codes@promo_codes_idx_west - CONFIGURE ZONE USING - num_replicas = 3, - constraints = '{"+region=us-west1": 1}', - lease_preferences = '[[+region=us-west1]]'; - ~~~ - -## Step 8. Re-check replica distribution - -1. Still in the SQL shell on one of your client VMs, use the [`SHOW RANGES`](show-ranges.html) statement to check replica placement of the geo-specific tables after partitioning: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT * FROM [SHOW RANGES FROM TABLE users] - WHERE "start_key" NOT LIKE '%Prefix%'; - SELECT * FROM [SHOW RANGES FROM TABLE vehicles] - WHERE "start_key" NOT LIKE '%Prefix%'; - SELECT * FROM [SHOW RANGES FROM INDEX vehicles_auto_index_fk_city_ref_users] - WHERE "start_key" NOT LIKE '%Prefix%'; - SELECT * FROM [SHOW RANGES FROM TABLE rides] - WHERE "start_key" NOT LIKE '%Prefix%'; - SELECT * FROM [SHOW RANGES FROM INDEX rides_auto_index_fk_city_ref_users] - WHERE "start_key" NOT LIKE '%Prefix%'; - SELECT * FROM [SHOW RANGES FROM INDEX rides_auto_index_fk_vehicle_city_ref_vehicles] - WHERE "start_key" NOT LIKE '%Prefix%'; - SELECT * FROM [SHOW RANGES FROM TABLE user_promo_codes] - WHERE "start_key" NOT LIKE '%Prefix%'; - SELECT * FROM [SHOW RANGES FROM TABLE vehicle_location_histories] - WHERE "start_key" NOT LIKE '%Prefix%'; - ~~~ - - ~~~ - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-------------+-----------------------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ - /"new york" | /"new york"/PrefixEnd | 105 | 0.933453 | 3 | cloud=gce,region=us-east1,zone=us-east1-d | {1,2,3} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-east1,zone=us-east1-d"} - /"chicago" | /"chicago"/PrefixEnd | 107 | 0.860034 | 6 | cloud=gce,region=us-central1,zone=us-central1-c | {4,5,6} | {"cloud=gce,region=us-central1,zone=us-central1-a","cloud=gce,region=us-central1,zone=us-central1-b","cloud=gce,region=us-central1,zone=us-central1-c"} - /"seattle" | /"seattle"/PrefixEnd | 109 | 0.895921 | 7 | cloud=gce,region=us-west1,zone=us-west1-a | {7,8,9} | {"cloud=gce,region=us-west1,zone=us-west1-a","cloud=gce,region=us-west1,zone=us-west1-b","cloud=gce,region=us-west1,zone=us-west1-c"} - (3 rows) - - Time: 1.645458616s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-------------+-----------------------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ - /"new york" | /"new york"/PrefixEnd | 125 | 2.11175 | 2 | cloud=gce,region=us-east1,zone=us-east1-c | {1,2,3} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-east1,zone=us-east1-d"} - /"chicago" | /"chicago"/PrefixEnd | 129 | 1.9099 | 5 | cloud=gce,region=us-central1,zone=us-central1-b | {4,5,6} | {"cloud=gce,region=us-central1,zone=us-central1-a","cloud=gce,region=us-central1,zone=us-central1-b","cloud=gce,region=us-central1,zone=us-central1-c"} - /"seattle" | /"seattle"/PrefixEnd | 56 | 2.04172 | 9 | cloud=gce,region=us-west1,zone=us-west1-c | {7,8,9} | {"cloud=gce,region=us-west1,zone=us-west1-a","cloud=gce,region=us-west1,zone=us-west1-b","cloud=gce,region=us-west1,zone=us-west1-c"} - (3 rows) - - Time: 1.260863914s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-------------+-----------------------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ - /"new york" | /"new york"/PrefixEnd | 127 | 0.119543 | 1 | cloud=gce,region=us-east1,zone=us-east1-b | {1,2,3} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-east1,zone=us-east1-d"} - /"chicago" | /"chicago"/PrefixEnd | 130 | 0.106442 | 5 | cloud=gce,region=us-central1,zone=us-central1-b | {4,5,6} | {"cloud=gce,region=us-central1,zone=us-central1-a","cloud=gce,region=us-central1,zone=us-central1-b","cloud=gce,region=us-central1,zone=us-central1-c"} - /"seattle" | /"seattle"/PrefixEnd | 46 | 0.110188 | 9 | cloud=gce,region=us-west1,zone=us-west1-c | {7,8,9} | {"cloud=gce,region=us-west1,zone=us-west1-a","cloud=gce,region=us-west1,zone=us-west1-b","cloud=gce,region=us-west1,zone=us-west1-c"} - (3 rows) - - Time: 3.392228893s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-------------+-----------------------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ - /"new york" | /"new york"/PrefixEnd | 76 | 2.498621 | 2 | cloud=gce,region=us-east1,zone=us-east1-c | {1,2,3} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-east1,zone=us-east1-d"} - /"chicago" | /"chicago"/PrefixEnd | 83 | 2.243434 | 5 | cloud=gce,region=us-central1,zone=us-central1-b | {4,5,6} | {"cloud=gce,region=us-central1,zone=us-central1-a","cloud=gce,region=us-central1,zone=us-central1-b","cloud=gce,region=us-central1,zone=us-central1-c"} - /"seattle" | /"seattle"/PrefixEnd | 148 | 2.39411 | 7 | cloud=gce,region=us-west1,zone=us-west1-a | {7,8,9} | {"cloud=gce,region=us-west1,zone=us-west1-a","cloud=gce,region=us-west1,zone=us-west1-b","cloud=gce,region=us-west1,zone=us-west1-c"} - (3 rows) - - Time: 1.294584902s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-------------+-----------------------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ - /"new york" | /"new york"/PrefixEnd | 78 | 0.533722 | 1 | cloud=gce,region=us-east1,zone=us-east1-b | {1,2,3} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-east1,zone=us-east1-d"} - /"chicago" | /"chicago"/PrefixEnd | 82 | 0.477912 | 4 | cloud=gce,region=us-central1,zone=us-central1-a | {4,5,6} | {"cloud=gce,region=us-central1,zone=us-central1-a","cloud=gce,region=us-central1,zone=us-central1-b","cloud=gce,region=us-central1,zone=us-central1-c"} - /"seattle" | /"seattle"/PrefixEnd | 149 | 0.505345 | 7 | cloud=gce,region=us-west1,zone=us-west1-a | {7,8,9} | {"cloud=gce,region=us-west1,zone=us-west1-a","cloud=gce,region=us-west1,zone=us-west1-b","cloud=gce,region=us-west1,zone=us-west1-c"} - (3 rows) - - Time: 3.346661477s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-------------+-----------------------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ - /"new york" | /"new york"/PrefixEnd | 80 | 0.61871 | 2 | cloud=gce,region=us-east1,zone=us-east1-c | {1,2,3} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-east1,zone=us-east1-d"} - /"chicago" | /"chicago"/PrefixEnd | 84 | 0.547892 | 6 | cloud=gce,region=us-central1,zone=us-central1-c | {4,5,6} | {"cloud=gce,region=us-central1,zone=us-central1-a","cloud=gce,region=us-central1,zone=us-central1-b","cloud=gce,region=us-central1,zone=us-central1-c"} - /"seattle" | /"seattle"/PrefixEnd | 150 | 0.579083 | 7 | cloud=gce,region=us-west1,zone=us-west1-a | {7,8,9} | {"cloud=gce,region=us-west1,zone=us-west1-a","cloud=gce,region=us-west1,zone=us-west1-b","cloud=gce,region=us-west1,zone=us-west1-c"} - (3 rows) - - Time: 3.341758512s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-------------+-----------------------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ - /"new york" | /"new york"/PrefixEnd | 133 | 0.365143 | 1 | cloud=gce,region=us-east1,zone=us-east1-b | {1,2,3} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-east1,zone=us-east1-d"} - /"chicago" | /"chicago"/PrefixEnd | 135 | 0.313355 | 6 | cloud=gce,region=us-central1,zone=us-central1-c | {4,5,6} | {"cloud=gce,region=us-central1,zone=us-central1-a","cloud=gce,region=us-central1,zone=us-central1-b","cloud=gce,region=us-central1,zone=us-central1-c"} - /"seattle" | /"seattle"/PrefixEnd | 137 | 0.343468 | 9 | cloud=gce,region=us-west1,zone=us-west1-c | {7,8,9} | {"cloud=gce,region=us-west1,zone=us-west1-a","cloud=gce,region=us-west1,zone=us-west1-b","cloud=gce,region=us-west1,zone=us-west1-c"} - (3 rows) - - Time: 1.105110359s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-------------+-----------------------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ - /"new york" | /"new york"/PrefixEnd | 112 | 20.852122 | 3 | cloud=gce,region=us-east1,zone=us-east1-d | {1,2,3} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-east1,zone=us-east1-c","cloud=gce,region=us-east1,zone=us-east1-d"} - /"chicago" | /"chicago"/PrefixEnd | 114 | 17.631255 | 4 | cloud=gce,region=us-central1,zone=us-central1-a | {4,5,6} | {"cloud=gce,region=us-central1,zone=us-central1-a","cloud=gce,region=us-central1,zone=us-central1-b","cloud=gce,region=us-central1,zone=us-central1-c"} - /"seattle" | /"seattle"/PrefixEnd | 116 | 19.677135 | 8 | cloud=gce,region=us-west1,zone=us-west1-b | {7,8,9} | {"cloud=gce,region=us-west1,zone=us-west1-a","cloud=gce,region=us-west1,zone=us-west1-b","cloud=gce,region=us-west1,zone=us-west1-c"} - (3 rows) - - Time: 1.612425537s - ~~~ - - You'll see that the replicas for each partition are now located on nodes in the relevant region: - - New York partitions are on nodes 1 - 3 - - Chicago partitions are on nodes 4 - 6 - - Seattle partitions are on nodes 7 - 9 - - This means that requests from users in a city no longer leave the region, thus removing all cross-region latencies. - -2. Now use the [`SHOW RANGES`](show-ranges.html) statement to check replica placement of the `promo_codes` reference table and indexes: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW RANGES FROM TABLE promo_codes; - SHOW RANGES FROM INDEX promo_codes_idx_east; - SHOW RANGES FROM INDEX promo_codes_idx_west; - ~~~ - - ~~~ - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 87 | 0.835044 | 6 | cloud=gce,region=us-central1,zone=us-central1-c | {1,6,9} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-c"} - (1 row) - - Time: 517.443988ms - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 89 | 0.83622 | 1 | cloud=gce,region=us-east1,zone=us-east1-b | {1,6,9} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-c"} - (1 row) - - Time: 2.449771429s - - start_key | end_key | range_id | range_size_mb | lease_holder | lease_holder_locality | replicas | replica_localities - +-----------+---------+----------+---------------+--------------+-------------------------------------------+----------+---------------------------------------------------------------------------------------------------------------------------------------------+ - NULL | NULL | 90 | 0.836372 | 9 | cloud=gce,region=us-west1,zone=us-west1-c | {1,6,9} | {"cloud=gce,region=us-east1,zone=us-east1-b","cloud=gce,region=us-central1,zone=us-central1-c","cloud=gce,region=us-west1,zone=us-west1-c"} - (1 row) - - Time: 2.621930607s - ~~~ - - You'll see that the replicas for each index are spread across regions, with the leaseholders each in a distinct region: - - The leaseholder for the `promo_codes` primary index is on node 6 in US Central - - The leaseholder for the `promo_codes_idx_east` secondary index is on node 1 in US East - - The leaseholder for the `promo_codes_idx_west` secondary index is on node 9 in US West - - As you'll see in a just a bit, with one leaseholder in each region, CockroachDB's cost-based optimizer will make sure that reads always access the local leaseholder, keeping reads from this table very fast. - -## Step 9. Re-check latency - -1. Now that you've verified that replicas are located properly, go back to the Admin UI, click **Metrics** on the left, and hover over the **Service Latency: SQL, 99th percentile** timeseries graph: - - Geo-partitioning SQL latency - - For each node, you'll see that **99% of all queries are now under 4 milliseconds**. - -2. 99th percentile latency can be influenced by occasional slow queries. For a more accurate sense of typical SQL latency, go to the following URL to view a custom graph for 90th percentile latency: - - ~~~ - http://:8080/#/debug/chart?charts=%5B%7B%22metrics%22%3A%5B%7B%22downsampler%22%3A3%2C%22aggregator%22%3A3%2C%22derivative%22%3A0%2C%22perNode%22%3Atrue%2C%22source%22%3A%22%22%2C%22metric%22%3A%22cr.node.sql.exec.latency-p90%22%7D%5D%2C%22axisUnits%22%3A2%7D%5D - ~~~ - - Geo-partitioning SQL latency - - As you can see, **90% of all SQL queries execute in less than 2 milliseconds**. In some cases, latency is even sub-millisecond. - -3. Most of the latency reduction is due to the geo-partitioned tables. However, the duplicate indexes approach for the `promo_codes` table is also relevant. To validate that the cost-based optimizer is picking the appropriate leaseholder from reads from `promo_codes` in each region, click **Statements** on the left, select **APP > MOVR-EAST**, and then click the `SELECT FROM promo_codes` statement: - - Geo-partitioning SQL latency - - In the "Logical Plan" area, note the `table = promo_codes@promo_codes_idx_east` scan. This proves that the cost-based optimizer used the leaseholder for that index and, thus, didn't leave the region for the instance of MovR running in US East. - - To validate this behavior in the other regions, click **Statements** again on the left and follow the same steps for the other apps instances. - -## Step 10. Test resiliency - -There are various resiliency levels in your cluster: - -- For the [geo-partitioned data](topology-geo-partitioned-replicas.html#resiliency), each partition is constrained to a specific region and balanced across the 3 AZs in the region, so one AZ can fail per region without interrupting access to the partitions in that region. -- For the [duplicated reference data](topology-duplicate-indexes.html#resiliency), replicas are balanced across regions, so one entire region can fail without interrupting access. - -Given that most of the data in your cluster is geo-partitioned, let's focus on AZ-level failure. - -1. SSH to the client VM in the US East region. - -2. Use the [`cockroach quit`](cockroach-quit.html) command to stop one node, effectively simulating one of the 3 AZ's failing: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=
      - ~~~ - -3. Back in the Admin UI, click **Overview** and note that the cluster now considers that node "suspect": - - Geo-partitioning resiliency - -4. Despite the node being unavailable, the MovR instance in US East continues to make progress because the other 2 nodes, each in a distinct AZ, remain available, and all partitions in the region therefore remain available. To verify this, click **Metrics**, select node 1 or 2 from the **Graph** menu, and check that SQL traffic continues on the node: - - Geo-partitioning resiliency - -## See also - -- Related Topology Patterns - - [Geo-Partitioned Replicas Topology](topology-geo-partitioned-replicas.html) - - [Duplicate Indexes Topology](topology-duplicate-indexes.html) - -- Related Case Studies - - [Electronic lock manufacturer](https://www.cockroachlabs.com/case-studies/european-electronic-lock-manufacturer-modernizes-iam-system-with-managed-cockroachdb/) - - [Multi-national bank](https://www.cockroachlabs.com/case-studies/top-five-multinational-bank-modernizes-its-european-core-banking-services-migrating-from-oracle-to-cockroachdb/) - -- [Reads and Writes in CockroachDB](architecture/reads-and-writes-overview.html) diff --git a/src/current/v19.2/demo-replication-and-rebalancing.md b/src/current/v19.2/demo-replication-and-rebalancing.md deleted file mode 100644 index aeb1c5592b3..00000000000 --- a/src/current/v19.2/demo-replication-and-rebalancing.md +++ /dev/null @@ -1,239 +0,0 @@ ---- -title: Replication and Rebalancing -summary: Use a local cluster to explore how CockroachDB replicates and rebalances data. -toc: true ---- - -This page walks you through a simple demonstration of how CockroachDB replicates, distributes, and rebalances data. Starting with a 3-node local cluster, you'll write some data and verify that it replicates in triplicate by default. You'll then add 2 more nodes and watch how CockroachDB automatically rebalances replicas to efficiently use all available capacity. - -## Before you begin - -Make sure you have already [installed CockroachDB](install-cockroachdb.html). - -## Step 1. Start a 3-node cluster - -1. Use the [`cockroach start`](cockroach-start.html) command to start 3 nodes: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=rep-node1 \ - --listen-addr=localhost:26257 \ - --http-addr=localhost:8080 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=rep-node2 \ - --listen-addr=localhost:26258 \ - --http-addr=localhost:8081 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --insecure \ - --store=rep-node3 \ - --listen-addr=localhost:26259 \ - --http-addr=localhost:8082 \ - --join=localhost:26257,localhost:26258,localhost:26259 \ - --background - ~~~ - -2. Use the [`cockroach init`](cockroach-init.html) command to perform a one-time initialization of the cluster: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach init \ - --insecure \ - --host=localhost:26257 - ~~~ - -## Step 2. Write data - -1. Use the [`cockroach workload`](cockroach-workload.html) command to generate an example `intro` database: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach workload init intro \ - 'postgresql://root@localhost:26257?sslmode=disable' - ~~~ - -2. Open the [built-in SQL shell](cockroach-sql.html) and verify that the new `intro` database was added with one table, `mytable`: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure --host=localhost:26257 - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW DATABASES; - ~~~ - - ~~~ - database_name - +---------------+ - defaultdb - intro - postgres - system - (4 rows) - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW TABLES FROM intro; - ~~~ - - ~~~ - table_name - +------------+ - mytable - (1 row) - ~~~ - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT * FROM intro.mytable WHERE (l % 2) = 0; - ~~~ - - ~~~ - l | v - +----+------------------------------------------------------+ - 0 | !__aaawwmqmqmwwwaas,,_ .__aaawwwmqmqmwwaaa,, - 2 | !"VT?!"""^~~^"""??T$Wmqaa,_auqmWBT?!"""^~~^^""??YV^ - 4 | ! "?##mW##?"- - 6 | ! C O N G R A T S _am#Z??A#ma, Y - 8 | ! _ummY" "9#ma, A - 10 | ! vm#Z( )Xmms Y - 12 | ! .j####mmm#####mm#m##6. - 14 | ! W O W ! jmm###mm######m#mmm##6 - 16 | ! ]#me*Xm#m#mm##m#m##SX##c - 18 | ! dm#||+*$##m#mm#m#Svvn##m - 20 | ! :mmE=|+||S##m##m#1nvnnX##; A - 22 | ! :m#h+|+++=Xmm#m#1nvnnvdmm; M - 24 | ! Y $#m>+|+|||##m#1nvnnnnmm# A - 26 | ! O ]##z+|+|+|3#mEnnnnvnd##f Z - 28 | ! U D 4##c|+|+|]m#kvnvnno##P E - 30 | ! I 4#ma+|++]mmhvnnvq##P` ! - 32 | ! D I ?$#q%+|dmmmvnnm##! - 34 | ! T -4##wu#mm#pw##7' - 36 | ! -?$##m####Y' - 38 | ! !! "Y##Y"- - 40 | ! - (21 rows) - ~~~ - -3. Exit the SQL shell: - - {% include copy-clipboard.html %} - ~~~ sql - > \q - ~~~ - -## Step 3. Verify replication - -1. To understand replication in CockroachDB, it's important to review a few concepts from the [architecture](architecture/overview.html): - - - Concept | Description - --------|------------ - **Range** | CockroachDB stores all user data (tables, indexes, etc.) and almost all system data in a giant sorted map of key-value pairs. This keyspace is divided into "ranges", contiguous chunks of the keyspace, so that every key can always be found in a single range.

      From a SQL perspective, a table and its secondary indexes initially map to a single range, where each key-value pair in the range represents a single row in the table (also called the primary index because the table is sorted by the primary key) or a single row in a secondary index. As soon as that range reaches 64 MiB in size, it splits into two ranges. This process continues for these new ranges as the table and its indexes continue growing. - **Replica** | CockroachDB replicates each range (3 times by default) and stores each replica on a different node. - -2. With those concepts in mind, open the Admin UI at http://localhost:8080 and view the **Node List**: - - CockroachDB Admin UI - - Note that the **Replicas** count is the same on all three nodes. This indicates: - - There are this many "ranges" of data in the cluster. These are mostly internal "system" ranges since you haven't added much table data. - - Each range has been replicated 3 times (according to the CockroachDB default). - - For each range, each replica is stored on different nodes. - -## Step 4. Add two more nodes - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---store=rep-node4 \ ---listen-addr=localhost:26260 \ ---http-addr=localhost:8083 \ ---join=localhost:26257 \ ---background -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start \ ---insecure \ ---store=rep-node5 \ ---listen-addr=localhost:26261 \ ---http-addr=localhost:8084 \ ---join=localhost:26257 \ ---background -~~~ - -## Step 5. Watch data rebalance - -Back in the Admin UI, you'll see that there are now 5 nodes listed: - -CockroachDB Admin UI - -At first, the replica count will be lower for nodes 4 and 5. Very soon, however, you'll see those numbers even out across all nodes, indicating that data is being automatically rebalanced to utilize the additional capacity of the new nodes. - -## Step 6. Stop the cluster - -1. When you're done with your test cluster, use the [`cockroach quit`](cockroach-quit.html) command to gracefully shut down each node. - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26257 - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26258 - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26259 - ~~~ - - {{site.data.alerts.callout_info}} - For the last 2 nodes, the shutdown process will take longer (about a minute each) and will eventually force the nodes to stop. This is because, with only 2 of 5 nodes left, a majority of replicas are not available, and so the cluster is no longer operational. - {{site.data.alerts.end}} - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26260 - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach quit --insecure --host=localhost:26261 - ~~~ - -2. To restart the cluster at a later time, run the same `cockroach start` commands as earlier from the directory containing the nodes' data stores. - - If you do not plan to restart the cluster, you may want to remove the nodes' data stores: - - {% include copy-clipboard.html %} - ~~~ shell - $ rm -rf rep-node1 rep-node2 rep-node3 rep-node4 rep-node5 - ~~~ - -## What's next? - -Explore other core CockroachDB benefits and features: - -{% include {{ page.version.version }}/misc/explore-benefits-see-also.md %} diff --git a/src/current/v19.2/demo-serializable.md b/src/current/v19.2/demo-serializable.md deleted file mode 100644 index 5da1d2bee87..00000000000 --- a/src/current/v19.2/demo-serializable.md +++ /dev/null @@ -1,562 +0,0 @@ ---- -title: Serializable Transactions -summary: -toc: true ---- - -In contrast to most databases, CockroachDB always uses `SERIALIZABLE` isolation, which is the strongest of the four [transaction isolation levels](https://en.wikipedia.org/wiki/Isolation_(database_systems)) defined by the SQL standard and is stronger than the `SNAPSHOT` isolation level developed later. `SERIALIZABLE` isolation guarantees that even though transactions may execute in parallel, the result is the same as if they had executed one at a time, without any concurrency. This ensures data correctness by preventing all "anomalies" allowed by weaker isolation levels. - -In this tutorial, you'll work through a hypothetical scenario that demonstrates the importance of `SERIALIZABLE` isolation for data correctness. - -1. You'll start by reviewing the scenario and its schema. -2. You'll then execute the scenario at one of the weaker isolation levels, `READ COMMITTED`, observing the write skew anomaly and its implications. Because CockroachDB always uses `SERIALIZABLE` isolation, you'll run this portion of the tutorial on Postgres, which defaults to `READ COMMITTED`. -3. You'll finish by executing the scenario at `SERIALIZABLE` isolation, observing how it guarantees correctness. You'll use CockroachDB for this portion. - -{{site.data.alerts.callout_info}} -For a deeper discussion of transaction isolation and the write skew anomaly, see the [Real Transactions are Serializable](https://www.cockroachlabs.com/blog/acid-rain/) and [What Write Skew Looks Like](https://www.cockroachlabs.com/blog/what-write-skew-looks-like/) blog posts. -{{site.data.alerts.end}} - -## Overview - -### Scenario - -- A hospital has an application for doctors to manage their on-call shifts. -- The hospital has a rule that at least one doctor must be on call at any one time. -- Two doctors are on-call for a particular shift, and both of them try to request leave for the shift at approximately the same time. -- In Postgres, with the default `READ COMMITTED` isolation level, the [write skew](#write-skew) anomaly results in both doctors successfully booking leave and the hospital having no doctors on call for that particular shift. -- In CockroachDB, with the `SERIALIZABLE` isolation level, write skew is prevented, one doctor is allowed to book leave and the other is left on-call, and lives are saved. - -#### Write skew - -When write skew happens, a transaction reads something, makes a decision based on the value it saw, and writes the decision to the database. However, by the time the write is made, the premise of the decision is no longer true. Only `SERIALIZABLE` and some implementations of `REPEATABLE READ` isolation prevent this anomaly. - -### Schema - -Schema for serializable transaction tutorial - -## Scenario on Postgres - -### Step 1. Start Postgres - -1. If you haven't already, install Postgres locally. On Mac, you can use [Homebrew](https://brew.sh/): - - {% include copy-clipboard.html %} - ~~~ shell - $ brew install postgres - ~~~ - -2. [Start Postgres](https://www.postgresql.org/docs/10/static/server-start.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ postgres -D /usr/local/var/postgres & - ~~~ - -### Step 2. Create the schema - -1. Open a SQL connection to Postgres: - - {% include copy-clipboard.html %} - ~~~ shell - $ psql - ~~~ - -2. Create the `doctors` table: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE doctors ( - id INT PRIMARY KEY, - name TEXT - ); - ~~~ - -3. Create the `schedules` table: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE schedules ( - day DATE, - doctor_id INT REFERENCES doctors (id), - on_call BOOL, - PRIMARY KEY (day, doctor_id) - ); - ~~~ - -### Step 3. Insert data - -1. Add two doctors to the `doctors` table: - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO doctors VALUES - (1, 'Abe'), - (2, 'Betty'); - ~~~ - -2. Insert one week's worth of data into the `schedules` table: - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO schedules VALUES - ('2018-10-01', 1, true), - ('2018-10-01', 2, true), - ('2018-10-02', 1, true), - ('2018-10-02', 2, true), - ('2018-10-03', 1, true), - ('2018-10-03', 2, true), - ('2018-10-04', 1, true), - ('2018-10-04', 2, true), - ('2018-10-05', 1, true), - ('2018-10-05', 2, true), - ('2018-10-06', 1, true), - ('2018-10-06', 2, true), - ('2018-10-07', 1, true), - ('2018-10-07', 2, true); - ~~~ - -3. Confirm that at least one doctor is on call each day of the week: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT day, count(*) AS doctors_on_call FROM schedules - WHERE on_call = true - GROUP BY day - ORDER BY day; - ~~~ - - ~~~ - day | doctors_on_call - ------------+----------------- - 2018-10-01 | 2 - 2018-10-02 | 2 - 2018-10-03 | 2 - 2018-10-04 | 2 - 2018-10-05 | 2 - 2018-10-06 | 2 - 2018-10-07 | 2 - (7 rows) - ~~~ - -### Step 4. Doctor 1 requests leave - -Doctor 1, Abe, starts to request leave for 10/5/18 using the hospital's schedule management application. - -1. The application starts a transaction: - - {% include copy-clipboard.html %} - ~~~ sql - > BEGIN; - ~~~ - -2. The application checks to make sure at least one other doctor is on call for the requested date: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT count(*) FROM schedules - WHERE on_call = true - AND day = '2018-10-05' - AND doctor_id != 1; - ~~~ - - ~~~ - count - ------- - 1 - (1 row) - ~~~ - -### Step 5. Doctor 2 requests leave - -Around the same time, doctor 2, Betty, starts to request leave for the same day using the hospital's schedule management application. - -1. In a new terminal, start a second SQL session: - - {% include copy-clipboard.html %} - ~~~ shell - $ psql - ~~~ - -2. The application starts a transaction: - - {% include copy-clipboard.html %} - ~~~ sql - > BEGIN; - ~~~ - -3. The application checks to make sure at least one other doctor is on call for the requested date: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT count(*) FROM schedules - WHERE on_call = true - AND day = '2018-10-05' - AND doctor_id != 2; - ~~~ - - ~~~ - count - ------- - 1 - (1 row) - ~~~ - -### Step 6. Leave is incorrectly booked for both doctors - -1. In the terminal for doctor 1, since the previous check confirmed that another doctor is on call for 10/5/18, the application tries to update doctor 1's schedule: - - {% include copy-clipboard.html %} - ~~~ sql - > UPDATE schedules SET on_call = false - WHERE day = '2018-10-05' - AND doctor_id = 1; - ~~~ - -2. In the terminal for doctor 2, since the previous check confirmed the same thing, the application tries to update doctor 2's schedule: - - {% include copy-clipboard.html %} - ~~~ sql - > UPDATE schedules SET on_call = false - WHERE day = '2018-10-05' - AND doctor_id = 2; - ~~~ - -3. In the terminal for doctor 1, the application commits the transaction, despite the fact that the previous check (the `SELECT` query) is no longer true: - - {% include copy-clipboard.html %} - ~~~ sql - > COMMIT; - ~~~ - -4. In the terminal for doctor 2, the application commits the transaction, despite the fact that the previous check (the `SELECT` query) is no longer true: - - {% include copy-clipboard.html %} - ~~~ sql - > COMMIT; - ~~~ - -### Step 7. Check data correctness - -So what just happened? Each transaction started by reading a value that, before the end of the transaction, became incorrect. Despite that fact, each transaction was allowed to commit. This is known as write skew, and the result is that 0 doctors are scheduled to be on call on 10/5/18. - -To check this, in either terminal, run: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM schedules WHERE day = '2018-10-05'; -~~~ - -~~~ - day | doctor_id | on_call -------------+-----------+--------- - 2018-10-05 | 1 | f - 2018-10-05 | 2 | f -(2 rows) -~~~ - -Again, this anomaly is the result of Postgres' default isolation level of `READ COMMITTED`, but note that this would happen with any isolation level except `SERIALIZABLE` and some implementations of `REPEATABLE READ`: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TRANSACTION_ISOLATION; -~~~ - -~~~ - transaction_isolation ------------------------ - read committed -(1 row) -~~~ - -### Step 8. Stop Postgres - -Exit each SQL shell with `\q` and then terminate the Postgres server: - -{% include copy-clipboard.html %} -~~~ shell -$ pkill -9 postgres -~~~ - -## Scenario on CockroachDB - -When you repeat the scenario on CockroachDB, you'll see that the anomaly is prevented by CockroachDB's `SERIALIZABLE` transaction isolation. - -### Step 1. Start CockroachDB - -1. If you haven't already, [install CockroachDB](install-cockroachdb.html) locally. - -2. Use the [`cockroach start-single-node`](cockroach-start-single-node.html) command to start a one-node CockroachDB cluster in insecure mode: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start-single-node \ - --insecure \ - --store=serializable-demo \ - --listen-addr=localhost \ - --background - ~~~ - -### Step 2. Create the schema - -1. As the `root` user, open the [built-in SQL client](cockroach-sql.html): - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure --host=localhost - ~~~ - -2. Create the `doctors` table: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE doctors ( - id INT PRIMARY KEY, - name TEXT - ); - ~~~ - -3. Create the `schedules` table: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE TABLE schedules ( - day DATE, - doctor_id INT REFERENCES doctors (id), - on_call BOOL, - PRIMARY KEY (day, doctor_id) - ); - ~~~ - -### Step 3. Insert data - -1. Add two doctors to the `doctors` table: - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO doctors VALUES - (1, 'Abe'), - (2, 'Betty'); - ~~~ - -2. Insert one week's worth of data into the `schedules` table: - - {% include copy-clipboard.html %} - ~~~ sql - > INSERT INTO schedules VALUES - ('2018-10-01', 1, true), - ('2018-10-01', 2, true), - ('2018-10-02', 1, true), - ('2018-10-02', 2, true), - ('2018-10-03', 1, true), - ('2018-10-03', 2, true), - ('2018-10-04', 1, true), - ('2018-10-04', 2, true), - ('2018-10-05', 1, true), - ('2018-10-05', 2, true), - ('2018-10-06', 1, true), - ('2018-10-06', 2, true), - ('2018-10-07', 1, true), - ('2018-10-07', 2, true); - ~~~ - -3. Confirm that at least one doctor is on call each day of the week: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT day, count(*) AS on_call FROM schedules - WHERE on_call = true - GROUP BY day - ORDER BY day; - ~~~ - - ~~~ - day | on_call - +---------------------------+---------+ - 2018-10-01 00:00:00+00:00 | 2 - 2018-10-02 00:00:00+00:00 | 2 - 2018-10-03 00:00:00+00:00 | 2 - 2018-10-04 00:00:00+00:00 | 2 - 2018-10-05 00:00:00+00:00 | 2 - 2018-10-06 00:00:00+00:00 | 2 - 2018-10-07 00:00:00+00:00 | 2 - (7 rows) - ~~~ - -### Step 4. Doctor 1 requests leave - -Doctor 1, Abe, starts to request leave for 10/5/18 using the hospital's schedule management application. - -1. The application starts a transaction: - - {% include copy-clipboard.html %} - ~~~ sql - > BEGIN; - ~~~ - -2. The application checks to make sure at least one other doctor is on call for the requested date: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT count(*) FROM schedules - WHERE on_call = true - AND day = '2018-10-05' - AND doctor_id != 1; - ~~~ - - Press enter a second time to have the server return the result: - - ~~~ - count - +-------+ - 1 - (1 row) - ~~~ - -### Step 5. Doctor 2 requests leave - -Around the same time, doctor 2, Betty, starts to request leave for the same day using the hospital's schedule management application. - -1. In a new terminal, start a second SQL session: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --insecure --host=localhost - ~~~ - -2. The application starts a transaction: - - {% include copy-clipboard.html %} - ~~~ sql - > BEGIN; - ~~~ - -3. The application checks to make sure at least one other doctor is on call for the requested date: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT count(*) FROM schedules - WHERE on_call = true - AND day = '2018-10-05' - AND doctor_id != 2; - ~~~ - - Press enter a second time to have the server return the result: - - ~~~ - count - +-------+ - 1 - (1 row) - ~~~ - -### Step 6. Leave is booked for only 1 doctor - -1. In the terminal for doctor 1, since the previous check confirmed that another doctor is on call for 10/5/18, the application tries to update doctor 1's schedule: - - {% include copy-clipboard.html %} - ~~~ sql - > UPDATE schedules SET on_call = false - WHERE day = '2018-10-05' - AND doctor_id = 1; - ~~~ - -2. In the terminal for doctor 2, since the previous check confirmed the same thing, the application tries to update doctor 2's schedule: - - {% include copy-clipboard.html %} - ~~~ sql - > UPDATE schedules SET on_call = false - WHERE day = '2018-10-05' - AND doctor_id = 2; - ~~~ - -3. In the terminal for doctor 1, the application tries to commit the transaction: - - {% include copy-clipboard.html %} - ~~~ sql - > COMMIT; - ~~~ - - Since CockroachDB uses `SERIALIZABLE` isolation, the database detects that the previous check (the `SELECT` query) is no longer true due to a concurrent transaction. It therefore prevents the transaction from committing, returning a retry error that indicates that the transaction must be attempted again: - - ~~~ - pq: restart transaction: TransactionRetryWithProtoRefreshError: TransactionRetryError: retry txn (RETRY_SERIALIZABLE): id=373bbefe key=/Table/53/1/17809/1/0 rw=true pri=0.03885012 stat=PENDING epo=0 ts=1569638527.268184000,1 orig=1569638507.593587000,0 min=1569638507.593587000,0 max=1569638507.593587000,0 wto=false seq=2 - ~~~ - - {{site.data.alerts.callout_success}} - For this kind of error, CockroachDB recommends a [client-side transaction retry loop](transactions.html#client-side-intervention) that would transparently observe that the one doctor cannot take time off because the other doctor already succeeded in asking for it. You can find generic transaction retry functions for various languages in our [Build an App](hello-world-example-apps.html) tutorials. - {{site.data.alerts.end}} - -4. In the terminal for doctor 2, the application tries to commit the transaction: - - {% include copy-clipboard.html %} - ~~~ sql - > COMMIT; - ~~~ - - Since the transaction for doctor 1 failed, the transaction for doctor 2 can commit without causing any data correctness problems. - -### Step 7. Check data correctness - -1. In either terminal, confirm that one doctor is still on call for 10/5/18: - - {% include copy-clipboard.html %} - ~~~ sql - > SELECT * FROM schedules WHERE day = '2018-10-05'; - ~~~ - - ~~~ - day | doctor_id | on_call - +---------------------------+-----------+---------+ - 2018-10-05 00:00:00+00:00 | 1 | true - 2018-10-05 00:00:00+00:00 | 2 | false - (2 rows) - ~~~ - -2. Again, the write skew anomaly was prevented by CockroachDB using the `SERIALIZABLE` isolation level: - - {% include copy-clipboard.html %} - ~~~ sql - > SHOW TRANSACTION_ISOLATION; - ~~~ - - ~~~ - transaction_isolation - +-----------------------+ - serializable - (1 row) - ~~~ - -3. Exit the SQL shell in each terminal: - - {% include copy-clipboard.html %} - ~~~ sql - > \q - ~~~ - -### Step 8. Stop CockroachDB - -Once you're done with your test cluster, exit each SQL shell with `\q` and then stop the node: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach quit --insecure --host=localhost -~~~ - -If you do not plan to restart the cluster, you may want to remove the node's data store: - -{% include copy-clipboard.html %} -~~~ shell -$ rm -rf serializable-demo -~~~ - -## What's next? - -Explore other core CockroachDB benefits and features: - -{% include {{ page.version.version }}/misc/explore-benefits-see-also.md %} - -You might also want to learn more about how transactions work in CockroachDB and in general: - -- [Transactions Overview](transactions.html) -- [Real Transactions are Serializable](https://www.cockroachlabs.com/blog/acid-rain/) -- [What Write Skew Looks Like](https://www.cockroachlabs.com/blog/what-write-skew-looks-like/) diff --git a/src/current/v19.2/deploy-cockroachdb-on-aws-insecure.md b/src/current/v19.2/deploy-cockroachdb-on-aws-insecure.md deleted file mode 100644 index 5d470289c24..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-aws-insecure.md +++ /dev/null @@ -1,164 +0,0 @@ ---- -title: Deploy CockroachDB on AWS EC2 (Insecure) -summary: Learn how to deploy CockroachDB on Amazon's AWS EC2 platform. -toc: true -toc_not_nested: true -ssh-link: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html ---- - - - -This page shows you how to manually deploy an insecure multi-node CockroachDB cluster on Amazon's AWS EC2 platform, using AWS's managed load balancing service to distribute client traffic. - -{{site.data.alerts.callout_danger}}If you plan to use CockroachDB in production, we strongly recommend using a secure cluster instead. Select Secure above for instructions.{{site.data.alerts.end}} - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/insecure-requirements.md %} - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/insecure-recommendations.md %} - -- All Amazon EC2 instances running CockroachDB should be members of the same [security group](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html). - -## Step 1. Create instances - -Open the [Amazon EC2 console](https://console.aws.amazon.com/ec2/) and [launch an instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/launching-instance.html#launch-instance-console) for each node you plan to have in your cluster. If you plan to [run our sample workload](#step-8-run-a-sample-workload) against the cluster, create a separate instance for that workload. - -- Run at least 3 nodes to ensure survivability. - -- Your instances will rely on Amazon Time Sync Service for clock synchronization. When choosing an AMI, note that some machines are preconfigured to use Amazon Time Sync Service (e.g., Amazon Linux AMIs) and others are not. - -- Use `m` (general purpose), `c` (compute-optimized), or `i` (storage-optimized) instance types, with SSD-backed [EBS volumes](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html) or [Instance Store volumes](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ssd-instance-store.html). For example, Cockroach Labs has used `c5d.4xlarge` (16 vCPUs and 32 GiB of RAM per instance, EBS) for internal testing. - - - **Do not** use ["burstable" `t2` instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/t2-instances.html), which limit the load on a single core. - -- Note the ID of the VPC you select. You will need to look up its IP range when setting inbound rules for your security group. - -- Make sure all your instances are in the same security group. - - - If you are creating a new security group, add the [inbound rules](#step-2-configure-your-network) from the next step. Otherwise note the ID of the security group. - -For more details, see [Hardware Recommendations](recommended-production-settings.html#hardware) and [Cluster Topology](recommended-production-settings.html#topology). - -## Step 2. Configure your network - -CockroachDB requires TCP communication on two ports: - -- `26257` for inter-node communication (i.e., working as a cluster), for applications to connect to the load balancer, and for routing from the load balancer to nodes -- `8080` for exposing your Admin UI, and for routing from the load balancer to the health check - -If you haven't already done so, [create inbound rules](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html#adding-security-group-rule) for your security group. - -#### Inter-node and load balancer-node communication - - Field | Recommended Value --------|------------------- - Type | Custom TCP Rule - Protocol | TCP - Port Range | **26257** - Source | The ID of your security group (e.g., *sg-07ab277a*) - -#### Application data - - Field | Recommended Value --------|------------------- - Type | Custom TCP Rules - Protocol | TCP - Port Range | **26257** - Source | Your application's IP ranges - -If you plan to [run our sample workload](#step-8-run-a-sample-workload) on an instance, the traffic source is the internal (private) IP address of that instance. To find this, open the Instances section of the Amazon EC2 console and click on the instance. - -#### Admin UI - - Field | Recommended Value --------|------------------- - Type | Custom TCP Rule - Protocol | TCP - Port Range | **8080** - Source | Your network's IP ranges - -You can set your network IP by selecting "My IP" in the Source field. - -#### Load balancer-health check communication - - Field | Recommended Value --------|------------------- - Type | Custom TCP Rule - Protocol | TCP - Port Range | **8080** - Source | The IP range of your VPC in CIDR notation (e.g., 10.12.0.0/16) - -To get the IP range of a VPC, open the [Amazon VPC console](https://console.aws.amazon.com/vpc/) and find the VPC listed in the section called Your VPCs. You can also click on the VPC where it is listed in the EC2 console. - -## Step 3. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 4. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - -AWS offers fully-managed load balancing to distribute traffic between instances. - -1. [Add AWS load balancing](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancer-getting-started.html). Be sure to: - - Select a **Network Load Balancer** and use the ports we specify below. - - Select the VPC and *all* availability zones of your instances. This is important, as you cannot change the availability zones once the load balancer is created. The availability zone of an instance is determined by its subnet, found by inspecting the instance in the Amazon EC2 Console. - - Set the load balancer port to **26257**. - - Create a new target group that uses TCP port **26257**. Traffic from your load balancer is routed to this target group, which contains your instances. - - Configure health checks to use HTTP port **8080** and path `/health?ready=1`. This [health endpoint](monitoring-and-alerting.html#health-ready-1) ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests. - - Register your instances with the target group you created, specifying port **26257**. You can add and remove instances later. -2. To test load balancing and connect your application to the cluster, you will need the provisioned internal (private) **IP address** for the load balancer. To find this, open the Network Interfaces section of the Amazon EC2 console and look up the load balancer by its name. - -{{site.data.alerts.callout_info}}If you would prefer to use HAProxy instead of AWS's managed load balancing, see the On-Premises tutorial for guidance.{{site.data.alerts.end}} - -## Step 5. Start nodes - -{% include {{ page.version.version }}/prod-deployment/insecure-start-nodes.md %} - -## Step 6. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-initialize-cluster.md %} - -## Step 7. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-test-cluster.md %} - -## Step 8. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/insecure-test-load-balancing.md %} - -## Step 9. Monitor the cluster - -In the Target Groups section of the Amazon EC2 console, [check the health](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/target-group-health-checks.html) of your instances by inspecting your target group and opening the Targets tab. - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 10. Scale the cluster - -Before adding a new node, [create a new instance](#step-1-create-instances) as you did earlier. - -{% include {{ page.version.version }}/prod-deployment/insecure-scale-cluster.md %} - -## Step 11. Use the cluster - -Now that your deployment is working, you can: - -1. [Implement your data model](sql-statements.html). -2. [Create users](authorization.html) and [grant them privileges](grant.html). -3. [Connect your application](install-client-drivers.html). Be sure to connect your application to the AWS load balancer, not to a CockroachDB node. - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-aws.md b/src/current/v19.2/deploy-cockroachdb-on-aws.md deleted file mode 100644 index 1c234776b97..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-aws.md +++ /dev/null @@ -1,167 +0,0 @@ ---- -title: Deploy CockroachDB on AWS EC2 -summary: Learn how to deploy CockroachDB on Amazon's AWS EC2 platform. -toc: true -toc_not_nested: true -ssh-link: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html - ---- - -
      - - -
      - -This page shows you how to manually deploy a secure multi-node CockroachDB cluster on Amazon's AWS EC2 platform, using AWS's managed load balancing service to distribute client traffic. - -If you are only testing CockroachDB, or you are not concerned with protecting network communication with TLS encryption, you can use an insecure cluster instead. Select **Insecure** above for instructions. - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/secure-requirements.md %} - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/secure-recommendations.md %} - -- All Amazon EC2 instances running CockroachDB should be members of the same [security group](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html). - -## Step 1. Create instances - -Open the [Amazon EC2 console](https://console.aws.amazon.com/ec2/) and [launch an instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/launching-instance.html#launch-instance-console) for each node you plan to have in your cluster. If you plan to [run our sample workload](#step-9-run-a-sample-workload) against the cluster, create a separate instance for that workload. - -- Run at least 3 nodes to ensure survivability. - -- Your instances will rely on Amazon Time Sync Service for clock synchronization. When choosing an AMI, note that some machines are preconfigured to use Amazon Time Sync Service (e.g., Amazon Linux AMIs) and others are not. - -- Use `m` (general purpose), `c` (compute-optimized), or `i` (storage-optimized) instance types, with SSD-backed [EBS volumes](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html) or [Instance Store volumes](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ssd-instance-store.html). For example, Cockroach Labs has used `c5d.4xlarge` (16 vCPUs and 32 GiB of RAM per instance, EBS) for internal testing. - - - **Do not** use ["burstable" `t2` instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/t2-instances.html), which limit the load on a single core. - -- Note the ID of the VPC you select. You will need to look up its IP range when setting inbound rules for your security group. - -- Make sure all your instances are in the same security group. - - - If you are creating a new security group, add the [inbound rules](#step-2-configure-your-network) from the next step. Otherwise note the ID of the security group. - -- When creating the instance, you will download a private key file used to securely connect to your instances. Decide where to place this file, and note the file path for later commands. - -For more details, see [Hardware Recommendations](recommended-production-settings.html#hardware) and [Cluster Topology](recommended-production-settings.html#topology). - -## Step 2. Configure your network - -CockroachDB requires TCP communication on two ports: - -- `26257` for inter-node communication (i.e., working as a cluster), for applications to connect to the load balancer, and for routing from the load balancer to nodes -- `8080` for exposing your Admin UI, and for routing from the load balancer to the health check - -If you haven't already done so, [create inbound rules](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html#adding-security-group-rule) for your security group. - -#### Inter-node and load balancer-node communication - - Field | Recommended Value --------|------------------- - Type | Custom TCP Rule - Protocol | TCP - Port Range | **26257** - Source | The ID of your security group (e.g., *sg-07ab277a*) - -#### Application data - - Field | Recommended Value --------|------------------- - Type | Custom TCP Rules - Protocol | TCP - Port Range | **26257** - Source | Your application's IP ranges - -If you plan to [run our sample workload](#step-9-run-a-sample-workload) on an instance, the traffic source is the internal (private) IP address of that instance. To find this, open the Instances section of the Amazon EC2 console and click on the instance. - -#### Admin UI - - Field | Recommended Value --------|------------------- - Type | Custom TCP Rule - Protocol | TCP - Port Range | **8080** - Source | Your network's IP ranges - -You can set your network IP by selecting "My IP" in the Source field. - -#### Load balancer-health check communication - - Field | Recommended Value --------|------------------- - Type | Custom TCP Rule - Protocol | TCP - Port Range | **8080** - Source | The IP range of your VPC in CIDR notation (e.g., 10.12.0.0/16) - -To get the IP range of a VPC, open the [Amazon VPC console](https://console.aws.amazon.com/vpc/) and find the VPC listed in the section called Your VPCs. You can also click on the VPC where it is listed in the EC2 console. - -## Step 3. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 4. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - -AWS offers fully-managed load balancing to distribute traffic between instances. - -1. [Add AWS load balancing](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancer-getting-started.html). Be sure to: - - Select a **Network Load Balancer** and use the ports we specify below. - - Select the VPC and *all* availability zones of your instances. This is important, as you cannot change the availability zones once the load balancer is created. The availability zone of an instance is determined by its subnet, found by inspecting the instance in the Amazon EC2 Console. - - Set the load balancer port to **26257**. - - Create a new target group that uses TCP port **26257**. Traffic from your load balancer is routed to this target group, which contains your instances. - - Configure health checks to use HTTP port **8080** and path `/health?ready=1`. This [health endpoint](monitoring-and-alerting.html#health-ready-1) ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests. - - Register your instances with the target group you created, specifying port **26257**. You can add and remove instances later. -2. To test load balancing and connect your application to the cluster, you will need the provisioned internal (private) **IP address** for the load balancer. To find this, open the Network Interfaces section of the Amazon EC2 console and look up the load balancer by its name. - -{{site.data.alerts.callout_info}}If you would prefer to use HAProxy instead of AWS's managed load balancing, see the On-Premises tutorial for guidance.{{site.data.alerts.end}} - -## Step 5. Generate certificates - -{% include {{ page.version.version }}/prod-deployment/secure-generate-certificates.md %} - -## Step 6. Start nodes - -{% include {{ page.version.version }}/prod-deployment/secure-start-nodes.md %} - -## Step 7. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-initialize-cluster.md %} - -## Step 8. Test your cluster - -{% include {{ page.version.version }}/prod-deployment/secure-test-cluster.md %} - -## Step 9. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/secure-test-load-balancing.md %} - -## Step 10. Monitor the cluster - -In the Target Groups section of the Amazon EC2 console, [check the health](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/target-group-health-checks.html) of your instances by inspecting your target group and opening the Targets tab. - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 11. Scale the cluster - -Before adding a new node, [create a new instance](#step-1-create-instances) as you did earlier. Then [generate and upload a certificate and key](#step-5-generate-certificates) for the new node. - -{% include {{ page.version.version }}/prod-deployment/secure-scale-cluster.md %} - -## Step 12. Use the database - -{% include {{ page.version.version }}/prod-deployment/use-cluster.md %} - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-digital-ocean-insecure.md b/src/current/v19.2/deploy-cockroachdb-on-digital-ocean-insecure.md deleted file mode 100644 index 7b5e3114c14..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-digital-ocean-insecure.md +++ /dev/null @@ -1,110 +0,0 @@ ---- -title: Deploy CockroachDB on Digital Ocean (Insecure) -summary: Learn how to deploy a CockroachDB cluster on Digital Ocean. -toc: true -toc_not_nested: true -ssh-link: https://www.digitalocean.com/community/tutorials/how-to-connect-to-your-droplet-with-ssh ---- - - - -This page shows you how to deploy an insecure multi-node CockroachDB cluster on Digital Ocean, using Digital Ocean's managed load balancing service to distribute client traffic. - -{{site.data.alerts.callout_danger}}If you plan to use CockroachDB in production, we strongly recommend using a secure cluster instead. Select Secure above for instructions.{{site.data.alerts.end}} - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/insecure-requirements.md %} - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/insecure-recommendations.md %} - -- If all of your CockroachDB nodes and clients will run on Droplets in a single region, consider using [private networking](https://docs.digitalocean.com/products/networking/vpc/how-to/create/). - -## Step 1. Create Droplets - -[Create Droplets](https://www.digitalocean.com/community/tutorials/how-to-create-your-first-digitalocean-droplet) for each node you plan to have in your cluster. If you plan to run a sample workload against the cluster, create a separate droplet for that workload. - -- Run at least 3 nodes to [ensure survivability](recommended-production-settings.html#topology). - -- Use any [droplets](https://www.digitalocean.com/pricing/) except standard droplets with only 1 GB of RAM, which is below our minimum requirement. All Digital Ocean droplets use SSD storage. - -For more details, see [Hardware Recommendations](recommended-production-settings.html#hardware) and [Cluster Topology](recommended-production-settings.html#topology). - -## Step 2. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 3. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - -Digital Ocean offers fully-managed load balancers to distribute traffic between Droplets. - -1. [Create a Digital Ocean Load Balancer](https://www.digitalocean.com/community/tutorials/an-introduction-to-digitalocean-load-balancers). Be sure to: - - Set forwarding rules to route TCP traffic from the load balancer's port **26257** to port **26257** on the node Droplets. - - Configure health checks to use HTTP port **8080** and path `/health?ready=1`. This [health endpoint](monitoring-and-alerting.html#health-ready-1) ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests. -2. Note the provisioned **IP Address** for the load balancer. You'll use this later to test load balancing and to connect your application to the cluster. - -{{site.data.alerts.callout_info}}If you would prefer to use HAProxy instead of Digital Ocean's managed load balancing, see the On-Premises tutorial for guidance.{{site.data.alerts.end}} - -## Step 4. Configure your network - -Set up a firewall for each of your Droplets, allowing TCP communication on the following two ports: - -- **26257** (`tcp:26257`) for inter-node communication (i.e., working as a cluster), for applications to connect to the load balancer, and for routing from the load balancer to nodes -- **8080** (`tcp:8080`) for exposing your Admin UI - -For guidance, you can use Digital Ocean's guide to configuring firewalls based on the Droplet's OS: - -- Ubuntu and Debian can use [`ufw`](https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server). -- FreeBSD can use [`ipfw`](https://www.digitalocean.com/community/tutorials/recommended-steps-for-new-freebsd-10-1-servers). -- Fedora can use [`iptables`](https://www.digitalocean.com/community/tutorials/initial-setup-of-a-fedora-22-server). -- CoreOS can use [`iptables`](https://www.digitalocean.com/community/tutorials/how-to-secure-your-coreos-cluster-with-tls-ssl-and-firewall-rules). -- CentOS can use [`firewalld`](https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-using-firewalld-on-centos-7). - -## Step 5. Start nodes - -{% include {{ page.version.version }}/prod-deployment/insecure-start-nodes.md %} - -## Step 6. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-initialize-cluster.md %} - -## Step 7. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-test-cluster.md %} - -## Step 8. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/insecure-test-load-balancing.md %} - -## Step 9. Monitor the cluster - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 10. Scale the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-scale-cluster.md %} - -## Step 11. Use the cluster - -Now that your deployment is working, you can: - -1. [Implement your data model](sql-statements.html). -2. [Create users](create-user.html) and [grant them privileges](grant.html). -3. [Connect your application](install-client-drivers.html). Be sure to connect your application to the Digital Ocean Load Balancer, not to a CockroachDB node. - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-digital-ocean.md b/src/current/v19.2/deploy-cockroachdb-on-digital-ocean.md deleted file mode 100644 index e93737681fa..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-digital-ocean.md +++ /dev/null @@ -1,110 +0,0 @@ ---- -title: Deploy CockroachDB on Digital Ocean -summary: Learn how to deploy a CockroachDB cluster on Digital Ocean. -toc: true -toc_not_nested: true -ssh-link: https://www.digitalocean.com/community/tutorials/how-to-connect-to-your-droplet-with-ssh ---- - -
      - - -
      - -This page shows you how to deploy a secure multi-node CockroachDB cluster on Digital Ocean, using Digital Ocean's managed load balancing service to distribute client traffic. - -If you are only testing CockroachDB, or you are not concerned with protecting network communication with TLS encryption, you can use an insecure cluster instead. Select **Insecure** above for instructions. - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/secure-requirements.md %} - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/secure-recommendations.md %} - -- If all of your CockroachDB nodes and clients will run on Droplets in a single region, consider using [private networking](https://docs.digitalocean.com/products/networking/vpc/how-to/create/). - -## Step 1. Create Droplets - -[Create Droplets](https://www.digitalocean.com/community/tutorials/how-to-create-your-first-digitalocean-droplet) for each node you plan to have in your cluster. If you plan to run a sample workload against the cluster, create a separate Droplet for that workload. - -- Run at least 3 nodes to [ensure survivability](recommended-production-settings.html#topology). - -- Use any [droplets](https://www.digitalocean.com/pricing/) except standard droplets with only 1 GB of RAM, which is below our minimum requirement. All Digital Ocean droplets use SSD storage. - -For more details, see [Hardware Recommendations](recommended-production-settings.html#hardware) and [Cluster Topology](recommended-production-settings.html#topology). - -## Step 2. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 3. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - -Digital Ocean offers fully-managed load balancers to distribute traffic between Droplets. - -1. [Create a Digital Ocean Load Balancer](https://www.digitalocean.com/community/tutorials/an-introduction-to-digitalocean-load-balancers). Be sure to: - - Set forwarding rules to route TCP traffic from the load balancer's port **26257** to port **26257** on the node Droplets. - - Configure health checks to use HTTP port **8080** and path `/health?ready=1`. This [health endpoint](monitoring-and-alerting.html#health-ready-1) ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests. -2. Note the provisioned **IP Address** for the load balancer. You'll use this later to test load balancing and to connect your application to the cluster. - -{{site.data.alerts.callout_info}}If you would prefer to use HAProxy instead of Digital Ocean's managed load balancing, see the On-Premises tutorial for guidance.{{site.data.alerts.end}} - -## Step 4. Configure your network - -Set up a firewall for each of your Droplets, allowing TCP communication on the following two ports: - -- **26257** (`tcp:26257`) for inter-node communication (i.e., working as a cluster), for applications to connect to the load balancer, and for routing from the load balancer to nodes -- **8080** (`tcp:8080`) for exposing your Admin UI - -For guidance, you can use Digital Ocean's guide to configuring firewalls based on the Droplet's OS: - -- Ubuntu and Debian can use [`ufw`](https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server). -- FreeBSD can use [`ipfw`](https://www.digitalocean.com/community/tutorials/recommended-steps-for-new-freebsd-10-1-servers). -- Fedora can use [`iptables`](https://www.digitalocean.com/community/tutorials/initial-setup-of-a-fedora-22-server). -- CoreOS can use [`iptables`](https://www.digitalocean.com/community/tutorials/how-to-secure-your-coreos-cluster-with-tls-ssl-and-firewall-rules). -- CentOS can use [`firewalld`](https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-using-firewalld-on-centos-7). - -## Step 5. Generate certificates - -{% include {{ page.version.version }}/prod-deployment/secure-generate-certificates.md %} - -## Step 6. Start nodes - -{% include {{ page.version.version }}/prod-deployment/secure-start-nodes.md %} - -## Step 7. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-initialize-cluster.md %} - -## Step 8. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-test-cluster.md %} - -## Step 9. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/secure-test-load-balancing.md %} - -## Step 10. Monitor the cluster - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 11. Scale the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-scale-cluster.md %} - -## Step 12. Use the database - -{% include {{ page.version.version }}/prod-deployment/use-cluster.md %} - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-google-cloud-platform-insecure.md b/src/current/v19.2/deploy-cockroachdb-on-google-cloud-platform-insecure.md deleted file mode 100644 index 4169dab5ecf..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-google-cloud-platform-insecure.md +++ /dev/null @@ -1,135 +0,0 @@ ---- -title: Deploy CockroachDB on Google Cloud Platform GCE (Insecure) -summary: Learn how to deploy CockroachDB on Google Cloud Platform's Compute Engine. -toc: true -toc_not_nested: true -ssh-link: https://cloud.google.com/compute/docs/instances/connecting-to-instance ---- - - - -This page shows you how to manually deploy an insecure multi-node CockroachDB cluster on Google Cloud Platform's Compute Engine (GCE), using Google's TCP Proxy Load Balancing service to distribute client traffic. - -{{site.data.alerts.callout_danger}}If you plan to use CockroachDB in production, we strongly recommend using a secure cluster instead. Select Secure above for instructions.{{site.data.alerts.end}} - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/insecure-requirements.md %} - -- This article covers the use of Linux instances with GCE. You may wish to review the instructions for [connecting to Windows instances](https://cloud.google.com/compute/docs/instances/connecting-to-instance#windows). - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/insecure-recommendations.md %} - -## Step 1. Configure your network - -CockroachDB requires TCP communication on two ports: - -- `26257` for inter-node communication (i.e., working as a cluster) -- `8080` for exposing your Admin UI - -To expose your Admin UI and allow traffic from the TCP proxy load balancer and health checker to your instances, [create firewall rules](https://cloud.google.com/compute/docs/vpc/firewalls) for your project. When creating firewall rules, we recommend using Google Cloud Platform's **tag** feature to apply the rule only to instances with the same tag. - -#### Admin UI - - Field | Recommended Value --------|------------------- - Name | **cockroachadmin** - Source filter | IP ranges - Source IP ranges | Your local network's IP ranges - Allowed protocols... | **tcp:8080** - Target tags | **cockroachdb** - -#### Application data - -Applications will not connect directly to your CockroachDB nodes. Instead, they'll connect to GCE's TCP Proxy Load Balancing service, which automatically routes traffic to the instances that are closest to the user. Because this service is implemented at the edge of the Google Cloud, you'll need to create a firewall rule to allow traffic from the load balancer and health checker to your instances. This is covered in [Step 4](#step-4-set-up-load-balancing). - -## Step 2. Create instances - -[Create an instance](https://cloud.google.com/compute/docs/instances/create-start-instance) for each node you plan to have in your cluster. If you plan to run a sample workload against the cluster, create a separate instance for that workload. - -- Run at least 3 nodes to [ensure survivability](recommended-production-settings.html#topology). - -- Use `n1-standard` or `n1-highcpu` [predefined VMs](https://cloud.google.com/compute/pricing#predefined_machine_types), or [custom VMs](https://cloud.google.com/compute/pricing#custommachinetypepricing), with [Local SSDs](https://cloud.google.com/compute/docs/disks/#localssds) or [SSD persistent disks](https://cloud.google.com/compute/docs/disks/#pdspecs). For example, Cockroach Labs has used `n1-standard-16` (16 vCPUs and 60 GB of RAM per VM, local SSD) for internal testing. - -- **Do not** use `f1` or `g1` [shared-core machines](https://cloud.google.com/compute/docs/machine-types#sharedcore), which limit the load on a single core. - -- If you used a tag for your firewall rules, when you create the instance, click **Management, security, disks, networking, sole tenancy**. Under the **Networking** tab, in the **Network tags** field, enter **cockroachdb**. - -For more details, see [Hardware Recommendations](recommended-production-settings.html#hardware) and [Cluster Topology](recommended-production-settings.html#topology). - -## Step 3. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 4. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - -GCE offers fully-managed [TCP Proxy Load Balancing](https://cloud.google.com/load-balancing/docs/tcp/). This service lets you use a single IP address for all users around the world, automatically routing traffic to the instances that are closest to the user. - -{{site.data.alerts.callout_danger}} -When using TCP Proxy Load Balancing, you cannot use firewall rules to control access to the load balancer. If you need such control, consider using [Network TCP Load Balancing](https://cloud.google.com/compute/docs/load-balancing/network/) instead, but note that it cannot be used across regions. You might also consider using the HAProxy load balancer (see the [On-Premises](deploy-cockroachdb-on-premises-insecure.html) tutorial for guidance). -{{site.data.alerts.end}} - -To use GCE's TCP Proxy Load Balancing service: - -1. For each zone in which you're running an instance, [create a distinct instance group](https://cloud.google.com/compute/docs/instance-groups/creating-groups-of-unmanaged-instances). - - To ensure that the load balancer knows where to direct traffic, specify a port name mapping, with `tcp26257` as the **Port name** and `26257` as the **Port number**. -2. [Add the relevant instances to each instance group](https://cloud.google.com/compute/docs/instance-groups/creating-groups-of-unmanaged-instances#addinstances). -3. [Configure Proxy Load Balancing](https://cloud.google.com/load-balancing/docs/tcp/setting-up-tcp#configure_load_balancer). - - During backend configuration, create a health check, setting the **Protocol** to `HTTP`, the **Port** to `8080`, and the **Request path** to path `/health?ready=1`. This [health endpoint](monitoring-and-alerting.html#health-ready-1) ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests. - - If you want to maintain long-lived SQL connections that may be idle for more than tens of seconds, increase the backend timeout setting accordingly. - - During frontend configuration, reserve a static IP address and choose a port. Note this address/port combination, as you'll use it for all of you client connections. -4. [Create a firewall rule](https://cloud.google.com/load-balancing/docs/tcp/setting-up-tcp#config-hc-firewall) to allow traffic from the load balancer and health checker to your instances. This is necessary because TCP Proxy Load Balancing is implemented at the edge of the Google Cloud. - - Be sure to set **Source IP ranges** to `130.211.0.0/22` and `35.191.0.0/16` and set **Target tags** to `cockroachdb` (not to the value specified in the linked instructions). - -## Step 5. Start nodes - -{{site.data.alerts.callout_info}} -By default, inter-node communication uses the internal IP addresses of your GCE instances. -{{site.data.alerts.end}} - -{% include {{ page.version.version }}/prod-deployment/insecure-start-nodes.md %} - -## Step 6. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-initialize-cluster.md %} - -## Step 7. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-test-cluster.md %} - -## Step 8. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/insecure-test-load-balancing.md %} - -## Step 9. Monitor the cluster - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 10. Scale the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-scale-cluster.md %} - -## Step 11. Use the cluster - -Now that your deployment is working, you can: - -1. [Implement your data model](sql-statements.html). -2. [Create users](create-user.html) and [grant them privileges](grant.html). -3. [Connect your application](install-client-drivers.html). Be sure to connect your application to the GCE load balancer, not to a CockroachDB node. - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-google-cloud-platform.md b/src/current/v19.2/deploy-cockroachdb-on-google-cloud-platform.md deleted file mode 100644 index 408a28038b1..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-google-cloud-platform.md +++ /dev/null @@ -1,135 +0,0 @@ ---- -title: Deploy CockroachDB on Google Cloud Platform GCE -summary: Learn how to deploy CockroachDB on Google Cloud Platform's Compute Engine. -toc: true -toc_not_nested: true -ssh-link: https://cloud.google.com/compute/docs/instances/connecting-to-instance ---- - -
      - - -
      - -This page shows you how to manually deploy a secure multi-node CockroachDB cluster on Google Cloud Platform's Compute Engine (GCE), using Google's TCP Proxy Load Balancing service to distribute client traffic. - -If you are only testing CockroachDB, or you are not concerned with protecting network communication with TLS encryption, you can use an insecure cluster instead. Select **Insecure** above for instructions. - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/secure-requirements.md %} - -- This article covers the use of Linux instances with GCE. You may wish to review the instructions for [connecting to Windows instances](https://cloud.google.com/compute/docs/instances/connecting-to-instance#windows). - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/secure-recommendations.md %} - -## Step 1. Configure your network - -CockroachDB requires TCP communication on two ports: - -- `26257` for inter-node communication (i.e., working as a cluster) -- `8080` for exposing your Admin UI - -To expose your Admin UI and allow traffic from the TCP proxy load balancer and health checker to your instances, [create firewall rules](https://cloud.google.com/compute/docs/vpc/firewalls) for your project. When creating firewall rules, we recommend using Google Cloud Platform's **tag** feature to apply the rule only to instances with the same tag. - -#### Admin UI - - Field | Recommended Value --------|------------------- - Name | **cockroachadmin** - Source filter | IP ranges - Source IP ranges | Your local network's IP ranges - Allowed protocols... | **tcp:8080** - Target tags | **cockroachdb** - -#### Application data - -Applications will not connect directly to your CockroachDB nodes. Instead, they'll connect to GCE's TCP Proxy Load Balancing service, which automatically routes traffic to the instances that are closest to the user. Because this service is implemented at the edge of the Google Cloud, you'll need to create a firewall rule to allow traffic from the load balancer and health checker to your instances. This is covered in [Step 4](#step-4-set-up-load-balancing). - -## Step 2. Create instances - -[Create an instance](https://cloud.google.com/compute/docs/instances/create-start-instance) for each node you plan to have in your cluster. If you plan to run a sample workload against the cluster, create a separate instance for that workload. - -- Run at least 3 nodes to [ensure survivability](recommended-production-settings.html#topology). - -- Use `n1-standard` or `n1-highcpu` [predefined VMs](https://cloud.google.com/compute/pricing#predefined_machine_types), or [custom VMs](https://cloud.google.com/compute/pricing#custommachinetypepricing), with [Local SSDs](https://cloud.google.com/compute/docs/disks/#localssds) or [SSD persistent disks](https://cloud.google.com/compute/docs/disks/#pdspecs). For example, Cockroach Labs has used `n1-standard-16` (16 vCPUs and 60 GB of RAM per VM, local SSD) for internal testing. - -- **Do not** use `f1` or `g1` [shared-core machines](https://cloud.google.com/compute/docs/machine-types#sharedcore), which limit the load on a single core. - -- If you used a tag for your firewall rules, when you create the instance, click **Management, security, disks, networking, sole tenancy**. Under the **Networking** tab, in the **Network tags** field, enter **cockroachdb**. - -For more details, see [Hardware Recommendations](recommended-production-settings.html#hardware) and [Cluster Topology](recommended-production-settings.html#topology). - -## Step 3. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 4. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - -GCE offers fully-managed [TCP Proxy Load Balancing](https://cloud.google.com/load-balancing/docs/tcp/). This service lets you use a single IP address for all users around the world, automatically routing traffic to the instances that are closest to the user. - -{{site.data.alerts.callout_danger}} -When using TCP Proxy Load Balancing, you cannot use firewall rules to control access to the load balancer. If you need such control, consider using [Network TCP Load Balancing](https://cloud.google.com/compute/docs/load-balancing/network/) instead, but note that it cannot be used across regions. You might also consider using the HAProxy load balancer (see the [On-Premises](deploy-cockroachdb-on-premises.html) tutorial for guidance). -{{site.data.alerts.end}} - -To use GCE's TCP Proxy Load Balancing service: - -1. For each zone in which you're running an instance, [create a distinct instance group](https://cloud.google.com/compute/docs/instance-groups/creating-groups-of-unmanaged-instances). - - To ensure that the load balancer knows where to direct traffic, specify a port name mapping, with `tcp26257` as the **Port name** and `26257` as the **Port number**. -2. [Add the relevant instances to each instance group](https://cloud.google.com/compute/docs/instance-groups/creating-groups-of-unmanaged-instances#addinstances). -3. [Configure TCP Proxy Load Balancing](https://cloud.google.com/load-balancing/docs/tcp/setting-up-tcp#configure_load_balancer). - - During backend configuration, create a health check, setting the **Protocol** to `HTTPS`, the **Port** to `8080`, and the **Request path** to path `/health?ready=1`. This [health endpoint](monitoring-and-alerting.html#health-ready-1) ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests. - - If you want to maintain long-lived SQL connections that may be idle for more than tens of seconds, increase the backend timeout setting accordingly. - - During frontend configuration, reserve a static IP address and note the IP address and the port you select. You'll use this address and port for all client connections. -4. [Create a firewall rule](https://cloud.google.com/load-balancing/docs/tcp/setting-up-tcp#config-hc-firewall) to allow traffic from the load balancer and health checker to your instances. This is necessary because TCP Proxy Load Balancing is implemented at the edge of the Google Cloud. - - Be sure to set **Source IP ranges** to `130.211.0.0/22` and `35.191.0.0/16` and set **Target tags** to `cockroachdb` (not to the value specified in the linked instructions). - -## Step 5. Generate certificates - -{% include {{ page.version.version }}/prod-deployment/secure-generate-certificates.md %} - -## Step 6. Start nodes - -{{site.data.alerts.callout_info}} -By default, inter-node communication uses the internal IP addresses of your GCE instances. -{{site.data.alerts.end}} - -{% include {{ page.version.version }}/prod-deployment/secure-start-nodes.md %} - -## Step 7. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-initialize-cluster.md %} - -## Step 8. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-test-cluster.md %} - -## Step 9. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/secure-test-load-balancing.md %} - -## Step 10. Monitor the cluster - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 11. Scale the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-scale-cluster.md %} - -## Step 12. Use the database - -{% include {{ page.version.version }}/prod-deployment/use-cluster.md %} - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-microsoft-azure-insecure.md b/src/current/v19.2/deploy-cockroachdb-on-microsoft-azure-insecure.md deleted file mode 100644 index 09ccde01786..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-microsoft-azure-insecure.md +++ /dev/null @@ -1,144 +0,0 @@ ---- -title: Deploy CockroachDB on Microsoft Azure (Insecure) -summary: Learn how to deploy CockroachDB on Microsoft Azure. -toc: true -toc_not_nested: true -ssh-link: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/mac-create-ssh-keys ---- - - - -This page shows you how to manually deploy an insecure multi-node CockroachDB cluster on Microsoft Azure, using Azure's managed load balancing service to distribute client traffic. - -{{site.data.alerts.callout_danger}}If you plan to use CockroachDB in production, we strongly recommend using a secure cluster instead. Select Secure above for instructions.{{site.data.alerts.end}} - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/insecure-requirements.md %} - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/insecure-recommendations.md %} - -## Step 1. Configure your network - -CockroachDB requires TCP communication on two ports: - -- **26257** (`tcp:26257`) for inter-node communication (i.e., working as a cluster), for applications to connect to the load balancer, and for routing from the load balancer to nodes -- **8080** (`tcp:8080`) for exposing your Admin UI - -To enable this in Azure, you must create a Resource Group, Virtual Network, and Network Security Group. - -1. [Create a Resource Group](https://azure.microsoft.com/en-us/updates/create-empty-resource-groups/). - -2. [Create a Virtual Network](https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-create-vnet-arm-pportal) that uses your **Resource Group**. - -3. [Create a Network Security Group](https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-create-nsg-arm-pportal) that uses your **Resource Group**, and then add the following **inbound** rules to it: - - **Admin UI support**: - - Field | Recommended Value - -------|------------------- - Name | **cockroachadmin** - Source | **IP Addresses** - Source IP addresses/CIDR ranges | Your local network’s IP ranges - Source port ranges | * - Destination | **Any** - Destination port range | **8080** - Protocol | **TCP** - Action | **Allow** - Priority | Any value > 1000 - - **Application support**: - - {{site.data.alerts.callout_success}}If your application is also hosted on the same Azure Virtual Network, you will not need to create a firewall rule for your application to communicate with your load balancer.{{site.data.alerts.end}} - - Field | Recommended Value - -------|------------------- - Name | **cockroachapp** - Source | **IP Addresses** - Source IP addresses/CIDR ranges | Your local network’s IP ranges - Source port ranges | * - Destination | **Any** - Destination port range | **26257** - Protocol | **TCP** - Action | **Allow** - Priority | Any value > 1000 - - -## Step 2. Create VMs - -[Create Linux VMs](https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/quick-create-portal) for each node you plan to have in your cluster. If you plan to run a sample workload against the cluster, create a separate VM for that workload. - -- Run at least 3 nodes to [ensure survivability](recommended-production-settings.html#topology). - -- Use compute-optimized [F-series](https://docs.microsoft.com/en-us/azure/virtual-machines/fsv2-series) VMs with [Premium Storage](https://docs.microsoft.com/en-us/azure/virtual-machines/windows/premium-storage) or local SSD storage with a Linux filesystem such as `ext4` (not the Windows `ntfs` filesystem). For example, Cockroach Labs has used `Standard_F16s_v2` VMs (16 vCPUs and 32 GiB of RAM per VM) for internal testing. - - - If you choose local SSD storage, on reboot, the VM can come back with the `ntfs` filesystem. Be sure your automation monitors for this and reformats the disk to the Linux filesystem you chose initially. - -- **Do not** use ["burstable" B-series](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/b-series-burstable) VMs, which limit the load on a single core. Also, Cockroach Labs has experienced data corruption issues on A-series VMs and irregular disk performance on D-series VMs, so we recommend avoiding those as well. - -- When creating the VMs, make sure to select the **Resource Group**, **Virtual Network**, and **Network Security Group** you created. - -For more details, see [Hardware Recommendations](recommended-production-settings.html#hardware) and [Cluster Topology](recommended-production-settings.html#topology). - -## Step 3. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 4. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - -Microsoft Azure offers fully-managed load balancing to distribute traffic between instances. - -1. [Add Azure load balancing](https://docs.microsoft.com/en-us/azure/load-balancer/load-balancer-overview). Be sure to: - - Set forwarding rules to route TCP traffic from the load balancer's port **26257** to port **26257** on the nodes. - - Configure health checks to use HTTP port **8080** and path `/health?ready=1`. This [health endpoint](monitoring-and-alerting.html#health-ready-1) ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests. - -2. Note the provisioned **IP Address** for the load balancer. You'll use this later to test load balancing and to connect your application to the cluster. - -{{site.data.alerts.callout_info}}If you would prefer to use HAProxy instead of Azure's managed load balancing, see the On-Premises tutorial for guidance.{{site.data.alerts.end}} - -## Step 5. Start nodes - -{% include {{ page.version.version }}/prod-deployment/insecure-start-nodes.md %} - -## Step 6. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-initialize-cluster.md %} - -## Step 7. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-test-cluster.md %} - -## Step 8. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/insecure-test-load-balancing.md %} - -## Step 9. Monitor the cluster - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 10. Scale the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-scale-cluster.md %} - -## Step 11. Use the cluster - -Now that your deployment is working, you can: - -1. [Implement your data model](sql-statements.html). -2. [Create users](create-user.html) and [grant them privileges](grant.html). -3. [Connect your application](install-client-drivers.html). Be sure to connect your application to the Azure load balancer, not to a CockroachDB node. - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-microsoft-azure.md b/src/current/v19.2/deploy-cockroachdb-on-microsoft-azure.md deleted file mode 100644 index 2678bf7cad8..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-microsoft-azure.md +++ /dev/null @@ -1,141 +0,0 @@ ---- -title: Deploy CockroachDB on Microsoft Azure -summary: Learn how to deploy CockroachDB on Microsoft Azure. -toc: true -toc_not_nested: true -ssh-link: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/mac-create-ssh-keys ---- - -
      - - -
      - -This page shows you how to manually deploy a secure multi-node CockroachDB cluster on Microsoft Azure, using Azure's managed load balancing service to distribute client traffic. - -If you are only testing CockroachDB, or you are not concerned with protecting network communication with TLS encryption, you can use an insecure cluster instead. Select **Insecure** above for instructions. - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/secure-requirements.md %} - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/secure-recommendations.md %} - -## Step 1. Configure your network - -CockroachDB requires TCP communication on two ports: - -- **26257** (`tcp:26257`) for inter-node communication (i.e., working as a cluster), for applications to connect to the load balancer, and for routing from the load balancer to nodes -- **8080** (`tcp:8080`) for exposing your Admin UI - -To enable this in Azure, you must create a Resource Group, Virtual Network, and Network Security Group. - -1. [Create a Resource Group](https://azure.microsoft.com/en-us/updates/create-empty-resource-groups/). -2. [Create a Virtual Network](https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-create-vnet-arm-pportal) that uses your **Resource Group**. -3. [Create a Network Security Group](https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-create-nsg-arm-pportal) that uses your **Resource Group**, and then add the following **inbound** rules to it: - - **Admin UI support**: - - Field | Recommended Value - -------|------------------- - Name | **cockroachadmin** - Source | **IP Addresses** - Source IP addresses/CIDR ranges | Your local network’s IP ranges - Source port ranges | * - Destination | **Any** - Destination port range | **8080** - Protocol | **TCP** - Action | **Allow** - Priority | Any value > 1000 - - **Application support**: - - {{site.data.alerts.callout_success}}If your application is also hosted on the same Azure Virtual Network, you will not need to create a firewall rule for your application to communicate with your load balancer.{{site.data.alerts.end}} - - Field | Recommended Value - -------|------------------- - Name | **cockroachapp** - Source | **IP Addresses** - Source IP addresses/CIDR ranges | Your local network’s IP ranges - Source port ranges | * - Destination | **Any** - Destination port range | **26257** - Protocol | **TCP** - Action | **Allow** - Priority | Any value > 1000 - -## Step 2. Create VMs - -[Create Linux VMs](https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/quick-create-portal) for each node you plan to have in your cluster. If you plan to run a sample workload against the cluster, create a separate VM for that workload. - -- Run at least 3 nodes to [ensure survivability](recommended-production-settings.html#topology). - -- Use compute-optimized [F-series](https://docs.microsoft.com/en-us/azure/virtual-machines/fsv2-series) VMs with [Premium Storage](https://docs.microsoft.com/en-us/azure/virtual-machines/windows/premium-storage) or local SSD storage with a Linux filesystem such as `ext4` (not the Windows `ntfs` filesystem). For example, Cockroach Labs has used `Standard_F16s_v2` VMs (16 vCPUs and 32 GiB of RAM per VM) for internal testing. - - - If you choose local SSD storage, on reboot, the VM can come back with the `ntfs` filesystem. Be sure your automation monitors for this and reformats the disk to the Linux filesystem you chose initially. - -- **Do not** use ["burstable" B-series](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/b-series-burstable) VMs, which limit the load on a single core. Also, Cockroach Labs has experienced data corruption issues on A-series VMs and irregular disk performance on D-series VMs, so we recommend avoiding those as well. - -- When creating the VMs, make sure to select the **Resource Group**, **Virtual Network**, and **Network Security Group** you created. - -For more details, see [Hardware Recommendations](recommended-production-settings.html#hardware) and [Cluster Topology](recommended-production-settings.html#topology). - -## Step 3. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 4. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - -Microsoft Azure offers fully-managed load balancing to distribute traffic between instances. - -1. [Add Azure load balancing](https://docs.microsoft.com/en-us/azure/load-balancer/load-balancer-overview). Be sure to: - - Set forwarding rules to route TCP traffic from the load balancer's port **26257** to port **26257** on the nodes. - - Configure health checks to use HTTP port **8080** and path `/health?ready=1`. This [health endpoint](monitoring-and-alerting.html#health-ready-1) ensures that load balancers do not direct traffic to nodes that are live but not ready to receive requests. - -2. Note the provisioned **IP Address** for the load balancer. You'll use this later to test load balancing and to connect your application to the cluster. - -{{site.data.alerts.callout_info}}If you would prefer to use HAProxy instead of Azure's managed load balancing, see the On-Premises tutorial for guidance.{{site.data.alerts.end}} - -## Step 5. Generate certificates - -{% include {{ page.version.version }}/prod-deployment/secure-generate-certificates.md %} - -## Step 6. Start nodes - -{% include {{ page.version.version }}/prod-deployment/secure-start-nodes.md %} - -## Step 7. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-initialize-cluster.md %} - -## Step 8. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-test-cluster.md %} - -## Step 9. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/secure-test-load-balancing.md %} - -## Step 10. Monitor the cluster - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 11. Scale the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-scale-cluster.md %} - -## Step 12. Use the database - -{% include {{ page.version.version }}/prod-deployment/use-cluster.md %} - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-premises-insecure.md b/src/current/v19.2/deploy-cockroachdb-on-premises-insecure.md deleted file mode 100644 index 26ad93a131b..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-premises-insecure.md +++ /dev/null @@ -1,118 +0,0 @@ ---- -title: Deploy CockroachDB On-Premises (Insecure) -summary: Learn how to manually deploy an insecure, multi-node CockroachDB cluster on multiple machines. -toc: true -ssh-link: https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 ---- - - - -This tutorial shows you how to manually deploy an insecure multi-node CockroachDB cluster on multiple machines, using [HAProxy](http://www.haproxy.org/) load balancers to distribute client traffic. - -{{site.data.alerts.callout_danger}}If you plan to use CockroachDB in production, we strongly recommend using a secure cluster instead. Select Secure above for instructions.{{site.data.alerts.end}} - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/insecure-requirements.md %} - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/insecure-recommendations.md %} - -## Step 1. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 2. Start nodes - -{% include {{ page.version.version }}/prod-deployment/insecure-start-nodes.md %} - -## Step 3. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-initialize-cluster.md %} - -## Step 4. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-test-cluster.md %} - -## Step 5. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - {{site.data.alerts.callout_success}}With a single load balancer, client connections are resilient to node failure, but the load balancer itself is a point of failure. It's therefore best to make load balancing resilient as well by using multiple load balancing instances, with a mechanism like floating IPs or DNS to select load balancers for clients.{{site.data.alerts.end}} - -[HAProxy](http://www.haproxy.org/) is one of the most popular open-source TCP load balancers, and CockroachDB includes a built-in command for generating a configuration file that is preset to work with your running cluster, so we feature that tool here. - -1. SSH to the machine where you want to run HAProxy. - -2. Install HAProxy: - - {% include copy-clipboard.html %} - ~~~ shell - $ apt-get install haproxy - ~~~ - -3. Download the [CockroachDB archive](https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz) for Linux, and extract the binary: - - {% include copy-clipboard.html %} - ~~~ shell - $ curl https://binaries.cockroachdb.com/cockroach-{{ page.release_info.version }}.linux-amd64.tgz \ - | tar -xz - ~~~ - -4. Copy the binary into the `PATH`: - - {% include copy-clipboard.html %} - ~~~ shell - $ cp -i cockroach-{{ page.release_info.version }}.linux-amd64/cockroach /usr/local/bin/ - ~~~ - - If you get a permissions error, prefix the command with `sudo`. - -5. Run the [`cockroach gen haproxy`](cockroach-gen.html) command, specifying the address of any CockroachDB node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach gen haproxy --insecure \ - --host=
      \ - --port=26257 - ~~~ - - {% include {{ page.version.version }}/misc/haproxy.md %} - -6. Start HAProxy, with the `-f` flag pointing to the `haproxy.cfg` file: - - {% include copy-clipboard.html %} - ~~~ shell - $ haproxy -f haproxy.cfg - ~~~ - -7. Repeat these steps for each additional instance of HAProxy you want to run. - -## Step 6. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/insecure-test-load-balancing.md %} - -## Step 7. Monitor the cluster - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 8. Scale the cluster - -{% include {{ page.version.version }}/prod-deployment/insecure-scale-cluster.md %} - -## Step 9. Use the cluster - -{% include {{ page.version.version }}/prod-deployment/use-cluster.md %} - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/deploy-cockroachdb-on-premises.md b/src/current/v19.2/deploy-cockroachdb-on-premises.md deleted file mode 100644 index 017bc5d7dad..00000000000 --- a/src/current/v19.2/deploy-cockroachdb-on-premises.md +++ /dev/null @@ -1,113 +0,0 @@ ---- -title: Deploy CockroachDB On-Premises -summary: Learn how to manually deploy a secure, multi-node CockroachDB cluster on multiple machines. -toc: true -ssh-link: https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 - ---- - - - -This tutorial shows you how to manually deploy a secure multi-node CockroachDB cluster on multiple machines, using [HAProxy](http://www.haproxy.org/) load balancers to distribute client traffic. - -If you are only testing CockroachDB, or you are not concerned with protecting network communication with TLS encryption, you can use an insecure cluster instead. Select **Insecure** above for instructions. - -## Before you begin - -### Requirements - -{% include {{ page.version.version }}/prod-deployment/secure-requirements.md %} - -### Recommendations - -{% include {{ page.version.version }}/prod-deployment/secure-recommendations.md %} - -## Step 1. Synchronize clocks - -{% include {{ page.version.version }}/prod-deployment/synchronize-clocks.md %} - -## Step 2. Generate certificates - -{% include {{ page.version.version }}/prod-deployment/secure-generate-certificates.md %} - -## Step 3. Start nodes - -{% include {{ page.version.version }}/prod-deployment/secure-start-nodes.md %} - -## Step 4. Initialize the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-initialize-cluster.md %} - -## Step 5. Test the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-test-cluster.md %} - -## Step 6. Set up load balancing - -Each CockroachDB node is an equally suitable SQL gateway to your cluster, but to ensure client performance and reliability, it's important to use load balancing: - -- **Performance:** Load balancers spread client traffic across nodes. This prevents any one node from being overwhelmed by requests and improves overall cluster performance (queries per second). - -- **Reliability:** Load balancers decouple client health from the health of a single CockroachDB node. In cases where a node fails, the load balancer redirects client traffic to available nodes. - {{site.data.alerts.callout_success}}With a single load balancer, client connections are resilient to node failure, but the load balancer itself is a point of failure. It's therefore best to make load balancing resilient as well by using multiple load balancing instances, with a mechanism like floating IPs or DNS to select load balancers for clients.{{site.data.alerts.end}} - -[HAProxy](http://www.haproxy.org/) is one of the most popular open-source TCP load balancers, and CockroachDB includes a built-in command for generating a configuration file that is preset to work with your running cluster, so we feature that tool here. - -1. On your local machine, run the [`cockroach gen haproxy`](cockroach-gen.html) command with the `--host` flag set to the address of any node and security flags pointing to the CA cert and the client cert and key: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach gen haproxy \ - --certs-dir=certs \ - --host=
      - ~~~ - - {% include {{ page.version.version }}/misc/haproxy.md %} - -2. Upload the `haproxy.cfg` file to the machine where you want to run HAProxy: - - {% include copy-clipboard.html %} - ~~~ shell - $ scp haproxy.cfg @:~/ - ~~~ - -3. SSH to the machine where you want to run HAProxy. - -4. Install HAProxy: - - {% include copy-clipboard.html %} - ~~~ shell - $ apt-get install haproxy - ~~~ - -5. Start HAProxy, with the `-f` flag pointing to the `haproxy.cfg` file: - - {% include copy-clipboard.html %} - ~~~ shell - $ haproxy -f haproxy.cfg - ~~~ - -6. Repeat these steps for each additional instance of HAProxy you want to run. - -## Step 7. Run a sample workload - -{% include {{ page.version.version }}/prod-deployment/secure-test-load-balancing.md %} - -## Step 8. Monitor the cluster - -{% include {{ page.version.version }}/prod-deployment/monitor-cluster.md %} - -## Step 9. Scale the cluster - -{% include {{ page.version.version }}/prod-deployment/secure-scale-cluster.md %} - -## Step 10. Use the cluster - -{% include {{ page.version.version }}/prod-deployment/use-cluster.md %} - -## See also - -{% include {{ page.version.version }}/prod-deployment/prod-see-also.md %} diff --git a/src/current/v19.2/developer-guide-overview.md b/src/current/v19.2/developer-guide-overview.md deleted file mode 100644 index a306b03562d..00000000000 --- a/src/current/v19.2/developer-guide-overview.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -title: Developer Guide Overview -summary: An overview of common tasks that come up when you build an application using CockroachDB -toc: true ---- - -This guide shows you how to do common tasks that come up when you build an application using CockroachDB. - -For instructions showing how to do a specific task, see the links below. - -## Common Tasks - -- [Install a Postgres Client](install-client-drivers.html) -- [Connect to the Database](connect-to-the-database.html) -- [Insert Data](insert-data.html) -- [Query Data](query-data.html) -- [Update Data](update-data.html) -- [Delete Data](delete-data.html) -- [Run Multi-Statement Transactions](run-multi-statement-transactions.html) -- [Error Handling and Troubleshooting](error-handling-and-troubleshooting.html) -- [Make Queries Fast](make-queries-fast.html) -- ['Hello World' Example Apps](hello-world-example-apps.html) - -## See also - -- [Migrate to CockroachDB](migration-overview.html) -- [Connection parameters](connection-parameters.html) -- [Selection queries](selection-queries.html) -- [Joins](joins.html) -- [Transactions](transactions.html) diff --git a/src/current/v19.2/diagnostics-reporting.md b/src/current/v19.2/diagnostics-reporting.md deleted file mode 100644 index c415cf19896..00000000000 --- a/src/current/v19.2/diagnostics-reporting.md +++ /dev/null @@ -1,66 +0,0 @@ ---- -title: Diagnostics Reporting -summary: Learn about the diagnostic details that get shared with CockroachDB and how to opt out of sharing. -toc: true ---- - -By default, the Admin UI and each node of a CockroachDB cluster share anonymous usage details with Cockroach Labs. These details, which are completely scrubbed of identifiable information, greatly help us understand and improve how the system behaves in real-world scenarios. - -This page summarizes the details that get shared, how to view the details yourself, and how to opt out of sharing. - -{{site.data.alerts.callout_success}} -For insights into your cluster's performance and health, use the built-in [Admin UI](admin-ui-overview.html) or a third-party monitoring tool like [Prometheus](monitor-cockroachdb-with-prometheus.html). -{{site.data.alerts.end}} - -## What gets shared - -When diagnostics reporting is on, each node of a CockroachDB cluster shares anonymized details on an hourly basis, including (but not limited to): - -- Deployment and configuration characteristics, such as size of hardware, [cluster settings](cluster-settings.html) that have been altered from defaults, number of [replication zones](configure-replication-zones.html) configured, etc. -- Usage and cluster health details, such as crashes, unexpected errors, attempts to use unsupported features, types of queries run and their execution characteristics as well as types of schemas used, etc. - -To view the full diagnostics details that a node reports to Cockroach Labs, use the `http://:/_status/diagnostics/local` JSON endpoint. - -{{site.data.alerts.callout_info}} -In all cases, names and other string values are scrubbed and replaced with underscores. Also, the details that get shared may change over time, but as that happens, we will announce the changes in release notes. -{{site.data.alerts.end}} - -## Opt out of diagnostics reporting - -### At cluster initialization - -To make sure that absolutely no diagnostic details are shared, you can set the environment variable `COCKROACH_SKIP_ENABLING_DIAGNOSTIC_REPORTING=true` before starting the first node of the cluster. Note that this works only when set before starting the first node of the cluster. Once the cluster is running, you need to use the `SET CLUSTER SETTING` method described below. - -### After cluster initialization - -To stop sending diagnostic details to Cockroach Labs once a cluster is running, [use the built-in SQL client](cockroach-sql.html) to execute the following [`SET CLUSTER SETTING`](set-cluster-setting.html) statement, which switches the `diagnostics.reporting.enabled` [cluster setting](cluster-settings.html) to `false`: - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING diagnostics.reporting.enabled = false; -~~~ - -This change will not be instantaneous, as it must be propagated to other nodes in the cluster. - -## Check the state of diagnostics reporting - -To check the state of diagnostics reporting, [use the built-in SQL client](cockroach-sql.html) to execute the following [`SHOW CLUSTER SETTING`](show-cluster-setting.html) statement: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CLUSTER SETTING diagnostics.reporting.enabled; -~~~ - -~~~ - diagnostics.reporting.enabled -+-------------------------------+ - false -(1 row) -~~~ - -If the setting is `false`, diagnostics reporting is off; if the setting is `true`, diagnostics reporting is on. - -## See also - -- [Cluster Settings](cluster-settings.html) -- [Start a Node](cockroach-start.html) diff --git a/src/current/v19.2/drop-column.md b/src/current/v19.2/drop-column.md deleted file mode 100644 index 4129215c632..00000000000 --- a/src/current/v19.2/drop-column.md +++ /dev/null @@ -1,93 +0,0 @@ ---- -title: DROP COLUMN -summary: Use the ALTER COLUMN statement to remove columns from tables. -toc: true ---- - -The `DROP COLUMN` [statement](sql-statements.html) is part of `ALTER TABLE` and removes columns from a table. - -{% include {{ page.version.version }}/sql/combine-alter-table-commands.md %} - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_column.html %}
      - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the table. - -## Parameters - - Parameter | Description ------------|------------- - `table_name` | The name of the table with the column you want to drop. - `name` | The name of the column you want to drop.

      When a column with a `CHECK` constraint is dropped, the `CHECK` constraint is also dropped. - `CASCADE` | Drop the column even if objects (such as [views](views.html)) depend on it; drop the dependent objects, as well.

      `CASCADE` does not list objects it drops, so should be used cautiously. However, `CASCADE` will not drop dependent indexes; you must use [`DROP INDEX`](drop-index.html).

      `CASCADE` will drop a column with a foreign key constraint if it is the only column in the reference. - `RESTRICT` | *(Default)* Do not drop the column if any objects (such as [views](views.html)) depend on it. - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Examples - -### Drop columns - -If you no longer want a column in a table, you can drop it. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE orders DROP COLUMN billing_zip; -~~~ - -### Prevent dropping columns with dependent objects (`RESTRICT`) - -If the column has dependent objects, such as [views](views.html), CockroachDB will not drop the column by default; however, if you want to be sure of the behavior you can include the `RESTRICT` clause. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE orders DROP COLUMN customer RESTRICT; -~~~ -~~~ -pq: cannot drop column "customer" because view "customer_view" depends on it -~~~ - -### Drop column and dependent objects (`CASCADE`) - -If you want to drop the column and all of its dependent options, include the `CASCADE` clause. - -{{site.data.alerts.callout_danger}}CASCADE does not list objects it drops, so should be used cautiously.{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE customer_view; -~~~ - -~~~ -+---------------+----------------------------------------------------------------+ -| table_name | create_statement | -+---------------+----------------------------------------------------------------+ -| customer_view | CREATE VIEW customer_view AS SELECT customer FROM store.orders | -+---------------+----------------------------------------------------------------+ -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE orders DROP COLUMN customer CASCADE; -~~~ - -{% include copy-clipboard.html %} -~~~ -> SHOW CREATE customer_view; -~~~ - -~~~ -pq: view "customer_view" does not exist -~~~ - -## See also - -- [`DROP CONSTRAINT`](drop-constraint.html) -- [`DROP INDEX`](drop-index.html) -- [`ALTER TABLE`](alter-table.html) -- [`SHOW JOBS`](show-jobs.html) diff --git a/src/current/v19.2/drop-constraint.md b/src/current/v19.2/drop-constraint.md deleted file mode 100644 index 7ffedc6b28d..00000000000 --- a/src/current/v19.2/drop-constraint.md +++ /dev/null @@ -1,80 +0,0 @@ ---- -title: DROP CONSTRAINT -summary: Use the ALTER CONSTRAINT statement to remove constraints from columns. -toc: true ---- - -The `DROP CONSTRAINT` [statement](sql-statements.html) is part of `ALTER TABLE` and removes Check and Foreign Key constraints from columns. - -{{site.data.alerts.callout_info}} -For information about removing other constraints, see [Constraints: Remove Constraints](constraints.html#remove-constraints). -{{site.data.alerts.end}} - -{% include {{ page.version.version }}/sql/combine-alter-table-commands.md %} - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_constraint.html %}
      - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on the table. - -## Parameters - - Parameter | Description ------------|------------- - `table_name` | The name of the table with the constraint you want to drop. - `name` | The name of the constraint you want to drop. - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Example - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CONSTRAINTS FROM orders; -~~~ -~~~ -+--------+---------------------------+-------------+-----------+----------------+ -| Table | Name | Type | Column(s) | Details | -+--------+---------------------------+-------------+-----------+----------------+ -| orders | fk_customer_ref_customers | FOREIGN KEY | customer | customers.[id] | -| orders | primary | PRIMARY KEY | id | NULL | -+--------+---------------------------+-------------+-----------+----------------+ -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE orders DROP CONSTRAINT fk_customer_ref_customers; -~~~ -~~~ -ALTER TABLE -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CONSTRAINTS FROM orders; -~~~ -~~~ -+--------+---------+-------------+-----------+---------+ -| Table | Name | Type | Column(s) | Details | -+--------+---------+-------------+-----------+---------+ -| orders | primary | PRIMARY KEY | id | NULL | -+--------+---------+-------------+-----------+---------+ -~~~ - -{{site.data.alerts.callout_info}}You cannot drop the primary constraint, which indicates your table's Primary Key.{{site.data.alerts.end}} - -## See also - -- [`ADD CONSTRAINT`](add-constraint.html) -- [`SHOW CONSTRAINTS`](show-constraints.html) -- [`RENAME CONSTRAINT`](rename-constraint.html) -- [`VALIDATE CONSTRAINT`](validate-constraint.html) -- [`DROP COLUMN`](drop-column.html) -- [`DROP INDEX`](drop-index.html) -- [`ALTER TABLE`](alter-table.html) -- [`SHOW JOBS`](show-jobs.html) diff --git a/src/current/v19.2/drop-database.md b/src/current/v19.2/drop-database.md deleted file mode 100644 index bd6839fe2da..00000000000 --- a/src/current/v19.2/drop-database.md +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: DROP DATABASE -summary: The DROP DATABASE statement removes a database and all its objects from a CockroachDB cluster. -toc: true ---- - -The `DROP DATABASE` [statement](sql-statements.html) removes a database and all its objects from a CockroachDB cluster. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -The user must have the `DROP` [privilege](authorization.html#assign-privileges) on the database and on all tables in the database. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_database.html %}
      - -## Parameters - -Parameter | Description -----------|------------ -`IF EXISTS` | Drop the database if it exists; if it does not exist, do not return an error. -`name` | The name of the database you want to drop. You cannot drop a database if it is set as the [current database](sql-name-resolution.html#current-database) or if [`sql_safe_updates = true`](set-vars.html). -`CASCADE` | _(Default)_ Drop all tables and views in the database as well as all objects (such as [constraints](constraints.html) and [views](views.html)) that depend on those tables.

      `CASCADE` does not list objects it drops, so should be used cautiously. -`RESTRICT` | Do not drop the database if it contains any [tables](create-table.html) or [views](create-view.html). - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Examples - -### Drop a database and its objects (`CASCADE`) - -For non-interactive sessions (e.g., client applications), `DROP DATABASE` applies the `CASCADE` option by default, which drops all tables and views in the database as well as all objects (such as [constraints](constraints.html) and [views](views.html)) that depend on those tables. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM db2; -~~~ - -~~~ -+------------+ -| table_name | -+------------+ -| t1 | -| v1 | -+------------+ -(2 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP DATABASE db2; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM db2; -~~~ - -~~~ -pq: database "db2" does not exist -~~~ - -For interactive sessions from the [built-in SQL client](cockroach-sql.html), either the `CASCADE` option must be set explicitly or the `--unsafe-updates` flag must be set when starting the shell. - -### Prevent dropping a non-empty database (`RESTRICT`) - -When a database is not empty, the `RESTRICT` option prevents the database from being dropped: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM db2; -~~~ - -~~~ -+------------+ -| table_name | -+------------+ -| t1 | -| v1 | -+------------+ -(2 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP DATABASE db2 RESTRICT; -~~~ - -~~~ -pq: database "db2" is not empty and CASCADE was not specified -~~~ - -## See also - -- [`CREATE DATABASE`](create-database.html) -- [`SHOW DATABASES`](show-databases.html) -- [`RENAME DATABASE`](rename-database.html) -- [`SET DATABASE`](set-vars.html) -- [`SHOW JOBS`](show-jobs.html) -- [Other SQL Statements](sql-statements.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/drop-index.md b/src/current/v19.2/drop-index.md deleted file mode 100644 index 6483d79b560..00000000000 --- a/src/current/v19.2/drop-index.md +++ /dev/null @@ -1,139 +0,0 @@ ---- -title: DROP INDEX -summary: The DROP INDEX statement removes indexes from tables. -toc: true ---- - -The `DROP INDEX` [statement](sql-statements.html) removes indexes from tables. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_index.html %}
      - -## Required privileges - -The user must have the `CREATE` [privilege](authorization.html#assign-privileges) on each specified table. - -## Parameters - - Parameter | Description ------------|------------- - `IF EXISTS` | Drop the named indexes if they exist; if they do not exist, do not return an error. - `table_name` | The name of the table with the index you want to drop. Find table names with [`SHOW TABLES`](show-tables.html). - `index_name` | The name of the index you want to drop. Find index names with [`SHOW INDEX`](show-index.html).

      You cannot drop a table's `primary` index. - `CASCADE` | Drop all objects (such as [constraints](constraints.html)) that depend on the indexes. `CASCADE` does not list objects it drops, so should be used cautiously.

      New in v19.2: To drop an index created with [`CREATE UNIQUE INDEX`](create-index.html#unique-indexes), you do not need to use `CASCADE`. - `RESTRICT` | _(Default)_ Do not drop the indexes if any objects (such as [constraints](constraints.html)) depend on them. - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Examples - -### Remove an index (no dependencies) - -{% include copy-clipboard.html %} -~~~ sql -> SHOW INDEX FROM tl; -~~~ - -~~~ -+------------+-------------+------------+--------------+-------------+-----------+---------+----------+ -| table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit | -+------------+-------------+------------+--------------+-------------+-----------+---------+----------+ -| t1 | primary | false | 1 | id | ASC | false | false | -| t1 | t1_name_idx | true | 1 | name | ASC | false | false | -| t1 | t1_name_idx | true | 2 | id | ASC | false | true | -+------------+-------------+------------+--------------+-------------+-----------+---------+----------+ -(3 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP INDEX t1@t1_name_idx; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW INDEX FROM tbl; -~~~ - -~~~ -+------------+------------+------------+--------------+-------------+-----------+---------+----------+ -| table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit | -+------------+------------+------------+--------------+-------------+-----------+---------+----------+ -| t1 | primary | false | 1 | id | ASC | false | false | -+------------+------------+------------+--------------+-------------+-----------+---------+----------+ -(1 row) -~~~ - -### Remove an index and dependent objects with `CASCADE` - -{{site.data.alerts.callout_danger}}CASCADE drops all dependent objects without listing them, which can lead to inadvertent and difficult-to-recover losses. To avoid potential harm, we recommend dropping objects individually in most cases.{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> SHOW INDEX FROM orders; -~~~ - -~~~ -+------------+---------------------------------------------+------------+--------------+-------------+-----------+---------+----------+ -| table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit | -+------------+---------------------------------------------+------------+--------------+-------------+-----------+---------+----------+ -| orders | primary | false | 1 | id | ASC | false | false | -| orders | orders_auto_index_fk_customer_ref_customers | true | 1 | customer | ASC | false | false | -| orders | orders_auto_index_fk_customer_ref_customers | true | 2 | id | ASC | false | true | -+------------+---------------------------------------------+------------+--------------+-------------+-----------+---------+----------+ -(3 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP INDEX orders_auto_index_fk_customer_ref_customers; -~~~ - -~~~ -pq: index "orders_auto_index_fk_customer_ref_customers" is in use as a foreign key constraint -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CONSTRAINTS FROM orders; -~~~ - -~~~ -+------------+---------------------------+-----------------+--------------------------------------------------+-----------+ -| table_name | constraint_name | constraint_type | details | validated | -+------------+---------------------------+-----------------+--------------------------------------------------+-----------+ -| orders | fk_customer_ref_customers | FOREIGN KEY | FOREIGN KEY (customer) REFERENCES customers (id) | true | -| orders | primary | PRIMARY KEY | PRIMARY KEY (id ASC) | true | -+------------+---------------------------+-----------------+--------------------------------------------------+-----------+ -(2 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP INDEX orders_auto_index_fk_customer_ref_customers CASCADE; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CONSTRAINTS FROM orders; -~~~ - -~~~ -+------------+-----------------+-----------------+----------------------+-----------+ -| table_name | constraint_name | constraint_type | details | validated | -+------------+-----------------+-----------------+----------------------+-----------+ -| orders | primary | PRIMARY KEY | PRIMARY KEY (id ASC) | true | -+------------+-----------------+-----------------+----------------------+-----------+ -(1 row) -~~~ - -## See Also - -- [Indexes](indexes.html) -- [Online Schema Changes](online-schema-changes.html) -- [`SHOW JOBS`](show-jobs.html) diff --git a/src/current/v19.2/drop-role.md b/src/current/v19.2/drop-role.md deleted file mode 100644 index 202e79dd27c..00000000000 --- a/src/current/v19.2/drop-role.md +++ /dev/null @@ -1,70 +0,0 @@ ---- -title: DROP ROLE (Enterprise) -summary: The DROP ROLE statement removes one or more SQL roles. -toc: true ---- - -The `DROP ROLE` [statement](sql-statements.html) removes one or more SQL roles. - -{{site.data.alerts.callout_info}}DROP ROLE is an enterprise-only feature.{{site.data.alerts.end}} - - -## Considerations - -- The `admin` role cannot be dropped, and `root` must always be a member of `admin`. -- A role cannot be dropped if it has privileges. Use [`REVOKE`](revoke.html) to remove privileges. - -## Required privileges - -Roles can only be dropped by super users, i.e., members of the `admin` role. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_role.html %}
      - - -## Parameters - - Parameter | Description -------------|-------------- -`name` | The name of the role to remove. To remove multiple roles, use a comma-separate list of roles.

      You can use [`SHOW ROLES`](show-roles.html) to find the names of roles. - -## Example - -In this example, first check a role's privileges. Then, revoke the role's privileges and remove the role. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW GRANTS ON documents FOR dev_ops; -~~~ -~~~ -+------------+--------+-----------+---------+------------+ -| Database | Schema | Table | User | Privileges | -+------------+--------+-----------+---------+------------+ -| jsonb_test | public | documents | dev_ops | INSERT | -+------------+--------+-----------+---------+------------+ -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> REVOKE INSERT ON documents FROM dev_ops; -~~~ - -{{site.data.alerts.callout_info}}All of a role's privileges must be revoked before the role can be dropped.{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> DROP ROLE dev_ops; -~~~ -~~~ -DROP ROLE 1 -~~~ - -## See also - -- [Authorization](authorization.html) -- [`CREATE ROLE` (Enterprise)](create-role.html) -- [`SHOW ROLES`](show-roles.html) -- [`GRANT`](grant.html) -- [`SHOW GRANTS`](show-grants.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/drop-sequence.md b/src/current/v19.2/drop-sequence.md deleted file mode 100644 index f055f4844be..00000000000 --- a/src/current/v19.2/drop-sequence.md +++ /dev/null @@ -1,102 +0,0 @@ ---- -title: DROP SEQUENCE -summary: -toc: true ---- - -The `DROP SEQUENCE` [statement](sql-statements.html) removes a sequence from a database. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -The user must have the `DROP` [privilege](authorization.html#assign-privileges) on the specified sequence(s). - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_sequence.html %}
      - -## Parameters - - - - Parameter | Description ------------|------------ -`IF EXISTS` | Drop the sequence only if it exists; if it does not exist, do not return an error. -`sequence_name` | The name of the sequence you want to drop. Find the sequence name with `SHOW CREATE` on the table that uses the sequence. -`RESTRICT` | _(Default)_ Do not drop the sequence if any objects (such as [constraints](constraints.html) and tables) use it. -`CASCADE` | Not yet implemented. Currently, you can only drop a sequence if nothing depends on it. - - - -## Examples - -### Remove a sequence (no dependencies) - -In this example, other objects do not depend on the sequence being dropped. - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.sequences; -~~~ -~~~ -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -| sequence_catalog | sequence_schema | sequence_name | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value | maximum_value | increment | cycle_option | -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -| def | db_2 | test_4 | INT | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO | -| def | test_db | customer_seq | INT | 64 | 2 | 0 | 101 | 1 | 9223372036854775807 | 2 | NO | -| def | test_db | desc_customer_list | INT | 64 | 2 | 0 | 1000 | -9223372036854775808 | -1 | -2 | NO | -| def | test_db | test_sequence3 | INT | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO | -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -(4 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP SEQUENCE customer_seq; -~~~ -~~~ -DROP SEQUENCE -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.sequences -~~~ -~~~ -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -| sequence_catalog | sequence_schema | sequence_name | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value | maximum_value | increment | cycle_option | -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -| def | db_2 | test_4 | INT | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO | -| def | test_db | desc_customer_list | INT | 64 | 2 | 0 | 1000 | -9223372036854775808 | -1 | -2 | NO | -| def | test_db | test_sequence3 | INT | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO | -+------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+----------------------+---------------------+-----------+--------------+ -(4 rows) -~~~ - - - - -## See also -- [`CREATE SEQUENCE`](create-sequence.html) -- [`ALTER SEQUENCE`](alter-sequence.html) -- [`RENAME SEQUENCE`](rename-sequence.html) -- [`SHOW SEQUENCES`](show-sequences.html) -- [Functions and Operators](functions-and-operators.html) -- [Other SQL Statements](sql-statements.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/drop-table.md b/src/current/v19.2/drop-table.md deleted file mode 100644 index 5e0f485a9ed..00000000000 --- a/src/current/v19.2/drop-table.md +++ /dev/null @@ -1,204 +0,0 @@ ---- -title: DROP TABLE -summary: The DROP TABLE statement removes a table and all its indexes from a database. -toc: true ---- - -The `DROP TABLE` [statement](sql-statements.html) removes a table and all its indexes from a database. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -The user must have the `DROP` [privilege](authorization.html#assign-privileges) on the specified table(s). If `CASCADE` is used, the user must have the privileges required to drop each dependent object as well. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_table.html %}
      - -## Parameters - -Parameter | Description -----------|------------ -`IF EXISTS` | Drop the table if it exists; if it does not exist, do not return an error. -`table_name` | A comma-separated list of table names. To find table names, use [`SHOW TABLES`](show-tables.html). -`CASCADE` | Drop all objects (such as [constraints](constraints.html) and [views](views.html)) that depend on the table.

      `CASCADE` does not list objects it drops, so should be used cautiously. -`RESTRICT` | _(Default)_ Do not drop the table if any objects (such as [constraints](constraints.html) and [views](views.html)) depend on it. - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Examples - -{% include {{page.version.version}}/sql/movr-statements.md %} - -### Remove a table (no dependencies) - -In this example, other objects do not depend on the table being dropped. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM movr; -~~~ - -~~~ - table_name -+----------------------------+ - promo_codes - rides - user_promo_codes - users - vehicle_location_histories - vehicles -(6 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP TABLE promo_codes; -~~~ - -~~~ -DROP TABLE -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM movr; -~~~ - -~~~ - table_name -+----------------------------+ - rides - user_promo_codes - users - vehicle_location_histories - vehicles -(5 rows) -~~~ - -### Remove a table and dependent objects with `CASCADE` - -In this example, a [foreign key](foreign-key.html) from a different table references the table being dropped. Therefore, it's only possible to drop the table while simultaneously dropping the dependent foreign key constraint using `CASCADE`. - -{{site.data.alerts.callout_danger}}CASCADE drops all dependent objects without listing them, which can lead to inadvertent and difficult-to-recover losses. To avoid potential harm, we recommend dropping objects individually in most cases.{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM movr; -~~~ - -~~~ - table_name -+----------------------------+ - rides - user_promo_codes - users - vehicle_location_histories - vehicles -(5 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP TABLE users; -~~~ - -~~~ -pq: "users" is referenced by foreign key from table "vehicles" -~~~ - -To see how `users` is referenced from `vehicles`, you can use the [`SHOW CREATE`](show-create.html) statement. `SHOW CREATE` shows how the columns in a table are created, including data types, default values, indexes, and constraints. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE TABLE vehicles; -~~~ - -~~~ - table_name | create_statement -+------------+-----------------------------------------------------------------------------------------------+ - vehicles | CREATE TABLE vehicles ( - | id UUID NOT NULL, - | city VARCHAR NOT NULL, - | type VARCHAR NULL, - | owner_id UUID NULL, - | creation_time TIMESTAMP NULL, - | status VARCHAR NULL, - | current_location VARCHAR NULL, - | ext JSONB NULL, - | CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - | CONSTRAINT fk_city_ref_users FOREIGN KEY (city, owner_id) REFERENCES users(city, id), - | INDEX vehicles_auto_index_fk_city_ref_users (city ASC, owner_id ASC), - | FAMILY "primary" (id, city, type, owner_id, creation_time, status, current_location, ext) - | ) -(1 row) -~~~ - - -{% include copy-clipboard.html %} -~~~sql -> DROP TABLE users CASCADE; -~~~ - -~~~ -DROP TABLE -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW TABLES FROM movr; -~~~ - -~~~ - table_name -+----------------------------+ - rides - user_promo_codes - vehicle_location_histories - vehicles -(4 rows) -~~~ - -Use a `SHOW CREATE TABLE` statement to verify that the foreign key constraint has been removed from `vehicles`. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CREATE TABLE vehicles; -~~~ - -~~~ - table_name | create_statement -+------------+-----------------------------------------------------------------------------------------------+ - vehicles | CREATE TABLE vehicles ( - | id UUID NOT NULL, - | city STRING NOT NULL, - | type STRING NULL, - | owner_id UUID NULL, - | creation_time TIMESTAMP NULL, - | status STRING NULL, - | current_location STRING NULL, - | ext JSONB NULL, - | CONSTRAINT "primary" PRIMARY KEY (city ASC, id ASC), - | INDEX vehicles_auto_index_fk_city_ref_users (city ASC, owner_id ASC), - | FAMILY "primary" (id, city, type, owner_id, creation_time, status, current_location, ext) - | ) -(1 row) -~~~ - -## See also - -- [`ALTER TABLE`](alter-table.html) -- [`CREATE TABLE`](create-table.html) -- [`INSERT`](insert.html) -- [`RENAME TABLE`](rename-table.html) -- [`SHOW COLUMNS`](show-columns.html) -- [`SHOW TABLES`](show-tables.html) -- [`UPDATE`](update.html) -- [`DELETE`](delete.html) -- [`DROP INDEX`](drop-index.html) -- [`DROP VIEW`](drop-view.html) -- [`SHOW JOBS`](show-jobs.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/drop-user.md b/src/current/v19.2/drop-user.md deleted file mode 100644 index 88ad650cd5d..00000000000 --- a/src/current/v19.2/drop-user.md +++ /dev/null @@ -1,63 +0,0 @@ ---- -title: DROP USER -summary: The DROP USER statement removes one or more SQL users. -toc: true ---- - -The `DROP USER` [statement](sql-statements.html) removes one or more SQL users. - -## Required privileges - -The user must have the `DELETE` [privilege](authorization.html#assign-privileges) on the `system.users` table. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_user.html %}
      - -## Parameters - - Parameter | Description ------------|------------- -`user_name` | The username of the user to remove. To remove multiple users, use a comma-separate list of usernames.

      You can use [`SHOW USERS`](show-users.html) to find usernames. - -## Example - -All of a user's privileges must be revoked before the user can be dropped. - -In this example, first check a user's privileges. Then, revoke the user's privileges before removing the user. - -{% include copy-clipboard.html %} -~~~ sql -> SHOW GRANTS ON test.customers FOR mroach; -~~~ - -~~~ -+-----------+--------+------------+ -| Table | User | Privileges | -+-----------+--------+------------+ -| customers | mroach | CREATE | -| customers | mroach | INSERT | -| customers | mroach | UPDATE | -+-----------+--------+------------+ -(3 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> REVOKE CREATE,INSERT,UPDATE ON test.customers FROM mroach; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP USER mroach; -~~~ - -## See also - -- [`CREATE USER`](create-user.html) -- [`ALTER USER`](alter-user.html) -- [`SHOW USERS`](show-users.html) -- [`GRANT`](grant.html) -- [`SHOW GRANTS`](show-grants.html) -- [Create Security Certificates](cockroach-cert.html) -- [Other SQL Statements](sql-statements.html) diff --git a/src/current/v19.2/drop-view.md b/src/current/v19.2/drop-view.md deleted file mode 100644 index bd7fb919008..00000000000 --- a/src/current/v19.2/drop-view.md +++ /dev/null @@ -1,131 +0,0 @@ ---- -title: DROP VIEW -summary: The DROP VIEW statement removes a view from a database. -toc: true ---- - -The `DROP VIEW` [statement](sql-statements.html) removes a [view](views.html) from a database. - -{% include {{{ page.version.version }}/misc/schema-change-stmt-note.md %} - -## Required privileges - -The user must have the `DROP` [privilege](authorization.html#assign-privileges) on the specified view(s). If `CASCADE` is used to drop dependent views, the user must have the `DROP` privilege on each dependent view as well. - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/drop_view.html %}
      - -## Parameters - - Parameter | Description -----------|------------- - `IF EXISTS` | Drop the view if it exists; if it does not exist, do not return an error. - `table_name` | A comma-separated list of view names. To find view names, use:

      `SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';` - `CASCADE` | Drop other views that depend on the view being dropped.

      `CASCADE` does not list views it drops, so should be used cautiously. - `RESTRICT` | _(Default)_ Do not drop the view if other views depend on it. - -## Examples - -### Remove a view (no dependencies) - -In this example, other views do not depend on the view being dropped. - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.tables WHERE table_type = 'VIEW'; -~~~ - -~~~ -+---------------+-------------------+--------------------+------------+---------+ -| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | VERSION | -+---------------+-------------------+--------------------+------------+---------+ -| def | bank | user_accounts | VIEW | 1 | -| def | bank | user_emails | VIEW | 1 | -+---------------+-------------------+--------------------+------------+---------+ -(2 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP VIEW bank.user_emails; -~~~ - -~~~ -DROP VIEW -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.tables WHERE table_type = 'VIEW'; -~~~ - -~~~ -+---------------+-------------------+--------------------+------------+---------+ -| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | VERSION | -+---------------+-------------------+--------------------+------------+---------+ -| def | bank | user_accounts | VIEW | 1 | -+---------------+-------------------+--------------------+------------+---------+ -(1 row) -~~~ - -### Remove a view (with dependencies) - -In this example, another view depends on the view being dropped. Therefore, it's only possible to drop the view while simultaneously dropping the dependent view using `CASCADE`. - -{{site.data.alerts.callout_danger}}CASCADE drops all dependent views without listing them, which can lead to inadvertent and difficult-to-recover losses. To avoid potential harm, we recommend dropping objects individually in most cases.{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.tables WHERE table_type = 'VIEW'; -~~~ - -~~~ -+---------------+-------------------+--------------------+------------+---------+ -| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | VERSION | -+---------------+-------------------+--------------------+------------+---------+ -| def | bank | user_accounts | VIEW | 1 | -| def | bank | user_emails | VIEW | 1 | -+---------------+-------------------+--------------------+------------+---------+ -(2 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> DROP VIEW bank.user_accounts; -~~~ - -~~~ -pq: cannot drop view "user_accounts" because view "user_emails" depends on it -~~~ - -{% include copy-clipboard.html %} -~~~sql -> DROP VIEW bank.user_accounts CASCADE; -~~~ - -~~~ -DROP VIEW -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM information_schema.tables WHERE table_type = 'VIEW'; -~~~ - -~~~ -+---------------+-------------------+--------------------+------------+---------+ -| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | VERSION | -+---------------+-------------------+--------------------+------------+---------+ -| def | bank | create_test | VIEW | 1 | -+---------------+-------------------+--------------------+------------+---------+ -(1 row) -~~~ - -## See also - -- [Views](views.html) -- [`CREATE VIEW`](create-view.html) -- [`SHOW CREATE`](show-create.html) -- [`ALTER VIEW`](alter-view.html) -- [Online Schema Changes](online-schema-changes.html) diff --git a/src/current/v19.2/enable-node-map.md b/src/current/v19.2/enable-node-map.md deleted file mode 100644 index c2b2fe9864a..00000000000 --- a/src/current/v19.2/enable-node-map.md +++ /dev/null @@ -1,191 +0,0 @@ ---- -title: Enable the Node Map -summary: Learn how to enable the node map in the Admin UI. -toc: true ---- - -{{site.data.alerts.callout_info}} -On a secure cluster, this area of the Admin UI can only be accessed by an `admin` user. See [Admin UI access](admin-ui-overview.html#admin-ui-access). -{{site.data.alerts.end}} - -The **Node Map** visualizes the geographical configuration of a multi-regional cluster by plotting the node localities on a world map. The **Node Map** also provides real-time cluster metrics, with the ability to drill down to individual nodes to monitor and troubleshoot the cluster health and performance. - -This page walks you through the process of setting up and enabling the **Node Map**. - -{{site.data.alerts.callout_info}}The Node Map is an enterprise-only feature. However, you can request a trial license to try it out. {{site.data.alerts.end}} - -CockroachDB Admin UI - -## Set up and enable the Node Map - -To enable the **Node Map**, you need to start the cluster with the correct `--locality` flags and assign the latitudes and longitudes for each locality. - -{{site.data.alerts.callout_info}}The Node Map will not be displayed until all nodes are started with the correct --locality flags and all localities are assigned the corresponding latitudes and longitudes. {{site.data.alerts.end}} - -Consider a scenario of a four-node geo-distributed cluster with the following configuration: - -| Node | Region | Datacenter | -| ------ | ------ | ------ | -| Node1 | us-east-1 | us-east-1a | -| Node2 | us-east-1 | us-east-1b | -| Node3 | us-west-1 | us-west-1a | -| Node4 | eu-west-1 | eu-west-1a | - -### Step 1. Start the nodes with the correct `--locality` flags - -To start a new cluster with the correct `--locality` flags: - -Start Node 1: - -{% include copy-clipboard.html %} -~~~ -$ cockroach start \ ---insecure \ ---locality=region=us-east-1,datacenter=us-east-1a \ ---advertise-addr= \ ---cache=.25 \ ---max-sql-memory=.25 \ ---join=,,, -~~~ - -Start Node 2: - -{% include copy-clipboard.html %} -~~~ -$ cockroach start \ ---insecure \ ---locality=region=us-east-1,datacenter=us-east-1b \ ---advertise-addr= \ ---cache=.25 \ ---max-sql-memory=.25 \ ---join=,,, -~~~ - -Start Node 3: - -{% include copy-clipboard.html %} -~~~ -$ cockroach start \ ---insecure \ ---locality=region=us-west-1,datacenter=us-west-1a \ ---advertise-addr= \ ---cache=.25 \ ---max-sql-memory=.25 \ ---join=,,, -~~~ - -Start Node 4: - -{% include copy-clipboard.html %} -~~~ -$ cockroach start \ ---insecure \ ---locality=region=eu-west-1,datacenter=eu-west-1a \ ---advertise-addr= \ ---cache=.25 \ ---max-sql-memory=.25 \ ---join=,,, -~~~ - -Use the [`cockroach init`](cockroach-init.html) command to perform a one-time initialization of the cluster: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach init --insecure --host=
      -~~~ - -[Access the Admin UI](admin-ui-access-and-navigate.html#access-the-admin-ui). The following page is displayed: - -CockroachDB Admin UI - -### Step 2. [Set the enterprise license](enterprise-licensing.html) and refresh the Admin UI - -The following page should be displayed: - -CockroachDB Admin UI - -### Step 3. Set the latitudes and longitudes for the localities - -Launch the built-in SQL client: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure --host=
      -~~~ - -Insert the approximate latitudes and longitudes of each region into the `system.locations` table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO system.locations VALUES - ('region', 'us-east-1', 37.478397, -76.453077), - ('region', 'us-west-1', 38.837522, -120.895824), - ('region', 'eu-west-1', 53.142367, -7.692054); -~~~ - -{{site.data.alerts.callout_info}}The Node Map will not be displayed until all regions are assigned the corresponding latitudes and longitudes. {{site.data.alerts.end}} - -For the latitudes and longitudes of AWS, Azure, and Google Cloud regions, see [Location Coordinates for Reference](#location-coordinates-for-reference). - -### Step 4. View the Node Map - -[Open the **Overview page**](admin-ui-access-and-navigate.html) and select **Node Map** from the **View** drop-down menu. The **Node Map** will be displayed: - -CockroachDB Admin UI - -### Step 5. Navigate the Node Map - -Let's say you want to navigate to Node 2, which is in datacenter `us-east-1a` in the `us-east-1` region: - -1. Click on the map component marked as **region=us-east-1** on the **Node Map**. The datacenter view is displayed. -2. Click on the datacenter component marked as **datacenter=us-east-1a**. The individual node components are displayed. -3. To navigate back to the cluster view, either click on **Cluster** in the bread-crumb trail at the top of the **Node Map**, or click **Up to region=us-east-1** and then click **Up to Cluster** in the lower left-hand side of the **Node Map**. - -CockroachDB Admin UI - -## Troubleshoot the Node Map - -### Node Map not displayed - -The **Node Map** will not be displayed until all nodes have localities and are assigned the corresponding latitudes and longitudes. To verify if you have assigned localities as well as latitude and longitudes assigned to all nodes, navigate to the Localities debug page (`https://
      :8080/#/reports/localities`) in the Admin UI. - -The Localities debug page displays the following: - -- Localities configuration that you set up while starting the nodes with the `--locality` flags. -- Nodes corresponding to each locality. -- Latitude and longitude coordinates for each locality/node. - -On the page, ensure that every node has a locality as well as latitude/longitude coordinates assigned to them. - -### Node Map not displayed for all locality levels - -The **Node Map** is displayed only for the locality levels that have latitude/longitude coordinates assigned to them: - -- If you assign the latitude/longitude coordinates at the region level, the **Node Map** shows the regions on the world map. However, when you drill down to the datacenter and further to the individual nodes, the world map disappears and the datacenters/nodes are plotted in a circular layout. -- If you assign the latitude/longitude coordinates at the datacenter level, the **Node Map** shows the regions with single datacenters at the same location assigned to the datacenter, while regions with multiple datacenters are shown at the center of the datacenter coordinates in the region. When you drill down to the datacenter levels, the **Node Map** shows the datacenter at their assigned coordinates. Further drilling down to individual nodes shows the nodes in a circular layout. - -[Assign latitude/longitude coordinates](#step-3-set-the-latitudes-and-longitudes-for-the-localities) at the locality level that you want to view on the **Node Map**. - -## Known limitations - -### Unable to assign latitude/longitude coordinates to localities - -{% include {{ page.version.version }}/known-limitations/node-map.md %} - -### **Capacity Used** value displayed is more than configured Capacity - -{% include {{ page.version.version }}/misc/available-capacity-metric.md %} - -## Location coordinates for reference - -### AWS locations - -{% include {{ page.version.version }}/misc/aws-locations.md %} - -### Azure locations - -{% include {{ page.version.version }}/misc/azure-locations.md %} - -### Google Cloud locations - -{% include {{ page.version.version }}/misc/gce-locations.md %} diff --git a/src/current/v19.2/encryption.md b/src/current/v19.2/encryption.md deleted file mode 100644 index bcaacb7ee9a..00000000000 --- a/src/current/v19.2/encryption.md +++ /dev/null @@ -1,230 +0,0 @@ ---- -title: Encryption -summary: Learn about the encryption features for secure CockroachDB clusters. -toc: true ---- - -Data encryption and decryption is the process of transforming plaintext data to cipher-text and vice versa using a key or password. - -## Encryption in flight - -CockroachDB uses TLS 1.2 for inter-node and client-node [authentication](authentication.html) as well as setting up a secure communication channel. Once the secure channel is set up, all inter-node and client-node network communication is encrypted using a [shared encryption key](https://en.wikipedia.org/wiki/Transport_Layer_Security) as per the TLS 1.2 protocol. This feature is enabled by default for all secure clusters and needs no additional configuration. - -## Encryption at Rest (Enterprise) - -Encryption at Rest provides transparent encryption of a node's data on the local disk. It allows encryption of all files on disk using [AES](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard) in [counter mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)), with all key -sizes allowed. - -Encryption is performed in the [storage layer](architecture/storage-layer.html) and configured per store. -All files used by the store, regardless of contents, are encrypted with the desired algorithm. - -To allow arbitrary rotation schedules and ensure security of the keys, we use two layers of keys: - -- **Store keys** are provided by the user in a file. They are used to encrypt the list of data keys (see below). This is known as a **key encryption key**: its only purpose is to encrypt other keys. Store keys are never persisted by CockroachDB. Since very little data is encrypted using this key, it can have a very long lifetime without risk of reuse. - -- **Data keys** are automatically generated by CockroachDB. They are used to encrypt all files on disk. This is known as a **data encryption key**. Data keys are persisted in a key registry file, encrypted using the store key. The key has a short lifetime to avoid reuse. - -Store keys are specified at node startup by passing a path to a locally readable file. The file must contain 32 bytes (the key ID) followed by the key (16, 24, or 32 bytes). The size of the key dictates the version of AES to use (AES-128, AES-192, or AES-256). For an example showing how to create a store key, see [Generating key files](#generating-store-key-files) below. - -Also during node startup, CockroachDB uses a data key with the same length as the store key. If encryption has just been enabled, -the key size has changed, or the data key is too old (default lifetime is one week), CockroachDB generates a new data key. - -Any new file created by the store uses the currently-active data key. All data keys (both active and previous) are stored in a key registry file and encrypted with the active store key. - -After startup, if the active data key is too old, CockroachDB generates a new data key and marks it as active, using it for all further encryption. - -CockroachDB does not currently force re-encryption of older files but instead relies on normal RocksDB churn to slowly rewrite all data with the desired encryption. - -### Rotating keys - -Key rotation is necessary for Encryption at Rest for multiple reasons: - -- To prevent key reuse with the same encryption parameters (after encrypting many files). -- To reduce the risk of key exposure. - -Store keys are specified by the user and must be rotated by specifying different keys. -This is done by setting the `key` parameter of the `--enterprise-encryption` flag to the path to the new key, -and `old-key` to the previously-used key. - -Data keys will automatically be rotated at startup if any of the following conditions are met: - -- The active store key has changed. -- The encryption type has changed (different key size, or plaintext to/from encryption). -- The current data key is `rotation-period` old or more. - -Data keys will automatically be rotated at runtime if the current data key is `rotation-period` old or more. - -Once rotated, an old store key cannot be made the active key again. - -Upon store key rotation the data keys registry is decrypted using the old key and encrypted with the new -key. The newly-generated data key is used to encrypt all new data from this point on. - -### Changing encryption type - -The user can change the encryption type from plaintext to encryption, between different encryption algorithms (using various key sizes), or from encryption to plaintext. - -When changing the encryption type to plaintext, the data key registry is no longer encrypted and all previous data keys are readable by anyone. All data on the store is effectively readable. - -When changing from plaintext to encryption, it will take some time for all data to eventually be re-written and encrypted. - -### Recommendations - -There are a number of considerations to keep in mind when running with encryption. - -#### Deployment configuration - -To prevent key leakage, production deployments should: - -* Use encrypted swap, or disable swap entirely. -* Disable core files. - -CockroachDB attempts to disable core files at startup when encryption is requested, but it may fail. - -#### Key handling - -Key management is the most dangerous aspect of encryption. The following rules should be kept in mind: - -* Make sure that only the UNIX user running the `cockroach` process has access to the keys. -* Do not store the keys on the same partition/drive as the CockroachDB data. It is best to load keys at run time from a separate system (e.g., [Keywhiz](https://square.github.io/keywhiz/), Vault). -* Rotate store keys frequently (every few weeks to months). -* Keep the data key rotation period low (default is one week). - -#### Other recommendations - -A few other recommendations apply for best security practices: - -* Do not switch from encrypted to plaintext, this leaks data keys. When plaintext is selected, all previously encrypted data must be considered reachable. -* Do not copy the encrypted files, as the data keys are not easily available. -* If encryption is desired, start a node with it enabled from the first run, without ever running in plaintext. - -{{site.data.alerts.callout_danger}} -Note that backups taken with the [`BACKUP`](backup.html) statement **are not encrypted** even if Encryption at Rest is enabled. Encryption at Rest only applies to the CockroachDB node's data on the local disk. If you want encrypted backups, you will need to encrypt your backup files using your preferred encryption method. -{{site.data.alerts.end}} - -### Examples - -#### Generating store key files - -Cockroach determines which encryption algorithm to use based on the size of the key file. -The key file must contain random data making up the key ID (32 bytes) and the actual key (16, 24, or 32 -bytes depending on the encryption algorithm). - -| Algorithm | Key size | Key file size | -|-|-|-| -| AES-128 | 128 bits (16 bytes) | 48 bytes | -| AES-192 | 192 bits (24 bytes) | 56 bytes | -| AES-256 | 256 bits (32 bytes) | 64 bytes | - -Generating a key file can be done using the `cockroach` CLI: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach gen encryption-key -s 128 /path/to/my/aes-128.key -~~~ - -Or the equivalent [openssl](https://www.openssl.org/docs/man1.1.1/man1/openssl.html) CLI command: - -{% include copy-clipboard.html %} -~~~ shell -$ openssl rand -out /path/to/my/aes-128.key 48 -~~~ - -#### Starting a node with encryption - -Encryption at Rest is configured at node start time using the `--enterprise-encryption` command line flag. -The flag specifies the encryption options for one of the stores on the node. If multiple stores exist, -the flag must be specified for each store. - -The flag takes the form: `--enterprise-encryption=path=,key=,old-key=,rotation-period=`. - -The allowed components in the flag are: - -| Component | Requirement | Description | -|-|-|-| -| `path` | Required | Path of the store to apply encryption to. | -| `key` | Required | Path to the key file to encrypt data with, or `plain` for plaintext. | -| `old-key` | Required | Path to the key file the data is encrypted with, or `plain` for plaintext. | -| `rotation-period` | Optional | How often data keys should be automatically rotated. Default: one week. | - -The `key` and `old-key` components must **always** be specified. They allow for transitions between -encryption algorithms, and between plaintext and encrypted. - -Starting a node for the first time using AES-128 encryption can be done using: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --store=cockroach-data --enterprise-encryption=path=cockroach-data,key=/path/to/my/aes-128.key,old-key=plain -~~~ - -{{site.data.alerts.callout_danger}} -Once specified for a given store, the `--enterprise-encryption` flag must always be present. -{{site.data.alerts.end}} - -#### Checking encryption status - -Encryption status can be seen on the node's stores report, reachable through: `http(s)://nodeaddress:8080/#/reports/stores/local` (or replace `local` with the node ID). For example, if you are running a [local cluster](secure-a-cluster.html), you can see the node's stores report at `https://localhost:8080/#/reports/stores/local`. - -The report shows encryption status for all stores on the selected node, including: - -* Encryption algorithm. -* Active store key information. -* Active data key information. -* The fraction of files/bytes encrypted using the active data key. - -CockroachDB relies on RocksDB compactions to write new files using the latest encryption key. It may take several days for all files to be replaced. Some files are only rewritten at startup, and some keep older copies around, requiring multiple restarts. You can force RocksDB compaction with the `cockroach debug compact` command (the node must first be [stopped](cockroach-quit.html)). - -Information about keys is written to [the logs](debug-and-error-logs.html), including: - -* Active/old key information at startup. -* New key information after data key rotation. - -Alternatively, you can use the [`cockroach debug encryption-active-key`](cockroach-debug-encryption-active-key.html) command to view information about a store's encryption algorithm and store key. - -#### Changing encryption algorithm or keys - -Encryption type and keys can be changed at any time by restarting the node. -To change keys or encryption type, the `key` component of the `--enterprise-encryption` flag is set to the new key, -while the key previously used must be specified in the `old-key` component. - -For example, we can switch from AES-128 to AES-256 using: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach start --store=cockroach-data --enterprise-encryption=path=cockroach-data,key=/path/to/my/aes-256.key,old-key=/path/to/my/aes-128.key -~~~ - -Upon starting, the node will read the existing data keys using the old encryption key (`aes-128.key`), then rewrite -the data keys using the new key (`aes-256.key`). A new data key will be generated to match the desired AES-256 algorithm. - -To check that the new key is active, use the stores report page in the Admin UI to [check the encryption status](#checking-encryption-status). - -To disable encryption, specify `key=plain`. The data keys will be stored in plaintext and new data will not be encrypted. - -To rotate keys, specify `key=/path/to/my/new-aes-128.key` and `old-key=/path/to/my/old-aes-128.key`. The data keys -will be decrypted using the old key and then encrypted using the new key. A new data key will also be generated. - -## Encryption caveats - -### Unencrypted backups - -Backups taken with the `BACKUP` statement are not encrypted even if Encryption at Rest is enabled. Encryption at Rest only applies to the CockroachDB node's data on the local disk. If you want encrypted backups, you will need to encrypt your backup files using your preferred encryption method. - -A workaround for the issue is to use a cloud storage provider that is configured to transparently encrypt your data (e.g., AWS S3 default encryption). - -### Higher CPU utilization - -Enabling Encryption at Rest might result in a higher CPU utilization. We estimate a 5-10% increase in CPU utilization. - -### Encryption for touchpoints with other services - -- S3 backup encryption -- Encrypted comms with Kafka - - -## See also - -- [Client Connection Parameters](connection-parameters.html) -- [Manual Deployment](manual-deployment.html) -- [Orchestrated Deployment](orchestration.html) -- [Local Deployment](secure-a-cluster.html) -- [Other Cockroach Commands](cockroach-commands.html) diff --git a/src/current/v19.2/enterprise-licensing.md b/src/current/v19.2/enterprise-licensing.md deleted file mode 100644 index 07de4024462..00000000000 --- a/src/current/v19.2/enterprise-licensing.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -title: Enterprise Features -summary: Request and set trial and enterprise license keys for CockroachDB -toc: true ---- - -CockroachDB distributes a single binary that contains both core and [enterprise features](https://www.cockroachlabs.com/pricing/). You can use core features without any license key. However, to use the enterprise features, you need either a trial or an enterprise license key. - -This page lists enterprise features, and shows you how to obtain and set trial and enterprise license keys for CockroachDB. - -## Enterprise features - -{% include {{ page.version.version }}/misc/enterprise-features.md %} - -## Types of licenses - -Type | Description --------------|------------ -**Trial License** | A trial license enables you to try out CockroachDB enterprise features for 30 days for free. -**Enterprise License** | A paid enterprise license enables you to use CockroachDB enterprise features for longer periods (one year or more). - -{{site.data.alerts.callout_success}} -New in v19.2: For quick local testing of Enterprise features, you can use the [`cockroach demo`](cockroach-demo.html) command, which starts a temporary, in-memory cluster with a SQL shell open and a trial license applied automatically. -{{site.data.alerts.end}} - -## Obtain a license - -To obtain a trial license, fill out [the registration form](https://www.cockroachlabs.com/get-cockroachdb/enterprise/) and receive your trial license via email within a few minutes. - -To upgrade to an enterprise license, contact Sales. - -## Set a license - -As the CockroachDB `root` user, open the [built-in SQL shell](cockroach-sql.html) in insecure or secure mode, as per your CockroachDB setup. In the following example, we assume that CockroachDB is running in insecure mode. Then use the `SET CLUSTER SETTING` command to set the name of your organization and the license key: - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING cluster.organization = 'Acme Company'; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING enterprise.license = 'xxxxxxxxxxxx'; -~~~ - -## Verify a license - -To verify a license, open the [built-in SQL shell](cockroach-sql.html) and use the `SHOW CLUSTER SETTING` command to check the organization name and license key: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CLUSTER SETTING cluster.organization; -~~~ -~~~ - cluster.organization -+----------------------+ - Acme Company -(1 row) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CLUSTER SETTING enterprise.license; -~~~ -~~~ - enterprise.license -+-------------------------------------------+ - crl-0-ChB1x... -(1 row) -~~~ - -The license setting is also logged in the cockroach.log on the node where the command is run: - -{% include copy-clipboard.html %} -~~~ sql -$ cat cockroach.log | grep license -~~~ -~~~ -I171116 18:11:48.279604 1514 sql/event_log.go:102 [client=[::1]:56357,user=root,n1] Event: "set_cluster_setting", target: 0, info: {SettingName:enterprise.license Value:xxxxxxxxxxxx User:root} -~~~ - -## Renew an expired license - -After your license expires, the enterprise features stop working, but your production setup is unaffected. For example, the backup and restore features would not work until the license is renewed, but you would be able to continue using all other features of CockroachDB without interruption. - -To renew an expired license, contact Sales and then [set](enterprise-licensing.html#set-a-license) the new license. - -## See also - -- [`SET CLUSTER SETTING`](set-cluster-setting.html) -- [`SHOW CLUSTER SETTING`](show-cluster-setting.html) -- [Enterprise Trial –– Get Started](get-started-with-enterprise-trial.html) diff --git a/src/current/v19.2/error-handling-and-troubleshooting.md b/src/current/v19.2/error-handling-and-troubleshooting.md deleted file mode 100644 index 60fa24186f0..00000000000 --- a/src/current/v19.2/error-handling-and-troubleshooting.md +++ /dev/null @@ -1,91 +0,0 @@ ---- -title: Error Handling and Troubleshooting -summary: How to troubleshoot problems and handle transaction retry errors during application development -toc: true ---- - -This page has instructions for handling errors and troubleshooting problems that may arise during application development. - -## Troubleshooting query problems - -If you are not satisfied with your SQL query performance, follow the instructions in [Make Queries Fast][fast] to be sure you are avoiding common performance problems like full table scans, missing indexes, etc. - -If you have already optimized your SQL queries as described in [Make Queries Fast][fast] and are still having issues such as - -- Hanging or "stuck" queries -- Queries that are slow some of the time (but not always) -- Low throughput of queries - -take a look at [Query Behavior Troubleshooting](query-behavior-troubleshooting.html). - -## Transaction retry errors - -Messages with the Postgres error code `40001` indicate that a transaction failed because it [conflicted with another concurrent or recent transaction accessing the same data](performance-best-practices-overview.html#understanding-and-avoiding-transaction-contention). The transaction needs to be retried by the client. - -If your language's client driver or ORM implements transaction retry logic internally (e.g., if you are using Python and [SQLAlchemy with the CockroachDB dialect](build-a-python-app-with-cockroachdb-sqlalchemy.html)), then you do not need to handle this logic from your application. - -If your driver or ORM does not implement this logic, then you will need to implement a retry loop in your application. - -{% include {{page.version.version}}/misc/client-side-intervention-example.md %} - -{{site.data.alerts.callout_info}} -If a consistently high percentage of your transactions are resulting in transaction retry errors, then you may need to evaluate your schema design and data access patterns to find and remove sources of contention. For more information, see [Understanding and Avoiding Transaction Contention](performance-best-practices-overview.html#understanding-and-avoiding-transaction-contention). -{{site.data.alerts.end}} - -For more information about transaction retry errors, see [Transaction retries](transactions.html#client-side-intervention). - -## Unsupported SQL features - -CockroachDB has support for [most SQL features](sql-feature-support.html). - -Additionally, CockroachDB supports [the PostgreSQL wire protocol and the majority of its syntax](postgresql-compatibility.html). This means that existing applications can often be migrated to CockroachDB without changing application code. - -However, you may encounter features of SQL or the Postgres dialect that are not supported by CockroachDB. For example, the following Postgres features are not supported: - -{% include {{page.version.version}}/sql/unsupported-postgres-features.md %} - -For more information about the differences between CockroachDB and Postgres feature support, see [PostgreSQL Compatibility](postgresql-compatibility.html). - -For more information about the SQL standard features supported by CockroachDB, see [SQL Feature Support](sql-feature-support.html) - -## Troubleshooting cluster problems - -As a developer, you will mostly be working with the CockroachDB [SQL API](sql-statements.html). - -However, you may need to access the underlying cluster to troubleshoot issues where the root cause is not your SQL, but something happening at the cluster level. Symptoms of cluster-level issues can include: - -- Cannot join a node to an existing cluster -- Networking, client connection, or authentication issues -- Clock sync, replication, or node liveness issues -- Capacity planning, storage, or memory issues -- Node decommissioning failures - -For more information about how to troubleshoot cluster-level issues, see [Troubleshoot Cluster Setup](cluster-setup-troubleshooting.html). - -## See also - -Reference information related to this page: - -- [Troubleshoot Query Behavior](query-behavior-troubleshooting.html) -- [Troubleshoot Cluster Setup](cluster-setup-troubleshooting.html) -- [Common errors](common-errors.html) -- [Transactions](transactions.html) -- [Transaction retries](transactions.html#client-side-intervention) -- [Understanding and Avoiding Transaction Contention](performance-best-practices-overview.html#understanding-and-avoiding-transaction-contention) -- [SQL Layer][sql] - -Other common tasks: - -- [Connect to the Database](connect-to-the-database.html) -- [Insert Data](insert-data.html) -- [Query Data](query-data.html) -- [Update Data](update-data.html) -- [Delete Data](delete-data.html) -- [Run Multi-Statement Transactions](run-multi-statement-transactions.html) -- [Make Queries Fast][fast] -- [Hello World Example apps](hello-world-example-apps.html) - - - -[sql]: architecture/sql-layer.html -[fast]: make-queries-fast.html diff --git a/src/current/v19.2/experimental-audit.md b/src/current/v19.2/experimental-audit.md deleted file mode 100644 index 428fe435078..00000000000 --- a/src/current/v19.2/experimental-audit.md +++ /dev/null @@ -1,139 +0,0 @@ ---- -title: EXPERIMENTAL_AUDIT -summary: Use the EXPERIMENTAL_AUDIT subcommand to turn SQL audit logging on or off for a table. -toc: true ---- - -`EXPERIMENTAL_AUDIT` is a subcommand of [`ALTER TABLE`](alter-table.html) that is used to turn SQL audit logging on or off for a table. - -SQL audit logs contain detailed information about queries being executed against your system, including: - -- Full text of the query (which may include personally identifiable information (PII)) -- Date/Time -- Client address -- Application name - -For a detailed description of exactly what is logged, see the [Audit Log File Format](#audit-log-file-format) section below. - -CockroachDB stores audit log information in a way that ensures durability, but negatively impacts performance. As a result, we recommend using SQL audit logs for security purposes only. For more information, see [Performance considerations](#performance-considerations). - -{% include {{ page.version.version }}/misc/experimental-warning.md %} - -{% include {{ page.version.version }}/sql/combine-alter-table-commands.md %} - -## Synopsis - -
      -{% include {{ page.version.version }}/sql/diagrams/experimental_audit.html %} -
      - -## Required privileges - -Only members of the `admin` role can enable audit logs on a table. By default, the `root` user belongs to the `admin` role. - -## Parameters - - Parameter | Description ---------------+---------------------------------------------------------- - `table_name` | The name of the table you want to create audit logs for. - `READ` | Log all table reads to the audit log file. - `WRITE` | Log all table writes to the audit log file. - `OFF` | Turn off audit logging. - -{{site.data.alerts.callout_info}} -As of version 2.0, this command logs all reads and writes, and both the READ and WRITE parameters are required (as shown in the examples below). In a future release, this should change to allow logging only reads, only writes, or both. -{{site.data.alerts.end}} - -## Audit log file format - -The audit log file format is as shown below. The numbers above each column are not part of the format; they correspond to the descriptions that follow. - -~~~ -[1] [2] [3] [4] [5a] [5b] [5c] [6] [7a] [7b] [7c] [7d] [7e] [7f] [7g] [7h] [7i] -I180211 07:30:48.832004 317 sql/exec_log.go:90 [client=127.0.0.1:62503, user=root, n1] 13 exec "cockroach" {"ab"[53]:READ} "SELECT nonexistent FROM ab" {} 0.123 12 ERROR 0 -~~~ - -1. Log level (`INFO`, `WARN`, `ERROR`, or `FATAL`) and date (in YYMMDD format) -2. Time (in UTC) -3. Goroutine ID - this column is used for troubleshooting CockroachDB and may change its meaning at any time -4. Where the log line was generated -5. Logging tags - - a. Client address - - b. Username - - c. Node ID -6. Log entry counter -7. Log message: - - a. Label indicating where the data was generated (useful for troubleshooting) - - b. Current value of the [`application_name`](set-vars.html) session setting - - c. Logging trigger: - - The list of triggering tables and access modes for audit logs, since only certain (read/write) activities are added to the audit log - - d. Full text of the query (Note: May contain PII) - - e. Placeholder values, if any - - f. Query execution time (in milliseconds) - - g. Number of rows produced (e.g., for `SELECT`) or processed (e.g., for `INSERT` or `UPDATE`). - - h. Status of the query - - `OK` for success - - `ERROR` otherwise - - i. Number of times the statement was [retried automatically](transactions.html#automatic-retries) by the server so far. - -## Audit log file storage location - -By default, audit logs are stored in the same directory as the other logs generated by CockroachDB. - -To store the audit log files in a specific directory, pass the `--sql-audit-dir` flag to [`cockroach start`](cockroach-start.html). - -{{site.data.alerts.callout_success}} -If your deployment requires particular lifecycle and access policies for audit log files, point `--sql-audit-dir` at a directory that has permissions set so that only CockroachDB can create/delete files. -{{site.data.alerts.end}} - -## Viewing schema changes - -{% include {{ page.version.version }}/misc/schema-change-view-job.md %} - -## Performance considerations - -To ensure [non-repudiation](https://en.wikipedia.org/wiki/Non-repudiation) in audit logs, CockroachDB synchronously logs all of the activity of every user on a cluster in a way that is durable to system failures. Every query that causes a logging event must access the disk of the node on which audit logging is enabled. As a result, enabling SQL audit logs negatively impacts performance, and we recommend using SQL audit logs for security purposes only. - -For debugging and troubleshooting on production clusters, the most performant way to log all queries is to turn on the [cluster-wide setting](cluster-settings.html) `sql.trace.log_statement_execute`. For details, see [Troubleshoot Query Behavior](query-behavior-troubleshooting.html#cluster-wide-execution-logs). - -## Examples - -### Turn on audit logging - -Let's say you have a `customers` table that contains personally identifiable information (PII). To turn on audit logs for that table, run the following command: - -{% include copy-clipboard.html %} -~~~ sql -ALTER TABLE customers EXPERIMENTAL_AUDIT SET READ WRITE; -~~~ - -Now, every access of customer data is added to the audit log with a line that looks like the following: - -~~~ -I180211 07:30:48.832004 317 sql/exec_log.go:90 [client=127.0.0.1:62503,user=root,n1] 13 exec "cockroach" {"customers"[53]:READ} "SELECT * FROM customers" {} 123.45 12 OK -I180211 07:30:48.832004 317 sql/exec_log.go:90 [client=127.0.0.1:62503,user=root,n1] 13 exec "cockroach" {"customers"[53]:READ} "SELECT nonexistent FROM customers" {} 0.123 12 ERROR -~~~ - -To turn on auditing for more than one table, issue a separate `ALTER` statement for each table. - -For a description of the log file format, see the [Audit Log File Format](#audit-log-file-format) section. - -{{site.data.alerts.callout_success}} -For a more detailed example, see [SQL Audit Logging](sql-audit-logging.html). -{{site.data.alerts.end}} - -### Turn off audit logging - -To turn off logging, issue the following command: - -{% include copy-clipboard.html %} -~~~ sql -ALTER TABLE customers EXPERIMENTAL_AUDIT SET OFF; -~~~ - -## See also - -- [SQL Audit Logging](sql-audit-logging.html) -- [`ALTER TABLE`](alter-table.html) -- [`cockroach start` logging flags](cockroach-start.html) -- [`SHOW JOBS`](show-jobs.html) diff --git a/src/current/v19.2/experimental-features.md b/src/current/v19.2/experimental-features.md deleted file mode 100644 index 01f998fc00e..00000000000 --- a/src/current/v19.2/experimental-features.md +++ /dev/null @@ -1,152 +0,0 @@ ---- -title: Experimental Features -summary: Learn about the experimental features available in CockroachDB -toc: true ---- - -This page lists the experimental features that are available in CockroachDB {{ page.version.version }}. - -{{site.data.alerts.callout_danger}} -**This page describes experimental features.** Their interfaces and outputs are subject to change, and there may be bugs. -
      -
      -If you encounter a bug, please [file an issue](file-an-issue.html). -{{site.data.alerts.end}} - -## Session variables - -The table below lists the experimental session settings that are available. For a complete list of session variables, see [`SHOW` (session settings)](show-vars.html). - -| Variable | Default Value | Description | -|-------------------------------------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `experimental_force_split_at` | `'off'` | Indicates whether checks to prevent incorrect usage of [`ALTER TABLE ... SPLIT AT`](split-at.html) should be skipped. | -| `experimental_serial_normalization` | `'rowid'` | If set to `'virtual_sequence'`, make the [`SERIAL`](serial.html) pseudo-type optionally auto-create a sequence for [better compatibility with Hibernate sequences](https://forum.cockroachlabs.com/t/hibernate-sequence-generator-returns-negative-number-and-ignore-unique-rowid/). | - -## SQL statements - -### Keep SQL audit logs - -Log all queries against a table to a file, for security purposes. For more information, see [`ALTER TABLE ... EXPERIMENTAL_AUDIT`](experimental-audit.html). - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE t EXPERIMENTAL_AUDIT SET READ WRITE; -~~~ - -### Relocate leases and replicas - -You have the following options for controlling lease and replica location: - -1. Relocate leases and replicas using `EXPERIMENTAL_RELOCATE` -2. Relocate just leases using `EXPERIMENTAL_RELOCATE LEASE` - -For example, to distribute leases and ranges for N primary keys across N stores in the cluster, run a statement with the following structure: - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE t EXPERIMENTAL_RELOCATE SELECT ARRAY[, , ..., ], , , ..., ; -~~~ - -To relocate just the lease without moving the replicas, run a statement like the one shown below, which moves the lease for the range containing primary key 'foo' to store 1. - -{% include copy-clipboard.html %} -~~~ sql -> ALTER TABLE t EXPERIMENTAL_RELOCATE LEASE SELECT 1, 'foo'; -~~~ - -### Show table fingerprints - -Table fingerprints are used to compute an identification string of an entire table, for the purpose of gauging whether two tables have the same data. This is useful, for example, when restoring a table from backup. - -Example: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW EXPERIMENTAL_FINGERPRINTS FROM TABLE t; -~~~ - -~~~ - index_name | fingerprint -------------+--------------------- - primary | 1999042440040364641 -(1 row) -~~~ - -### Turn on KV event tracing - -Use session tracing (via [`SHOW TRACE FOR SESSION`](show-trace.html)) to report the replicas of all KV events that occur during its execution. - -Example: - -{% include copy-clipboard.html %} -~~~ sql -> SET tracing = on; -> SELECT * from t; -> SET tracing = off; -> SHOW EXPERIMENTAL_REPLICA TRACE FOR SESSION; -~~~ - -~~~ - timestamp | node_id | store_id | replica_id -----------------------------------+---------+----------+------------ - 2018-10-18 15:50:13.345879+00:00 | 3 | 3 | 7 - 2018-10-18 15:50:20.628383+00:00 | 2 | 2 | 26 -~~~ - -### Check for constraint violations with `SCRUB` - -Checks the consistency of [`UNIQUE`](unique.html) indexes, [`CHECK`](check.html) constraints, and more. Partially implemented; see [cockroachdb/cockroach#10425](https://github.com/cockroachdb/cockroach/issues/10425) for details. - -{{site.data.alerts.callout_info}} -This example uses the `users` table from our open-source, fictional peer-to-peer vehicle-sharing application, [MovR](movr.html). -{{site.data.alerts.end}} - -{% include copy-clipboard.html %} -~~~ sql -> EXPERIMENTAL SCRUB table movr.users; -~~~ - -~~~ - job_uuid | error_type | database | table | primary_key | timestamp | repaired | details -----------+--------------------------+----------+-------+----------------------------------------------------------+---------------------------+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - | index_key_decoding_error | movr | users | ('boston','0009eeb5-d779-4bf8-b1bd-8566533b105c') | 2018-10-18 16:00:38.65916 | f | {"error_message": "key ordering did not match datum ordering. IndexDescriptor=ASC", "index_name": "primary", "row_data": {"address": "e'06484 Christine Villages\\nGrantport, TN 01572'", "city": "'boston'", "credit_card": "'4634253150884'", "id": "'0009eeb5-d779-4bf8-b1bd-8566533b105c'", "name": "'Jessica Webb'"}} - | index_key_decoding_error | movr | users | ('los angeles','0001252c-fc16-4006-b6dc-c6b1a0fd1f5b') | 2018-10-18 16:00:38.65916 | f | {"error_message": "key ordering did not match datum ordering. IndexDescriptor=ASC", "index_name": "primary", "row_data": {"address": "e'91309 Warner Springs\\nLake Danielmouth, PR 33400'", "city": "'los angeles'", "credit_card": "'3584736360686445'", "id": "'0001252c-fc16-4006-b6dc-c6b1a0fd1f5b'", "name": "'Rebecca Gibson'"}} - | index_key_decoding_error | movr | users | ('new york','000169a5-e337-4441-b664-dae63e682980') | 2018-10-18 16:00:38.65916 | f | {"error_message": "key ordering did not match datum ordering. IndexDescriptor=ASC", "index_name": "primary", "row_data": {"address": "e'0787 Christopher Highway Apt. 363\\nHamptonmouth, TX 91864-2620'", "city": "'new york'", "credit_card": "'4578562547256688'", "id": "'000169a5-e337-4441-b664-dae63e682980'", "name": "'Christopher Johnson'"}} - | index_key_decoding_error | movr | users | ('paris','00089fc4-e5b1-48f6-9f0b-409905f228c4') | 2018-10-18 16:00:38.65916 | f | {"error_message": "key ordering did not match datum ordering. IndexDescriptor=ASC", "index_name": "primary", "row_data": {"address": "e'46735 Martin Summit\\nMichaelview, OH 10906-5889'", "city": "'paris'", "credit_card": "'5102207609888778'", "id": "'00089fc4-e5b1-48f6-9f0b-409905f228c4'", "name": "'Nicole Fuller'"}} - | index_key_decoding_error | movr | users | ('rome','000209fc-69a1-4dd5-8053-3b5e5769876d') | 2018-10-18 16:00:38.65916 | f | {"error_message": "key ordering did not match datum ordering. IndexDescriptor=ASC", "index_name": "primary", "row_data": {"address": "e'473 Barrera Vista Apt. 890\\nYeseniaburgh, CO 78087'", "city": "'rome'", "credit_card": "'3534605564661093'", "id": "'000209fc-69a1-4dd5-8053-3b5e5769876d'", "name": "'Sheryl Shea'"}} - | index_key_decoding_error | movr | users | ('san francisco','00058767-1e83-4e18-999f-13b5a74d7225') | 2018-10-18 16:00:38.65916 | f | {"error_message": "key ordering did not match datum ordering. IndexDescriptor=ASC", "index_name": "primary", "row_data": {"address": "e'5664 Acevedo Drive Suite 829\\nHernandezview, MI 13516'", "city": "'san francisco'", "credit_card": "'376185496850202'", "id": "'00058767-1e83-4e18-999f-13b5a74d7225'", "name": "'Kevin Turner'"}} - | index_key_decoding_error | movr | users | ('seattle','0002e904-1256-4528-8b5f-abad16e695ff') | 2018-10-18 16:00:38.65916 | f | {"error_message": "key ordering did not match datum ordering. IndexDescriptor=ASC", "index_name": "primary", "row_data": {"address": "e'81499 Samuel Crescent Suite 631\\nLake Christopherborough, PR 50401'", "city": "'seattle'", "credit_card": "'38743493725890'", "id": "'0002e904-1256-4528-8b5f-abad16e695ff'", "name": "'Mark Williams'"}} - | index_key_decoding_error | movr | users | ('washington dc','00007caf-2014-4696-85b0-840e7d8b6db9') | 2018-10-18 16:00:38.65916 | f | {"error_message": "key ordering did not match datum ordering. IndexDescriptor=ASC", "index_name": "primary", "row_data": {"address": "e'4578 Holder Trafficway\\nReynoldsside, IL 23520-7418'", "city": "'washington dc'", "credit_card": "'30454993082943'", "id": "'00007caf-2014-4696-85b0-840e7d8b6db9'", "name": "'Marie Miller'"}} -(8 rows) -~~~ - -### Show range information for a specific row - -The [`SHOW RANGE ... FOR ROW`](show-range-for-row.html) statement shows information about a [range](architecture/overview.html#glossary) for a particular row of data. This information is useful for verifying how SQL data maps to underlying ranges, and where the replicas for a range are located. - -## Functions and Operators - -The table below lists the experimental SQL functions and operators available in CockroachDB. For more information, see each function's documentation at [Functions and Operators](functions-and-operators.html). - -| Function | Description | -|----------------------------------------------------------------------------------+-------------------------------------------------| -| [`experimental_strftime`](functions-and-operators.html#date-and-time-functions) | Format time using standard `strftime` notation. | -| [`experimental_strptime`](functions-and-operators.html#date-and-time-functions) | Format time using standard `strptime` notation. | -| [`experimental_uuid_v4()`](functions-and-operators.html#id-generation-functions) | Return a UUID. | - -## Vectorized execution on memory-intensive operations - -[Vectorized query execution](vectorized-execution.html) in CockroachDB is experimental for the following [memory-intensive operations](vectorized-execution.html#memory-intensive-operations): - -{% include {{page.version.version}}/sql/disk-spilling-ops.md %} - -To turn vectorized execution on for all operations, set the `vectorize` [session variable](set-vars.html) to `experimental_on`. - -## See Also - -- [`SHOW` (session)](show-vars.html) -- [Functions and Operators](functions-and-operators.html) -- [`ALTER TABLE ... EXPERIMENTAL_AUDIT`](experimental-audit.html) -- [`SHOW TRACE FOR SESSION`](show-trace.html) -- [`SHOW RANGE ... FOR ROW`](show-range-for-row.html) -- [Vectorized Query Execution](vectorized-execution.html) diff --git a/src/current/v19.2/explain-analyze.md b/src/current/v19.2/explain-analyze.md deleted file mode 100644 index 89a0b2caa92..00000000000 --- a/src/current/v19.2/explain-analyze.md +++ /dev/null @@ -1,112 +0,0 @@ ---- -title: EXPLAIN ANALYZE -summary: The EXPLAIN ANALYZE statement executes a query and generates a physical query plan with execution statistics. -toc: true ---- - -The `EXPLAIN ANALYZE` [statement](sql-statements.html) **executes a SQL query** and generates a URL for a physical query plan with execution statistics. Query plans can be used to [troubleshoot slow queries](query-behavior-troubleshooting.html) by indicating where time is being spent, how long a processor (i.e., a component that takes streams of input rows and processes them according to a specification) is not doing work, etc. For more information about distributed SQL queries, see the [DistSQL section of our SQL Layer Architecture docs](architecture/sql-layer.html#distsql). - -{{site.data.alerts.callout_info}} -{% include {{ page.version.version }}/sql/physical-plan-url.md %} -{{site.data.alerts.end}} - -## Aliases - -In CockroachDB, the following are aliases for `EXPLAIN ANALYZE`: - -- `EXPLAIN ANALYSE` - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/explain_analyze.html %}
      - -## Required privileges - -The user requires the appropriate [privileges](authorization.html#assign-privileges) for the statement being explained. - -## Parameters - -Parameter | Description --------------------|----------- -`DISTSQL` | _(Default)_ Generate a link to a distributed SQL physical query plan tree. -`preparable_stmt` | The [statement](sql-grammar.html#preparable_stmt) you want details about. All preparable statements are explainable. - -## Success responses - -Successful `EXPLAIN ANALYZE` statements return a table with the following columns: - - Column | Description ---------|------------ -**automatic** | If `true`, the query is distributed. For more information about distributed SQL queries, see the [DistSQL section of our SQL Layer Architecture docs](architecture/sql-layer.html#distsql). -**url** | The URL generated for a physical query plan that provides high level information about how a query will be executed. For details about reading the physical query plan, see [DistSQL Plan Viewer](#distsql-plan-viewer).

      {% include {{ page.version.version }}/sql/physical-plan-url.md %} - -## DistSQL Plan Viewer - -The DistSQL Plan Viewer displays the physical query plan, as well as execution statistics: - -Field | Description -------+------------ -<ProcessorName>/<n> | The processor and processor ID used to read data into the SQL execution engine.

      A processor is a component that takes streams of input rows, processes them according to a specification, and outputs one stream of rows. For example, an "aggregator" aggregates input rows. -<index>@<table> | The index used. -Out | The output columns. -@<n> | The index of the column relative to the input. -Render | The stage that renders the output. -unordered / ordered | _(Blue box)_ A synchronizer that takes one or more output streams and merges them to be consumable by a processor. An ordered synchronizer is used to merge ordered streams and keeps the rows in sorted order. -left(@<n>)=right(@<n>) | The equality columns used in the join. -rows read | The number of rows read by the processor. -stall time | How long the processor spent not doing work. This is aggregated into the stall time numbers as the query progresses down the tree (i.e., stall time is added up and overlaps with previous time). -stored side | The smaller table that was stored as an in-memory hash table. -max memory used | How much memory (if any) is used to buffer rows. -by hash | _(Orange box)_ The router, which is a component that takes one stream of input rows and sends them to a node according to a routing algorithm.

      For example, a hash router hashes columns of a row and sends the results to the node that is aggregating the result rows. -max disk used | How much disk (if any) is used to buffer rows. Routers and processors will spill to disk buffering if there is not enough memory to buffer the rows. -rows routed | How many rows were sent by routers, which can be used to understand network usage. -bytes sent | The number of actual bytes sent (i.e., encoding of the rows). This is only relevant when doing network communication. -Response | The response back to the client. - -{{site.data.alerts.callout_info}} -Any or all of the above fields may display for a given query plan. -{{site.data.alerts.end}} - -## Example - -The following `EXPLAIN ANALYZE` statement executes a simple query against the [TPC-H database](http://www.tpc.org/tpch/) loaded to a 3-node CockroachDB cluster, and then generates a link to a physical query plan with execution statistics: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN ANALYZE SELECT l_shipmode, AVG(l_extendedprice) FROM lineitem GROUP BY l_shipmode; -~~~ - -~~~ - automatic | url -+-----------+----------------------------------------------+ - true | https://cockroachdb.github.io/distsqlplan... -~~~ - -To view the [DistSQL Plan Viewer](#distsql-plan-viewer), point your browser to the URL provided: - -EXPLAIN ANALYZE (DISTSQL) - -## See also - -- [`ALTER TABLE`](alter-table.html) -- [`ALTER SEQUENCE`](alter-sequence.html) -- [`BACKUP`](backup.html) -- [`CANCEL JOB`](cancel-job.html) -- [`CREATE DATABASE`](create-database.html) -- [`DROP DATABASE`](drop-database.html) -- [`EXPLAIN`](explain.html) -- [`EXECUTE`](sql-grammar.html#execute_stmt) -- [`IMPORT`](import.html) -- [Indexes](indexes.html) -- [`INSERT`](insert.html) -- [`PAUSE JOB`](pause-job.html) -- [`RESET`](reset-vars.html) -- [`RESTORE`](restore.html) -- [`RESUME JOB`](resume-job.html) -- [`SELECT`](select-clause.html) -- [Selection Queries](selection-queries.html) -- [`SET`](set-vars.html) -- [`SET CLUSTER SETTING`](set-cluster-setting.html) -- [`SHOW COLUMNS`](show-columns.html) -- [`UPDATE`](update.html) -- [`UPSERT`](upsert.html) diff --git a/src/current/v19.2/explain.md b/src/current/v19.2/explain.md deleted file mode 100644 index 82802174247..00000000000 --- a/src/current/v19.2/explain.md +++ /dev/null @@ -1,573 +0,0 @@ ---- -title: EXPLAIN -summary: The EXPLAIN statement provides information you can use to optimize SQL queries. -toc: true ---- - -The `EXPLAIN` [statement](sql-statements.html) returns CockroachDB's query plan for an [explainable statement](sql-grammar.html#preparable_stmt). You can then use this information to optimize the query. - -{{site.data.alerts.callout_success}} -To actually execute a statement and return a physical query plan with execution statistics, use [`EXPLAIN ANALYZE`](explain-analyze.html). -{{site.data.alerts.end}} - -## Query optimization - -Using `EXPLAIN`'s output, you can optimize your queries by taking the following points into consideration: - -- Queries with fewer levels execute more quickly. Restructuring queries to require fewer levels of processing will generally improve performance. - -- Avoid scanning an entire table, which is the slowest way to access data. You can avoid this by [creating indexes](indexes.html) that contain at least one of the columns that the query is filtering in its `WHERE` clause. - -- New in v19.2: By default, the [vectorized execution](vectorized-execution.html) engine is enabled for all [supported operations](vectorized-execution.html#memory-intensive-operations) and [data types](vectorized-execution.html#supported-data-types). If you are querying a table with a small number of rows, it might be more efficient to use row-oriented execution. The `vectorize_row_count_threshold` [cluster setting](cluster-settings.html) specifies the minimum number of rows required to use the vectorized engine to execute a query plan. - -You can find out if your queries are performing entire table scans by using `EXPLAIN` to see which: - -- Indexes the query uses; shown as the **Description** value of rows with the **Field** value of `table` - -- Key values in the index are being scanned; shown as the **Description** value of rows with the **Field** value of `spans` - -For more information, see [Find the Indexes and Key Ranges a Query Uses](#find-the-indexes-and-key-ranges-a-query-uses). - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/explain.html %}
      - -## Required privileges - -The user requires the appropriate [privileges](authorization.html#assign-privileges) for the statement being explained. - -## Parameters - - Parameter | Description ---------------------+------------ - `VERBOSE` | Show as much information as possible about the query plan. - `TYPES` | Include the intermediate [data types](data-types.html) CockroachDB chooses to evaluate intermediate SQL expressions. - `OPT` | Display the query plan tree generated by the [cost-based optimizer](cost-based-optimizer.html).

      To include cost details used by the optimizer in planning the query, use `OPT, VERBOSE`. To include cost and type details, use `OPT, TYPES`. To include all details used by the optimizer, including statistics, use `OPT, ENV`. - `VEC` | Show detailed information about the [vectorized execution](vectorized-execution.html) plan for a query. If the table queried includes [unsupported data types](vectorized-execution.html#supported-data-types), an unhandled data type error is returned. - `preparable_stmt` | The [statement](sql-grammar.html#preparable_stmt) you want details about. All preparable statements are explainable. - `DISTSQL` | Generate a URL to a [distributed SQL physical query plan tree](explain-analyze.html#distsql-plan-viewer).

      {% include {{ page.version.version }}/sql/physical-plan-url.md %} - -{{site.data.alerts.callout_danger}} -`EXPLAIN` also includes other modes besides query plans that are useful only to CockroachDB developers, which are not documented here. -{{site.data.alerts.end}} - -## Success responses - -Successful `EXPLAIN` statements return tables with the following columns: - - Column | Description ------------|------------- -**Tree** | A tree representation of the hierarchy of the query plan. -**Field** | The name of a property for the query plan.

      The `distributed` and `vectorized` properties apply to the entire query plan. All other properties apply to the query plan node in the **Tree** column. -**Description** | Additional information about the parameter in **Field**. -**Columns** | The columns provided to the processes at lower levels of the hierarchy. Included in `TYPES` and `VERBOSE` output. -**Ordering** | The order in which results are presented to the processes at each level of the hierarchy, as well as other properties of the result set at each level. Included in `TYPES` and `VERBOSE` output. - -## Examples - -The following examples use the [`startrek` example dataset](cockroach-demo.html#datasets). To follow along, you can use `cockroach demo startrek` to start a temporary, in-memory cluster with the `startrek` dataset preloaded. - -### Default query plans - - -By default, `EXPLAIN` includes the least detail about the query plan but can be useful to find out which indexes and index key ranges are used by a query. For example: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC; -~~~ - -~~~ - tree | field | description -+-----------+-------------+------------------+ - | distributed | true - | vectorized | false - sort | | - │ | order | +season - └── scan | | - | table | episodes@primary - | spans | ALL - | filter | season > 3 -(8 rows) -~~~ - -The `tree` column of the output shows the tree structure of the query plan, in this case a `sort` and then a `scan`. - -The `field` and `description` columns describe a set of properties, some global to the query, and some specific to an operation listed in the `tree` column (in this case, `sort` or `scan`): - -- `distributed`:`true` -
      The query plan will be distributed to multiple nodes on the cluster. -- `vectorized`:`false` -
      The plan will be executed with the row-oriented execution engine, and not the [vectorized engine](vectorized-execution.html). -- `order`:`+season` -
      The sort will be ordered ascending on the `season` column. -- `table`:`episodes@primary` -
      The table is scanned on the `primary` index. -- `spans`:`ALL` -
      The table is scanned on *all* key ranges of the `primary` index (i.e., a full table scan). For more information on indexes and key ranges, see the [example](#find-the-indexes-and-key-ranges-a-query-uses) below. -- `filter`: `season > 3` -
      The scan filters on the `season` column. - -If you run `EXPLAIN` on a [join](joins.html) query, the output will display which type of join will be executed. For example, the following `EXPLAIN` output shows that the query will perform a [hash join](joins.html#hash-joins): - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN SELECT * FROM quotes AS q -JOIN episodes AS e ON q.episode = e.id; -~~~ - -~~~ - tree | field | description -+-----------+--------------------+------------------+ - | distributed | true - | vectorized | false - hash-join | | - │ | type | inner - │ | equality | (episode) = (id) - │ | right cols are key | - ├── scan | | - │ | table | quotes@primary - │ | spans | ALL - └── scan | | - | table | episodes@primary - | spans | ALL -(12 rows) -~~~ - -And the following output shows that the query will perform a simple cross join: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN SELECT * FROM quotes AS q -JOIN episodes AS e ON q.episode = '2'; -~~~ - -~~~ - tree | field | description -+----------------------+-------------+---------------------------+ - | distributed | true - | vectorized | false - render | | - └── cross-join | | - │ | type | cross - ├── scan | | - │ | table | episodes@primary - │ | spans | ALL - └── index-join | | - │ | table | quotes@primary - │ | key columns | rowid - └── scan | | - | table | quotes@quotes_episode_idx - | spans | /2-/3 -(14 rows) -~~~ - -### `VERBOSE` option - -The `VERBOSE` option: - -- Includes SQL expressions that are involved in each processing stage, providing more granular detail about which portion of your query is represented at each level. -- Includes detail about which columns are being used by each level, as well as properties of the result set on that level. - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (VERBOSE) SELECT * FROM quotes AS q -JOIN episodes AS e ON q.episode = e.id -WHERE e.season = '1' -ORDER BY e.stardate ASC; -~~~ - -New in v19.2:: The output of [`EXPLAIN`](explain.html#verbose-option) has been updated to show whether `equality cols are key` for [lookup joins](joins.html#lookup-joins), which means that the lookup columns form a key in the target table such that each lookup has at most one result. - -~~~ - tree | field | description | columns | ordering -+--------------------------+-----------------------+---------------------------+-----------------------------------------------------------------------------------------+-----------+ - | distributed | true | | - | vectorized | false | | - render | | | (quote, characters, stardate, episode, id, season, num, title, stardate) | +stardate - │ | render 0 | quote | | - │ | render 1 | characters | | - │ | render 2 | stardate | | - │ | render 3 | episode | | - │ | render 4 | id | | - │ | render 5 | season | | - │ | render 6 | num | | - │ | render 7 | title | | - │ | render 8 | stardate | | - └── lookup-join | | | (id, season, num, title, stardate, episode, rowid[hidden], quote, characters, stardate) | +stardate - │ | table | quotes@primary | | - │ | type | inner | | - │ | equality | (rowid) = (rowid) | | - │ | equality cols are key | | | - └── lookup-join | | | (id, season, num, title, stardate, episode, rowid[hidden]) | +stardate - │ | table | quotes@quotes_episode_idx | | - │ | type | inner | | - │ | equality | (id) = (episode) | | - └── sort | | | (id, season, num, title, stardate) | +stardate - │ | order | +stardate | | - └── scan | | | (id, season, num, title, stardate) | - | table | episodes@primary | | - | spans | ALL | | - | filter | season = 1 | | -(27 rows) -~~~ - -### `TYPES` option - -The `TYPES` mode includes the types of the values used in the query plan. It also includes the SQL expressions that were involved in each processing stage, and includes the columns used by each level. - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (TYPES) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC; -~~~ - -~~~ - tree | field | description | columns | ordering -+-----------+-------------+----------------------------------+---------------------------------------------------------------+----------+ - | distributed | true | | - | vectorized | false | | - sort | | | (id int, season int, num int, title string, stardate decimal) | +season - │ | order | +season | | - └── scan | | | (id int, season int, num int, title string, stardate decimal) | - | table | episodes@primary | | - | spans | ALL | | - | filter | ((season)[int] > (3)[int])[bool] | | -(8 rows) -~~~ - -### `OPT` option - -The `OPT` option displays the query plan tree generated by the [cost-based optimizer](cost-based-optimizer.html). For example: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (OPT) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC; -~~~ - -~~~ - text -+---------------------------+ - sort - └── select - ├── scan episodes - └── filters - └── season > 3 -(5 rows) -~~~ - - - -To include cost details used by the optimizer in planning the query, use `OPT, VERBOSE`: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (OPT, VERBOSE) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC; -~~~ - -~~~ - text -+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ - sort - ├── columns: id:1 season:2 num:3 title:4 stardate:5 - ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=1, null(2)=0] - ├── cost: 98.6419109 - ├── key: (1) - ├── fd: (1)-->(2-5) - ├── ordering: +2 - ├── prune: (1,3-5) - ├── interesting orderings: (+1) - └── select - ├── columns: id:1 season:2 num:3 title:4 stardate:5 - ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=1, null(2)=0] - ├── cost: 95.62 - ├── key: (1) - ├── fd: (1)-->(2-5) - ├── prune: (1,3-5) - ├── interesting orderings: (+1) - ├── scan episodes - │ ├── columns: id:1 season:2 num:3 title:4 stardate:5 - │ ├── stats: [rows=79, distinct(1)=79, null(1)=0, distinct(2)=3, null(2)=0] - │ │ histogram(1)= 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 - │ │ <--- 1 --- 2 --- 3 --- 4 --- 5 --- 6 --- 7 --- 8 --- 9 --- 10 --- 11 --- 12 --- 13 --- 14 --- 15 --- 16 --- 17 --- 18 --- 19 --- 20 --- 21 --- 22 --- 23 --- 24 --- 25 --- 26 --- 27 --- 28 --- 29 --- 30 --- 31 --- 32 --- 33 --- 34 --- 35 --- 36 --- 37 --- 38 --- 39 --- 40 --- 41 --- 42 --- 43 --- 44 --- 45 --- 46 --- 47 --- 48 --- 49 --- 50 --- 51 --- 52 --- 53 --- 54 --- 55 --- 56 --- 57 --- 58 --- 59 --- 60 --- 61 --- 62 --- 63 --- 64 --- 65 --- 66 --- 67 --- 68 --- 69 --- 70 --- 71 --- 72 --- 73 --- 74 --- 75 --- 76 --- 77 --- 78 --- 79 - │ ├── cost: 94.82 - │ ├── key: (1) - │ ├── fd: (1)-->(2-5) - │ ├── prune: (1-5) - │ └── interesting orderings: (+1) - └── filters - └── season > 3 [outer=(2), constraints=(/2: [/4 - ]; tight)] -(29 rows) -~~~ - - - -To include cost and type details, use `OPT, TYPES`: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (OPT, TYPES) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC; -~~~ - -~~~ - text -+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ - sort - ├── columns: id:1(int!null) season:2(int!null) num:3(int) title:4(string) stardate:5(decimal) - ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=1, null(2)=0] - ├── cost: 98.6419109 - ├── key: (1) - ├── fd: (1)-->(2-5) - ├── ordering: +2 - ├── prune: (1,3-5) - ├── interesting orderings: (+1) - └── select - ├── columns: id:1(int!null) season:2(int!null) num:3(int) title:4(string) stardate:5(decimal) - ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=1, null(2)=0] - ├── cost: 95.62 - ├── key: (1) - ├── fd: (1)-->(2-5) - ├── prune: (1,3-5) - ├── interesting orderings: (+1) - ├── scan episodes - │ ├── columns: id:1(int!null) season:2(int) num:3(int) title:4(string) stardate:5(decimal) - │ ├── stats: [rows=79, distinct(1)=79, null(1)=0, distinct(2)=3, null(2)=0] - │ │ histogram(1)= 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 - │ │ <--- 1 --- 2 --- 3 --- 4 --- 5 --- 6 --- 7 --- 8 --- 9 --- 10 --- 11 --- 12 --- 13 --- 14 --- 15 --- 16 --- 17 --- 18 --- 19 --- 20 --- 21 --- 22 --- 23 --- 24 --- 25 --- 26 --- 27 --- 28 --- 29 --- 30 --- 31 --- 32 --- 33 --- 34 --- 35 --- 36 --- 37 --- 38 --- 39 --- 40 --- 41 --- 42 --- 43 --- 44 --- 45 --- 46 --- 47 --- 48 --- 49 --- 50 --- 51 --- 52 --- 53 --- 54 --- 55 --- 56 --- 57 --- 58 --- 59 --- 60 --- 61 --- 62 --- 63 --- 64 --- 65 --- 66 --- 67 --- 68 --- 69 --- 70 --- 71 --- 72 --- 73 --- 74 --- 75 --- 76 --- 77 --- 78 --- 79 - │ ├── cost: 94.82 - │ ├── key: (1) - │ ├── fd: (1)-->(2-5) - │ ├── prune: (1-5) - │ └── interesting orderings: (+1) - └── filters - └── gt [type=bool, outer=(2), constraints=(/2: [/4 - ]; tight)] - ├── variable: season [type=int] - └── const: 3 [type=int] -(31 rows) -~~~ - - - -To include all details used by the optimizer, including statistics, use `OPT, ENV`. - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (OPT, ENV) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC; -~~~ - -~~~ - text -+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ - https://cockroachdb.github.io/text/decode.html#eJzEVcFum0wQPmefYsQl9v8DYQl1bCJVImTT0mIcwTatFUWIwLZZBbMWLE2jqlIewsc-nZ-kwjGUXNIeGpkDEt_HNzP7DTNcsLLiorDBFeltKZL05vQEXNeHr3iim7qhlamOYfBtPIpHlpYslznTsqS84wW2VLiueS7BNPDkABsH2AI8ts2xbVoqfBFYx6b-aoiQGxKHEqDOiU-ALXklMlbBAO3xDLyAjiGYUQg--L6K9iqWVKLYwo9QUS-ePEsucwYRDb3gTSeTSZklksEpcb2p47e4OwsiGjpeQEFZlnyRlPcKnIfe1Ann8J7MYcAzcCJ3qKK9M2fq-fPeewOeqfBYkApFvVBhk1qFNtsQDY8RcnxKwu3pGkaW7FZf1tc5T_XutF7wjrgUIupQL6KeG8H-JQIA-L65N5eSirxeFJViw2UHbgieKR1wpfYEJUsky-JEKjYoTRs0jDXDAoxt07TNkW5aE-PI_N8wbMNQesqMV5IXqYxTUReN-mjSY294JUWcijyW90vWxG787-uLZLHB4zippYjjJ1yd511co0eU4q6Xb4P_UP_ahMdGvLgRh8_6sGMPinrx4gaYz38JO3ZgM4G7noZdT8J2_by8DaN_b4P1BxvQ1f4xQuTTue94AQxm51QFElwMISJ-s0H_g7NwNv39G_n4loRku6XhNRzCLDwlIZzMW8yJ3GOkaZqGKlFKBOvVar16WK8eoGI5S-W2nPXqZwunSdHF79hW9JnnkpVVvyP9iG0Z6FcAAAD__yq9370= -(1 row) -~~~ - -The output of `EXPLAIN (OPT,ENV)` is now a URL with the data encoded in the fragment portion. Opening the URL shows a page with the decoded data. This change makes it easier to share debugging information across different systems without encountering formatting issues. - -Note that the data is processed in the local browser session and is never sent out over the network. Keep in mind that if you are using any browser extensions, they may be able to access the data locally. - -When you visit the URL above you should see the following output in your browser. - -~~~ -Version: CockroachDB CCL v19.2.0-rc.1 (x86_64-apple-darwin14, built 2019/10/14 18:28:24, go1.12.5) - -CREATE TABLE episodes ( - id INT8 NOT NULL, - season INT8 NULL, - num INT8 NULL, - title STRING NULL, - stardate DECIMAL NULL, - CONSTRAINT "primary" PRIMARY KEY (id ASC), - FAMILY "primary" (id, season, num, title, stardate) -); - -ALTER TABLE startrek.public.episodes INJECT STATISTICS '[ - { - "columns": [ - "id" - ], - "created_at": "2019-11-04 11:22:26.249072+00:00", - "distinct_count": 79, - "histo_col_type": "INT8", - "name": "__auto__", - "null_count": 0, - "row_count": 79 - }, - { - "columns": [ - "season" - ], - "created_at": "2019-11-04 11:22:26.249072+00:00", - "distinct_count": 3, - "histo_col_type": "", - "name": "__auto__", - "null_count": 0, - "row_count": 79 - }, - { - "columns": [ - "num" - ], - "created_at": "2019-11-04 11:22:26.249072+00:00", - "distinct_count": 29, - "histo_col_type": "", - "name": "__auto__", - "null_count": 0, - "row_count": 79 - }, - { - "columns": [ - "title" - ], - "created_at": "2019-11-04 11:22:26.249072+00:00", - "distinct_count": 79, - "histo_col_type": "", - "name": "__auto__", - "null_count": 0, - "row_count": 79 - }, - { - "columns": [ - "stardate" - ], - "created_at": "2019-11-04 11:22:26.249072+00:00", - "distinct_count": 76, - "histo_col_type": "", - "name": "__auto__", - "null_count": 4, - "row_count": 79 - } -]'; - -EXPLAIN (OPT, ENV) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC; ----- -sort - └── select - ├── scan episodes - └── filters - └── season > 3 -~~~ - -### `VEC` option - -The `VEC` option shows details about the [vectorized execution plan](vectorized-execution.html#how-vectorized-execution-works) for the query. - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (VEC) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC; -~~~ - -~~~ - text -+---------------------------------------+ - │ - └ Node 1 - └ *colexec.sortOp - └ *colexec.selGTInt64Int64ConstOp - └ *colexec.colBatchScan -(5 rows) -~~~ - -The output shows the different internal functions that will be used to process each batch of column-oriented data. - -### `DISTSQL` option - -The `DISTSQL` option generates a URL for a physical query plan that provides high level information about how a query will be executed. For details about reading the physical query plan, see [DistSQL Plan Viewer](explain-analyze.html#distsql-plan-viewer). For more information about distributed SQL queries, see the [DistSQL section of our SQL Layer Architecture docs](architecture/sql-layer.html#distsql). - -{{site.data.alerts.callout_info}} -{% include {{ page.version.version }}/sql/physical-plan-url.md %} -{{site.data.alerts.end}} - -For example, the following `EXPLAIN(DISTSQL)` statement generates a physical plan for a simple query against the [TPC-H database](http://www.tpc.org/tpch/) loaded to a 3-node CockroachDB cluster: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN (DISTSQL) SELECT l_shipmode, AVG(l_extendedprice) FROM lineitem GROUP BY l_shipmode; -~~~ - -~~~ - automatic | url ------------+---------------------------------------------- - true | https://cockroachdb.github.io/distsqlplan... -~~~ - -To view the [DistSQL Plan Viewer](explain-analyze.html#distsql-plan-viewer), point your browser to the URL provided: - -EXPLAIN (DISTSQL) - -### Find the indexes and key ranges a query uses - -You can use `EXPLAIN` to understand which indexes and key ranges queries use, which can help you ensure a query isn't performing a full table scan. - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE kv (k INT PRIMARY KEY, v INT); -~~~ - -Because column `v` is not indexed, queries filtering on it alone scan the entire table: - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN SELECT * FROM kv WHERE v BETWEEN 4 AND 5; -~~~ - -~~~ - tree | field | description -+------+-------------+-----------------------+ - | distributed | true - | vectorized | false - scan | | - | table | kv@primary - | spans | ALL - | filter | (v >= 4) AND (v <= 5) -(6 rows) -~~~ - -If there were an index on `v`, CockroachDB would be able to avoid scanning the entire table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE INDEX v ON kv (v); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> EXPLAIN SELECT * FROM kv WHERE v BETWEEN 4 AND 5; -~~~ - -~~~ - tree | field | description -+------+-------------+-------------+ - | distributed | false - | vectorized | false - scan | | - | table | kv@v - | spans | /4-/6 -(5 rows) -~~~ - -Now, only part of the index `v` is getting scanned, specifically the key range starting at (and including) 4 and stopping before 6. Also note that this query plan is not distributed across nodes on the cluster. - -## See also - -- [`ALTER TABLE`](alter-table.html) -- [`ALTER SEQUENCE`](alter-sequence.html) -- [`BACKUP`](backup.html) -- [`CANCEL JOB`](cancel-job.html) -- [`CREATE DATABASE`](create-database.html) -- [`DROP DATABASE`](drop-database.html) -- [`EXECUTE`](sql-grammar.html#execute_stmt) -- [`EXPLAIN ANALYZE`](explain-analyze.html) -- [`IMPORT`](import.html) -- [Indexes](indexes.html) -- [`INSERT`](insert.html) -- [`PAUSE JOB`](pause-job.html) -- [`RESET`](reset-vars.html) -- [`RESTORE`](restore.html) -- [`RESUME JOB`](resume-job.html) -- [`SELECT`](select-clause.html) -- [Selection Queries](selection-queries.html) -- [`SET`](set-vars.html) -- [`SET CLUSTER SETTING`](set-cluster-setting.html) -- [`SHOW COLUMNS`](show-columns.html) -- [`UPDATE`](update.html) -- [`UPSERT`](upsert.html) diff --git a/src/current/v19.2/export.md b/src/current/v19.2/export.md deleted file mode 100644 index ff7895a356f..00000000000 --- a/src/current/v19.2/export.md +++ /dev/null @@ -1,157 +0,0 @@ ---- -title: EXPORT -summary: Export tabular data from a CockroachDB cluster in CSV format. -toc: true ---- - -The `EXPORT` [statement](sql-statements.html) exports tabular data or the results of arbitrary `SELECT` statements to CSV files. - -Using the [CockroachDB distributed execution engine](architecture/sql-layer.html#distsql), `EXPORT` parallelizes CSV creation across all nodes in the cluster, making it possible to quickly get large sets of data out of CockroachDB in a format that can be ingested by downstream systems. If you do not need distributed exports, you can use the [non-enterprise feature to export tabular data in CSV format](#non-distributed-export-using-the-sql-shell). - -{{site.data.alerts.callout_danger}} -This is an [enterprise feature](enterprise-licensing.html). Also, it is in **beta** and is currently undergoing continued testing. Please [file a Github issue](file-an-issue.html) with us if you identify a bug. -{{site.data.alerts.end}} - -## Export file location - -You can use remote cloud storage (Amazon S3, Google Cloud Platform, etc.) to store the exported CSV data. Alternatively, you can use an [HTTP server](create-a-file-server.html) accessible from all nodes. - -For simplicity's sake, it's **strongly recommended** to use cloud/remote storage for the data you want to export. Local files are supported; however, they must be accessible identically from all nodes in the cluster. - -## Cancelling export - -After the export has been initiated, you can cancel it with [`CANCEL QUERY`](cancel-query.html). - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/export.html %}
      - -{{site.data.alerts.callout_info}}The EXPORT statement cannot be used within a transaction.{{site.data.alerts.end}} - -## Required privileges - -Only members of the `admin` role can run `EXPORT`. By default, the `root` user belongs to the `admin` role. - -## Parameters - - Parameter | Description ------------|------------- - `file_location` | Specify the URL of the file location where you want to store the exported CSV data. - `WITH kv_option` | Control your export's behavior with [these options](#export-options). - `select_stmt` | Specify the query whose result you want to export to CSV format. - `table_name` | Specify the name of the table you want to export to CSV format. - -### Export file URL - -URLs for the file directory location you want to export to must use the following format: - -{% include {{ page.version.version }}/misc/external-urls.md %} - -You can specify the base directory where you want to store the exported .csv files. CockroachDB will create several files in the specified directory with programmatically generated names (e.g., n1.1.csv, n1.2.csv, n2.1.csv, ...). - -### Export options - -You can control the [`EXPORT`](export.html) process's behavior using any of the following key-value pairs as a `kv_option`. - -#### `delimiter` - -If not using comma as your column delimiter, you can specify another ASCII character as the delimiter. - -
      - - - - - - - - - - - - - - - - - - -
      Required?No
      Keydelimiter
      ValueThe ASCII character that delimits columns in your rows
      ExampleTo use tab-delimited values: WITH delimiter = e'\t'
      - -#### `nullas` - -Convert SQL *NULL* values so they match the specified string. - - - - - - - - - - - - - - - - - - - - -
      Required?No
      Keynullas
      ValueThe string that should be used to represent NULL values. To avoid collisions, it is important to pick nullas values that does not appear in the exported data.
      ExampleTo use empty columns as NULL: WITH nullas = ''
      - -## Examples - -### Export a table - -{% include copy-clipboard.html %} -~~~ sql -> EXPORT INTO CSV - 'azure://acme-co/customer-export-data?AZURE_ACCOUNT_KEY=hash&AZURE_ACCOUNT_NAME=acme-co' - WITH delimiter = '|' FROM TABLE bank.customers; -~~~ - -### Export using a `SELECT` statement - -{% include copy-clipboard.html %} -~~~ sql -> EXPORT INTO CSV - 'azure://acme-co/customer-export-data?AZURE_ACCOUNT_KEY=hash&AZURE_ACCOUNT_NAME=acme-co' - FROM SELECT * FROM bank.customers WHERE id >= 100; -~~~ - -### Non-distributed export using the SQL shell - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql -e "SELECT * from bank.customers WHERE id>=100;" --format=csv > my.csv -~~~ - -### View a running export - -View running exports by using [`SHOW QUERIES`](show-queries.html): - -{% include copy-clipboard.html %} -~~~ sql -> SHOW QUERIES; -~~~ - -### Cancel a running export - -Use [`SHOW QUERIES`](show-queries.html) to get a running export's `query_id`, which can be used to [cancel the export](cancel-query.html): - -{% include copy-clipboard.html %} -~~~ sql -> CANCEL QUERY '14dacc1f9a781e3d0000000000000001'; -~~~ - -## Known limitation - -`EXPORT` may fail with an error if the SQL statements are incompatible with DistSQL. In that case, use the [non-enterprise feature to export tabular data in CSV format](#non-distributed-export-using-the-sql-shell). - -## See also - -- [Create a File Server](create-a-file-server.html) diff --git a/src/current/v19.2/file-an-issue.md b/src/current/v19.2/file-an-issue.md deleted file mode 100644 index 065725484dd..00000000000 --- a/src/current/v19.2/file-an-issue.md +++ /dev/null @@ -1,66 +0,0 @@ ---- -title: File an Issue -summary: Learn how to file a GitHub issue with CockroachDB. -toc: false ---- - -If you've tried to [troubleshoot](troubleshooting-overview.html) an issue yourself, have [reached out for help](support-resources.html), and are still stumped, you can file an issue in GitHub. - -To file an issue in GitHub, we need the following information: - -1. A summary of the issue. - -2. The steps to reproduce the issue. - -3. The result you expected. - -4. The result that actually occurred. - -5. The first few lines of the log file from each node in the cluster in a timeframe as close as possible to reproducing the issue. On most Unix-based systems running with defaults, you can get this information using the following command: - - {% include copy-clipboard.html %} - ~~~ shell - $ grep -F '[config]' cockroach-data/logs/cockroach.log - ~~~ - {{site.data.alerts.callout_info}}You might need to replace cockroach-data/logs with the location of your logs.{{site.data.alerts.end}} - If the logs are not available, please include the output of `cockroach version` for each node in the cluster. - -### Template - -You can use this as a template for [filing an issue in GitHub](https://github.com/cockroachdb/cockroach/issues/new): - -~~~ - -## Summary - - - -## Steps to reproduce - -1. -2. -3. - -## Expected Result - - - -## Actual Result - - - -## Log files/version - -### Node 1 - - - -### Node 2 - - - -### Node 3 - - - -~~~ diff --git a/src/current/v19.2/float.md b/src/current/v19.2/float.md deleted file mode 100644 index 8a1df115e63..00000000000 --- a/src/current/v19.2/float.md +++ /dev/null @@ -1,106 +0,0 @@ ---- -title: FLOAT -summary: The FLOAT data type stores inexact, floating-point numbers with up to 17 digits in total and at least one digit to the right of the decimal point. -toc: true ---- - -CockroachDB supports various inexact, floating-point number [data types](data-types.html) with up to 17 digits of decimal precision. - -They are handled internally using the [standard double-precision (64-bit binary-encoded) IEEE754 format](https://en.wikipedia.org/wiki/IEEE_floating_point). - - -## Names and Aliases - -Name | Aliases ------|-------- -`FLOAT` | None -`REAL` | `FLOAT4` -`DOUBLE PRECISION` | `FLOAT8` - -## Syntax - -A constant value of type `FLOAT` can be entered as a [numeric literal](sql-constants.html#numeric-literals). -For example: `1.414` or `-1234`. - -The special IEEE754 values for positive infinity, negative infinity -and [NaN (Not-a-Number)](https://en.wikipedia.org/wiki/NaN) cannot be -entered using numeric literals directly and must be converted using an -[interpreted literal](sql-constants.html#interpreted-literals) or an -[explicit conversion](scalar-expressions.html#explicit-type-coercions) -from a string literal instead. - -The following values are recognized: - - Syntax | Value -----------------------------------------|------------------------------------------------ - `inf`, `infinity`, `+inf`, `+infinity` | +∞ - `-inf`, `-infinity` | -∞ - `nan` | [NaN (Not-a-Number)](https://en.wikipedia.org/wiki/NaN) - -For example: - -- `FLOAT '+Inf'` -- `'-Inf'::FLOAT` -- `CAST('NaN' AS FLOAT)` - -## Size - -A `FLOAT` column supports values up to 8 bytes in width, but the total storage size is likely to be larger due to CockroachDB metadata. - -## Examples - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE floats (a FLOAT PRIMARY KEY, b REAL, c DOUBLE PRECISION); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW COLUMNS FROM floats; -~~~ - -~~~ -+-------------+------------------+-------------+----------------+-----------------------+-------------+ -| column_name | data_type | is_nullable | column_default | generation_expression | indices | -+-------------+------------------+-------------+----------------+-----------------------+-------------+ -| a | FLOAT | false | NULL | | {"primary"} | -| b | REAL | true | NULL | | {} | -| c | DOUBLE PRECISION | true | NULL | | {} | -+-------------+------------------+-------------+----------------+-----------------------+-------------+ -(3 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO floats VALUES (1.012345678901, 2.01234567890123456789, CAST('+Inf' AS FLOAT)); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM floats; -~~~ - -~~~ -+----------------+--------------------+------+ -| a | b | c | -+----------------+--------------------+------+ -| 1.012345678901 | 2.0123456789012346 | +Inf | -+----------------+--------------------+------+ -(1 row) -# Note that the value in "b" has been limited to 17 digits. -~~~ - -## Supported casting and conversion - -`FLOAT` values can be [cast](data-types.html#data-type-conversions-and-casts) to any of the following data types: - -Type | Details ------|-------- -`INT` | Truncates decimal precision and requires values to be between -2^63 and 2^63-1 -`DECIMAL` | Causes an error to be reported if the value is NaN or +/- Inf. -`BOOL` | **0** converts to `false`; all other values convert to `true` -`STRING` | -- - -## See also - -[Data Types](data-types.html) diff --git a/src/current/v19.2/follower-reads.md b/src/current/v19.2/follower-reads.md deleted file mode 100644 index 16b5d38a4c1..00000000000 --- a/src/current/v19.2/follower-reads.md +++ /dev/null @@ -1,116 +0,0 @@ ---- -title: Follower Reads -summary: To reduce latency for read queries, you can choose to have the closest node serve the request using the follower reads feature. -toc: true ---- - -To reduce latency for read queries, you can use the follower reads feature, which lets the closest replica serve the read request at the expense of only not guaranteeing that data is up to date. - -{{site.data.alerts.callout_info}} -The follower reads feature is an [enterprise-only](enterprise-licensing.html) feature. For insight into how to use this feature to get low latency, historical reads in multi-region deployments, see the [Follower Reads](topology-follower-reads.html) topology pattern. -{{site.data.alerts.end}} - -## What are Follower reads? - -Follower reads are a mechanism to let any replica of a range serve a read request, but are only available for read queries that are sufficiently in the past, i.e., using `AS OF SYSTEM TIME`. Currently, follower reads are available for any read operation at least 48 seconds in the past, though there is active work to reduce that window. - -In widely distributed deployments, using follower reads can reduce the latency of read operations (which can also increase throughput) by letting the replica closest to the gateway serve the request, instead of forcing the gateway to communicate with the leaseholder, which could be geographically distant. - -To make it easier to know when it's safe for your application to make follower reads, we've also included a convenience function (`experimental_follower_read_timestamp()`) that runs your queries at a time as close as possible to the present time. - -## Settings - -### Enable/disable follower reads - -Use [`SET CLUSTER SETTING`](set-cluster-setting.html) to set `kv.closed_timestamp.follower_reads_enabled` to: - -- `true` to enable follower reads _(default)_ -- `false` to disable follower reads - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING kv.closed_timestamp.follower_reads_enabled = false; -~~~ - -#### When to use follower reads - -Follower reads return consistent historical reads; currently a minimum of 48 seconds in the past, though we are actively working on reducing that number. - -As long as your `SELECT` operations can tolerate slightly outdated data, Follower reads can reduce read latencies and increase throughput. - -#### When not to use follower reads - -You should not use follower reads when you need up-to-date data. - -### Make follower read-compatible queries - -Any `SELECT` statement with an `AS OF SYSTEM TIME` value at least 48 seconds in the past can be served by any replica (i.e., can be a Follower Read). - -To simplify this calculation, we've added a convenience function that will always set the `AS OF SYSTEM TIME` value to the minimum required for follower reads, `experimental_follower_read_timestamp()`: - -``` sql -SELECT ... FROM ... AS OF SYSTEM TIME experimental_follower_read_timestamp(); -``` - -### Make follower read-compatible transactions - -You can set the `AS OF SYSTEM TIME` value for all operations in a read-only transaction: - -```sql -BEGIN; - -SET TRANSACTION AS OF SYSTEM TIME experimental_follower_read_timestamp(); - -SAVEPOINT cockroach_restart; - -SELECT ... -SELECT ... - -COMMIT; -``` - -{{site.data.alerts.callout_success}} -Using the [`SET TRANSACTION`](set-transaction.html#use-the-as-of-system-time-option) statement as shown in the example above will make it easier to use the follower reads feature from [drivers and ORMs](install-client-drivers.html). -{{site.data.alerts.end}} - -## How follower reads works - -In CockroachDB's general architecture, all reads are served by a range's [leaseholder](architecture/replication-layer.html#leases), which is a replica elected to coordinate all write operations. Because this node contains information about all of a range's writes, it can also serve reads for the range while still guaranteeing `SERIALIZABLE` isolation. With this architecture, the client might need to communicate with a machine that is far away, creating greater network latencies. - -However, if you were to lower the isolation requirements of an operation, it's possible to serve the read from _any_ replica, not only the leaseholder, given that the data can be sufficiently old. - -To accomplish this in CockroachDB, we've created a mechanism to let you express that any node can serve the request (`kv.closed_timestamp.follower_reads_enabled`) and that a historical read is sufficient (`AS OF SYSTEM TIME`), given that the argument to `AS OF SYSTEM TIME` is sufficiently in the past (`experimental_follower_read_timestamp()`. - -For a more detailed explanation, you can also read the [follower reads RFC](https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20180603_follower_reads.md). - -### Reading from followers - -Each CockroachDB node tracks a property called its "closed timestamp", which means that no new writes can ever be introduced below that timestamp. The closed timestamp advances forward by some target interval behind the current time. If the replica receives a write at a timestamp less than its closed timestamp, it rejects the write. - -With follower reads enabled, any replica on a node can serve a read for a key as long as the time at which the operation is performed (i.e., the `AS OF SYSTEM TIME` value) is less or equal to the node's closed timestamp. - -### Determining which node to read from - -Every node keeps a record of its latency with all other nodes in the system. When a gateway node in cluster with follower reads enabled receives a request to read a key with a sufficiently old `AS OF SYSTEM TIME` value, it forwards the request to the closest node that contains a replica of the data––whether it be a follower or the leaseholder. - -### Interactions with long-running writes - -Long-running write transactions will create write intents with a timestamp near when the transaction began. When a follower read encounters a write intent, it will often end up in a `Wait Queue`, waiting for the operation to complete; however, this runs counter to the benefit follower reads provides. - -To counteract this, you can issue all follower reads in explicit transactions set with `HIGH` priority: - -```sql -BEGIN PRIORITY HIGH AS OF SYSTEM TIME experimental_follower_read_timestamp(); - -SAVEPOINT cockroach_restart; - -SELECT ... -SELECT ... - -COMMIT; -``` - -## See Also - -- [Cluster Settings Overview](cluster-settings.html) -- [Load-Based Splitting](load-based-splitting.html) diff --git a/src/current/v19.2/foreign-key.md b/src/current/v19.2/foreign-key.md deleted file mode 100644 index dd33df0769c..00000000000 --- a/src/current/v19.2/foreign-key.md +++ /dev/null @@ -1,817 +0,0 @@ ---- -title: Foreign Key Constraint -summary: The `FOREIGN KEY` constraint specifies a column can contain only values exactly matching existing values from the column it references. -toc: true ---- - -A foreign key is a column (or combination of columns) in a table whose values must match values of a column in some other table. `FOREIGN KEY` constraints enforce [referential integrity](https://en.wikipedia.org/wiki/Referential_integrity), which essentially says that if column value A refers to column value B, then column value B must exist. - -For example, given an `orders` table and a `customers` table, if you create a column `orders.customer_id` that references the `customers.id` primary key: - -- Each value inserted or updated in `orders.customer_id` must exactly match a value in `customers.id`, or be `NULL`. -- Values in `customers.id` that are referenced by `orders.customer_id` cannot be deleted or updated, unless you have [cascading actions](#use-a-foreign-key-constraint-with-cascade). However, values of `customers.id` that are _not_ present in `orders.customer_id` can be deleted or updated. - -## Details - -### Rules for creating foreign keys - -**Foreign Key Columns** - -- Foreign key columns must use their referenced column's [type](data-types.html). -- Each column cannot belong to more than 1 `FOREIGN KEY` constraint. -- A foreign key column cannot be a [computed column](computed-columns.html). -- Foreign key columns must be [indexed](indexes.html). - - If you are adding the `FOREIGN KEY` constraint to an existing table, and the columns you want to constraint are not already indexed, use [`CREATE INDEX`](create-index.html) to index them and only then use the [`ADD CONSTRAINT`](add-constraint.html) statement to add the `FOREIGN KEY` constraint to the columns. - - If you are creating a new table, there are a number of ways that you can meet the indexing requirement: - - - You can create indexes explicitly using the [`INDEX`](create-table.html#create-a-table-with-secondary-and-inverted-indexes) clause of `CREATE TABLE`. - - You can rely on indexes created by the [`PRIMARY KEY`](primary-key.html) or [`UNIQUE`](unique.html) constraints. - - If you add a foreign key constraint to an empty table, and an index on the referencing columns does not already exist, CockroachDB automatically creates one. For an example, see [Add the foreign key constraint with `CASCADE`](add-constraint.html#add-the-foreign-key-constraint-with-cascade). It's important to note that if you later remove the `FOREIGN KEY` constraint, this automatically created index _is not_ removed. - - {{site.data.alerts.callout_success}} - Using the foreign key columns as the prefix of an index's columns also satisfies the requirement for an index. For example, if you create foreign key columns `(A, B)`, an index of columns `(A, B, C)` satisfies the requirement for an index. - {{site.data.alerts.end}} - -**Referenced Columns** - -- Referenced columns must contain only unique sets of values. This means the `REFERENCES` clause must use exactly the same columns as a [`UNIQUE`](unique.html) or [`PRIMARY KEY`](primary-key.html) constraint on the referenced table. For example, the clause `REFERENCES tbl (C, D)` requires `tbl` to have either the constraint `UNIQUE (C, D)` or `PRIMARY KEY (C, D)`. -- In the `REFERENCES` clause, if you specify a table but no columns, CockroachDB references the table's primary key. In these cases, the `FOREIGN KEY` constraint and the referenced table's primary key must contain the same number of columns. -- Referenced columns must be [indexed](indexes.html). There are a number of ways to meet this requirement: - - - You can create indexes explicitly using the [`INDEX`](create-table.html#create-a-table-with-secondary-and-inverted-indexes) clause of `CREATE TABLE`. - - You can rely on indexes created by the [`PRIMARY KEY`](primary-key.html) or [`UNIQUE`](unique.html) constraints. - - If an index on the referenced column does not already exist, CockroachDB automatically creates one. It's important to note that if you later remove the `FOREIGN KEY` constraint, this automatically created index _is not_ removed. - - {{site.data.alerts.callout_success}} - Using the referenced columns as the prefix of an index's columns also satisfies the requirement for an index. For example, if you create foreign key that references the columns `(A, B)`, an index of columns `(A, B, C)` satisfies the requirement for an index. - {{site.data.alerts.end}} - -### Null values - -Single-column foreign keys accept null values. - -Multiple-column (composite) foreign keys only accept null values in the following scenarios: - -- The write contains null values for all foreign key columns (if `MATCH FULL` is specified). -- The write contains null values for at least one foreign key column (if `MATCH SIMPLE` is specified). - -For more information about composite foreign keys, see the [composite foreign key matching](#composite-foreign-key-matching) section. - -Note that allowing null values in either your foreign key or referenced columns can degrade their referential integrity, since any key with a null value is never checked against the referenced table. To avoid this, you can use a [`NOT NULL` constraint](not-null.html) on foreign keys when [creating your tables](create-table.html). - -{{site.data.alerts.callout_info}} -A `NOT NULL` constraint cannot be added to existing tables. -{{site.data.alerts.end}} - -### Composite foreign key matching - -By default, composite foreign keys are matched using the `MATCH SIMPLE` algorithm (which is the same default as Postgres). `MATCH FULL` is available if specified. - -In versions 2.1 and earlier, the only option for composite foreign key matching was an incorrect implementation of `MATCH FULL`. This allowed null values in the referencing key columns to correspond to null values in the referenced key columns. This was incorrect in two ways: - -1. `MATCH FULL` should not allow mixed null and non-null values. See below for more details on the differences between comparison methods. -2. Null values cannot ever be compared to each other. - -To correct these issues, all composite key matches defined prior to version 19.1 will now use the `MATCH SIMPLE` comparison method. We have also added the ability to specify both `MATCH FULL` and `MATCH SIMPLE`. If you had a composite foreign key constraint and have just upgraded to version 19.1, then please check that `MATCH SIMPLE` works for your schema and consider replacing that foreign key constraint with a `MATCH FULL` one. - -#### How it works - -For matching purposes, composite foreign keys can be in one of three states: - -- **Valid**: Keys that can be used for matching foreign key relationships. - -- **Invalid**: Keys that will not be used for matching (including for any cascading operations). - -- **Unacceptable**: Keys that cannot be inserted at all (an error is signalled). - -`MATCH SIMPLE` stipulates that: - -- **Valid** keys may not contain any null values. - -- **Invalid** keys contain one or more null values. - -- **Unacceptable** keys do not exist from the point of view of `MATCH SIMPLE`; all composite keys are acceptable. - -`MATCH FULL` stipulates that: - -- **Valid** keys may not contain any null values. - -- **Invalid** keys must have all null values. - -- **Unacceptable** keys have any combination of both null and non-null values. In other words, `MATCH FULL` requires that if any column of a composite key is `NULL`, then all columns of the key must be `NULL`. - -For examples showing how these key matching algorithms work, see [Match composite foreign keys with `MATCH SIMPLE` and `MATCH FULL`](#match-composite-foreign-keys-with-match-simple-and-match-full). - -{{site.data.alerts.callout_info}} -CockroachDB does not support `MATCH PARTIAL`. For more information, see issue [#20305](https://github.com/cockroachdb/cockroach/issues/20305). -{{site.data.alerts.end}} - -### Foreign key actions - -When you set a foreign key constraint, you can control what happens to the constrained column when the column it's referencing (the foreign key) is deleted or updated. - -Parameter | Description -----------|------------ -`ON DELETE NO ACTION` | _Default action._ If there are any existing references to the key being deleted, the transaction will fail at the end of the statement. The key can be updated, depending on the `ON UPDATE` action.

      Alias: `ON DELETE RESTRICT` -`ON UPDATE NO ACTION` | _Default action._ If there are any existing references to the key being updated, the transaction will fail at the end of the statement. The key can be deleted, depending on the `ON DELETE` action.

      Alias: `ON UPDATE RESTRICT` -`ON DELETE RESTRICT` / `ON UPDATE RESTRICT` | `RESTRICT` and `NO ACTION` are currently equivalent until options for deferring constraint checking are added. To set an existing foreign key action to `RESTRICT`, the foreign key constraint must be dropped and recreated. -`ON DELETE CASCADE` / `ON UPDATE CASCADE` | When a referenced foreign key is deleted or updated, all rows referencing that key are deleted or updated, respectively. If there are other alterations to the row, such as a `SET NULL` or `SET DEFAULT`, the delete will take precedence.

      Note that `CASCADE` does not list objects it drops or updates, so it should be used cautiously. -`ON DELETE SET NULL` / `ON UPDATE SET NULL` | When a referenced foreign key is deleted or updated, respectively, the columns of all rows referencing that key will be set to `NULL`. The column must allow `NULL` or this update will fail. -`ON DELETE SET DEFAULT` / `ON UPDATE SET DEFAULT` | When a referenced foreign key is deleted or updated, the columns of all rows referencing that key are set to the default value for that column.

      If the default value for the column is null, or if no default value is provided and the column does not have a [`NOT NULL`](not-null.html) constraint, this will have the same effect as `ON DELETE SET NULL` or `ON UPDATE SET NULL`. The default value must still conform with all other constraints, such as `UNIQUE`. - -### Performance - -Because the foreign key constraint requires per-row checks on two tables, statements involving foreign key or referenced columns can take longer to execute. You're most likely to notice this with operations like bulk inserts into the table with the foreign keys. For bulk inserts into new tables, use the [`IMPORT`](import.html) statement instead of [`INSERT`](insert.html). - -You can improve the performance of some statements that use foreign keys by also using [`INTERLEAVE IN PARENT`](interleave-in-parent.html), but there are tradeoffs. For more information about the performance implications of interleaved tables (as well as the limitations), see the **Interleave tables** section of [Performance best practices](performance-best-practices-overview.html#interleave-tables). - -## Syntax - -Foreign key constraints can be defined at the [table level](#table-level). However, if you only want the constraint to apply to a single column, it can be applied at the [column level](#column-level). - -{{site.data.alerts.callout_info}} -You can also add the `FOREIGN KEY` constraint to existing tables through [`ADD CONSTRAINT`](add-constraint.html#add-the-foreign-key-constraint-with-cascade). -{{site.data.alerts.end}} - -### Column level - -
      {% include {{ page.version.version }}/sql/diagrams/foreign_key_column_level.html %}
      - -| Parameter | Description | -|-----------|-------------| -| `table_name` | The name of the table you're creating. | -| `column_name` | The name of the foreign key column. | -| `column_type` | The foreign key column's [data type](data-types.html). | -| `parent_table` | The name of the table the foreign key references. | -| `ref_column_name` | The name of the column the foreign key references.

      If you do not include the `ref_column_name` you want to reference from the `parent_table`, CockroachDB uses the first column of `parent_table`'s primary key. -| `column_constraints` | Any other column-level [constraints](constraints.html) you want to apply to this column. | -| `column_def` | Definitions for any other columns in the table. | -| `table_constraints` | Any table-level [constraints](constraints.html) you want to apply. | - -**Example** - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE IF NOT EXISTS orders ( - id INT PRIMARY KEY, - customer INT NOT NULL REFERENCES customers (id) ON DELETE CASCADE, - orderTotal DECIMAL(9,2), - INDEX (customer) - ); -~~~ -{{site.data.alerts.callout_danger}} -`CASCADE` does not list objects it drops or updates, so it should be used cautiously. -{{site.data.alerts.end}} - -### Table level - -
      {% include {{ page.version.version }}/sql/diagrams/foreign_key_table_level.html %}
      - -| Parameter | Description | -|-----------|-------------| -| `table_name` | The name of the table you're creating. | -| `column_def` | Definitions for the table's columns. | -| `name` | The name of the constraint. | -| `fk_column_name` | The name of the foreign key column. | -| `parent_table` | The name of the table the foreign key references. | -| `ref_column_name` | The name of the column the foreign key references.

      If you do not include the `column_name` you want to reference from the `parent_table`, CockroachDB uses the first column of `parent_table`'s primary key. -| `table_constraints` | Any other table-level [constraints](constraints.html) you want to apply. | - -**Example** - -{% include copy-clipboard.html %} -~~~ sql -CREATE TABLE packages ( - customer INT, - "order" INT, - id INT, - address STRING(50), - delivered BOOL, - delivery_date DATE, - PRIMARY KEY (customer, "order", id), - CONSTRAINT fk_order FOREIGN KEY (customer, "order") REFERENCES orders - ) INTERLEAVE IN PARENT orders (customer, "order") - ; -~~~ - -## Usage examples - -### Use a foreign key constraint with default actions - -In this example, we'll create a table with a foreign key constraint with the default [actions](#foreign-key-actions) (`ON UPDATE NO ACTION ON DELETE NO ACTION`). - -First, create the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE customers (id INT PRIMARY KEY, email STRING UNIQUE); -~~~ - -Next, create the referencing table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE IF NOT EXISTS orders ( - id INT PRIMARY KEY, - customer INT NOT NULL REFERENCES customers (id), - orderTotal DECIMAL(9,2), - INDEX (customer) - ); -~~~ - -Let's insert a record into each table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO customers VALUES (1001, 'a@co.tld'), (1234, 'info@cockroachlabs.com'); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders VALUES (1, 1002, 29.99); -~~~ -~~~ -pq: foreign key violation: value [1002] not found in customers@primary [id] -~~~ - -The second record insertion returns an error because the customer `1002` doesn't exist in the referenced table. - -Let's insert a record into the referencing table and try to update the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders VALUES (1, 1001, 29.99); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE customers SET id = 1002 WHERE id = 1001; -~~~ -~~~ -pq: foreign key violation: value(s) [1001] in columns [id] referenced in table "orders" -~~~ - -The update to the referenced table returns an error because `id = 1001` is referenced and the default [foreign key action](#foreign-key-actions) is enabled (`ON UPDATE NO ACTION`). However, `id = 1234` is not referenced and can be updated: - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE customers SET id = 1111 WHERE id = 1234; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customers; -~~~ -~~~ - id | email -+------+------------------------+ - 1001 | a@co.tld - 1111 | info@cockroachlabs.com -(2 rows) -~~~ - -Now let's try to delete a referenced row: - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM customers WHERE id = 1001; -~~~ -~~~ -pq: foreign key violation: value(s) [1001] in columns [id] referenced in table "orders" -~~~ - -Similarly, the deletion returns an error because `id = 1001` is referenced and the default [foreign key action](#foreign-key-actions) is enabled (`ON DELETE NO ACTION`). However, `id = 1111` is not referenced and can be deleted: - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM customers WHERE id = 1111; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customers; -~~~ -~~~ - id | email -+------+----------+ - 1001 | a@co.tld -(1 row) -~~~ - -### Use a Foreign Key Constraint with `CASCADE` - -In this example, we'll create a table with a foreign key constraint with the [foreign key actions](#foreign-key-actions) `ON UPDATE CASCADE` and `ON DELETE CASCADE`. - -First, create the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE customers_2 ( - id INT PRIMARY KEY - ); -~~~ - -Then, create the referencing table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE orders_2 ( - id INT PRIMARY KEY, - customer_id INT REFERENCES customers_2(id) ON UPDATE CASCADE ON DELETE CASCADE - ); -~~~ - -Insert a few records into the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO customers_2 VALUES (1), (2), (3); -~~~ - -Insert some records into the referencing table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders_2 VALUES (100,1), (101,2), (102,3), (103,1); -~~~ - -Now, let's update an `id` in the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE customers_2 SET id = 23 WHERE id = 1; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customers_2; -~~~ -~~~ - id -+----+ - 2 - 3 - 23 -(3 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_2; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 100 | 23 - 101 | 2 - 102 | 3 - 103 | 23 -(4 rows) -~~~ - -When `id = 1` was updated to `id = 23` in `customers_2`, the update propagated to the referencing table `orders_2`. - -Similarly, a deletion will cascade. Let's delete `id = 23` from `customers_2`: - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM customers_2 WHERE id = 23; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customers_2; -~~~ -~~~ - id -+----+ - 2 - 3 -(2 rows) -~~~ - -Let's check to make sure the rows in `orders_2` where `customers_id = 23` were also deleted: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_2; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 101 | 2 - 102 | 3 -(2 rows) -~~~ - -### Use a Foreign Key Constraint with `SET NULL` - -In this example, we'll create a table with a foreign key constraint with the [foreign key actions](#foreign-key-actions) `ON UPDATE SET NULL` and `ON DELETE SET NULL`. - -First, create the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE customers_3 ( - id INT PRIMARY KEY - ); -~~~ - -Then, create the referencing table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE orders_3 ( - id INT PRIMARY KEY, - customer_id INT REFERENCES customers_3(id) ON UPDATE SET NULL ON DELETE SET NULL - ); -~~~ - -Insert a few records into the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO customers_3 VALUES (1), (2), (3); -~~~ - -Insert some records into the referencing table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders_3 VALUES (100,1), (101,2), (102,3), (103,1); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_3; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 100 | 1 - 101 | 2 - 102 | 3 - 103 | 1 -(4 rows) -~~~ - -Now, let's update an `id` in the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE customers_3 SET id = 23 WHERE id = 1; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customers_3; -~~~ -~~~ - id -+----+ - 2 - 3 - 23 -(3 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_3; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 100 | NULL - 101 | 2 - 102 | 3 - 103 | NULL -(4 rows) -~~~ - -When `id = 1` was updated to `id = 23` in `customers_3`, the referencing `customer_id` was set to `NULL`. - -Similarly, a deletion will set the referencing `customer_id` to `NULL`. Let's delete `id = 2` from `customers_3`: - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM customers_3 WHERE id = 2; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customers_3; -~~~ -~~~ - id -+----+ - 3 - 23 -(2 rows) -~~~ - -Let's check to make sure the row in `orders_3` where `customers_id = 2` was updated to `NULL`: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_3; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 100 | NULL - 101 | NULL - 102 | 3 - 103 | NULL -(4 rows) -~~~ - -### Use a Foreign Key Constraint with `SET DEFAULT` - -In this example, we'll create a table with a `FOREIGN` constraint with the [foreign key actions](#foreign-key-actions) `ON UPDATE SET DEFAULT` and `ON DELETE SET DEFAULT`. - -First, create the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE customers_4 ( - id INT PRIMARY KEY - ); -~~~ - -Then, create the referencing table with the `DEFAULT` value for `customer_id` set to `9999`: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE orders_4 ( - id INT PRIMARY KEY, - customer_id INT DEFAULT 9999 REFERENCES customers_4(id) ON UPDATE SET DEFAULT ON DELETE SET DEFAULT - ); -~~~ - -Insert a few records into the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO customers_4 VALUES (1), (2), (3), (9999); -~~~ - -Insert some records into the referencing table: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders_4 VALUES (100,1), (101,2), (102,3), (103,1); -~~~ - - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_4; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 100 | 1 - 101 | 2 - 102 | 3 - 103 | 1 -(4 rows) -~~~ - -Now, let's update an `id` in the referenced table: - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE customers_4 SET id = 23 WHERE id = 1; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customers_4; -~~~ -~~~ - id -+------+ - 2 - 3 - 23 - 9999 -(4 rows) -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_4; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 100 | 9999 - 101 | 2 - 102 | 3 - 103 | 9999 -(4 rows) -~~~ - -When `id = 1` was updated to `id = 23` in `customers_4`, the referencing `customer_id` was set to `DEFAULT` (i.e., `9999`). You can see this in the first and last rows of `orders_4`, where `id = 100` and the `customer_id` is now `9999` - -Similarly, a deletion will set the referencing `customer_id` to the `DEFAULT` value. Let's delete `id = 2` from `customers_4`: - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM customers_4 WHERE id = 2; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM customers_4; -~~~ -~~~ - id -+------+ - 3 - 23 - 9999 -(3 rows) -~~~ - -Let's check to make sure the corresponding `customer_id` value to `id = 101`, was updated to the `DEFAULT` value (i.e., `9999`) in `orders_4`: - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_4; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 100 | 9999 - 101 | 9999 - 102 | 3 - 103 | 9999 -(4 rows) -~~~ - -If the default value for the `customer_id` column is not set, and the column does not have a [`NOT NULL`](not-null.html) constraint, `ON UPDATE SET DEFAULT` and `ON DELETE SET DEFAULT` actions set referenced column values to `NULL`. - -For example, let's create a new `customers_5` table and insert some values: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE customers_5 ( - id INT PRIMARY KEY - ); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO customers_5 VALUES (1), (2), (3), (4); -~~~ - -Then we can create a new `orders_5` table that references the `customers_5` table, but with no default value specified for the `ON UPDATE SET DEFAULT` and `ON DELETE SET DEFAULT` actions: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE orders_5 ( - id INT PRIMARY KEY, - customer_id INT REFERENCES customers_5(id) ON UPDATE SET DEFAULT ON DELETE SET DEFAULT - ); -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> INSERT INTO orders_5 VALUES (200,1), (201,2), (202,3), (203,4); -~~~ - -Deleting and updating values in the `customers_5` table sets the referenced values in `orders_5` to `NULL`: - -{% include copy-clipboard.html %} -~~~ sql -> DELETE FROM customers_5 WHERE id = 3; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> UPDATE customers_5 SET id = 0 WHERE id = 1; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SELECT * FROM orders_5; -~~~ -~~~ - id | customer_id -+-----+-------------+ - 200 | NULL - 201 | 2 - 202 | NULL - 203 | 4 -(4 rows) -~~~ - - -### Match composite foreign keys with `MATCH SIMPLE` and `MATCH FULL` - -The examples in this section show how composite foreign key matching works for both the `MATCH SIMPLE` and `MATCH FULL` algorithms. For a conceptual overview, see [Composite foreign key matching](#composite-foreign-key-matching). - -First, let's create some tables. `parent` is a table with a composite key: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE parent (x INT, y INT, z INT, UNIQUE (x, y, z)); -~~~ - -`full_test` has a foreign key on `parent` that uses the `MATCH FULL` algorithm: - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE full_test ( - x INT, - y INT, - z INT, - FOREIGN KEY (x, y, z) REFERENCES parent (x, y, z) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE - ); -~~~ - -`simple_test` has a foreign key on `parent` that uses the `MATCH SIMPLE` algorithm (the default): - -{% include copy-clipboard.html %} -~~~ sql -> CREATE TABLE simple_test ( - x INT, - y INT, - z INT, - FOREIGN KEY (x, y, z) REFERENCES parent (x, y, z) ON DELETE CASCADE ON UPDATE CASCADE - ); -~~~ - -Next, we populate `parent` with some values: - -{% include copy-clipboard.html %} -~~~ sql -> INSERT - INTO parent - VALUES (1, 1, 1), - (2, 1, 1), - (1, 2, 1), - (1, 1, 2), - (NULL, NULL, NULL), - (1, NULL, NULL), - (NULL, 1, NULL), - (NULL, NULL, 1), - (1, 1, NULL), - (1, NULL, 1), - (NULL, 1, 1); -~~~ - -Now let's look at some `INSERT` statements to see how the different key matching algorithms work. - -- [MATCH SIMPLE](#match-simple) -- [MATCH FULL](#match-full) - -#### MATCH SIMPLE - -Inserting values into the table using the `MATCH SIMPLE` algorithm (described [above](#composite-foreign-key-matching)) gives the following results: - -| Statement | Can insert? | Throws error? | Notes | -|---------------------------------------------------+-------------+---------------+-------------------------------| -| `INSERT INTO simple_test VALUES (1,1,1)` | Yes | No | References `parent (1,1,1)`. | -| `INSERT INTO simple_test VALUES (NULL,NULL,NULL)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO simple_test VALUES (1,NULL,NULL)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO simple_test VALUES (NULL,1,NULL)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO simple_test VALUES (NULL,NULL,1)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO simple_test VALUES (1,1,NULL)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO simple_test VALUES (1,NULL,1)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO simple_test VALUES (NULL,1,1)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO simple_test VALUES (2,2,NULL)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO simple_test VALUES (2,2,2)` | No | Yes | No `parent` reference exists. | - -#### MATCH FULL - -Inserting values into the table using the `MATCH FULL` algorithm (described [above](#composite-foreign-key-matching)) gives the following results: - -| Statement | Can insert? | Throws error? | Notes | -|-------------------------------------------------+-------------+---------------+-----------------------------------------------------| -| `INSERT INTO full_test VALUES (1,1,1)` | Yes | No | References `parent(1,1,1)`. | -| `INSERT INTO full_test VALUES (NULL,NULL,NULL)` | Yes | No | Does not reference `parent`. | -| `INSERT INTO full_test VALUES (1,NULL,NULL)` | No | Yes | Can't mix null and non-null values in `MATCH FULL`. | -| `INSERT INTO full_test VALUES (NULL,1,NULL)` | No | Yes | Can't mix null and non-null values in `MATCH FULL`. | -| `INSERT INTO full_test VALUES (NULL,NULL,1)` | No | Yes | Can't mix null and non-null values in `MATCH FULL`. | -| `INSERT INTO full_test VALUES (1,1,NULL)` | No | Yes | Can't mix null and non-null values in `MATCH FULL`. | -| `INSERT INTO full_test VALUES (1,NULL,1)` | No | Yes | Can't mix null and non-null values in `MATCH FULL`. | -| `INSERT INTO full_test VALUES (NULL,1,1)` | No | Yes | Can't mix null and non-null values in `MATCH FULL`. | -| `INSERT INTO full_test VALUES (2,2,NULL)` | No | Yes | Can't mix null and non-null values in `MATCH FULL`. | -| `INSERT INTO full_test VALUES (2,2,2)` | No | Yes | No `parent` reference exists. | - -## See also - -- [Constraints](constraints.html) -- [`DROP CONSTRAINT`](drop-constraint.html) -- [`ADD CONSTRAINT`](add-constraint.html) -- [`CHECK` constraint](check.html) -- [`DEFAULT` constraint](default-value.html) -- [`NOT NULL` constraint](not-null.html) -- [`PRIMARY KEY` constraint](primary-key.html) -- [`UNIQUE` constraint](unique.html) -- [`SHOW CONSTRAINTS`](show-constraints.html) diff --git a/src/current/v19.2/frequently-asked-questions.md b/src/current/v19.2/frequently-asked-questions.md deleted file mode 100644 index a9e38a5d795..00000000000 --- a/src/current/v19.2/frequently-asked-questions.md +++ /dev/null @@ -1,184 +0,0 @@ ---- -title: Frequently Asked Questions -summary: CockroachDB FAQ - What is CockroachDB? How does it work? What makes it different from other databases? -tags: postgres, cassandra, google cloud spanner -toc: true ---- - -## What is CockroachDB? - -CockroachDB is a [distributed SQL](https://www.cockroachlabs.com/blog/what-is-distributed-sql/) database built on a transactional and strongly-consistent key-value store. It **scales** horizontally; **survives** disk, machine, rack, and even datacenter failures with minimal latency disruption and no manual intervention; supports **strongly-consistent** ACID transactions; and provides a familiar **SQL** API for structuring, manipulating, and querying data. - -CockroachDB is inspired by Google's [Spanner](http://research.google.com/archive/spanner.html) and [F1](http://research.google.com/pubs/pub38125.html) technologies, and the [source code](https://github.com/cockroachdb/cockroach) is freely available. - -## When is CockroachDB a good choice? - -CockroachDB is well suited for applications that require reliable, available, and correct data, and millisecond response times, regardless of scale. It is built to automatically replicate, rebalance, and recover with minimal configuration and operational overhead. Specific use cases include: - -- Distributed or replicated OLTP -- Multi-datacenter deployments -- Multi-region deployments -- Cloud migrations -- Infrastructure initiatives built for the cloud - -CockroachDB returns single-row reads in 2ms or less and single-row writes in 4ms or less, and supports a variety of [SQL and operational tuning practices](performance-tuning.html) for optimizing query performance. However, CockroachDB is not yet suitable for heavy analytics / OLAP. - -## How easy is it to install CockroachDB? - -It's as easy as downloading a binary or running our official Kubernetes configurations or Docker image. There are other simple install methods as well, such as running our Homebrew recipe on OS X or building from source files on both OS X and Linux. - -For more details, see [Install CockroachDB](install-cockroachdb.html). - -## How does CockroachDB scale? - -CockroachDB scales horizontally with minimal operator overhead. You can run it on your local computer, a single server, a corporate development cluster, or a private or public cloud. [Adding capacity](cockroach-start.html) is as easy as pointing a new node at the running cluster. - -At the key-value level, CockroachDB starts off with a single, empty range. As you put data in, this single range eventually reaches a threshold size (64MB by default). When that happens, the data splits into two ranges, each covering a contiguous segment of the entire key-value space. This process continues indefinitely; as new data flows in, existing ranges continue to split into new ranges, aiming to keep a relatively small and consistent range size. - -When your cluster spans multiple nodes (physical machines, virtual machines, or containers), newly split ranges are automatically rebalanced to nodes with more capacity. CockroachDB communicates opportunities for rebalancing using a peer-to-peer [gossip protocol](https://en.wikipedia.org/wiki/Gossip_protocol) by which nodes exchange network addresses, store capacity, and other information. - -## How does CockroachDB survive failures? - -CockroachDB is designed to survive software and hardware failures, from server restarts to datacenter outages. This is accomplished without confusing artifacts typical of other distributed systems (e.g., stale reads) using strongly-consistent replication as well as automated repair after failures. - -**Replication** - -CockroachDB replicates your data for availability and guarantees consistency between replicas using the [Raft consensus algorithm](https://raft.github.io/), a popular alternative to Paxos. You can [define the location of replicas](configure-replication-zones.html) in various ways, depending on the types of failures you want to secure against and your network topology. You can locate replicas on: - -- Different servers within a rack to tolerate server failures -- Different servers on different racks within a datacenter to tolerate rack power/network failures -- Different servers in different datacenters to tolerate large scale network or power outages - -In a CockroachDB cluster spread across multiple geographic regions, the round-trip latency between regions will have a direct effect on your database's performance. In such cases, it is important to think about the latency requirements of each table and then use the appropriate [data topologies](topology-patterns.html) to locate data for optimal performance and resiliency. For a step-by-step demonstration, see [Low Latency Multi-Region Deployment](demo-low-latency-multi-region-deployment.html). - -**Automated Repair** - -For short-term failures, such as a server restart, CockroachDB uses Raft to continue seamlessly as long as a majority of replicas remain available. Raft makes sure that a new “leader” for each group of replicas is elected if the former leader fails, so that transactions can continue and affected replicas can rejoin their group once they’re back online. For longer-term failures, such as a server/rack going down for an extended period of time or a datacenter outage, CockroachDB automatically rebalances replicas from the missing nodes, using the unaffected replicas as sources. Using capacity information from the gossip network, new locations in the cluster are identified and the missing replicas are re-replicated in a distributed fashion using all available nodes and the aggregate disk and network bandwidth of the cluster. - -## How is CockroachDB strongly-consistent? - -CockroachDB guarantees [serializable SQL transactions](demo-serializable.html), the highest isolation level defined by the SQL standard. It does so by combining the Raft consensus algorithm for writes and a custom time-based synchronization algorithms for reads. - -- Stored data is versioned with MVCC, so [reads simply limit their scope to the data visible at the time the read transaction started](architecture/transaction-layer.html#time-and-hybrid-logical-clocks). - -- Writes are serviced using the [Raft consensus algorithm](https://raft.github.io/), a popular alternative to Paxos. A consensus algorithm guarantees that any majority of replicas together always agree on whether an update was committed successfully. Updates (writes) must reach a majority of replicas (2 out of 3 by default) before they are considered committed. - - To ensure that a write transaction does not interfere with read transactions that start after it, CockroachDB also uses a [timestamp cache](architecture/transaction-layer.html#timestamp-cache) which remembers when data was last read by ongoing transactions. - - This ensures that clients always observe serializable consistency with regards to other concurrent transactions. - -## How is CockroachDB both highly available and strongly consistent? - -The [CAP theorem](https://en.wikipedia.org/wiki/CAP_theorem) states that it is impossible for a distributed system to simultaneously provide more than two out of the following three guarantees: - -- Consistency -- Availability -- Partition Tolerance - -CockroachDB is a CP (consistent and partition tolerant) system. This means -that, in the presence of partitions, the system will become unavailable rather than do anything which might cause inconsistent results. For example, writes require acknowledgements from a majority of replicas, and reads require a lease, which can only be transferred to a different node when writes are possible. - -Separately, CockroachDB is also Highly Available, although "available" here means something different than the way it is used in the CAP theorem. In the CAP theorem, availability is a binary property, but for High Availability, we talk about availability as a spectrum (using terms like "five nines" for a system that is available 99.999% of the time). - -Being both CP and HA means that whenever a majority of replicas can talk to each other, they should be able to make progress. For example, if you deploy CockroachDB to three datacenters and the network link to one of them fails, the other two datacenters should be able to operate normally with only a few seconds' disruption. We do this by attempting to detect partitions and failures quickly and efficiently, transferring leadership to nodes that are able to communicate with the majority, and routing internal traffic away from nodes that are partitioned away. - -## Why is CockroachDB SQL? - -At the lowest level, CockroachDB is a distributed, strongly-consistent, transactional key-value store, but the external API is Standard SQL with extensions. This provides developers familiar relational concepts such as schemas, tables, columns, and indexes and the ability to structure, manipulate, and query data using well-established and time-proven tools and processes. Also, since CockroachDB supports the PostgreSQL wire protocol, it’s simple to get your application talking to Cockroach; just find your [PostgreSQL language-specific driver](install-client-drivers.html) and start building. - -For more details, learn our [basic CockroachDB SQL statements](learn-cockroachdb-sql.html), explore the [full SQL grammar](sql-grammar.html), and try it out via our [built-in SQL client](cockroach-sql.html). Also, to understand how CockroachDB maps SQL table data to key-value storage and how CockroachDB chooses the best index for running a query, see [SQL in CockroachDB](https://www.cockroachlabs.com/blog/sql-in-cockroachdb-mapping-table-data-to-key-value-storage/) and [Index Selection in CockroachDB](https://www.cockroachlabs.com/blog/index-selection-cockroachdb-2/). - -## Does CockroachDB support distributed transactions? - -Yes. CockroachDB distributes transactions across your cluster, whether it’s a few servers in a single location or many servers across multiple datacenters. Unlike with sharded setups, you do not need to know the precise location of data; you just talk to any node in your cluster and CockroachDB gets your transaction to the right place seamlessly. Distributed transactions proceed without downtime or additional latency while rebalancing is underway. You can even move tables – or entire databases – between data centers or cloud infrastructure providers while the cluster is under load. - -## Do transactions in CockroachDB guarantee ACID semantics? - -Yes. Every [transaction](transactions.html) in CockroachDB guarantees [ACID semantics](https://en.wikipedia.org/wiki/ACID) spanning arbitrary tables and rows, even when data is distributed. - -- **Atomicity:** Transactions in CockroachDB are “all or nothing.” If any part of a transaction fails, the entire transaction is aborted, and the database is left unchanged. If a transaction succeeds, all mutations are applied together with virtual simultaneity. For a detailed discussion of atomicity in CockroachDB transactions, see [How CockroachDB Distributes Atomic Transactions](https://www.cockroachlabs.com/blog/how-cockroachdb-distributes-atomic-transactions/). -- **Consistency:** SQL operations never see any intermediate states and move the database from one valid state to another, keeping indexes up to date. Operations always see the results of previously completed statements on overlapping data and maintain specified constraints such as unique columns. For a detailed look at how we've tested CockroachDB for correctness and consistency, see [CockroachDB Beta Passes Jepsen Testing](https://www.cockroachlabs.com/blog/cockroachdb-beta-passes-jepsen-testing/). -- **Isolation:** Transactions in CockroachDB implement the strongest ANSI isolation level: serializable (`SERIALIZABLE`). This means that transactions will never result in anomalies. For more information about transaction isolation in CockroachDB, see [Transactions: Isolation Levels](transactions.html#isolation-levels). -- **Durability:** In CockroachDB, every acknowledged write has been persisted consistently on a majority of replicas (by default, at least 2) via the [Raft consensus algorithm](https://raft.github.io/). Power or disk failures that affect only a minority of replicas (typically 1) do not prevent the cluster from operating and do not lose any data. - -## Since CockroachDB is inspired by Spanner, does it require atomic clocks to synchronize time? - -No. CockroachDB was designed to work without atomic clocks or GPS clocks. It’s a database intended to be run on arbitrary collections of nodes, from physical servers in a corp development cluster to public cloud infrastructure using the flavor-of-the-month virtualization layer. It’d be a showstopper to require an external dependency on specialized hardware for clock synchronization. However, CockroachDB does require moderate levels of clock synchronization for correctness. If clocks drift past a maximum threshold, nodes will be taken offline. It's therefore highly recommended to run [NTP](http://www.ntp.org/) or other clock synchronization software on each node. - -For more details on how CockroachDB handles unsynchronized clocks, see [Clock Synchronization](recommended-production-settings.html#clock-synchronization). And for a broader discussion of clocks, and the differences between clocks in Spanner and CockroachDB, see [Living Without Atomic Clocks](https://www.cockroachlabs.com/blog/living-without-atomic-clocks/). - -## What languages can I use to work with CockroachDB? - -CockroachDB supports the PostgreSQL wire protocol, so you can use any available PostgreSQL client drivers. We've tested it from the following languages: - -- Go -- Python -- Ruby -- Java -- JavaScript (node.js) -- C++/C -- Clojure -- PHP -- Rust - -See [Install Client Drivers](install-client-drivers.html) for more details. - -## Why does CockroachDB use the PostgreSQL wire protocol instead of the MySQL protocol? - -CockroachDB uses the PostgreSQL wire protocol because it is better documented than the MySQL protocol, and because PostgreSQL has a liberal Open Source license, similar to BSD or MIT licenses, whereas MySQL has the more restrictive GNU General Public License. - -Note, however, that the protocol used doesn't significantly impact how easy it is to port applications. Swapping out SQL network drivers is rather straightforward in nearly every language. What makes it hard to move from one database to another is the dialect of SQL in use. CockroachDB's dialect is based on PostgreSQL as well. - -## What is CockroachDB’s security model? - -You can run a secure or insecure CockroachDB cluster. When secure, client/node and inter-node communication is encrypted, and SSL certificates authenticate the identity of both clients and nodes. When insecure, there's no encryption or authentication. - -Also, CockroachDB supports common SQL privileges on databases and tables. The `root` user has privileges for all databases, while unique users can be granted privileges for specific statements at the database and table-levels. - -For more details, see our [Security Overview](security-overview.html). - -## How does CockroachDB compare to MySQL or PostgreSQL? - -While all of these databases support SQL syntax, CockroachDB is the only one that scales easily (without the manual complexity of sharding), rebalances and repairs itself automatically, and distributes transactions seamlessly across your cluster. - -For more insight, see [CockroachDB in Comparison](cockroachdb-in-comparison.html). - -## How does CockroachDB compare to Cassandra, HBase, MongoDB, or Riak? - -While all of these are distributed databases, only CockroachDB supports distributed transactions and provides strong consistency. Also, these other databases provide custom APIs, whereas CockroachDB offers standard SQL with extensions. - -For more insight, see [CockroachDB in Comparison](cockroachdb-in-comparison.html). - -## Can a PostgreSQL or MySQL application be migrated to CockroachDB? - -Yes. Most users should be able to follow the instructions in [Migrate from Postgres](migrate-from-postgres.html) or [Migrate from MySQL](migrate-from-mysql.html). Due to differences in available features and syntax, some features supported by these databases may require manual effort to port to CockroachDB. Check those pages for details. - -We also fully support [importing your data via CSV](migrate-from-csv.html). - -## Does Cockroach Labs offer a cloud database as a service? - -Yes. The CockroachCloud offering is currently in Limited Availability and accepting customers on a qualified basis. The offering provides a running CockroachDB cluster suitable to your needs, fully managed by Cockroach Labs on GCP or AWS. Benefits include: - -- No provisioning or deployment efforts for you -- Daily full backups and hourly incremental backups of your data -- Upgrades to the latest stable release of CockroachDB -- Monitoring to provide SLA-level support - -For more details, see the [CockroachCloud](../cockroachcloud/quickstart.html) docs. - -## Why did Cockroach Labs change the license for CockroachDB? - -Our past outlook on the right business model relied on a crucial norm in the OSS world: that companies could build a business around a strong open source core product without a much larger technology platform company coming along and offering the same product as a service. - -Recently, however, OSS companies have seen the rise of highly-integrated providers take advantage of their unique position to offer “as-a-service” versions of OSS products, and offer a superior user experience as a consequence of their integrations. We’ve most recently seen it happen with Amazon’s forked version of ElasticSearch. - -To respond to this breed of competitor, we changed our software licensing terms. To learn more about our motivations, see the [Licensing FAQs](licensing-faqs.html) as well as our [blog post](https://www.cockroachlabs.com/blog/oss-relicensing-cockroachdb/) about the license change. - -## Have questions that weren’t answered? - -Try searching the rest of our docs for answers or using our other [support resources](support-resources.html), including: - -- [CockroachDB Community Forum](https://forum.cockroachlabs.com) -- [CockroachDB Community Slack](https://cockroachdb.slack.com) -- [StackOverflow](http://stackoverflow.com/questions/tagged/cockroachdb) -- [CockroachDB Support Portal](https://support.cockroachlabs.com) diff --git a/src/current/v19.2/functions-and-operators.md b/src/current/v19.2/functions-and-operators.md deleted file mode 100644 index 5c872a83823..00000000000 --- a/src/current/v19.2/functions-and-operators.md +++ /dev/null @@ -1,123 +0,0 @@ ---- -title: Functions and Operators -summary: CockroachDB supports many built-in functions, aggregate functions, and operators. -toc: true ---- - -CockroachDB supports the following SQL functions and operators for use in [scalar expressions](scalar-expressions.html). - -{{site.data.alerts.callout_success}}In the built-in SQL shell, use \hf [function] to get inline help about a specific function.{{site.data.alerts.end}} - -## Special syntax forms - -The following syntax forms are recognized for compatibility with the -SQL standard and PostgreSQL, but are equivalent to regular built-in -functions: - -{% include {{ page.version.version }}/sql/function-special-forms.md %} - -## Conditional and function-like operators - -The following table lists the operators that look like built-in -functions but have special evaluation rules: - - Operator | Description -----------|------------- - `ANNOTATE_TYPE(...)` | [Explicitly Typed Expression](scalar-expressions.html#explicitly-typed-expressions) - `ARRAY(...)` | [Conversion of Subquery Results to An Array](scalar-expressions.html#conversion-of-subquery-results-to-an-array) - `ARRAY[...]` | [Conversion of Scalar Expressions to An Array](scalar-expressions.html#array-constructors) - `CAST(...)` | [Type Cast](scalar-expressions.html#explicit-type-coercions) - `COALESCE(...)` | [First non-NULL expression with Short Circuit](scalar-expressions.html#coalesce-and-ifnull-expressions) - `EXISTS(...)` | [Existence Test on the Result of Subqueries](scalar-expressions.html#existence-test-on-the-result-of-subqueries) - `IF(...)` | [Conditional Evaluation](scalar-expressions.html#if-expressions) - `IFNULL(...)` | Alias for `COALESCE` restricted to two operands - `NULLIF(...)` | [Return `NULL` conditionally](scalar-expressions.html#nullif-expressions) - `ROW(...)` | [Tuple Constructor](scalar-expressions.html#tuple-constructor) - -## Built-in functions - -{% remote_include https://raw.githubusercontent.com/cockroachdb/cockroach/{{ page.release_info.crdb_branch_name }}/docs/generated/sql/functions.md %} - -## Aggregate functions - -For examples showing how to use aggregate functions, see [the `SELECT` clause documentation](select-clause.html#aggregate-functions). - -{{site.data.alerts.callout_info}} -New in v19.2: Non-commutative aggregate functions are sensitive to the order in which the rows are processed in the surrounding [`SELECT` clause](select-clause.html#aggregate-functions). To specify the order in which input rows are processed, you can add an [`ORDER BY`](query-order.html) clause within the function argument list. For examples, see the [`SELECT` clause](select-clause.html#order-aggregate-function-input-rows-by-column) documentation. -{{site.data.alerts.end}} - -{% remote_include https://raw.githubusercontent.com/cockroachdb/cockroach/{{ page.release_info.crdb_branch_name }}/docs/generated/sql/aggregates.md %} - -## Window functions - -{% remote_include https://raw.githubusercontent.com/cockroachdb/cockroach/{{ page.release_info.crdb_branch_name }}/docs/generated/sql/window_functions.md %} - -## Operators - -The following table lists all CockroachDB operators from highest to lowest precedence, i.e., the order in which they will be evaluated within a statement. Operators with the same precedence are left associative. This means that those operators are grouped together starting from the left and moving right. - -| Order of Precedence | Operator | Name | Operator Arity | -| ------------------- | -------- | ---- | -------------- | -| 1 | `.` | Member field access operator | binary | -| 2 | `::` | [Type cast](scalar-expressions.html#explicit-type-coercions) | binary | -| 3 | `-` | Unary minus | unary (prefix) | -| | `~` | Bitwise not | unary (prefix) | -| 4 | `^` | Exponentiation | binary | -| 5 | `*` | Multiplication | binary | -| | `/` | Division | binary | -| | `//` | Floor division | binary | -| | `%` | Modulo | binary | -| 6 | `+` | Addition | binary | -| | `-` | Subtraction | binary | -| 7 | `<<` | Bitwise left-shift | binary | -| | `>>` | Bitwise right-shift | binary | -| 8 | `&` | Bitwise AND | binary | -| 9 | `#` | Bitwise XOR | binary | -| 10 | | | Bitwise OR | binary | -| 11 | || | Concatenation | binary | -| | `< ANY`, ` SOME`, ` ALL` | [Multi-valued] "less than" comparison | binary | -| | `> ANY`, ` SOME`, ` ALL` | [Multi-valued] "greater than" comparison | binary | -| | `= ANY`, ` SOME`, ` ALL` | [Multi-valued] "equal" comparison | binary | -| | `<= ANY`, ` SOME`, ` ALL` | [Multi-valued] "less than or equal" comparison | binary | -| | `>= ANY`, ` SOME`, ` ALL` | [Multi-valued] "greater than or equal" comparison | binary | -| | `<> ANY` / `!= ANY`, `<> SOME` / `!= SOME`, `<> ALL` / `!= ALL` | [Multi-valued] "not equal" comparison | binary | -| | `[NOT] LIKE ANY`, `[NOT] LIKE SOME`, `[NOT] LIKE ALL` | [Multi-valued] `LIKE` comparison | binary | -| | `[NOT] ILIKE ANY`, `[NOT] ILIKE SOME`, `[NOT] ILIKE ALL` | [Multi-valued] `ILIKE` comparison | binary | -| 12 | `[NOT] BETWEEN` | Value is [not] within the range specified | binary | -| | `[NOT] BETWEEN SYMMETRIC` | Like `[NOT] BETWEEN`, but in non-sorted order. For example, whereas `a BETWEEN b AND c` means `b <= a <= c`, `a BETWEEN SYMMETRIC b AND c` means `(b <= a <= c) OR (c <= a <= b)`. | binary | -| | `[NOT] IN` | Value is [not] in the set of values specified | binary | -| | `[NOT] LIKE` | Matches [or not] LIKE expression, case sensitive | binary | -| | `[NOT] ILIKE` | Matches [or not] LIKE expression, case insensitive | binary | -| | `[NOT] SIMILAR` | Matches [or not] SIMILAR TO regular expression | binary | -| | `~` | Matches regular expression, case sensitive | binary | -| | `!~` | Does not match regular expression, case sensitive | binary | -| | `~*` | Matches regular expression, case insensitive | binary | -| | `!~*` | Does not match regular expression, case insensitive | binary | -| 13 | `=` | Equal | binary | -| | `<` | Less than | binary | -| | `>` | Greater than | binary | -| | `<=` | Less than or equal to | binary | -| | `>=` | Greater than or equal to | binary | -| | `!=`, `<>` | Not equal | binary | -| 14 | `IS [DISTINCT FROM]` | Equal, considering `NULL` as value | binary | -| | `IS NOT [DISTINCT FROM]` | `a IS NOT b` equivalent to `NOT (a IS b)` | binary | -| | `ISNULL`, `IS UNKNOWN` , `NOTNULL`, `IS NOT UNKNOWN` | Equivalent to `IS NULL` / `IS NOT NULL` | unary (postfix) | -| | `IS NAN`, `IS NOT NAN` | [Comparison with the floating-point NaN value](scalar-expressions.html#comparison-with-nan) | unary (postfix) | -| | `IS OF(...)` | Type predicate | unary (postfix) -| 15 | `NOT` | [Logical NOT](scalar-expressions.html#logical-operators) | unary | -| 16 | `AND` | [Logical AND](scalar-expressions.html#logical-operators) | binary | -| 17 | `OR` | [Logical OR](scalar-expressions.html#logical-operators) | binary | - -[Multi-valued]: scalar-expressions.html#multi-valued-comparisons - -### Supported operations - -{% include v19.2/sql/operators.md %} - - diff --git a/src/current/v19.2/get-started-with-enterprise-trial.md b/src/current/v19.2/get-started-with-enterprise-trial.md deleted file mode 100644 index bd0c0772a26..00000000000 --- a/src/current/v19.2/get-started-with-enterprise-trial.md +++ /dev/null @@ -1,55 +0,0 @@ ---- -title: Enterprise Trial –– Get Started -summary: Check out this page to get started with your CockroachDB Enterprise Trial -toc: true -license: true ---- - -Congratulations on starting your CockroachDB Enterprise Trial! With it, you'll not only get access to CockroachDB's core capabilities like [high availability](frequently-asked-questions.html#how-does-cockroachdb-survive-failures) and [`SERIALIZABLE` isolation](frequently-asked-questions.html#how-is-cockroachdb-strongly-consistent), but also our Enterprise-only features like distributed [`BACKUP`](backup.html) & [`RESTORE`](restore.html), [geo-partitioning](partitioning.html), and [cluster visualization](enable-node-map.html). - -## Install CockroachDB - -If you haven't already, you'll need to [locally install](install-cockroachdb.html), [remotely deploy](manual-deployment.html), or [orchestrate](orchestration.html) CockroachDB. - -## Enable Enterprise features - -As the CockroachDB `root` user, open the [built-in SQL shell](cockroach-sql.html) in insecure or secure mode, as per your CockroachDB setup. In the following example, we assume that CockroachDB is running in insecure mode. - -{% include copy-clipboard.html %} -~~~ shell -$ cockroach sql --insecure -~~~ - -{{site.data.alerts.callout_info}} -If you've secured your deployment, you'll need to [include the flags for your certificates](cockroach-cert.html) instead of the `--insecure` flag. -{{site.data.alerts.end}} - -Now, use the `SET CLUSTER SETTING` command to set the name of your organization and the license key: - -{% include copy-clipboard.html %} -~~~ sql -> SET CLUSTER SETTING cluster.organization = 'Acme Company'; SET CLUSTER SETTING enterprise.license = 'xxxxxxxxxxxx'; -~~~ - -Then verify your organization in response to the following query: - -{% include copy-clipboard.html %} -~~~ sql -> SHOW CLUSTER SETTING cluster.organization; -~~~ - -## Use Enterprise features - -Your cluster now has access to all of CockroachDB's enterprise features for the length of the trial: - -{% include {{ page.version.version }}/misc/enterprise-features.md %} - -## Getting help - -If you or your team need any help during your trial, our engineers are available on [CockroachDB Community Forum](https://forum.cockroachlabs.com), [our forum](https://forum.cockroachlabs.com/), or [GitHub](https://github.com/cockroachdb/cockroach).

      - -## See also - -- [Enterprise Licensing](enterprise-licensing.html) -- [`SET CLUSTER SETTING`](set-cluster-setting.html) -- [`SHOW CLUSTER SETTING`](show-cluster-setting.html) diff --git a/src/current/v19.2/grant-roles.md b/src/current/v19.2/grant-roles.md deleted file mode 100644 index 09d6f5a1c1f..00000000000 --- a/src/current/v19.2/grant-roles.md +++ /dev/null @@ -1,89 +0,0 @@ ---- -title: GRANT <roles> -summary: The GRANT statement grants user privileges for interacting with specific databases and tables. -toc: true ---- - -The `GRANT ` [statement](sql-statements.html) lets you add a [role](authorization.html#create-and-manage-roles) or [user](authorization.html#create-and-manage-users) as a member to a role. - -{{site.data.alerts.callout_info}} -GRANT <roles> is no longer an enterprise feature and is now freely available in the core version of CockroachDB. -{{site.data.alerts.end}} - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/grant_roles.html %}
      - -## Required privileges - -The user granting role membership must be a role admin (i.e., members with the `ADMIN OPTION`) or a superuser (i.e., a member of the `admin` role). - -## Considerations - -- Users and roles can be members of roles. -- The `root` user is automatically created as an `admin` role and assigned the `ALL` privilege for new databases. -- All privileges of a role are inherited by all its members. -- Membership loops are not allowed (direct: `A is a member of B is a member of A` or indirect: `A is a member of B is a member of C ... is a member of A`). - -## Parameters - -Parameter | Description -----------|------------ -`role_name` | The name of the role to which you want to add members. To add members to multiple roles, use a comma-separated list of role names. -`user_name` | The name of the [user](authorization.html#create-and-manage-users) or [role](authorization.html#create-and-manage-roles) to whom you want to grant membership. To add multiple members, use a comma-separated list of user and/or role names. -`WITH ADMIN OPTION` | Designate the user as an role admin. Role admins can grant or revoke membership for the specified role. - -## Examples - -### Grant role membership - -{% include copy-clipboard.html %} -~~~ sql -> GRANT design TO ernie; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW GRANTS ON ROLE design; -~~~ -~~~ -+--------+---------+---------+ -| role | member | isAdmin | -+--------+---------+---------+ -| design | barkley | false | -| design | ernie | false | -| design | lola | false | -| design | lucky | false | -+--------+---------+---------+ -~~~ - -### Grant the admin option - -{% include copy-clipboard.html %} -~~~ sql -> GRANT design TO ERNIE WITH ADMIN OPTION; -~~~ -{% include copy-clipboard.html %} -~~~ sql -> SHOW GRANTS ON ROLE design; -~~~ -~~~ -+--------+---------+---------+ -| role | member | isAdmin | -+--------+---------+---------+ -| design | barkley | false | -| design | ernie | true | -| design | lola | false | -| design | lucky | false | -+--------+---------+---------+ -~~~ - -## See also - -- [Authorization](authorization.html) -- [`REVOKE `](revoke-roles.html) -- [`GRANT `](grant.html) -- [`REVOKE `](revoke.html) -- [`SHOW GRANTS`](show-grants.html) -- [`SHOW ROLES`](show-roles.html) -- [Manage Users](authorization.html#create-and-manage-users) diff --git a/src/current/v19.2/grant.md b/src/current/v19.2/grant.md deleted file mode 100644 index 8cc315e63dc..00000000000 --- a/src/current/v19.2/grant.md +++ /dev/null @@ -1,158 +0,0 @@ ---- -title: GRANT <privileges> -summary: The GRANT statement grants user privileges for interacting with specific databases and tables. -toc: true ---- - -The `GRANT ` [statement](sql-statements.html) lets you control each [role](authorization.html#create-and-manage-roles) or [user's](authorization.html#create-and-manage-users) SQL [privileges](authorization.html#assign-privileges) for interacting with specific databases and tables. - -For privileges required by specific statements, see the documentation for the respective [SQL statement](sql-statements.html). - -## Synopsis - -
      {% include {{ page.version.version }}/sql/diagrams/grant_privileges.html %}
      - -## Required privileges - -The user granting privileges must have the `GRANT` privilege on the target databases or tables. - -## Supported privileges - -Roles and users can be granted the following privileges. Some privileges are applicable both for databases and tables, while other are applicable only for tables (see **Levels** in the table below). - -- When a role or user is granted privileges for a database, new tables created in the database will inherit the privileges, but the privileges can then be changed. - - {{site.data.alerts.callout_info}} - The user does not get privileges to existing tables in the database. To grant privileges to a user on all existing tables in a database, see [Grant privileges on all tables in a database](#grant-privileges-on-all-tables-in-a-database) - {{site.data.alerts.end}} - -- When a role or user is granted privileges for a table, the privileges are limited to the table. -- The `root` user automatically belongs to the `admin` role and has the `ALL` privilege for new databases. -- For privileges required by specific statements, see the documentation for the respective [SQL statement](sql-statements.html). - -Privilege | Levels -----------|------------ -`ALL` | Database, Table -`CREATE` | Database, Table -`DROP` | Database, Table -`GRANT` | Database, Table -`SELECT` | Table -`INSERT` | Table -`DELETE` | Table -`UPDATE` | Table - -## Parameters - -Parameter | Description -----------|------------ -`table_name` | A comma-separated list of table names. Alternately, to grant privileges to all tables, use `*`. `ON TABLE table.*` grants apply to all existing tables in a database but will not affect tables created after the grant. -`database_name` | A comma-separated list of database names.

      Privileges granted on databases will be inherited by any new tables created in the databases, but do not affect existing tables in the database. -`user_name` | A comma-separated list of [users](authorization.html#create-and-manage-users) and/or [roles](authorization.html#create-and-manage-roles) to whom you want to grant privileges. - -## Examples - -### Grant privileges on databases - -{% include copy-clipboard.html %} -~~~ sql -> GRANT CREATE ON DATABASE db1, db2 TO maxroach, betsyroach; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW GRANTS ON DATABASE db1, db2; -~~~ - -~~~ -+----------+------------+------------+ -| Database | User | Privileges | -+----------+------------+------------+ -| db1 | betsyroach | CREATE | -| db1 | maxroach | CREATE | -| db1 | root | ALL | -| db2 | betsyroach | CREATE | -| db2 | maxroach | CREATE | -| db2 | root | ALL | -+----------+------------+------------+ -(6 rows) -~~~ - -### Grant privileges on specific tables in a database - -{% include copy-clipboard.html %} -~~~ sql -> GRANT DELETE ON TABLE db1.t1, db1.t2 TO betsyroach; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW GRANTS ON TABLE db1.t1, db1.t2; -~~~ - -~~~ -+-------+------------+------------+ -| Table | User | Privileges | -+-------+------------+------------+ -| t1 | betsyroach | DELETE | -| t1 | root | ALL | -| t2 | betsyroach | DELETE | -| t2 | root | ALL | -+-------+------------+------------+ -(4 rows) -~~~ - -### Grant privileges on all tables in a database - -{% include copy-clipboard.html %} -~~~ sql -> GRANT SELECT ON TABLE db2.* TO henryroach; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW GRANTS ON TABLE db2.*; -~~~ - -~~~ -+-------+------------+------------+ -| Table | User | Privileges | -+-------+------------+------------+ -| t1 | henryroach | SELECT | -| t1 | root | ALL | -| t2 | henryroach | SELECT | -| t2 | root | ALL | -+-------+------------+------------+ -(4 rows) -~~~ - -### Make a table readable to every user in the system - -{% include copy-clipboard.html %} -~~~ sql -> GRANT SELECT ON TABLE myTable TO public; -~~~ - -{% include copy-clipboard.html %} -~~~ sql -> SHOW GRANTS ON TABLE myTable; -~~~ - -~~~ - database_name | schema_name | table_name | grantee | privilege_type -+---------------+-------------+------------+---------+----------------+ - defaultdb | public | mytable | admin | ALL - defaultdb | public | mytable | public | SELECT - defaultdb | public | mytable | root | ALL -(3 rows) -~~~ - - -## See also - -- [Authorization](authorization.html) -- [`REVOKE `](revoke-roles.html) -- [`GRANT `](grant-roles.html) -- [`REVOKE `](revoke.html) -- [`SHOW GRANTS`](show-grants.html) -- [`SHOW ROLES`](show-roles.html) -- [Manage Users](authorization.html#create-and-manage-users) diff --git a/src/current/v19.2/gssapi_authentication.md b/src/current/v19.2/gssapi_authentication.md deleted file mode 100644 index a423bf35efc..00000000000 --- a/src/current/v19.2/gssapi_authentication.md +++ /dev/null @@ -1,246 +0,0 @@ ---- -title: GSSAPI Authentication (Enterprise) -summary: Learn about the GSSAPI authentication features for secure CockroachDB clusters. -toc: true ---- - -CockroachDB supports the Generic Security Services API (GSSAPI) with Kerberos authentication. - -{{site.data.alerts.callout_info}} -GSSAPI authentication is an [enterprise-only](enterprise-licensing.html) feature. -{{site.data.alerts.end}} - -## Requirements - -- A working Active Directory or Kerberos environment -- A Service Principal -- A GSSAPI-compatible Postgres Client (psql, etc.) -- A client machine with a Kerberos client installed and configured - -## Configuring KDC for CockroachDB - -To use Kerberos authentication with CockroachDB, configure a Kerberos service principal name (SPN) for CockroachDB and generate a valid keytab file with the following specifications: - -- Set the SPN to the name specified by your client driver. For example, if you use the psql client, set SPN to `postgres`. -- Create SPNs for all DNS addresses that a user would use to connect to your CockroachDB cluster (including any TCP load balancers between the user and the CockroachDB node) and ensure that the keytab contains the keys for every SPN you create. - -### Active Directory - -For Active Directory, the client syntax for generating a keytab that maps a service principal to the SPN is as follows: - -{% include copy-clipboard.html %} -~~~ shell -$ ktpass -out {keytab_filename} -princ {Client_SPN}/{NODE/LB_FQDN}@{DOMAIN} -mapUser {Service_Principal}@{DOMAIN} -mapOp set -pType KRB5_NT_PRINCIPAL +rndPass -crypto AES256-SHA1 -~~~ - -Example: - -{% include copy-clipboard.html %} -~~~ shell -$ ktpass -out postgres.keytab -princ postgres/loadbalancer1.cockroach.industries@COCKROACH.INDUSTRIES -mapUser pguser@COCKROACH.INDUSTRIES -mapOp set -pType KRB5_NT_PRINCIPAL +rndPass -crypto AES256-SHA1 -~~~ - -Copy the resulting keytab to the database nodes. If clients are connecting to multiple addresses (more than one load balancer, or clients connecting directly to nodes), you will need to generate a keytab for each client endpoint. You may want to merge your keytabs together for easier management. You can do this using the `ktpass` command, using the following syntax: - -{% include copy-clipboard.html %} -~~~ shell -$ ktpass -out {new_keytab_filename} -in {old_keytab_filename} -princ {Client_SPN}/{NODE/LB_FQDN}@{DOMAIN} -mapUser {Service_Principal}@{DOMAIN} -mapOp add -pType KRB5_NT_PRINCIPAL +rndPass -crypto AES256-SHA1 -~~~ - -Example (adds `loadbalancer2` to the above example): - -{% include copy-clipboard.html %} -~~~ shell -$ ktpass -out postgres_2lb.keytab -in postgres.keytab -princ postgres/loadbalancer2.cockroach.industries@COCKROACH.INDUSTRIES -mapUser pguser@COCKROACH.INDUSTRIES -mapOp add -pType KRB5_NT_PRINCIPAL +rndPass -crypto AES256-SHA1 -~~~ - -### MIT KDC - -In MIT KDC, you cannot map a service principal to an SPN with a different username, so you will need to create a service principal that includes the SPN for your client. - -{% include copy-clipboard.html %} -~~~ shell -$ create-user: kadmin.local -q "addprinc {SPN}/{CLIENT_FQDN}@{DOMAIN}" -pw "{initial_password}" -~~~ - -{% include copy-clipboard.html %} -~~~ shell -$ create-keytab: kadmin.local -q "ktadd -k keytab {SPN}/{CLIENT_FQDN}@{DOMAIN}" -~~~ - -Example: - -{% include copy-clipboard.html %} -~~~ shell -$ kadmin.local -q "addprinc postgres/client2.cockroach.industries@COCKROACH.INDUSTRIES" -pw "testing12345!" -$ kadmin.local -q "ktadd -k keytab postgres/client2.cockroach.industries@COCKROACH.INDUSTRIES" -~~~ - -Copy the resulting keytab to the database nodes. If clients are connecting to multiple addresses (more than one load balancer, or clients connecting directly to nodes), you will need to generate a keytab for each client endpoint. You may want to merge your keytabs together for easier management. The `ktutil` command can be used to read multiple keytab files and output them into a single output [here](https://web.mit.edu/kerberos/krb5-devel/doc/admin/admin_commands/ktutil.html). - - -## Configuring the CockroachDB node -1. Copy the keytab file to a location accessible by the `cockroach` binary. - -2. [Create certificates](cockroach-cert.html) for inter-node and `root` user authentication: - - {% include copy-clipboard.html %} - ~~~ shell - $ mkdir certs my-safe-directory - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-ca \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-node \ - localhost \ - $(hostname) \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach cert create-client \ - root \ - --certs-dir=certs \ - --ca-key=my-safe-directory/ca.key - ~~~ - -3. Provide the path to the keytab in the `KRB5_KTNAME` environment variable. - - Example: `export KRB5_KTNAME=/home/cockroach/postgres.keytab` - -4. Start a CockroachDB node: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach start \ - --certs-dir=certs \ - --listen-addr=0.0.0.0 - ~~~ - -5. Connect to CockroachDB as `root` using the `root` client certificate generated above: - - {% include copy-clipboard.html %} - ~~~ shell - $ cockroach sql --certs-dir=certs - ~~~ - -6. [Enable an enterprise license](enterprise-licensing.html#obtain-a-license). - {{site.data.alerts.callout_info}} You need the enterprise license if you want to use the GSSAPI feature. However, if you only want to test that the GSSAPI setup is working, you do not need to enable an enterprise license. {{site.data.alerts.end}} - -7. Enable GSSAPI authentication: - - {% include copy-clipboard.html %} - ~~~ sql - > SET cluster setting server.host_based_authentication.configuration = 'host all all all gss include_realm=0'; - ~~~ - - Setting the `server.host_based_authentication.configuration` [cluster setting](cluster-settings.html) makes it mandatory for all users (except `root`) to authenticate using GSSAPI. The `root` user is still required to authenticate using its client certificate. - - The `include_realm=0` option is required to tell CockroachDB to remove the `@DOMAIN.COM` realm information from the username. We do not support any advanced mapping of GSSAPI usernames to CockroachDB usernames right now. If you want to limit which realms' users can connect, you can also add one or more `krb_realm` parameters to the end of the line as an allowlist, as follows: `host all all all gss include_realm=0 krb_realm=domain.com krb_realm=corp.domain.com` - - The syntax is based on the `pg_hba.conf` standard for PostgreSQL which is documented [here](https://www.postgresql.org/docs/current/auth-pg-hba-conf.html). It can be used to exclude other users from Kerberos authentication. - -8. Create CockroachDB users for every Kerberos user. Ensure the username does not have the `DOMAIN.COM` realm information. For example, if one of your Kerberos users has a username `carl@realm.com`, then you need to create a CockroachDB user with the username `carl`: - - {% include copy-clipboard.html %} - ~~~ sql - > CREATE USER carl; - ~~~ - - Grant privileges to the user: - - {% include copy-clipboard.html %} - ~~~ sql - > GRANT ALL ON DATABASE defaultdb TO carl; - ~~~ - -## Configuring the client - -{{site.data.alerts.callout_info}} -The `cockroach sql` shell does not yet support GSSAPI authentication. You need to use a GSSAPI-compatible Postgres client, such as Postgres's `psql` client. -{{site.data.alerts.end}} - -1. Install and configure your Kerberos client: - - For CentOS/RHEL systems, run: - - {% include copy-clipboard.html %} - ~~~ shell - $ yum install krb5-user - ~~~ - - For Ubuntu/Debian systems, run: - - {% include copy-clipboard.html %} - ~~~ shell - $ apt-get install krb5-user - ~~~ - - Edit the `/etc/krb5.conf` file to include: - - {% include copy-clipboard.html %} - ~~~ - [libdefaults] - default_realm = {REALM} - - [realms] - {REALM} = { - kdc = {fqdn-kdc-server or ad-server} - admin_server = {fqdn-kdc-server or ad-server} - default_domain = {realm-lower-case} - } - ~~~ - - Example: - - {% include copy-clipboard.html %} - ~~~ - - [libdefaults] - default_realm = COCKROACH.INDUSTRIES - - [realms] - COCKROACH.INDUSTRIES = { - kdc = ad.cockroach.industries - admin_server = ad.cockroach.industries - default_domain = cockroach.industries - } - ~~~ - -2. Get a ticket for the db user: - - {% include copy-clipboard.html %} - ~~~ shell - $ kinit carl - ~~~ - -3. Verify if a valid ticket has been generated: - - {% include copy-clipboard.html %} - ~~~ shell - $ klist - ~~~ - -4. Install the Postgres client (for example, postgresql-client-10 Debian package from postgresql.org). -5. Use the `psql` client, which supports GSSAPI authentication, to connect to CockroachDB: - - {% include copy-clipboard.html %} - ~~~ shell - $ psql "postgresql://localhost:26257/defaultdb?sslmode=require" -U carl - ~~~ - -4. If you specified an enterprise license earlier, you should now have a Postgres shell in CockroachDB, indicating that the GSSAPI authentication was successful. If you did not specify an enterprise license, you'll see a message like this: `psql: ERROR: use of GSS authentication requires an enterprise license.` If you see this message, GSSAPI authentication is set up correctly. - -## See also - -- [Authentication](authentication.html) -- [Create Security Certificates](cockroach-cert.html) diff --git a/src/current/v19.2/hello-world-example-apps.md b/src/current/v19.2/hello-world-example-apps.md deleted file mode 100644 index 4825c817083..00000000000 --- a/src/current/v19.2/hello-world-example-apps.md +++ /dev/null @@ -1,39 +0,0 @@ ---- -title: Hello World Example Apps -summary: Examples that show you how to build a simple "Hello World" application with CockroachDB -tags: golang, python, java -toc: true ---- - -The examples in this section show you how to build simple "Hello World" applications using CockroachDB. - -## Apps - -Click the links in the table below to see simple but complete example applications for each supported language and library combination. - -If you are looking to do a specific task such as connect to the database, insert data, or run multi-statement transactions, see [this list of tasks](#tasks). - -{% include {{page.version.version}}/misc/drivers.md %} - -## See also - -Reference information: - -- [Client drivers](install-client-drivers.html) -- [Third-party database tools](third-party-database-tools.html) -- [Connection parameters](connection-parameters.html) -- [Transactions](transactions.html) -- [Performance best practices](performance-best-practices-overview.html) - - - -Specific tasks: - -- [Connect to the Database](connect-to-the-database.html) -- [Insert Data](insert-data.html) -- [Query Data](query-data.html) -- [Update Data](update-data.html) -- [Delete Data](delete-data.html) -- [Make Queries Fast](make-queries-fast.html) -- [Run Multi-Statement Transactions](run-multi-statement-transactions.html) -- [Error Handling and Troubleshooting](error-handling-and-troubleshooting.html) diff --git a/src/current/v19.2/import-into.md b/src/current/v19.2/import-into.md deleted file mode 100644 index 47c5576664a..00000000000 --- a/src/current/v19.2/import-into.md +++ /dev/null @@ -1,205 +0,0 @@ ---- -title: IMPORT INTO -summary: Import CSV data into an existing CockroachDB table. -toc: true ---- - -New in v19.2: The `IMPORT INTO` [statement](sql-statements.html) imports CSV data into an [existing table](create-table.html). `IMPORT INTO` appends new rows onto the table. - -## Considerations - -- `IMPORT INTO` only works for existing tables. For information on how to import data into new tables, see [`IMPORT`](import.html). -- `IMPORT INTO` cannot be used within a [transaction](transactions.html) or during a [rolling upgrade](upgrade-cockroach-version.html). -- `IMPORT INTO` invalidates all [foreign keys](foreign-key.html) on the target table. To validate the foreign key(s), use the [`VALIDATE CONSTRAINT`](validate-constraint.html) statement. -- `IMPORT INTO` cannot be used to insert data into a column for an existing row. To do this, use [`INSERT`](insert.html). - -## Required privileges - -Only members of the `admin` role can run `IMPORT INTO`. By default, the `root` user belongs to the `admin` role. - -## Synopsis - -
      - {% include {{ page.version.version }}/sql/diagrams/import_into.html %} -
      - -{{site.data.alerts.callout_info}} -While importing into an existing table, the table is taken offline. -{{site.data.alerts.end}} - -## Parameters - -Parameter | Description -----------|------------ -`table_name` | The name of the table you want to import into. -`column_name` | The table columns you want to import.

      Note: Currently, target columns are not enforced. -`file_location` | The [URL](#import-file-urls) of a CSV file containing the table data. This can be a comma-separated list of URLs to CSV files. For an example, see [Import into an existing table from multiple CSV files](#import-into-an-existing-table-from-multiple-csv-files) below. -`