Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: assign sequence ownership created when create/alter table #74840

Merged

Conversation

chengxiong-ruan
Copy link
Contributor

@chengxiong-ruan chengxiong-ruan commented Jan 14, 2022

Fixes #69298

Release note (bug fix): A sequence is created when a column is
defined as SERIAL type and the serial_normalization session
variable is set to sql_sequence. In this case, the sequence is
owned by the column and the table where the column exists. The
sequence should be dropped when the owner table/column is dropped,
which is the postgres behavior. However, cockroach never set
ownership information correctly but only dependecy relationship.
So the sequence stays even the owner table/column does not exist
anymore. This fix assigns correct ownership information to sequence
descriptor and column descriptor so that cockroach can align with
postgres behavior.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@chengxiong-ruan chengxiong-ruan force-pushed the drop-sequences-owned-by-table-column branch from f54d5b2 to 89f7769 Compare January 14, 2022 15:37
Copy link
Contributor Author

@chengxiong-ruan chengxiong-ruan left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained


pkg/sql/add_column.go, line 170 at r1 (raw file):

	// If the new column has a DEFAULT or an ON UPDATE expression that uses a
	// sequence, add references between its descriptor and this column descriptor.
	if err := cdd.ForEachTypedExpr(func(expr tree.TypedExpr) error {

This block is mostly moved from above.


pkg/sql/add_column.go, line 178 at r1 (raw file):

		}
		for _, changedSeqDesc := range changedSeqDescs {
			if colOwnedSeqDesc != nil && colOwnedSeqDesc.ID == changedSeqDesc.ID {

I think in theory colOwnedSeqDesc and changedSeqDesc should refer to a same instance, we can set ownership outside of this loop by using colOwnedSeqDesc. But still want to make sure I'm modifying the right copy here just in case the assumption is not true any more in the future..


pkg/sql/create_sequence.go, line 177 at r1 (raw file):

}

func createSequencesForSerialColumns(

This function is mostly a copy paste extraction from create_table.go below. Also narrow down the params dependency to a subset of its fields.


pkg/sql/temporary_schema.go, line 272 at r1 (raw file):

		databaseIDToTempSchemaID[uint32(desc.GetParentID())] = uint32(desc.GetParentSchemaID())

		// If a sequence is owned by a table column, it is dropped when the owner

this is covered by an existing test


pkg/sql/catalog/tabledesc/table_desc.go, line 176 at r1 (raw file):

// SetSequenceOwner adds sequence id to the sequence id list owned by a column
// and set ownership values of sequence options.
func (desc *Mutable) SetSequenceOwner(colName tree.Name, table *Mutable) error {

I'm always not proud of myself whenever creating a function with side effects... We may not need a function for this at all.

@chengxiong-ruan chengxiong-ruan marked this pull request as ready for review January 14, 2022 15:48
@chengxiong-ruan chengxiong-ruan requested review from a team, shermanCRL and ajwerner and removed request for a team January 14, 2022 15:48
@shermanCRL shermanCRL requested review from adityamaru and removed request for shermanCRL January 14, 2022 16:00
@chengxiong-ruan chengxiong-ruan force-pushed the drop-sequences-owned-by-table-column branch 4 times, most recently from 79547b2 to f7e840c Compare January 20, 2022 16:29
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

I'm good with this. We should probably backport it, at least the last release. Backporting to 21.1 has gotten somewhat painful.

Reviewed 6 of 15 files at r1, 1 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru and @chengxiong-ruan)


pkg/ccl/importccl/read_import_mysql.go, line 433 at r2 (raw file):

		seqDesc, err = sql.NewSequenceTableDesc(
			ctx,
			nil,

nit: comment the nil


pkg/ccl/importccl/read_import_pgdump.go, line 319 at r2 (raw file):

		desc, err := sql.NewSequenceTableDesc(
			ctx,
			nil,

nit: comment the nil


pkg/sql/add_column.go, line 178 at r1 (raw file):

Previously, chengxiong-ruan (Chengxiong Ruan) wrote…

I think in theory colOwnedSeqDesc and changedSeqDesc should refer to a same instance, we can set ownership outside of this loop by using colOwnedSeqDesc. But still want to make sure I'm modifying the right copy here just in case the assumption is not true any more in the future..

I hear you. The assumption should be true. It used to not be true, but we did work in 2020 to make it such that if you resolve the same descriptor mutably in the same transaction you get the same object. You can rely on that. That being said, this is fine. It'd be better if you added some explanatory commentary.


pkg/sql/testutils.go, line 77 at r2 (raw file):

		desc, err := NewSequenceTableDesc(
			ctx,
			nil,

nit: comment the nil


pkg/sql/catalog/tabledesc/table_desc.go, line 176 at r1 (raw file):

Previously, chengxiong-ruan (Chengxiong Ruan) wrote…

I'm always not proud of myself whenever creating a function with side effects... We may not need a function for this at all.

How would you feel about having this as a helper function in the sql package as opposed to as a method on the descriptor?


pkg/sql/logictest/testdata/logic_test/serial, line 225 at r2 (raw file):

----
serial  CREATE TABLE public.serial (
        a INT8 NOT NULL DEFAULT nextval('public.serial_a_seq1'::REGCLASS),

what in this change caused the naming changes here?


pkg/ccl/importccl/testutils_test.go, line 63 at r2 (raw file):

		desc, err := sql.NewSequenceTableDesc(
			ctx,
			nil,

nit: comment the nil

Copy link
Contributor Author

@chengxiong-ruan chengxiong-ruan left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @adityamaru, @ajwerner, and @chengxiong-ruan)


pkg/sql/catalog/tabledesc/table_desc.go, line 176 at r1 (raw file):

Previously, ajwerner wrote…

How would you feel about having this as a helper function in the sql package as opposed to as a method on the descriptor?

yeah, I'm happy with that. It's better to have a function acting the side effect than a method on an object.


pkg/sql/logictest/testdata/logic_test/serial, line 225 at r2 (raw file):

Previously, ajwerner wrote…

what in this change caused the naming changes here?

there's a test before this line already create sequence with name public.serial_a_seq, and we have a logic to find the first available sequence name by appending incremental integers to a default sequence name. so there is a sequence of sequence name like public.serial_a_seq1, public.serial_a_seq2, public.serial_a_seq3 if we create more.... with this change since the sequence from previous test is dropped and the names become available when creating this table, these names change :(

@chengxiong-ruan chengxiong-ruan force-pushed the drop-sequences-owned-by-table-column branch 3 times, most recently from e63492e to 1ab1700 Compare January 27, 2022 19:29
Fixes cockroachdb#69298

Release note (bug fix): A sequence is created when a column is
defined as `SERIAL` type and the `serial_normalization` session
variable is set to `sql_sequence`. In this case, the sequence is
owned by the column and the table where the column exists. The
sequence should be dropped when the owner table/column is dropped,
which is the postgres behavior. However, cockroach never set
ownership information correctly but only dependecy relationship.
So the sequence stays even the owner table/column does not exist
anymore. This fix assigns correct ownership information to sequence
descriptor and column descriptor so that cockroach can align with
postgres behavior.
@chengxiong-ruan chengxiong-ruan force-pushed the drop-sequences-owned-by-table-column branch from 1ab1700 to 6afe54e Compare January 27, 2022 21:53
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 15 files at r1, 1 of 11 files at r3.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @adityamaru, @ajwerner, and @chengxiong-ruan)


pkg/ccl/importccl/read_import_pgdump.go, line 315 at r4 (raw file):

	for schemaAndTableName, seq := range createSeq {
		schema, err := getSchemaByNameFromMap(ctx, schemaAndTableName, schemaNameToDesc, execCfg.Settings.Version)
		if err != nil {

This is a good catch!

@chengxiong-ruan
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 29, 2022

Build failed:

@ajwerner
Copy link
Contributor

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 29, 2022

Build succeeded:

@craig craig bot merged commit 75ba483 into cockroachdb:master Jan 29, 2022
@blathers-crl
Copy link

blathers-crl bot commented Jan 29, 2022

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 6afe54e to blathers/backport-release-21.2-74840: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 21.2.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

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.

sql: related sequence from a SERIAL column is not deleted when DROP TABLE
3 participants