Skip to content

Conversation

@taroface
Copy link
Contributor

@taroface taroface commented Dec 8, 2025

DOC-14789

Update the ALTER TABLE example to drop & add constraint atomically. For the following reasons, this example did not work (and may have never worked):

  • The commands alter a PK with the wrong name (primary, not tablename_pkey as per convention).
  • The PK on the table used in the example (users) is referenced by several FKs in the MovR dataset, so the txn actually rolls back.
  • In 24.1, autocommit_before_ddl was introduced, which causes the txn to commit before issuing the DDL, which causes the statements to fail.
  • In 25.3, create_table_with_schema_locked was enabled by default, and the MovR tables were adjusted accordingly. This means the tables disallow schema changes unless the schema_locked storage param is disabled on the table.

The examples are updated (appropriately for each version) to use a different MovR table (promo_codes), use the correct PK name, disable autocommit_before_ddl, and disable/re-enable schema_locked to allow the example to work.

@netlify
Copy link

netlify bot commented Dec 8, 2025

Deploy Preview for cockroachdb-interactivetutorials-docs canceled.

Name Link
🔨 Latest commit 9fcb5fc
🔍 Latest deploy log https://app.netlify.com/projects/cockroachdb-interactivetutorials-docs/deploys/69373f2e990daf000841db53

@netlify
Copy link

netlify bot commented Dec 8, 2025

Deploy Preview for cockroachdb-api-docs canceled.

Name Link
🔨 Latest commit 9fcb5fc
🔍 Latest deploy log https://app.netlify.com/projects/cockroachdb-api-docs/deploys/69373f2e13835b0008003235

@netlify
Copy link

netlify bot commented Dec 8, 2025

Netlify Preview

Name Link
🔨 Latest commit 9fcb5fc
🔍 Latest deploy log https://app.netlify.com/projects/cockroachdb-docs/deploys/69373f2e88fe2f0008b305aa
😎 Deploy Preview https://deploy-preview-21542--cockroachdb-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@taroface taroface requested a review from a team as a code owner December 8, 2025 21:12
Copy link
Contributor

@rmloveland rmloveland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo the multi DDL inside one transaction thing which we explicitly recommend against in https://www.cockroachlabs.com/docs/v25.4/online-schema-changes#schema-changes-within-transactions

gem "rss"
gem "webrick"
gem "jekyll-minifier"
gem "bundler", "~> 2.4"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this should be a separate docs-infra type PR

#### Demo
{% include_cached youtube.html video_id="MPx-LXY2D-c" %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh man -1000 points to slytherin for adding YT links and then making private during a "cleanup"

#### Drop and add a primary key constraint

Suppose that you want to add `name` to the composite primary key of the `users` table, [without creating a secondary index of the existing primary key](#changing-primary-keys-with-add-constraint-primary-key).
Suppose that you want to add `creation_time` to the composite primary key of the `promo_codes` table, [without creating a secondary index of the existing primary key](#changing-primary-keys-with-add-constraint-primary-key).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ALTER TABLE promo_codes ALTER COLUMN creation_time SET NOT NULL;
~~~

1. Turn off autocommit for DDL for the current session, so that multiple DDL statements can execute within one explicit transaction:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not recommend this, running multiple DDLs in one transaction is not recommended: https://www.cockroachlabs.com/docs/v24.3/online-schema-changes#schema-changes-within-transactions

recommend doing this in two separate implicit transactions one after the other (unless there is a reason i'm missing here why they need to be in the same transaction? if it's just to keep the example neater i'd argue that's not a good reason)

#### Demo
{% include_cached youtube.html video_id="MPx-LXY2D-c" %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1000 points to ravenclaw

#### Demo
{% include_cached youtube.html video_id="MPx-LXY2D-c" %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1000 points to slytherin

ALTER TABLE promo_codes ALTER COLUMN creation_time SET NOT NULL;
~~~

1. Turn off autocommit for DDL for the current session, so that multiple DDL statements can execute within one explicit transaction:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revise to not do multi DDLs in one transaction per the holy book

#### Demo
{% include_cached youtube.html video_id="MPx-LXY2D-c" %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1000 points to gryffindor (i was hoping to avoid dinging gryffindor here but have to be fair)

ALTER TABLE promo_codes ALTER COLUMN creation_time SET NOT NULL;
~~~

1. Turn off autocommit for DDL for the current session, so that multiple DDL statements can execute within one explicit transaction:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above re: please revise to not do multi-ddl in one transaction, i think these can just be two implicit transactions in a row

#### Demo
{% include_cached youtube.html video_id="MPx-LXY2D-c" %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i shall look askance at YT links from now on i must admit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants