Skip to content

Commit c08eddf

Browse files
committed
Understanding the Internals of PostgreSQL
1 parent 687ed62 commit c08eddf

File tree

1 file changed

+135
-1
lines changed

1 file changed

+135
-1
lines changed

03.postgreSQL.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,138 @@ CREATE TABLE followers (
146146
follower_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
147147
UNIQUE(leader_id, follower_id)
148148
);
149-
```
149+
```
150+
151+
#### restoring database from a backup
152+
sql databases backup to a .sql file.
153+
154+
restore from backup:
155+
pgadmin -> servers-> localhost -> databases -> instagram -> *(right click) -> restore
156+
filename -> ... -> *(format -> all files or .sql) -> *(select file to restore from)
157+
158+
might have to set the 'PostgreSQL Binary Path':
159+
In pgAdmin select File -> Preferences and look for Path and then click on Binary Path and it needs your path where it says PostgreSQL Binary Path. Go to your computer -> C: (on windows) -> Program Files -> PostgreSQL -> your version -> bin
160+
161+
restore options tab -> enable:
162+
1. types of objects -> only data -> yes
163+
2. do not save -> owner -> yes
164+
3. queries -> single transaction -> yes
165+
4. disable -> trigger -> yes
166+
5. miscelleaneous / behavior -> verbose messages -> yes
167+
168+
#### restore database from scratch
169+
steps to restore:
170+
1. close all query tools windows
171+
2. stop all server activity except the first under pgadmin -> dashboard
172+
3. drop the database
173+
4. create new database
174+
5. restore from backup (same step as above) BUT restore options tab -> enable ONLY 4 steps from above
175+
176+
restore from backup:
177+
pgadmin -> servers-> localhost -> databases -> instagram -> *(right click) -> restore
178+
filename -> ... -> *(format -> all files or .sql) -> *(select file to restore from)
179+
180+
1. types of objects -> only data -> NO
181+
182+
2. do not save -> owner -> yes
183+
3. queries -> single transaction -> yes
184+
4. disable -> trigger -> yes
185+
5. miscelleaneous / behavior -> verbose messages -> yes
186+
187+
#### where postgreSQL stores data
188+
```SQL
189+
show data_directory;
190+
```
191+
this gives us a path like: /users/*username*/library/application support/postgres/var-12
192+
all database content is stored in 'base' folder
193+
194+
#### Folder where database is stored
195+
results are table(oid, datname).
196+
the oid is the internal identifier (which is also the folder name inside the 'base' folder) that is associated with a database
197+
```SQL
198+
SELECT oid, datname
199+
FROM pg_database;
200+
```
201+
202+
#### file where database table is stored
203+
inside that folder, each file represents one object inside database
204+
table (oid, relname, relnamespace, reltype ...etc)
205+
```SQL
206+
SELECT * FROM pg_class;
207+
```
208+
each row represents an object refers to eg. tables, indexes, primary keys.
209+
so find the table we want and look at associated 'oid',
210+
that will be name of file which will contain all data for that object. eg. 22445
211+
212+
* Heap file - file that contains all the data (rows) of our table
213+
* page or block - heap is divided into many different 'blocks' or 'pages'. Each page/block stores a number of rows
214+
* tuple or item - individual row from the table.
215+
216+
Heap file is divided up into many page/blocks and each page/block has many tuple/item/rows.
217+
each block is 8kb.
218+
219+
* Block is binary data (stores 0's and 1's) and divided up into parts.
220+
* begining part stores information about block,
221+
* then next few blocks stores information about rows, where to find data.
222+
* free space
223+
* actual data for tuples.
224+
225+
#### deciphering a page/block
226+
227+
[postgreSQL docs about page/block](http://postgresql.org/docs/current/storage-page-layout.html)
228+
* Table 68.2 Overall Page layout
229+
230+
* view with hex reader (vs code extensions - microsoft hex editor)
231+
* open the file with the hex editor (CTRL+SHIFT+P) -> hex editor
232+
* hex code is actual "shortcut" or encoded binary 1's and 0's
233+
* each hex is 1 byte.
234+
235+
#### Page Layout
236+
* PageHeaderData
237+
238+
##### PageHeaderData
239+
24 bytes long. contains information about page
240+
Table 68.3 PageHeaderData layout
241+
* pd_lsn - 8 bytes
242+
* pd_checksum - 2 bytes
243+
* pd_flags - 2 bytes
244+
* pd_lower - 2 bytes (*offset to start of free space)
245+
* pd_upper - 2 bytes (*offset to end of free space)
246+
* pd_special - 2 bytes
247+
* pd_pagesize_version
248+
* pd_prune_xid
249+
250+
pd_lower:
251+
Once we know where pd_lower is located - we know the number of bytes starting at beginning of page to start off free space.
252+
clicking on first byte -> then in hex editor (Data inspector) - pd_lower - Int16 value -> eg. 228
253+
228 means we count off 228 bytes from start of page/block (228 / 16 (each row has 16) = 14.25), next value is the start of free space
254+
255+
pd_higher:
256+
2 bytes - clicking on first byte -> data inspector -> int16 -> value is 296.
257+
296 gives value for end of free space area -> 296/16(each row in hex editor has 16 cols) = 18.5
258+
counting down from begining of block to 18.5, we will find the end of free space.
259+
260+
##### ItemIdData
261+
4 bytes per item. array of identifiers pointing to actual items.
262+
There are multiple of these after the page header.
263+
264+
how data is stored:
265+
click on start of 4 bytes, then go to the next byte (2nd of 4 bytes).
266+
Data Inspector -> look at 8 bit binary eg. 10110100
267+
look at the 2nd digit of the 8bit binary.
268+
find binary to decimal converter: 0110100
269+
then go back and copy the entire 8 bit binary 10110100
270+
271+
so in our example, concat 0110100 + 10110100 and convert to decimal = eg. 296
272+
this will give you a decimal number which is the number of bytes from the start of the page to the first item.
273+
296 / 16 = 18.5 rows. and that will be the start of the first item.
274+
275+
276+
##### Freespace - unlocated space.
277+
278+
##### Items - actual items themselves.
279+
to calculate length of data. click on 3rd byte of 4 the bytes. look at int16 value. eg. 172
280+
length of first item is 172.
281+
282+
68.6.1 Table row layout - And item is further made up of a fixed-size header (23 bytes), followed by optional data, then is the actual user data.
283+
the pointer to the first value -> data inspector -> Int16 -> eg. 203 is the actual data is the id of the data in the postgreSQL table.

0 commit comments

Comments
 (0)