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

Reconsider use of savepoints in transactions #2125

Closed
matt2e opened this issue Jul 22, 2024 · 3 comments · Fixed by #2143
Closed

Reconsider use of savepoints in transactions #2125

matt2e opened this issue Jul 22, 2024 · 3 comments · Fixed by #2143
Assignees

Comments

@matt2e
Copy link
Collaborator

matt2e commented Jul 22, 2024

While fiddling with partner project's transaction code to allow nested "transactions" we noticed that FTL is using savepoints, and it wasn't clear if we actually need to. Do we ever need to rollback a bit but then continue the transaction?

@moe:

it might be worth considering a similar pattern if FTL isn't using SAVEPOINTS for actual nested txns. there's quite a bit of hesitancy around their use wrt performance hits:
https://about.gitlab.com/blog/2021/09/29/why-we-spent-the-last-month-eliminating-postgresql-subtransactions/
https://www.cybertec-postgresql.com/en/subtransactions-and-performance-in-postgresql/
https://postgres.ai/blog/20210831-postgresql-subtransactions-considered-harmful

Used in FTL here: https://github.com/TBD54566975/ftl/blob/edbbf12d57ac2642cf5abf343f2f5a60a4785174/backend/controller/sql/conn.go#L51-L79

@github-actions github-actions bot added the triage Issue needs triaging label Jul 22, 2024
@ftl-robot ftl-robot mentioned this issue Jul 22, 2024
@deniseli
Copy link
Contributor

It looks like we only support savepoints currently but don't actually rely on them. The only place where we create a new savepoint is in tx.Begin in the file that you linked. That said, I don't know if there's any significance to that single savepoint's behavior.

@deniseli
Copy link
Contributor

After reading a bit more on savepoints, I can't find a reason we wouldn't be able to get rid of our savepoints usage and just rely on BEGIN;...COMMIT; to wrap the whole transaction. Unless I'm missing something higher up in our SQL setup...

@bradleydwyer bradleydwyer added the next Work that will be be picked up next label Jul 22, 2024
@github-actions github-actions bot removed the triage Issue needs triaging label Jul 22, 2024
@gak gak self-assigned this Jul 23, 2024
@github-actions github-actions bot removed the next Work that will be be picked up next label Jul 23, 2024
@gak
Copy link
Contributor

gak commented Jul 24, 2024

pgx internally does a savepoint internally for Begin too!

// Begin starts a pseudo nested transaction implemented with a savepoint.
func (tx *dbTx) Begin(ctx context.Context) (Tx, error) {
	if tx.closed {
		return nil, ErrTxClosed
	}

	tx.savepointNum++
	_, err := tx.conn.Exec(ctx, "savepoint sp_"+strconv.FormatInt(tx.savepointNum, 10))
	if err != nil {
		return nil, err
	}

	return &dbSimulatedNestedTx{tx: tx, savepointNum: tx.savepointNum}, nil
}

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 a pull request may close this issue.

4 participants