Inside PostgreSQL Shared Memory
Inside PostgreSQL Shared Memory
BRUCE MOMJIAN,
ENTERPRISEDB
October, 2008
Abstract
POSTGRESQL is an open-source, full-featured relational database. This
presentation gives an overview of the shared memory structures used
by Postgres.
Outline
3. Shared buffers
5. Locking
6. Other structures
Postgres /data
Postgres
Postgres
/global
Postgres /pg_clog
/pg_multixact
/pg_subtrans
Postgres /pg_tblspc
/pg_twophase
/pg_xlog
Inside PostgreSQL Shared Memory 3
File System /data/base/db
/1 (template1)
Postgres /16821 (test)
/17982 (devel)
/21452 (marketing)
Postgres
/27214 (order)
Postgres /25932 (product)
/25952 (employee)
/27839 (part)
Postgres
Postgres
Postgres
Postgres
Postgres
8K
Tuple
Postgres
Postgres
8K
Tuple
Tuple
Inside PostgreSQL Shared Memory 8
File System Tuple
int4in(’9241’) ’Martin’
Tuple
textout()
union
{
CommandId t_cid; /* inserting or deleting command ID, or both */
TransactionId t_xvac; /* VACUUM FULL xact ID */
} t_field3;
} HeapTupleFields;
/* ^ − 23 bytes − ^ */
k()
for
postmaster postgres postgres
Buffer Descriptors
Shared Buffers
Semaphores
Inside PostgreSQL Shared Memory 12
Shared Buffers
8k 8k 8k
Shared Buffers
read()
Tuple
Postgres
Tuple Tuple Special
Postgres
8k 8k 8k
Shared Buffers
8K
Tuple
HeapTuple
int4in(’9241’) ’Martin’
Tuple
textout()
#define fetch_att(T,attbyval,attlen) \
( \
(attbyval) ? \
( \
(attlen) == (int) sizeof(int32) ? \
Int32GetDatum(*((int32 *)(T))) \
: \
( \
(attlen) == (int) sizeof(int16) ? \
Int16GetDatum(*((int16 *)(T))) \
: \
( \
AssertMacro((attlen) == 1), \
CharGetDatum(*((char *)(T))) \
) \
) \
) \
: \
PointerGetDatum((char *) (T)) \
)
Inside PostgreSQL Shared Memory 16
Test And Set Lock
Can Succeed Or Fail
1 1
0/1
0 1
Success Failure
Was 0 on exchange Was 1 on exchange
Lock already taken
Inside PostgreSQL Shared Memory 17
Test And Set Lock
x86 Assembler
1 1
0 1
Success Failure
Was 0 on exchange Was 1 on exchange
Lock already taken
Sleep On Lock
Buffer Descriptors
Shared Buffers
Semaphores
Lock Hashes
PROC
Proc Array
Buffer Descriptors
Shared Buffers
Semaphores
Inside PostgreSQL Shared Memory 23
Conclusion