tx.go 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. package storage
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "sync"
  7. )
  8. // ErrSerialization is returned when a transaction's optimistic validation fails
  9. // because a concurrent transaction committed a conflicting change.
  10. var ErrSerialization = fmt.Errorf("serialization failure: concurrent transaction modified the database")
  11. // Session is a buffered SQL transaction/session wrapper around a TableManager.
  12. // While in a transaction, writes are staged in memory, reads observe that
  13. // staged overlay, and COMMIT issues a single atomic compare-and-swap batch
  14. // write. Rollback simply discards the staged changes (no compensating durable
  15. // writes). A Session is used for both autocommit statements (where operations
  16. // delegate straight to the durable TableManager) and buffered transactions.
  17. type Session struct {
  18. schema *SchemaManager
  19. table *TableManager
  20. mu sync.Mutex
  21. inTx bool
  22. aborted bool
  23. // autoLocks is true while a locking SELECT without an explicit transaction
  24. // holds its statement-scoped row locks.
  25. autoLocks bool
  26. // lockingRead suppresses table/predicate generation tracking while a
  27. // locking SELECT scans. A row lock plus the per-row LSN is the conflict
  28. // boundary for that read; recording a whole-table generation would abort
  29. // every queue worker whenever another worker claimed a different row.
  30. lockingRead bool
  31. lockingTable string
  32. // overlay holds staged writes keyed by lowercased table then data key.
  33. overlay map[string]map[string]*overlayEntry
  34. // log records mutations in order so savepoints can roll back.
  35. log []txMutation
  36. // reads records the durable LSN of every key the transaction observed
  37. // (0 means the key was observed absent). It becomes the compare set at
  38. // commit and also captures the base LSN of every written key.
  39. reads map[string]uint64
  40. // scanGens records the per-table generation captured by the first scan of
  41. // each table, validated at commit to detect phantoms. Indexed equality reads
  42. // use predicateGens so writes to other index values do not cause conflicts.
  43. scanGens map[string]uint64
  44. predicateGens map[string]predicateRead
  45. // schemaTx is the transaction-scoped catalog overlay. It exists only while a
  46. // transaction is open: DDL stages into it instead of publishing, and commit
  47. // merges it into the same batch write as the staged rows. A nil overlay means
  48. // every catalog read goes straight to the shared SchemaManager.
  49. schemaTx *SchemaTx
  50. // fkCatalog caches the foreign-key metadata of the schemaTx view, keyed by
  51. // the catalog version that produced it.
  52. fkCatalog *foreignKeyCatalog
  53. fkVersion uint64
  54. }
  55. type predicateRead struct {
  56. table string
  57. gen uint64
  58. }
  59. type overlayEntry struct {
  60. row Row
  61. absent bool
  62. }
  63. type txMutation struct {
  64. table string
  65. key string
  66. prev *overlayEntry
  67. }
  68. // NewSession creates a session wrapping the given schema and table managers.
  69. func NewSession(schema *SchemaManager, table *TableManager) *Session {
  70. return &Session{
  71. schema: schema,
  72. table: table,
  73. overlay: make(map[string]map[string]*overlayEntry),
  74. reads: make(map[string]uint64),
  75. scanGens: make(map[string]uint64),
  76. predicateGens: make(map[string]predicateRead),
  77. }
  78. }
  79. // Begin starts a buffered transaction.
  80. func (s *Session) Begin() error {
  81. s.mu.Lock()
  82. defer s.mu.Unlock()
  83. if s.inTx {
  84. return fmt.Errorf("cannot start a transaction within a transaction")
  85. }
  86. s.inTx = true
  87. s.aborted = false
  88. s.overlay = make(map[string]map[string]*overlayEntry)
  89. s.log = nil
  90. s.reads = make(map[string]uint64)
  91. s.scanGens = make(map[string]uint64)
  92. s.predicateGens = make(map[string]predicateRead)
  93. s.schemaTx = newSchemaTx(s)
  94. s.fkCatalog = nil
  95. s.fkVersion = 0
  96. return nil
  97. }
  98. // BeginLockStatement starts the statement-scoped lock lifetime used by an
  99. // autocommit SELECT ... FOR UPDATE. Inside an explicit transaction the locks
  100. // already live until COMMIT/ROLLBACK, so this is a no-op.
  101. func (s *Session) BeginLockStatement() {
  102. s.mu.Lock()
  103. defer s.mu.Unlock()
  104. if !s.inTx {
  105. s.autoLocks = true
  106. }
  107. }
  108. // EndLockStatement releases statement-scoped row locks after an autocommit
  109. // locking SELECT.
  110. func (s *Session) EndLockStatement() {
  111. s.mu.Lock()
  112. release := !s.inTx && s.autoLocks
  113. s.autoLocks = false
  114. s.mu.Unlock()
  115. if release {
  116. s.table.rowLocks.ReleaseAll(s)
  117. }
  118. }
  119. // RowLockKey returns the durable row key used by the row-lock manager.
  120. func (s *Session) RowLockKey(table, pk string) string {
  121. return s.table.dataKey(table, pk)
  122. }
  123. // AcquireRowLock takes a row lock for a locking SELECT. The caller must hold the
  124. // row lock before returning the row to the client and must re-read the row after
  125. // acquisition, because another transaction may have committed in between.
  126. func (s *Session) AcquireRowLock(table, pk string, mode LockMode, wait LockWait) error {
  127. return s.table.rowLocks.Acquire(s, s.RowLockKey(table, pk), mode, wait)
  128. }
  129. // guardKeys holds the row-lock commit gate while fn publishes a durable write,
  130. // and rejects the write if another transaction holds one of keys.
  131. func (s *Session) guardKeys(keys []string, fn func() error) error {
  132. release, err := s.table.rowLocks.BeginWrite(s, keys, nil)
  133. if err != nil {
  134. return fmt.Errorf("%w: %v", ErrSerialization, err)
  135. }
  136. defer release()
  137. return fn()
  138. }
  139. // guardTable is the coarse guard for autocommit bulk writes whose exact keys
  140. // are not known before the scan.
  141. func (s *Session) guardTable(table string, fn func() error) error {
  142. release, err := s.table.rowLocks.BeginTableWrite(s, s.table.dataPrefix(table))
  143. if err != nil {
  144. return fmt.Errorf("%w: %v", ErrSerialization, err)
  145. }
  146. defer release()
  147. return fn()
  148. }
  149. // BeginLockingRead marks scans of table as row-locking reads. Reads of other
  150. // tables in subqueries keep normal phantom tracking.
  151. func (s *Session) BeginLockingRead(table string) {
  152. s.mu.Lock()
  153. s.lockingRead = true
  154. s.lockingTable = table
  155. s.mu.Unlock()
  156. }
  157. // EndLockingRead restores normal phantom tracking.
  158. func (s *Session) EndLockingRead() {
  159. s.mu.Lock()
  160. s.lockingRead = false
  161. s.lockingTable = ""
  162. s.mu.Unlock()
  163. }
  164. // InTx reports whether a transaction is in progress.
  165. func (s *Session) InTx() bool {
  166. s.mu.Lock()
  167. defer s.mu.Unlock()
  168. return s.inTx
  169. }
  170. // Abort marks the transaction as aborted without discarding state.
  171. func (s *Session) Abort() {
  172. s.mu.Lock()
  173. defer s.mu.Unlock()
  174. if s.inTx {
  175. s.aborted = true
  176. }
  177. }
  178. // Snapshot returns the current mutation-log position for a savepoint.
  179. func (s *Session) Snapshot() int {
  180. s.mu.Lock()
  181. defer s.mu.Unlock()
  182. return len(s.log)
  183. }
  184. // RollbackTo discards mutations after the given savepoint position.
  185. func (s *Session) RollbackTo(pos int) {
  186. s.mu.Lock()
  187. defer s.mu.Unlock()
  188. s.rollbackToLocked(pos)
  189. s.aborted = false
  190. }
  191. // SessionSavepoint captures the complete session state at a SQL SAVEPOINT: the
  192. // staged rows with their mutation log and read set, and the staged DDL catalog.
  193. // A rollback restores all of it, so DDL issued after a savepoint is undone with
  194. // the rows that accompanied it.
  195. type SessionSavepoint struct {
  196. datalog int
  197. overlay map[string]map[string]*overlayEntry
  198. mutationLog []txMutation
  199. reads map[string]uint64
  200. scanGens map[string]uint64
  201. predicateGens map[string]predicateRead
  202. schema *schemaTxSnapshot
  203. }
  204. // Savepoint captures the session state a later RollbackToSavepoint restores.
  205. func (s *Session) Savepoint() *SessionSavepoint {
  206. s.mu.Lock()
  207. defer s.mu.Unlock()
  208. return s.savepointLocked()
  209. }
  210. // savepointLocked captures the session state (caller holds the session lock).
  211. func (s *Session) savepointLocked() *SessionSavepoint {
  212. sp := &SessionSavepoint{
  213. datalog: len(s.log),
  214. overlay: make(map[string]map[string]*overlayEntry, len(s.overlay)),
  215. mutationLog: append([]txMutation(nil), s.log...),
  216. reads: copyLSNMap(s.reads),
  217. scanGens: copyLSNMap(s.scanGens),
  218. predicateGens: make(map[string]predicateRead, len(s.predicateGens)),
  219. }
  220. for table, entries := range s.overlay {
  221. staged := make(map[string]*overlayEntry, len(entries))
  222. for key, entry := range entries {
  223. staged[key] = entry
  224. }
  225. sp.overlay[table] = staged
  226. }
  227. for key, predicate := range s.predicateGens {
  228. sp.predicateGens[key] = predicate
  229. }
  230. if s.schemaTx != nil {
  231. snapshot := s.schemaTx.snapshot()
  232. sp.schema = &snapshot
  233. }
  234. return sp
  235. }
  236. // RollbackToSavepoint restores the state captured by Savepoint.
  237. func (s *Session) RollbackToSavepoint(sp *SessionSavepoint) {
  238. if sp == nil {
  239. return
  240. }
  241. s.mu.Lock()
  242. defer s.mu.Unlock()
  243. s.rollbackToSavepointLocked(sp)
  244. s.aborted = false
  245. }
  246. // rollbackToSavepointLocked restores a savepoint (caller holds the session
  247. // lock).
  248. func (s *Session) rollbackToSavepointLocked(sp *SessionSavepoint) {
  249. // Copy rather than alias: the savepoint stays installed for a later
  250. // ROLLBACK TO SAVEPOINT, and writes staged after this restore must not
  251. // mutate the retained state.
  252. s.overlay = copyOverlay(sp.overlay)
  253. s.log = append([]txMutation(nil), sp.mutationLog...)
  254. s.reads = copyLSNMap(sp.reads)
  255. s.scanGens = copyLSNMap(sp.scanGens)
  256. s.predicateGens = make(map[string]predicateRead, len(sp.predicateGens))
  257. for key, predicate := range sp.predicateGens {
  258. s.predicateGens[key] = predicate
  259. }
  260. if s.schemaTx != nil && sp.schema != nil {
  261. s.schemaTx.restore(*sp.schema)
  262. }
  263. s.fkCatalog = nil
  264. s.fkVersion = 0
  265. }
  266. // copyOverlay copies the two-level staged row map. Overlay entries are
  267. // immutable once staged (rows are cloned before they are stored), so the inner
  268. // maps are copied but the entries themselves are shared.
  269. func copyOverlay(source map[string]map[string]*overlayEntry) map[string]map[string]*overlayEntry {
  270. cloned := make(map[string]map[string]*overlayEntry, len(source))
  271. for table, entries := range source {
  272. staged := make(map[string]*overlayEntry, len(entries))
  273. for key, entry := range entries {
  274. staged[key] = entry
  275. }
  276. cloned[table] = staged
  277. }
  278. return cloned
  279. }
  280. func copyLSNMap(source map[string]uint64) map[string]uint64 {
  281. cloned := make(map[string]uint64, len(source))
  282. for key, value := range source {
  283. cloned[key] = value
  284. }
  285. return cloned
  286. }
  287. // Rollback discards the transaction without writing anything durable.
  288. func (s *Session) Rollback() error {
  289. s.mu.Lock()
  290. defer s.mu.Unlock()
  291. if !s.inTx {
  292. return fmt.Errorf("cannot rollback: no transaction in progress")
  293. }
  294. s.resetLocked()
  295. return nil
  296. }
  297. // Commit validates and durably applies the staged transaction in one atomic
  298. // compare-and-swap batch write. It discards the commit LSN; callers that need a
  299. // ordering cursor use CommitWithLSN.
  300. func (s *Session) Commit() error {
  301. _, err := s.CommitWithLSN()
  302. return err
  303. }
  304. // CommitWithLSN behaves like Commit and additionally returns the LSN assigned
  305. // to the durable batch. A read-only or empty transaction returns zero because
  306. // no new durable state was published.
  307. func (s *Session) CommitWithLSN() (uint64, error) {
  308. s.mu.Lock()
  309. defer s.mu.Unlock()
  310. if !s.inTx {
  311. return 0, fmt.Errorf("cannot commit: no transaction in progress")
  312. }
  313. if s.aborted {
  314. s.resetLocked()
  315. return 0, fmt.Errorf("current transaction is aborted")
  316. }
  317. // Staged DDL is published by this same batch write, so the commit is atomic
  318. // across catalog changes and rows. hasDDL also forces the exclusive table
  319. // gates for the affected tables below, because the commit rewrites or
  320. // removes their durable rows.
  321. hasDDL := s.schemaTx != nil && s.schemaTx.hasDDL()
  322. // Row locks are optimistic: a write that touches a row locked by another
  323. // transaction fails with 40001 instead of waiting. Hold the lock-manager
  324. // read gate from this check through publication so a locking SELECT cannot
  325. // insert a lock in the window between the check and the durable batch.
  326. writeKeys := make([]string, 0)
  327. for _, entries := range s.overlay {
  328. for key := range entries {
  329. writeKeys = append(writeKeys, key)
  330. }
  331. }
  332. var ddlPrefixes []string
  333. if hasDDL {
  334. for _, table := range s.schemaTx.touchedTables() {
  335. ddlPrefixes = append(ddlPrefixes, s.table.dataPrefix(table))
  336. }
  337. }
  338. releaseRowLocks, lockErr := s.table.rowLocks.BeginWrite(s, writeKeys, ddlPrefixes)
  339. if lockErr != nil {
  340. s.resetLocked()
  341. return 0, fmt.Errorf("%w: %v", ErrSerialization, lockErr)
  342. }
  343. defer releaseRowLocks()
  344. // Lock order for every path that touches both a table and the catalog is
  345. // session lock -> foreign-key write gate -> table gates (sorted by name) ->
  346. // schema catalog mutex. Table gates are plain sync.RWMutex values and are
  347. // NOT reentrant, so nothing that re-acquires a table gate may run while the
  348. // commit still holds its validation gates; index cache maintenance is
  349. // therefore deferred until after releaseGates below.
  350. //
  351. // Referential integrity spans tables: a commit must validate the final
  352. // child rows against the final parent rows, including rows another table
  353. // staged in this same transaction. Take the foreign-key write gate before
  354. // any table gate (the canonical lock order) and register the constrained
  355. // component as scan reads so acquireCommitLocks holds it exclusively while
  356. // the validation scans run. A concurrent write on either side would bump one
  357. // of those generations and fail the commit with 40001 instead of committing
  358. // a dangling reference.
  359. closure, err := s.foreignKeyClosure()
  360. if err != nil {
  361. s.resetLocked()
  362. return 0, err
  363. }
  364. if len(closure) > 0 {
  365. s.table.foreignKeyMu.Lock()
  366. defer s.table.foreignKeyMu.Unlock()
  367. }
  368. for table := range closure {
  369. if _, ok := s.scanGens[table]; !ok {
  370. s.scanGens[table] = s.table.generation(table)
  371. }
  372. }
  373. // Acquire the per-table gates, escalating any table whose UNIQUE index
  374. // appeared after the initial unlocked probe. See acquireCommitLocks.
  375. _, _, unlockAll, uniqueTables, err := s.acquireCommitLocks()
  376. if err != nil {
  377. s.resetLocked()
  378. return 0, err
  379. }
  380. gatesHeld := true
  381. releaseGates := func() {
  382. if gatesHeld {
  383. unlockAll()
  384. gatesHeld = false
  385. }
  386. }
  387. defer releaseGates()
  388. // Another session may have published DDL while this transaction was open.
  389. // Re-read the committed catalog under the gates so the final row validation
  390. // below sees the catalog that is actually in effect.
  391. if s.schemaTx != nil {
  392. s.schemaTx.refreshCommitted()
  393. s.fkCatalog = nil
  394. s.fkVersion = 0
  395. }
  396. // A staged row was normalized against the schema as of its INSERT or UPDATE,
  397. // which a concurrent ADD COLUMN can predate. Re-apply the schema that is
  398. // current under the held gates before any derived state is computed: the CAS
  399. // check cannot reject such a row (a new key is only checked for absence), so
  400. // a NOT NULL column published after the row image was built would otherwise
  401. // be violated durably, and a UNIQUE index would not see the default the row
  402. // is about to receive. Run it before uniqueness and cache planning so both
  403. // observe the final values.
  404. if err := s.applyOverlaySchemaDefaults(); err != nil {
  405. s.resetLocked()
  406. return 0, err
  407. }
  408. // Validate scan generations for phantom detection.
  409. for t, gen := range s.scanGens {
  410. if s.table.generation(t) != gen {
  411. s.resetLocked()
  412. return 0, ErrSerialization
  413. }
  414. }
  415. for key, predicate := range s.predicateGens {
  416. if s.table.predicateGeneration(key) != predicate.gen {
  417. s.resetLocked()
  418. return 0, ErrSerialization
  419. }
  420. }
  421. // Compute the durable effects of staged DDL before derived state is planned:
  422. // the rows an ALTER TABLE rewrites and a RENAME TABLE moves are part of the
  423. // transaction's final row state, so uniqueness and cache planning must see
  424. // them. The catalog writes travel in the same batch as everything else.
  425. var ddlOps []BatchOp
  426. var ddlChecks []CompareCheck
  427. var ddlEntries map[string]map[string]*overlayEntry
  428. var touchedTables, touchedIndexes []string
  429. if hasDDL {
  430. catalogOps, catalogChecks, err := s.schemaTx.commitCatalogOps()
  431. if err != nil {
  432. s.resetLocked()
  433. return 0, err
  434. }
  435. dataOps, dataEntries, err := s.schemaTx.commitDataOps(&ddlBatchBudget{})
  436. if err != nil {
  437. s.resetLocked()
  438. return 0, err
  439. }
  440. ddlOps = append(catalogOps, dataOps...)
  441. ddlChecks = catalogChecks
  442. ddlEntries = dataEntries
  443. touchedTables = s.schemaTx.touchedTables()
  444. touchedIndexes = s.schemaTx.touchedIndexes()
  445. }
  446. // Validate the final overlay against every foreign key in the constrained
  447. // component. The per-table gates acquired above exclude concurrent writers,
  448. // so this sees a stable state; checking the final state (rather than each
  449. // statement) is what lets a transaction delete a parent and its children in
  450. // either order as long as the committed state stays consistent.
  451. if err := s.validateForeignKeyFinalState(closure); err != nil {
  452. s.resetLocked()
  453. return 0, err
  454. }
  455. // Validate the final overlay of every unique-indexed written table against
  456. // durable rows. This runs under the exclusive gate acquired above, so it
  457. // cannot race a concurrent writer. Duplicate unique values fail the commit
  458. // with a "UNIQUE constraint failed" error rather than ErrSerialization.
  459. uniquePlans := make(map[string]uniqueCachePlan, len(uniqueTables))
  460. for t := range uniqueTables {
  461. entries := s.finalRowEntries(t, ddlEntries)
  462. plan, err := s.table.prepareUniqueRowsWith(t, entries, s.uniqueIndexesLocked(t))
  463. if err != nil {
  464. s.resetLocked()
  465. return 0, err
  466. }
  467. uniquePlans[t] = plan
  468. }
  469. exclusiveTables := make(map[string]bool)
  470. for table := range uniqueTables {
  471. exclusiveTables[table] = true
  472. }
  473. for table := range s.scanGens {
  474. exclusiveTables[table] = true
  475. }
  476. for _, predicate := range s.predicateGens {
  477. exclusiveTables[predicate.table] = true
  478. }
  479. cachePlans := make(map[string]*tableCachePlan)
  480. for table := range s.overlay {
  481. if !exclusiveTables[table] {
  482. continue
  483. }
  484. entries := s.finalRowEntries(table, ddlEntries)
  485. plan, err := s.table.prepareTableCache(table, entries, s.reads)
  486. if err != nil {
  487. s.resetLocked()
  488. return 0, err
  489. }
  490. cachePlans[table] = plan
  491. }
  492. // Build the compare set from every observed key.
  493. checks := make([]CompareCheck, 0, len(s.reads))
  494. for key, lsn := range s.reads {
  495. checks = append(checks, CompareCheck{Key: []byte(key), LSN: lsn})
  496. }
  497. // Build the batch ops from the staged overlay.
  498. ops := make([]BatchOp, 0)
  499. for _, entries := range s.overlay {
  500. for key, e := range entries {
  501. if e.absent {
  502. ops = append(ops, BatchOp{Op: batchDelete, Key: []byte(key)})
  503. } else {
  504. data, err := encodeRow(e.row)
  505. if err != nil {
  506. s.resetLocked()
  507. return 0, err
  508. }
  509. ops = append(ops, BatchOp{Op: batchPut, Key: []byte(key), Value: data})
  510. }
  511. }
  512. }
  513. if hasDDL {
  514. ops = append(ops, ddlOps...)
  515. checks = append(checks, ddlChecks...)
  516. }
  517. sort.Slice(checks, func(i, j int) bool { return string(checks[i].Key) < string(checks[j].Key) })
  518. sort.Slice(ops, func(i, j int) bool { return string(ops[i].Key) < string(ops[j].Key) })
  519. // A read-only transaction has nothing to write; commit trivially.
  520. if len(ops) == 0 {
  521. s.resetLocked()
  522. return 0, nil
  523. }
  524. // Pre-flight the assembled batch so an oversized commit fails with an
  525. // actionable error instead of the storage protocol's generic limit error.
  526. if len(ops) > maxOperations {
  527. s.resetLocked()
  528. return 0, fmt.Errorf("transaction cannot be published atomically: %d operations exceed the storage batch limit of %d; split the transaction or run the DDL outside a transaction so it can page its writes", len(ops), maxOperations)
  529. }
  530. batchBytes := 0
  531. for _, op := range ops {
  532. batchBytes += batchOpBytes(op)
  533. }
  534. if batchBytes > maxTransactionSize {
  535. s.resetLocked()
  536. return 0, fmt.Errorf("transaction cannot be published atomically: %d bytes exceed the storage batch limit of %d; split the transaction or run the DDL outside a transaction so it can page its writes", batchBytes, maxTransactionSize)
  537. }
  538. var committed bool
  539. var commitLSN uint64
  540. err = s.table.pool.WithClient(func(c *KVClient) error {
  541. lsn, ok, err := c.CompareBatchWrite(checks, ops, nil)
  542. commitLSN = lsn
  543. committed = ok
  544. return err
  545. })
  546. if err != nil || !committed {
  547. // A transport error can hide a durable commit; a compare conflict can
  548. // also expose an out-of-band writer. Rebuild derived state next time.
  549. for table := range s.overlay {
  550. s.table.invalidateWriteFailure(table)
  551. }
  552. if hasDDL {
  553. // Staged DDL may have become durable even though the response was
  554. // lost, so the shared catalog view cannot be trusted either: drop it
  555. // for every touched table and reload from storage on the next read.
  556. s.schema.invalidateCommittedDDL(touchedTables)
  557. for _, table := range touchedTables {
  558. s.table.invalidateWriteFailure(table)
  559. }
  560. }
  561. }
  562. if err != nil {
  563. s.resetLocked()
  564. return 0, err
  565. }
  566. if !committed {
  567. s.resetLocked()
  568. return 0, ErrSerialization
  569. }
  570. // Publish derived state before generations while retaining the table gates.
  571. for t := range s.overlay {
  572. if plan := cachePlans[t]; plan != nil {
  573. s.table.applyTableCache(plan)
  574. } else {
  575. s.table.invalidateCache(t)
  576. }
  577. s.table.applyUniquePlan(uniquePlans[t], nil)
  578. s.table.bumpIndexPredicateWildcard(t)
  579. s.table.bumpGeneration(t)
  580. }
  581. if hasDDL {
  582. s.publishCommittedCatalog(touchedTables)
  583. }
  584. // Index cache maintenance needs the table gates itself (BuildIndex locks the
  585. // table gate to rebuild entries), and that gate is not reentrant, so it runs
  586. // only after this commit drops the gates it held for validation.
  587. releaseGates()
  588. if hasDDL {
  589. s.publishCommittedIndexes(touchedIndexes)
  590. }
  591. s.resetLocked()
  592. return commitLSN, nil
  593. }
  594. // publishCommittedCatalog publishes a committed catalog change to the rest of
  595. // the process. The durable batch is already written, so this only drops the
  596. // derived state that described the previous catalog: the shared schema catalog
  597. // caches and the row-derived caches of every touched table. Other sessions
  598. // reload lazily from the committed records. The caller still holds the table
  599. // gates, which is the documented lock order (table gate, then catalog mutex).
  600. func (s *Session) publishCommittedCatalog(tables []string) {
  601. s.schema.invalidateCommittedDDL(tables)
  602. for _, table := range tables {
  603. s.table.InvalidateCache(table)
  604. s.table.bumpIndexPredicateWildcard(table)
  605. s.table.bumpGeneration(table)
  606. }
  607. }
  608. // publishCommittedIndexes resets the derived entries of every index the
  609. // transaction changed. A dropped index loses its cache and is marked disabled
  610. // for indexing until it is created again; a committed index (including a
  611. // re-created name a previous session dropped) is rebuilt so its entries and
  612. // disabled marker match the catalog. The caller must not hold the table gates:
  613. // BuildIndex acquires the table gate itself.
  614. func (s *Session) publishCommittedIndexes(indexes []string) {
  615. committed, err := s.schema.ListIndexes()
  616. if err != nil {
  617. committed = nil
  618. }
  619. known := make(map[string]bool, len(committed))
  620. for _, name := range committed {
  621. known[strings.ToLower(name)] = true
  622. }
  623. for _, index := range indexes {
  624. if !known[index] {
  625. s.table.ClearIndex(index, "", nil) //nolint:errcheck
  626. continue
  627. }
  628. // A committed index may have reused a name this process dropped
  629. // earlier; rebuild it so its derived entries and disabled marker match
  630. // the catalog again.
  631. if definition, err := s.schema.GetIndex(index); err == nil {
  632. s.table.BuildIndex(definition.Name, definition.Table, nil) //nolint:errcheck
  633. }
  634. }
  635. }
  636. // acquireCommitLocks acquires the per-table gates needed to commit the staged
  637. // transaction, returning the held locks, whether each is exclusive, an unlock
  638. // function, and the set of written tables that carry a UNIQUE index. A written
  639. // table with a UNIQUE index is locked exclusively so its final overlay can be
  640. // validated against durable rows. The uniqueness probe is re-checked under the
  641. // held gates so a concurrent CREATE UNIQUE INDEX cannot slip in after the probe
  642. // and leave a duplicate unvalidated.
  643. func (s *Session) acquireCommitLocks() (locks []*sync.RWMutex, exclusive []bool, unlockAll func(), uniqueTables map[string]bool, err error) {
  644. unlock := func(ls []*sync.RWMutex, ex []bool) {
  645. for i := len(ls) - 1; i >= 0; i-- {
  646. if ex[i] {
  647. ls[i].Unlock()
  648. } else {
  649. ls[i].RUnlock()
  650. }
  651. }
  652. }
  653. for {
  654. // Tables read through scans or predicates need an exclusive validation
  655. // gate. Tables that are only written use the shared publication gate,
  656. // allowing disjoint optimistic commits to proceed concurrently while
  657. // still excluding scanner commits.
  658. affected := make(map[string]bool)
  659. for t := range s.overlay {
  660. affected[t] = false
  661. }
  662. for t := range s.scanGens {
  663. affected[t] = true
  664. }
  665. for _, predicate := range s.predicateGens {
  666. affected[predicate.table] = true
  667. }
  668. // Staged DDL rewrites or removes durable rows and publishes catalog
  669. // records, so the commit needs the exclusive gate of every touched table.
  670. if s.schemaTx != nil && s.schemaTx.hasDDL() {
  671. for _, t := range s.schemaTx.touchedTables() {
  672. affected[t] = true
  673. }
  674. }
  675. // Tables written in this transaction that carry a UNIQUE index need the
  676. // exclusive gate so their final overlay can be validated against durable
  677. // rows without racing another writer.
  678. uniqueTables = make(map[string]bool)
  679. for t := range s.overlay {
  680. uniq, err := s.hasUniqueIndexLocked(t)
  681. if err != nil {
  682. return nil, nil, nil, nil, err
  683. }
  684. if uniq {
  685. uniqueTables[t] = true
  686. affected[t] = true
  687. }
  688. }
  689. tables := make([]string, 0, len(affected))
  690. for t := range affected {
  691. tables = append(tables, t)
  692. }
  693. sort.Strings(tables)
  694. locks = make([]*sync.RWMutex, len(tables))
  695. exclusive = make([]bool, len(tables))
  696. for i, t := range tables {
  697. locks[i] = s.table.tableLock(t)
  698. exclusive[i] = affected[t]
  699. }
  700. for i, lock := range locks {
  701. if exclusive[i] {
  702. lock.Lock()
  703. } else {
  704. lock.RLock()
  705. }
  706. }
  707. // Re-check under the held gates. A concurrent CREATE UNIQUE INDEX can
  708. // only have completed before we acquired the gate (it needs the exclusive
  709. // gate), so this probe is authoritative; escalate and retry if a written
  710. // table gained a unique index.
  711. retry := false
  712. for i, t := range tables {
  713. if exclusive[i] {
  714. continue
  715. }
  716. if _, written := s.overlay[t]; !written {
  717. continue
  718. }
  719. uniq, err := s.hasUniqueIndexLocked(t)
  720. if err != nil {
  721. unlock(locks, exclusive)
  722. return nil, nil, nil, nil, err
  723. }
  724. if uniq {
  725. retry = true
  726. break
  727. }
  728. }
  729. if retry {
  730. unlock(locks, exclusive)
  731. continue
  732. }
  733. unlockAll = func() { unlock(locks, exclusive) }
  734. return locks, exclusive, unlockAll, uniqueTables, nil
  735. }
  736. }
  737. // applyOverlaySchemaDefaults re-applies the current schema to every staged row
  738. // (the caller holds the per-table gates). Columns published after a row image
  739. // was built receive their catalog default, and required columns are validated,
  740. // so a commit cannot persist a row that violates the schema now in effect.
  741. func (s *Session) applyOverlaySchemaDefaults() error {
  742. for table, entries := range s.overlay {
  743. schema, err := s.getSchemaLocked(table)
  744. if err != nil {
  745. return err
  746. }
  747. for _, entry := range entries {
  748. if entry.absent {
  749. continue
  750. }
  751. if err := s.applyStagedRewritesLocked(table, entry.row); err != nil {
  752. return err
  753. }
  754. if err := s.schema.applySchemaDefaults(schema, entry.row); err != nil {
  755. return err
  756. }
  757. if err := s.schema.validateChecks(schema, entry.row); err != nil {
  758. return err
  759. }
  760. }
  761. }
  762. return nil
  763. }
  764. // finalRowEntries merges the rows a staged DDL rewrite or rename produces into
  765. // the table's staged entries, so uniqueness and cache planning validate the row
  766. // image the commit is about to publish (caller holds the session lock).
  767. func (s *Session) finalRowEntries(table string, ddlEntries map[string]map[string]*overlayEntry) map[string]*overlayEntry {
  768. staged := s.overlay[table]
  769. rewritten := ddlEntries[table]
  770. if len(rewritten) == 0 {
  771. return staged
  772. }
  773. merged := make(map[string]*overlayEntry, len(staged)+len(rewritten))
  774. for key, entry := range staged {
  775. merged[key] = entry
  776. }
  777. for key, entry := range rewritten {
  778. merged[key] = entry
  779. }
  780. return merged
  781. }
  782. // uniqueIndexesLocked returns the UNIQUE indexes of a table as this session
  783. // observes them (caller holds the session lock).
  784. func (s *Session) uniqueIndexesLocked(table string) []*Index {
  785. var indexes []*Index
  786. if s.schemaTx != nil {
  787. indexes, _ = s.schemaTx.listTableIndexes(table)
  788. } else {
  789. indexes, _ = s.schema.ListTableIndexes(table)
  790. }
  791. unique := indexes[:0]
  792. for _, idx := range indexes {
  793. if idx.Unique {
  794. unique = append(unique, idx)
  795. }
  796. }
  797. return unique
  798. }
  799. // hasUniqueIndexLocked reports whether the session's view of a table carries a
  800. // UNIQUE index, so a transaction validates rows against an index it staged.
  801. func (s *Session) hasUniqueIndexLocked(table string) (bool, error) {
  802. if s.schemaTx == nil {
  803. return s.table.hasUniqueIndex(table)
  804. }
  805. indexes, err := s.schemaTx.listTableIndexes(table)
  806. if err != nil {
  807. return false, err
  808. }
  809. for _, idx := range indexes {
  810. if idx.Unique {
  811. return true, nil
  812. }
  813. }
  814. return false, nil
  815. }
  816. // lockingTableIs reports whether table is the main table of the active
  817. // locking SELECT, whose rows are covered by explicit row locks.
  818. func (s *Session) lockingTableIs(table string) bool {
  819. return s.lockingRead && strings.EqualFold(table, s.lockingTable)
  820. }
  821. func (s *Session) resetLocked() {
  822. s.inTx = false
  823. s.aborted = false
  824. s.overlay = make(map[string]map[string]*overlayEntry)
  825. s.log = nil
  826. s.reads = make(map[string]uint64)
  827. s.scanGens = make(map[string]uint64)
  828. s.predicateGens = make(map[string]predicateRead)
  829. s.schemaTx = nil
  830. s.fkCatalog = nil
  831. s.fkVersion = 0
  832. s.autoLocks = false
  833. s.lockingRead = false
  834. s.lockingTable = ""
  835. s.table.rowLocks.ReleaseAll(s)
  836. }
  837. func (s *Session) stagePut(table, key string, row Row) {
  838. tl := strings.ToLower(table)
  839. if s.overlay[tl] == nil {
  840. s.overlay[tl] = make(map[string]*overlayEntry)
  841. }
  842. s.log = append(s.log, txMutation{table: tl, key: key, prev: s.overlay[tl][key]})
  843. s.overlay[tl][key] = &overlayEntry{row: cloneRow(row)}
  844. if _, ok := s.reads[key]; !ok {
  845. s.reads[key] = 0
  846. }
  847. }
  848. func (s *Session) stageDelete(table, key string, row Row) {
  849. tl := strings.ToLower(table)
  850. if s.overlay[tl] == nil {
  851. s.overlay[tl] = make(map[string]*overlayEntry)
  852. }
  853. s.log = append(s.log, txMutation{table: tl, key: key, prev: s.overlay[tl][key]})
  854. // Keep the deleted row so COMMIT can exempt its rowid from the unique-index
  855. // scan and release its unique value.
  856. s.overlay[tl][key] = &overlayEntry{row: cloneRow(row), absent: true}
  857. if _, ok := s.reads[key]; !ok {
  858. s.reads[key] = 0
  859. }
  860. }
  861. // GetByPK retrieves a row by primary key, observing the staged overlay in a
  862. // transaction and recording the observed LSN for validation.
  863. func (s *Session) GetByPK(table, pk string) (Row, error) {
  864. return s.getByPK(table, pk, true)
  865. }
  866. // GetByPKNoRead is GetByPK without recording the durable LSN. A locking SELECT
  867. // uses it for the pre-lock candidate lookup: the read is only part of the
  868. // transaction's conflict set once the row lock has actually been acquired.
  869. func (s *Session) GetByPKNoRead(table, pk string) (Row, error) {
  870. return s.getByPK(table, pk, false)
  871. }
  872. func (s *Session) getByPK(table, pk string, recordRead bool) (Row, error) {
  873. s.mu.Lock()
  874. defer s.mu.Unlock()
  875. if !s.inTx {
  876. return s.table.GetByPK(table, pk)
  877. }
  878. stagedKey := s.table.dataKey(table, pk)
  879. tl := strings.ToLower(table)
  880. if e, ok := s.overlay[tl][stagedKey]; ok {
  881. if e.absent {
  882. return nil, ErrKeyNotFound
  883. }
  884. return cloneRow(e.row), nil
  885. }
  886. durableKey := s.durableDataKeyLocked(table, pk)
  887. row, lsn, err := s.table.getKeyWithLSN(durableKey)
  888. if err != nil {
  889. if err == ErrKeyNotFound {
  890. if recordRead {
  891. s.reads[durableKey] = 0
  892. }
  893. return nil, ErrKeyNotFound
  894. }
  895. return nil, err
  896. }
  897. if recordRead {
  898. s.reads[durableKey] = lsn
  899. }
  900. if err := s.applyStagedRewritesLocked(table, row); err != nil {
  901. return nil, err
  902. }
  903. return row, nil
  904. }
  905. // durableSourceLocked returns the committed table name whose data prefix holds
  906. // the rows of a table this transaction sees. RENAME TABLE moves the definition
  907. // immediately but leaves rows under the committed prefix until the commit, so
  908. // reads must follow the source (caller holds the session lock).
  909. func (s *Session) durableSourceLocked(table string) string {
  910. if s.schemaTx == nil {
  911. return table
  912. }
  913. lower := strings.ToLower(table)
  914. source, ok := s.schemaTx.dataSources[lower]
  915. if !ok || source == "" {
  916. return table
  917. }
  918. return source
  919. }
  920. // hasStagedSourceLocked reports whether the table's durable rows live under a
  921. // different (older) name because RENAME TABLE is staged.
  922. func (s *Session) hasStagedSourceLocked(table string) bool {
  923. if s.schemaTx == nil {
  924. return false
  925. }
  926. lower := strings.ToLower(table)
  927. source, ok := s.schemaTx.dataSources[lower]
  928. return ok && source != "" && source != lower
  929. }
  930. // durableDataKeyLocked returns the key a row is durably stored under, following
  931. // a staged rename (caller holds the session lock).
  932. func (s *Session) durableDataKeyLocked(table, pk string) string {
  933. return s.table.dataKey(s.durableSourceLocked(table), pk)
  934. }
  935. // Select scans a table, merging the staged overlay so a transaction sees its
  936. // own writes, and records per-row LSNs plus the table generation.
  937. func (s *Session) Select(table string, filter func(Row) bool) ([]Row, error) {
  938. s.mu.Lock()
  939. if !s.inTx {
  940. s.mu.Unlock()
  941. return s.table.Select(table, filter)
  942. }
  943. defer s.mu.Unlock()
  944. return s.selectLocked(table, filter)
  945. }
  946. // SelectLimited bounds an autocommit scan. Explicit transactions retain the
  947. // complete overlay/read-set behavior until bounded transactional scans have a
  948. // separate phantom/overlay contract; their caller still applies LIMIT later.
  949. func (s *Session) SelectLimited(table string, filter func(Row) bool, maxRows int) ([]Row, error) {
  950. s.mu.Lock()
  951. if s.inTx {
  952. defer s.mu.Unlock()
  953. return s.selectLocked(table, filter)
  954. }
  955. s.mu.Unlock()
  956. if maxRows == 0 {
  957. return []Row{}, nil
  958. }
  959. return s.table.SelectWithLimit(table, filter, maxRows, 0)
  960. }
  961. func (s *Session) selectLocked(table string, filter func(Row) bool) ([]Row, error) {
  962. schema, err := s.getSchemaLocked(table)
  963. if err != nil {
  964. return nil, err
  965. }
  966. tl := strings.ToLower(table)
  967. tableLock := s.table.tableLock(tl)
  968. tableLock.RLock()
  969. defer tableLock.RUnlock()
  970. if !s.lockingTableIs(table) {
  971. if _, ok := s.scanGens[tl]; !ok {
  972. s.scanGens[tl] = s.table.generation(tl)
  973. }
  974. }
  975. overlay := s.overlay[tl]
  976. source := s.durableSourceLocked(table)
  977. sourceLower := strings.ToLower(source)
  978. if sourceLower != tl && !s.lockingTableIs(table) {
  979. // A concurrent write to the committed name must invalidate the commit
  980. // just like a write to the staged name.
  981. if _, ok := s.scanGens[sourceLower]; !ok {
  982. s.scanGens[sourceLower] = s.table.generation(sourceLower)
  983. }
  984. }
  985. var rows []Row
  986. err = s.table.scanRowsAtPrefix(s.table.dataPrefix(source), func(durableKey string, row Row, lsn uint64) (bool, error) {
  987. pk, err := schema.PrimaryKeyForRow(row)
  988. if err != nil {
  989. return true, err
  990. }
  991. if _, ok := overlay[s.table.dataKey(table, pk)]; ok {
  992. return false, nil
  993. }
  994. if !s.lockingTableIs(table) {
  995. s.reads[durableKey] = lsn
  996. }
  997. if err := s.applyStagedRewritesLocked(table, row); err != nil {
  998. return true, err
  999. }
  1000. if filter == nil || filter(row) {
  1001. rows = append(rows, row)
  1002. }
  1003. return false, nil
  1004. })
  1005. if err != nil {
  1006. return nil, err
  1007. }
  1008. for _, e := range overlay {
  1009. if e.absent {
  1010. continue
  1011. }
  1012. row := cloneRow(e.row)
  1013. if err := s.applyStagedRewritesLocked(table, row); err != nil {
  1014. return nil, err
  1015. }
  1016. if filter == nil || filter(row) {
  1017. rows = append(rows, row)
  1018. }
  1019. }
  1020. return rows, nil
  1021. }
  1022. // applyStagedRewritesLocked applies the transaction's pending ALTER TABLE row
  1023. // transformations, so the session reads its own DDL before the commit writes
  1024. // the transformed images (caller holds the session lock).
  1025. func (s *Session) applyStagedRewritesLocked(table string, row Row) error {
  1026. if s.schemaTx == nil {
  1027. return nil
  1028. }
  1029. _, err := s.schemaTx.rewriteRow(table, row)
  1030. return err
  1031. }
  1032. // SelectByIndex reads only matching durable rows while capturing their LSNs,
  1033. // then merges the transaction overlay. The table generation protects against
  1034. // matching rows being inserted or removed after the lookup.
  1035. func (s *Session) SelectByIndex(table, indexName string, colValue interface{}) ([]Row, error) {
  1036. s.mu.Lock()
  1037. defer s.mu.Unlock()
  1038. if !s.inTx {
  1039. return s.table.SelectByIndex(table, indexName, colValue)
  1040. }
  1041. tableKey := strings.ToLower(table)
  1042. index, err := s.getIndexLocked(indexName)
  1043. if err != nil {
  1044. return nil, err
  1045. }
  1046. col := index.Columns[0].Name
  1047. want := formatIndexValue(colValue)
  1048. if s.hasStagedSourceLocked(table) || !s.indexIsCommittedLocked(index.Name) {
  1049. // The derived index caches describe the committed catalog: an index this
  1050. // transaction created has no entries yet, and a renamed table's entries
  1051. // are keyed under the old storage name. Either way, scan the rows the
  1052. // session observes instead.
  1053. return s.filterByIndexLocked(table, index, want)
  1054. }
  1055. versions, predicate, err := s.table.selectByIndexWithLSN(table, indexName, colValue)
  1056. if err != nil {
  1057. return nil, err
  1058. }
  1059. if !s.lockingTableIs(table) {
  1060. if _, ok := s.predicateGens[predicate.valueKey]; !ok {
  1061. s.predicateGens[predicate.valueKey] = predicateRead{table: tableKey, gen: predicate.valueGen}
  1062. }
  1063. if _, ok := s.predicateGens[predicate.wildcardKey]; !ok {
  1064. s.predicateGens[predicate.wildcardKey] = predicateRead{table: tableKey, gen: predicate.wildcardGen}
  1065. }
  1066. }
  1067. overlay := s.overlay[tableKey]
  1068. rows := make([]Row, 0, len(versions)+len(overlay))
  1069. for _, version := range versions {
  1070. if _, staged := overlay[version.key]; staged {
  1071. continue
  1072. }
  1073. if !s.lockingTableIs(table) {
  1074. s.reads[version.key] = version.lsn
  1075. }
  1076. if err := s.applyStagedRewritesLocked(table, version.row); err != nil {
  1077. return nil, err
  1078. }
  1079. rows = append(rows, version.row)
  1080. }
  1081. for _, entry := range overlay {
  1082. if !entry.absent && formatIndexValue(entry.row[col]) == want {
  1083. rows = append(rows, cloneRow(entry.row))
  1084. }
  1085. }
  1086. return rows, nil
  1087. }
  1088. // SelectByIndexKey retrieves the rows whose index key equals valueKey, merging
  1089. // the staged overlay so a transaction sees its own writes. valueKey is the
  1090. // formatted key from TableManager.IndexRowKey, which supports plain, composite,
  1091. // and expression indexes uniformly.
  1092. func (s *Session) SelectByIndexKey(table string, idx *Index, valueKey string) ([]Row, error) {
  1093. s.mu.Lock()
  1094. defer s.mu.Unlock()
  1095. if !s.inTx {
  1096. versions, _, err := s.table.selectByIndexKeyWithLSN(table, idx.Name, valueKey)
  1097. if err != nil {
  1098. return nil, err
  1099. }
  1100. rows := make([]Row, len(versions))
  1101. for i := range versions {
  1102. rows[i] = versions[i].row
  1103. }
  1104. return rows, nil
  1105. }
  1106. tableKey := strings.ToLower(table)
  1107. if s.hasStagedSourceLocked(table) || !s.indexIsCommittedLocked(idx.Name) {
  1108. return s.filterByIndexLocked(table, idx, valueKey)
  1109. }
  1110. versions, predicate, err := s.table.selectByIndexKeyWithLSN(table, idx.Name, valueKey)
  1111. if err != nil {
  1112. return nil, err
  1113. }
  1114. if !s.lockingTableIs(table) {
  1115. if _, ok := s.predicateGens[predicate.valueKey]; !ok {
  1116. s.predicateGens[predicate.valueKey] = predicateRead{table: tableKey, gen: predicate.valueGen}
  1117. }
  1118. if _, ok := s.predicateGens[predicate.wildcardKey]; !ok {
  1119. s.predicateGens[predicate.wildcardKey] = predicateRead{table: tableKey, gen: predicate.wildcardGen}
  1120. }
  1121. }
  1122. overlay := s.overlay[tableKey]
  1123. rows := make([]Row, 0, len(versions)+len(overlay))
  1124. for _, version := range versions {
  1125. if _, staged := overlay[version.key]; staged {
  1126. continue
  1127. }
  1128. if !s.lockingTableIs(table) {
  1129. s.reads[version.key] = version.lsn
  1130. }
  1131. if err := s.applyStagedRewritesLocked(table, version.row); err != nil {
  1132. return nil, err
  1133. }
  1134. rows = append(rows, version.row)
  1135. }
  1136. for _, entry := range overlay {
  1137. if entry.absent {
  1138. continue
  1139. }
  1140. key, kerr := s.table.IndexRowKey(idx, entry.row)
  1141. if kerr != nil {
  1142. return nil, kerr
  1143. }
  1144. if key == valueKey {
  1145. rows = append(rows, cloneRow(entry.row))
  1146. }
  1147. }
  1148. return rows, nil
  1149. }
  1150. // filterByIndexLocked returns the rows this session observes whose index key
  1151. // equals valueKey, without consulting the derived index caches. It is the
  1152. // fallback for a table renamed in this transaction, whose committed index
  1153. // entries still name the old storage prefix (caller holds the session lock).
  1154. func (s *Session) filterByIndexLocked(table string, idx *Index, valueKey string) ([]Row, error) {
  1155. rows, err := s.selectLocked(table, nil)
  1156. if err != nil {
  1157. return nil, err
  1158. }
  1159. matched := make([]Row, 0, len(rows))
  1160. for _, row := range rows {
  1161. key, err := s.table.IndexRowKey(idx, row)
  1162. if err != nil {
  1163. return nil, err
  1164. }
  1165. if key == valueKey {
  1166. matched = append(matched, row)
  1167. }
  1168. }
  1169. return matched, nil
  1170. }
  1171. // indexIsCommittedLocked reports whether the shared table manager can resolve an
  1172. // index, and therefore whether its derived index caches cover it (caller holds
  1173. // the session lock).
  1174. func (s *Session) indexIsCommittedLocked(name string) bool {
  1175. _, err := s.schema.GetIndex(name)
  1176. return err == nil
  1177. }
  1178. // getIndexLocked resolves an index definition against the session catalog
  1179. // (caller holds the session lock).
  1180. func (s *Session) getIndexLocked(name string) (*Index, error) {
  1181. if s.schemaTx != nil {
  1182. return s.schemaTx.getIndex(name)
  1183. }
  1184. return s.schema.GetIndex(name)
  1185. }
  1186. // CountFast returns the exact row count, observing the staged overlay in a
  1187. // transaction.
  1188. func (s *Session) CountFast(table string) (int, error) {
  1189. s.mu.Lock()
  1190. defer s.mu.Unlock()
  1191. if !s.inTx {
  1192. return s.table.CountFast(table)
  1193. }
  1194. rows, err := s.selectLocked(table, nil)
  1195. if err != nil {
  1196. return 0, err
  1197. }
  1198. return len(rows), nil
  1199. }
  1200. // Insert stages an insert in a transaction, or performs a durable autocommit
  1201. // insert otherwise.
  1202. func (s *Session) Insert(table string, row Row) error {
  1203. _, err := s.InsertWithRowID(table, row)
  1204. return err
  1205. }
  1206. // InsertWithRowID stages an insert in a transaction, or performs a durable
  1207. // autocommit insert otherwise, returning the actual generated ROWID.
  1208. func (s *Session) InsertWithRowID(table string, row Row) (int64, error) {
  1209. s.mu.Lock()
  1210. defer s.mu.Unlock()
  1211. if !s.inTx {
  1212. return s.table.InsertWithRowID(table, row)
  1213. }
  1214. return s.insertLocked(table, row)
  1215. }
  1216. // InsertBulk stages or durably bulk-inserts multiple rows.
  1217. func (s *Session) InsertBulk(table string, rows []Row) (int, error) {
  1218. count, _, err := s.InsertBulkWithLastRowID(table, rows)
  1219. return count, err
  1220. }
  1221. // InsertBulkWithLastRowID is InsertBulk, additionally returning the ROWID of the
  1222. // last staged/persisted row (0 when nothing was inserted).
  1223. func (s *Session) InsertBulkWithLastRowID(table string, rows []Row) (int, int64, error) {
  1224. s.mu.Lock()
  1225. defer s.mu.Unlock()
  1226. if !s.inTx {
  1227. return s.table.InsertBulkWithLastRowID(table, rows)
  1228. }
  1229. var lastRowID int64
  1230. count := 0
  1231. for _, row := range rows {
  1232. rid, err := s.insertLocked(table, row)
  1233. if err != nil {
  1234. return count, lastRowID, err
  1235. }
  1236. lastRowID = rid
  1237. count++
  1238. }
  1239. return count, lastRowID, nil
  1240. }
  1241. // insertLocked is the transaction insert helper (caller holds s.mu).
  1242. func (s *Session) insertLocked(table string, row Row) (int64, error) {
  1243. schema, err := s.getSchemaLocked(table)
  1244. if err != nil {
  1245. return 0, err
  1246. }
  1247. nr, key, err := s.table.prepareInsertWithSchema(table, schema, row)
  1248. if err != nil {
  1249. return 0, err
  1250. }
  1251. rowid, _ := rowIDFromRow(nr)
  1252. tl := strings.ToLower(table)
  1253. // The durable key stays internal; the duplicate message renders the key
  1254. // components from the normalized row.
  1255. pk := strings.TrimPrefix(key, s.table.dataPrefix(table))
  1256. if e, ok := s.overlay[tl][key]; ok {
  1257. if !e.absent {
  1258. return 0, newPrimaryKeyViolation(table, schema, nr)
  1259. }
  1260. } else {
  1261. durableKey := s.durableDataKeyLocked(table, pk)
  1262. _, lsn, err := s.table.getKeyWithLSN(durableKey)
  1263. if err == nil {
  1264. s.reads[durableKey] = lsn
  1265. return 0, newPrimaryKeyViolation(table, schema, nr)
  1266. }
  1267. if err != nil && err != ErrKeyNotFound {
  1268. return 0, err
  1269. }
  1270. s.reads[durableKey] = 0
  1271. }
  1272. s.stagePut(table, key, nr)
  1273. return rowid, nil
  1274. }
  1275. // UpdateByPK stages or durably applies a single-row update. A table that is the
  1276. // parent of a cascading constraint runs in an implicit transaction so the parent
  1277. // rewrite and every cascaded row publish in one commit.
  1278. func (s *Session) UpdateByPK(table, pk string, updateFn func(Row) (Row, error)) (Row, bool, error) {
  1279. s.mu.Lock()
  1280. if !s.inTx {
  1281. s.mu.Unlock()
  1282. cascading, unlockFK, err := s.beginCascadingPointWrite(table, false)
  1283. if err != nil {
  1284. return nil, false, err
  1285. }
  1286. if !cascading {
  1287. var row Row
  1288. var updated bool
  1289. err := s.guardKeys([]string{s.RowLockKey(table, pk)}, func() error {
  1290. var err error
  1291. row, updated, err = s.table.UpdateByPK(table, pk, updateFn)
  1292. return err
  1293. })
  1294. return row, updated, err
  1295. }
  1296. oldRow, updated, err := s.updateByPKStaged(table, pk, updateFn)
  1297. unlockFK()
  1298. if err != nil {
  1299. _ = s.Rollback()
  1300. return nil, false, err
  1301. }
  1302. if err := s.Commit(); err != nil {
  1303. return nil, false, err
  1304. }
  1305. return oldRow, updated, nil
  1306. }
  1307. defer s.mu.Unlock()
  1308. return s.updateByPKLocked(table, pk, updateFn)
  1309. }
  1310. func (s *Session) updateByPKStaged(table, pk string, updateFn func(Row) (Row, error)) (Row, bool, error) {
  1311. s.mu.Lock()
  1312. defer s.mu.Unlock()
  1313. return s.updateByPKLocked(table, pk, updateFn)
  1314. }
  1315. // updateByPKLocked stages a point update. The caller holds the session lock and
  1316. // a transaction.
  1317. func (s *Session) updateByPKLocked(table, pk string, updateFn func(Row) (Row, error)) (Row, bool, error) {
  1318. schema, err := s.getSchemaLocked(table)
  1319. if err != nil {
  1320. return nil, false, err
  1321. }
  1322. row, err := s.getByPKLocked(table, pk)
  1323. if err == ErrKeyNotFound {
  1324. return nil, false, nil
  1325. }
  1326. if err != nil {
  1327. return nil, false, err
  1328. }
  1329. oldRow := cloneRow(row)
  1330. updates, err := s.runUpdateFn(updateFn, row)
  1331. if err != nil {
  1332. return nil, false, err
  1333. }
  1334. for name, value := range updates {
  1335. for _, column := range schema.Columns {
  1336. if strings.EqualFold(name, column.Name) {
  1337. row[column.Name] = value
  1338. break
  1339. }
  1340. }
  1341. }
  1342. if err := s.schema.validateRowConstraints(schema, row); err != nil {
  1343. return nil, false, err
  1344. }
  1345. pos := len(s.log)
  1346. if err := s.stageCascadeUpdate(table, oldRow, row, newCascadeExpansion(s.schema)); err != nil {
  1347. s.rollbackToLocked(pos)
  1348. return nil, false, err
  1349. }
  1350. return oldRow, true, nil
  1351. }
  1352. // runUpdateFn releases the session lock while invoking the update callback so the
  1353. // callback can run nested reads (e.g. a scalar subquery in SET) through the same
  1354. // session without deadlocking on s.mu. A connection executes one statement at a
  1355. // time, so the staged overlay cannot change while the callback runs. The lock is
  1356. // re-acquired before returning.
  1357. func (s *Session) runUpdateFn(updateFn func(Row) (Row, error), row Row) (Row, error) {
  1358. s.mu.Unlock()
  1359. defer s.mu.Lock()
  1360. return updateFn(row)
  1361. }
  1362. // DeleteByPK stages or durably applies a single-row delete. A table that is the
  1363. // parent of a cascading constraint runs in an implicit transaction so the
  1364. // parent delete and every cascaded row publish in one commit.
  1365. func (s *Session) DeleteByPK(table, pk string) (Row, bool, error) {
  1366. s.mu.Lock()
  1367. if !s.inTx {
  1368. s.mu.Unlock()
  1369. cascading, unlockFK, err := s.beginCascadingPointWrite(table, true)
  1370. if err != nil {
  1371. return nil, false, err
  1372. }
  1373. if !cascading {
  1374. var row Row
  1375. var deleted bool
  1376. err := s.guardKeys([]string{s.RowLockKey(table, pk)}, func() error {
  1377. var err error
  1378. row, deleted, err = s.table.DeleteByPK(table, pk)
  1379. return err
  1380. })
  1381. return row, deleted, err
  1382. }
  1383. row, deleted, err := s.deleteByPKStaged(table, pk)
  1384. unlockFK()
  1385. if err != nil {
  1386. _ = s.Rollback()
  1387. return nil, false, err
  1388. }
  1389. if err := s.Commit(); err != nil {
  1390. return nil, false, err
  1391. }
  1392. return row, deleted, nil
  1393. }
  1394. defer s.mu.Unlock()
  1395. return s.deleteByPKLocked(table, pk)
  1396. }
  1397. func (s *Session) deleteByPKStaged(table, pk string) (Row, bool, error) {
  1398. s.mu.Lock()
  1399. defer s.mu.Unlock()
  1400. return s.deleteByPKLocked(table, pk)
  1401. }
  1402. // deleteByPKLocked stages a point delete. The caller holds the session lock and
  1403. // a transaction.
  1404. func (s *Session) deleteByPKLocked(table, pk string) (Row, bool, error) {
  1405. key := s.table.dataKey(table, pk)
  1406. row, err := s.getByPKLocked(table, pk)
  1407. if err == ErrKeyNotFound {
  1408. return nil, false, nil
  1409. }
  1410. if err != nil {
  1411. return nil, false, err
  1412. }
  1413. pos := len(s.log)
  1414. if err := s.stageCascadeDelete(table, key, row, newCascadeExpansion(s.schema)); err != nil {
  1415. s.rollbackToLocked(pos)
  1416. return nil, false, err
  1417. }
  1418. return row, true, nil
  1419. }
  1420. // getByPKLocked reads a row observing the overlay (caller holds s.mu). The
  1421. // staged key addresses the overlay; the durable read follows the committed name
  1422. // when RENAME TABLE has staged a move, so a point UPDATE/DELETE issued after a
  1423. // rename still finds the row instead of silently matching nothing.
  1424. func (s *Session) getByPKLocked(table, pk string) (Row, error) {
  1425. stagedKey := s.table.dataKey(table, pk)
  1426. tl := strings.ToLower(table)
  1427. if e, ok := s.overlay[tl][stagedKey]; ok {
  1428. if e.absent {
  1429. return nil, ErrKeyNotFound
  1430. }
  1431. return cloneRow(e.row), nil
  1432. }
  1433. durableKey := s.durableDataKeyLocked(table, pk)
  1434. row, lsn, err := s.table.getKeyWithLSN(durableKey)
  1435. if err != nil {
  1436. if err == ErrKeyNotFound {
  1437. s.reads[durableKey] = 0
  1438. return nil, ErrKeyNotFound
  1439. }
  1440. return nil, err
  1441. }
  1442. s.reads[durableKey] = lsn
  1443. if err := s.applyStagedRewritesLocked(table, row); err != nil {
  1444. return nil, err
  1445. }
  1446. return row, nil
  1447. }
  1448. // UpdateFunc stages or durably applies a scan-based update.
  1449. func (s *Session) UpdateFunc(table string, updateFn func(Row) (Row, error), filter func(Row) bool) (int, error) {
  1450. s.mu.Lock()
  1451. if !s.inTx {
  1452. s.mu.Unlock()
  1453. var count int
  1454. err := s.guardTable(table, func() error {
  1455. var err error
  1456. count, err = s.table.UpdateFunc(table, updateFn, filter)
  1457. return err
  1458. })
  1459. return count, err
  1460. }
  1461. defer s.mu.Unlock()
  1462. schema, err := s.getSchemaLocked(table)
  1463. if err != nil {
  1464. return 0, err
  1465. }
  1466. rows, err := s.selectLocked(table, filter)
  1467. if err != nil {
  1468. return 0, err
  1469. }
  1470. count := 0
  1471. exp := newCascadeExpansion(s.schema)
  1472. pos := len(s.log)
  1473. for _, row := range rows {
  1474. oldRow := cloneRow(row)
  1475. updates, err := s.runUpdateFn(updateFn, row)
  1476. if err != nil {
  1477. s.rollbackToLocked(pos)
  1478. return count, err
  1479. }
  1480. for name, value := range updates {
  1481. for _, column := range schema.Columns {
  1482. if strings.EqualFold(name, column.Name) {
  1483. row[column.Name] = value
  1484. break
  1485. }
  1486. }
  1487. }
  1488. if err := s.schema.validateRowConstraints(schema, row); err != nil {
  1489. s.rollbackToLocked(pos)
  1490. return count, err
  1491. }
  1492. if err := s.stageCascadeUpdate(table, oldRow, row, exp); err != nil {
  1493. s.rollbackToLocked(pos)
  1494. return count, err
  1495. }
  1496. count++
  1497. }
  1498. return count, nil
  1499. }
  1500. // Delete stages or durably applies a scan-based delete.
  1501. func (s *Session) Delete(table string, filter func(Row) bool) (int, error) {
  1502. s.mu.Lock()
  1503. if !s.inTx {
  1504. s.mu.Unlock()
  1505. var count int
  1506. err := s.guardTable(table, func() error {
  1507. var err error
  1508. count, err = s.table.Delete(table, filter)
  1509. return err
  1510. })
  1511. return count, err
  1512. }
  1513. defer s.mu.Unlock()
  1514. schema, err := s.getSchemaLocked(table)
  1515. if err != nil {
  1516. return 0, err
  1517. }
  1518. rows, err := s.selectLocked(table, filter)
  1519. if err != nil {
  1520. return 0, err
  1521. }
  1522. count := 0
  1523. exp := newCascadeExpansion(s.schema)
  1524. pos := len(s.log)
  1525. for _, row := range rows {
  1526. pk, err := schema.PrimaryKeyForRow(row)
  1527. if err != nil {
  1528. s.rollbackToLocked(pos)
  1529. return count, err
  1530. }
  1531. if err := s.stageCascadeDelete(table, s.table.dataKey(table, pk), row, exp); err != nil {
  1532. s.rollbackToLocked(pos)
  1533. return count, err
  1534. }
  1535. count++
  1536. }
  1537. return count, nil
  1538. }
  1539. // ClearIndex drops the in-memory entries of an index. Inside a transaction the
  1540. // drop is staged in the catalog overlay instead: the committed index (and its
  1541. // cache) stays intact for every other session, and the commit clears it once the
  1542. // catalog change is durable. This keeps the rollback invariant that a
  1543. // transaction never mutates shared derived state, so ROLLBACK has nothing to
  1544. // restore beyond the session's own schema snapshot and row overlay.
  1545. func (s *Session) ClearIndex(indexName, tableName string, columns []string) error {
  1546. s.mu.Lock()
  1547. defer s.mu.Unlock()
  1548. if s.schemaTx != nil {
  1549. return nil
  1550. }
  1551. return s.table.ClearIndex(indexName, tableName, columns)
  1552. }
  1553. // InvalidateCache drops the derived caches of a table. Inside a transaction the
  1554. // durable rows and committed catalog are still current, so nothing is
  1555. // invalidated until the commit publishes the catalog change; a rollback then
  1556. // leaves every shared cache exactly as it was.
  1557. func (s *Session) InvalidateCache(table string) {
  1558. s.mu.Lock()
  1559. defer s.mu.Unlock()
  1560. if s.schemaTx != nil {
  1561. return
  1562. }
  1563. s.table.InvalidateCache(table)
  1564. }
  1565. // BuildIndex builds index entries for every existing row. Inside a transaction
  1566. // the index is only staged; its entries are derived lazily from durable rows
  1567. // once the commit publishes it and the session is merged in afterwards. Staging
  1568. // instead of building keeps a rolled-back CREATE INDEX from leaving derived
  1569. // entries or a cleared disabled marker behind.
  1570. func (s *Session) BuildIndex(indexName, tableName string, columns []string) error {
  1571. s.mu.Lock()
  1572. defer s.mu.Unlock()
  1573. if s.schemaTx != nil {
  1574. return nil
  1575. }
  1576. return s.table.BuildIndex(indexName, tableName, columns)
  1577. }