Skip to content

postgresql select for update no wait #7

@v36372

Description

@v36372

https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE

To prevent the operation from waiting for other transactions to commit, use either the NOWAIT or SKIP LOCKED option. With NOWAIT, the statement reports an error, rather than waiting, if a selected row cannot be locked immediately

func (e *EquipmentRepo) LockByID(ctx context.Context, id int) error {
	err := e.db.Model(ctx, &domain.Equipment{}).
		Where("id = ?", id).
		For("UPDATE NO WAIT").
		Select()
	if err == pg.ErrNoRows {
		return nil
	}
	return err
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions