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: Support DISCARD SEQUENCES #86230

Merged
merged 1 commit into from
Aug 25, 2022
Merged

Conversation

e-mbrown
Copy link
Contributor

@e-mbrown e-mbrown commented Aug 16, 2022

Refs #83061

Release note (sql change): We now support DISCARD SEQUENCES, which discards
all sequence-related state such as currval/lastval information. DISCARD ALL
now also discards sequence-related state.

Release justification: Low risk, high benefit changes to existing functionality(DISCARD ALL)

@e-mbrown e-mbrown requested a review from a team as a code owner August 16, 2022 15:59
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@e-mbrown e-mbrown requested a review from a team August 16, 2022 17:21
@e-mbrown e-mbrown force-pushed the eb/discard branch 2 times, most recently from 5db6214 to edca742 Compare August 17, 2022 15:41
@e-mbrown e-mbrown requested a review from rafiss August 18, 2022 15:46
Copy link
Collaborator

@rafiss rafiss 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 @e-mbrown and @rafiss)


pkg/sql/discard.go line 45 at r1 (raw file):

		p.preparedStatements.DeleteAll(ctx)

		//DISCARD SEQUENCES

nit: add a space after //


pkg/sql/discard.go line 49 at r1 (raw file):

	case tree.DiscardModeSequences:
		p.SessionData().SequenceState = sessiondata.NewSequenceState()

these calls should both be:

		p.sessionDataMutatorIterator.applyOnEachMutator(func(m sessionDataMutator) {
			m.data.SequenceState = sessiondata.NewSequenceState()
			m.initSequenceCache() 
		})

applyOnEachMutator is needed because it iterates through the stack of sessionData -- when a new transaction is started, a new sessionData is added to the stack.


pkg/sql/logictest/testdata/logic_test/discard line 47 at r1 (raw file):


statement ok
DISCARD ALL

can you verify that DISCARD ALL also removes the sequence info too


pkg/sql/logictest/testdata/logic_test/discard line 62 at r1 (raw file):


statement ok
CREATE SEQUENCE lastval_test_2 START WITH 10

nit: maybe name it discard_seq_test


pkg/sql/logictest/testdata/logic_test/discard line 67 at r1 (raw file):

SELECT nextval('lastval_test_2')
----
10

we also want to make sure we match this behavior from postgres:

rafiss@127:postgres> create sequence s2 cache 10;
CREATE SEQUENCE

rafiss@127:postgres> select nextval('s2');
+---------+
| nextval |
|---------|
| 1       |
+---------+

rafiss@127:postgres> discard sequences;
DISCARD SEQUENCES

rafiss@127:postgres> select nextval('s2');
+---------+
| nextval |
|---------|
| 11      |
+---------+

pkg/sql/logictest/testdata/logic_test/discard line 72 at r1 (raw file):

SELECT lastval()
----
10

can you also check that SELECT currval('lastval_test_2') returns 10 here


pkg/sql/logictest/testdata/logic_test/discard line 78 at r1 (raw file):


statement error pgcode 55000 pq: lastval\(\): lastval is not yet defined in this session
SELECT lastval()

and check that SELECT currval('lastval_test_2') returns a "currval of sequence "s" is not yet defined in this session" error here


pkg/sql/sem/tree/discard.go line 28 at r1 (raw file):

	// DiscardModeSequences represents a DISCARD SEQUENCES statement
	DiscardModeSequences DiscardMode = iota

the iota keyword is special. it's defined once in the block, and then it causes everything else to count up from there. so you want

const (
	// DiscardModeAll represents a DISCARD ALL statement.
	DiscardModeAll DiscardMode = iota

	// DiscardModeSequences represents a DISCARD SEQUENCES statement
	DiscardModeSequences
)

the type is also added implicitly.

Copy link
Contributor Author

@e-mbrown e-mbrown 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 @rafiss)


pkg/sql/discard.go line 49 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

these calls should both be:

		p.sessionDataMutatorIterator.applyOnEachMutator(func(m sessionDataMutator) {
			m.data.SequenceState = sessiondata.NewSequenceState()
			m.initSequenceCache() 
		})

applyOnEachMutator is needed because it iterates through the stack of sessionData -- when a new transaction is started, a new sessionData is added to the stack.

Done.


pkg/sql/logictest/testdata/logic_test/discard line 47 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

can you verify that DISCARD ALL also removes the sequence info too

Done.


pkg/sql/logictest/testdata/logic_test/discard line 67 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

we also want to make sure we match this behavior from postgres:

rafiss@127:postgres> create sequence s2 cache 10;
CREATE SEQUENCE

rafiss@127:postgres> select nextval('s2');
+---------+
| nextval |
|---------|
| 1       |
+---------+

rafiss@127:postgres> discard sequences;
DISCARD SEQUENCES

rafiss@127:postgres> select nextval('s2');
+---------+
| nextval |
|---------|
| 11      |
+---------+

Done.


pkg/sql/logictest/testdata/logic_test/discard line 72 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

can you also check that SELECT currval('lastval_test_2') returns 10 here

Done.


pkg/sql/logictest/testdata/logic_test/discard line 78 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

and check that SELECT currval('lastval_test_2') returns a "currval of sequence "s" is not yet defined in this session" error here

Done.


pkg/sql/sem/tree/discard.go line 28 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

the iota keyword is special. it's defined once in the block, and then it causes everything else to count up from there. so you want

const (
	// DiscardModeAll represents a DISCARD ALL statement.
	DiscardModeAll DiscardMode = iota

	// DiscardModeSequences represents a DISCARD SEQUENCES statement
	DiscardModeSequences
)

the type is also added implicitly.

Done.

@e-mbrown e-mbrown requested a review from rafiss August 22, 2022 17:12
Copy link
Collaborator

@rafiss rafiss 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 @e-mbrown and @rafiss)


pkg/sql/logictest/testdata/logic_test/discard line 50 at r2 (raw file):


statement error pgcode 55000 pq: lastval\(\): lastval is not yet defined in this session
SELECT lastval()

this test isn't testing the right thing, since there was no sequence used before the call to DISCARD ALL. so we'd want to create and use a sequence before DISCARD ALL

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

lgtm! but it does need to be rebased first

Release note (sql change): We now support DISCARD SEQUENCES, which discards
all sequence-related state such as currval/lastval information. DISCARD ALL
now also discards sequence-related state.

Release justification: Bug Fix
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

this lgtm! i'm gonna try to bors it, since the failures seem too be flakes

bors r+

Reviewed 2 of 7 files at r1, 1 of 3 files at r2, 1 of 1 files at r3, 3 of 3 files at r4, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @e-mbrown and @rafiss)

@craig
Copy link
Contributor

craig bot commented Aug 24, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 24, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 25, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 25, 2022

Build succeeded:

@craig craig bot merged commit 036b50a into cockroachdb:master Aug 25, 2022
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.

3 participants