Skip to content

sql,externalconn: add ALTER EXTERNAL CONNECTION - #146829

Merged
craig[bot] merged 1 commit into
cockroachdb:masterfrom
edwardguo-crl:feature-alter-ext-connection-parser
Jul 10, 2025
Merged

craig[bot] merged 1 commit into
cockroachdb:masterfrom
edwardguo-crl:feature-alter-ext-connection-parser

Conversation

@edwardguo-crl

@edwardguo-crl edwardguo-crl commented May 15, 2025 •

Copy link
Copy Markdown
Contributor

Summary

Add AST parser support for the new SQL command ALTER EXTERNAL CONNECTION, allowing users to modify existing external connections.

Fixes #98610

@edwardguo-crl
edwardguo-crl requested a review from msbutler May 15, 2025 18:49
@edwardguo-crl
edwardguo-crl requested review from a team as code owners May 15, 2025 18:50
@edwardguo-crl
edwardguo-crl requested a review from a team May 15, 2025 18:50
@cockroach-teamcity

Copy link
Copy Markdown
Member

This change is Reviewable

@edwardguo-crl
edwardguo-crl force-pushed the feature-alter-ext-connection-parser branch 3 times, most recently from ba53baf to 8d269a4 Compare May 15, 2025 19:08
@edwardguo-crl
edwardguo-crl requested a review from a team as a code owner May 15, 2025 20:00
@edwardguo-crl
edwardguo-crl force-pushed the feature-alter-ext-connection-parser branch 2 times, most recently from 890218e to ae19f56 Compare May 16, 2025 17:42

@msbutler msbutler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nice start!

Comment thread pkg/sql/parser/sql.y Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i don't think the IF NOT EXISTS clause makes sense here. You can't alter an external connection that does not exist.

You should add an IF EXISTS clause instead. As an analogy, we got CREATE TABLE IF NOT EXISTS and ALTER TABLE IF EXISTS. To implement, I dont think you can use the labelSpec.

@edwardguo-crl edwardguo-crl May 16, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, I will update here and the implementation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like the comment here still needs a quick amend

@edwardguo-crl
edwardguo-crl force-pushed the feature-alter-ext-connection-parser branch from ae19f56 to f1ff80c Compare May 16, 2025 20:26
@edwardguo-crl edwardguo-crl changed the title sql,exterconn: add ALTER EXTERNAL CONNECTION parser sql,exterconn: add ALTER EXTERNAL CONNECTION May 16, 2025
Comment thread pkg/cloud/externalconn/record.go Outdated

// ExternalConnectionExist loads an mutableExternalConnection from system table. Allow owner to modifiy the URI.
func ExternalConnectionExist(ctx context.Context, name string, ownerID oid.Oid, txn isql.Txn) error {
row, _, err := txn.QueryRowExWithCols(ctx, "lookup-schedule", txn.KV(),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hey @msbutler,

I am not sure if I should use lookup-schedule or something else?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the name is fine, but I don't think you need this method at all -- you can just use LoadExternalConnection and then see what you get.

Comment thread pkg/cloud/externalconn/record.go Outdated
// CREATE EXTERNAL CONNECTION is only allowed for users with the
// `EXTERNALCONNECTION` system privilege. We run the query as `node`
// since the user might not have `INSERT` on the system table.
createQuery := "UPSERT INTO system.external_connections (%s) VALUES (%s) RETURNING connection_name"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should I use the standard PostgreSQL syntax here—e.g., INSERT … ON CONFLICT DO UPDATE? Can I assume the internal SQL planner still uses CRDB?

CC: @msbutler

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we want to insert here? should we be using UPDATE instead since we are being told to alter an existing connection, not make a new one?

Comment thread pkg/cloud/externalconn/record.go Outdated
return err
}

// CREATE EXTERNAL CONNECTION is only allowed for users with the

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh no, I will update the comment here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't we need to be an owner of the connection?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, this is a somewhat unusual place to do priv checks: typically we do our priv checks in the planner methods and if we have a helper like this one that handles CRUD ops, it just does the CRUD ops and we we use it after doing the priv check in the planner method.

Comment thread pkg/cloud/externalconn/record.go Outdated
if err != nil {
// If we see a UniqueViolation it means that we are attempting to create an
// External Connection with a `connection_name` that already exists.
if pgerror.GetPGCode(err) == pgcode.UniqueViolation {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see that CREATE EXTERNAL CONNECTION already enforces uniqueness. Can we skip that check for ALTER EXTERNAL CONNECTION?

Comment thread pkg/sql/parser/sql.y Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like the comment here still needs a quick amend

Comment thread pkg/cloud/externalconn/record.go Outdated
return err
}

// CREATE EXTERNAL CONNECTION is only allowed for users with the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't we need to be an owner of the connection?

Comment thread pkg/cloud/externalconn/record.go Outdated
// CREATE EXTERNAL CONNECTION is only allowed for users with the
// `EXTERNALCONNECTION` system privilege. We run the query as `node`
// since the user might not have `INSERT` on the system table.
createQuery := "UPSERT INTO system.external_connections (%s) VALUES (%s) RETURNING connection_name"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we want to insert here? should we be using UPDATE instead since we are being told to alter an existing connection, not make a new one?

Comment thread pkg/cloud/externalconn/record.go Outdated
// `EXTERNALCONNECTION` system privilege. We run the query as `node`
// since the user might not have `INSERT` on the system table.
createQuery := "UPSERT INTO system.external_connections (%s) VALUES (%s) RETURNING connection_name"
row, retCols, err := txn.QueryRowExWithCols(ctx, "ExternalConnection.Create", txn.KV(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe opName convention is lower-case-with-dashes?

Comment thread pkg/sql/alter_external_connection.go Outdated
return errors.Wrap(err, "failed to log and sanitize External Connection")
}

ex := externalconn.NewMutableExternalConnection()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I expected, since we're talking about a conn that already exists, we'd probably use externalconn.LoadExternalConnection(name) vs making an empty structure and setting all the fields up manually?

Comment thread pkg/cloud/externalconn/record.go Outdated
return e.InitFromDatums(row, retCols)
}

func (e *MutableExternalConnection) Update(ctx context.Context, txn isql.Txn) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

alternatively, what about having this API be "UpdateURI(ctx, newURI)" that takes the new URI but otherwise assumes it is being called on an existing, fully initialized MutableExternalConnection that represents an existing db-stored external conn? Then this method is just a thin wrapper around db.Exec(UPDATE system..., e.ConnectionName, newURI)`

Comment thread pkg/cloud/externalconn/record.go Outdated
return err
}

// CREATE EXTERNAL CONNECTION is only allowed for users with the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, this is a somewhat unusual place to do priv checks: typically we do our priv checks in the planner methods and if we have a helper like this one that handles CRUD ops, it just does the CRUD ops and we we use it after doing the priv check in the planner method.

Comment thread pkg/cloud/externalconn/record.go Outdated

// ExternalConnectionExist loads an mutableExternalConnection from system table. Allow owner to modifiy the URI.
func ExternalConnectionExist(ctx context.Context, name string, ownerID oid.Oid, txn isql.Txn) error {
row, _, err := txn.QueryRowExWithCols(ctx, "lookup-schedule", txn.KV(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the name is fine, but I don't think you need this method at all -- you can just use LoadExternalConnection and then see what you get.

@edwardguo-crl edwardguo-crl changed the title sql,exterconn: add ALTER EXTERNAL CONNECTION sql,externalconn: add ALTER EXTERNAL CONNECTION Jul 9, 2025
@edwardguo-crl
edwardguo-crl force-pushed the feature-alter-ext-connection-parser branch from f1ff80c to 9760a2c Compare July 9, 2025 06:27
@edwardguo-crl
edwardguo-crl requested review from dt and msbutler July 9, 2025 06:30
@msbutler

msbutler commented Jul 9, 2025 •

Copy link
Copy Markdown
Collaborator

@edwardguo-crl looks like the build failed here. As i mentioned before, could you split this PR in 2: one for the syntax change, and then a follow up PR with the logic in the externalconn pkg? This will allow us to merge the syntax change quickly in a somewhat hot file (sql.y), and avoid future merge conflicts.

@edwardguo-crl
edwardguo-crl force-pushed the feature-alter-ext-connection-parser branch 3 times, most recently from 6a63572 to 0966a1a Compare July 10, 2025 01:15
Add AST parser support for the new SQL command ALTER EXTERNAL
CONNECTION, allowing users to modify existing external connections.

Fixes cockroachdb#98610
@edwardguo-crl
edwardguo-crl force-pushed the feature-alter-ext-connection-parser branch from 0966a1a to 85546d5 Compare July 10, 2025 05:51

@msbutler msbutler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice!

'ALTER' 'JOB' a_expr 'OWNER' 'TO' role_spec

label_spec ::=
string_or_placeholder

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: stray diff with the delete block below

@edwardguo-crl

edwardguo-crl commented Jul 10, 2025 •

Copy link
Copy Markdown
Contributor Author

TFTR!

bors r+

@craig

craig Bot commented Jul 10, 2025

Copy link
Copy Markdown
Contributor

@craig
craig Bot merged commit 9e36b27 into cockroachdb:master Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

changefeedccl: ALTER EXTERNAL CONNECTION

4 participants