본문 바로가기

Programming/SQLite

SQLite 의 db-shm file 은?

SQLite 를 사용하는 system 에 SQLite DB 인 .db file 외에 .db-wal 과 .db-shm 파일을 발견할 수 있다.

이는 SQLite 의 WAL mode 를 사용할때 사용 하는 파일이고, 특히, .db-shm 은 SQLite 3.7 이후에 일시적으로 사용하게 된 wal-index file 이다.

자새한 설명은 아래 링크와 조잡한? 해석을 참고하면 된다.


http://stackoverflow.com/questions/7778723/what-are-the-db-shm-and-db-wal-extensions-in-sqlite-databases


These files(db-shm) are a new feature of SQLite 3.7.


http://www.sqlite.org/fileformat2.html#walindexformat



4.5 WAL-Index Format

Conceptually, the wal-index is shared memory, though the current VFS implementations use a mmapped file for the wal-index. The mmapped file is in the same directory as the database and has the same name as the database with a "-shm" suffix appended. Because the wal-index is shared memory, SQLite does not support journal_mode=WAL on a network filesystem when clients are on different machines. All users of the database must be able to share the same memory.

The purpose of the wal-index is to answer this question quickly:

Given a page number P and a maximum WAL frame index M, return the largest WAL frame index for page P that does not exceed M, or return NULL if there are no frames for page P that do not exceed M.

The M value in the previous paragraph is the "mxFrame" value defined in section 4.4 that is read at the start of a transaction and which defines the maximum frame from the WAL that the reader will use.

The wal-index is transient. After a crash, the wal-index is reconstructed from the original WAL file. The VFS is required to either truncate or zero the header of the wal-index when the last connection to it closes. Because the wal-index is transient, it can use an architecture-specific format; it does not have to be cross-platform. Hence, unlike the database and WAL file formats which store all values as big endian, the wal-index stores multi-byte values in the native byte order of the host computer.

This document is concerned with the persistent state of the database file, and since the wal-index is a transient structure, no further information about the format of the wal-index will be provided here. Complete details on the format of the wal-index are contained within comments in SQLite source code.


비록 현재 VFS implemetation 들이 wal-index 를 위한 mmapped file 을 사용하긴 하지만, 개념적으로, wal-index 는 shared memory 이다. mmapped file 은 db 와 같은 디랙토리에 있고 데이터와 같은 이름에 뒤에 "-shm"이 붙는다.

wal-index 는 shared memory 이기 때문에, SQLite 는 클라이언트가 다른 머신에 있는 네트워크 파일시스템에서는 WAL journal mode 를 지원하지 않는다. db의 모든 user 는 같은 memory 를 공유 가능해야 한다.


wal-index 의 목적은 이 질문에 빠르게 대답하기 위함이다.


"page number P 와 maximum WAL frame index M 이 주어짐,

M을 초과하지 않는 page P 에서 가장 큰 WAL frame index 를 return 하거나

없으면 NULL 을 return"


앞선 절에서 M 값은 섹션 4.4 에 정의된 "mxFrame" 이다. "mxFrame"은 트렌젝션의 시작때 읽고 reader 가 사용할 WAL 로부터 최대 frame 을 정의한다.

wal-index 는 일시적이다. crash 후에, wal-index 는 원본 WAL file 로부터 재구성된다. VFS 는 wal-index 의 해더를 비우거나 제로로 만드는 것을 요청받는다. / last connection 이 닫힐때.

그러므로 wal-index 는 일시적이다. 이것은 아키텍처에 따른 포맷을 사용할 수 있다. 이것은 cross-platform 의 성향을 가지지 않는다.

반면에 db 와 WAL 파일 포맷은 같지 않다.  / 모든 값은 big endian 으로 저장하는 / wal-index 는 multi-byte 값으로 저장한다. / host computer 의 자연스러운 바이트 오더에 따라...


이 문서는 db file 의 영구한 상태에 관련된다. 그리고 wal-index 는 임시 구조이기 때문에, 여기서는 더이상의 정보는 제공하지 않는다. wal-index 의 완전히 자세한 format 은 SQLite 소스코드의 comment 에 담겨있다.