Update Tuple
Update Tuple
Description:
Updates tuples that satisfy the specified conditions by performing a soft delete on the original tuple and inserting a new updated tuple version through the heap manager.
Function:
pub fn update_tuples(
catalog: &Catalog,
db_name: &str,
table_name: &str,
file: &mut File,
assignments: &[SetAssignment],
condition_groups: &[Vec<Condition>],
returning: bool,
) -> io::Result<UpdateResult>
Input:
catalog— Catalog containing database and table metadata.db_name— Name of the target database.table_name— Name of the target table.file— Open table heap file.assignments— ParsedSETclause assignments.condition_groups— ParsedWHEREclause conditions.returning— Indicates whether updated rows should be returned.
Output:
- Returns an
UpdateResultcontaining:updated_count— Number of tuples updated.returning_rows— Updated rows whenRETURNINGis enabled.
- Returns an
io::Errorif the database, table, page operations, or file operations fail.
Implementation:
- Validate that the specified database exists.
- Validate that the specified table exists.
- Retrieve the table schema from the catalog.
- Scan all heap pages in the table file.
- Read every active tuple from each page.
- Decode tuple data into column-value pairs.
- Evaluate the tuple against the provided
WHEREconditions. - For every matching tuple:
- Apply the
SETassignments. - Encode the updated tuple.
- Store the update in a pending update list.
- Apply the
- For each pending update:
- Acquire a page write lock.
- Mark the original tuple as deleted using
SLOT_FLAG_DELETED. - Write the updated page back to disk.
- Clear the corresponding Visibility Map entry.
- Increment the dead tuple count.
- Insert each updated tuple as a new tuple through the heap API.
- Collect updated rows if
RETURNINGis enabled. - Generate an update operation log entry.
- Return the total number of updated tuples and any returned rows.
Internal API Calls:
-
page_count(file)- Retrieves the total number of pages in the heap file.
-
file_identity_from_file(file)- Obtains a unique file identifier for page locking.
-
read_page(file, &mut page, page_id)- Reads a heap page from disk.
-
decode_tuple(&tuple_data, columns)- Converts stored tuple bytes into typed column values.
-
matches_condition_groups_pub(...)- Evaluates the tuple against the WHERE clause.
-
apply_assignments(decoded, assignments)- Applies SET clause modifications to the tuple.
-
encode_tuple(&updated_decoded, columns)- Serializes the updated tuple into storage format.
-
PageWriteLock::acquire(...)- Acquires a write lock for safe page modification.
-
write_page(file, &mut page, page_id)- Persists modified page data to disk.
-
vm_clear_page(db_name, table_name, page_id)- Marks the page as containing dead tuples in the Visibility Map.
-
increment_dead_tuple_count(file, count)- Updates dead tuple statistics.
-
heap_api::insert_raw_tuple(db_name, table_name, &new_bytes)- Inserts the updated tuple through the heap manager and FSM.
-
log_update(db_name, table_name, details, status)- Records the update operation in the operation log.
Files Created/Modified:
- Table heap file.
- Visibility Map (VM) file.
- Operation log file.
Storage Updates:
- Original tuple is soft-deleted by setting:
SLOT_FLAG_DELETED - Updated tuple is inserted as a new tuple version.
- Visibility Map is updated.
- Dead tuple count is incremented.
- Update operation is logged.
Notes:
- Updates follow an MVCC-style delete-and-insert approach rather than in-place modification.
- Original tuple data remains on disk but is ignored during future scans.
- New tuple placement is managed by the Heap Manager and Free Space Map (FSM).