tx.go 47 KB

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