Skip to content

Commit

Permalink
[rdbms][oracle] add docker
Browse files Browse the repository at this point in the history
  • Loading branch information
GradedJestRisk committed Jan 3, 2025
1 parent 78f286e commit c5007e9
Show file tree
Hide file tree
Showing 24 changed files with 647 additions and 173 deletions.
27 changes: 27 additions & 0 deletions RDBMS/ORACLE/docker/oracle-free/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
############
# Roles #
############

# Administrator
export POSTGRESQL_USER_NAME=postgres
export POSTGRESQL_USER_PASSWORD=password

############
# Database #
############
export POSTGRESQL_DATABASE_NAME=database

############
# Network #
############
export POSTGRESQL_INTERNAL_PORT=5432
export POSTGRESQL_EXPOSED_PORT=5434

############
# Client #
############
export CLIENT_APPLICATION_NAME=oracle

# Connexion
export CONNECTION_STRING=`echo host=localhost port=$POSTGRESQL_EXPOSED_PORT dbname=$POSTGRESQL_DATABASE_NAME user=$POSTGRESQL_USER_NAME password=$POSTGRESQL_USER_PASSWORD application_name=$CLIENT_APPLICATION_NAME`;
export CONNECTION_STRING_INTERNAL=`echo host=localhost port=$POSTGRESQL_INTERNAL_PORT dbname=$POSTGRESQL_DATABASE_NAME user=$POSTGRESQL_USER_NAME password=$POSTGRESQL_USER_PASSWORD application_name=$CLIENT_APPLICATION_NAME`;
21 changes: 21 additions & 0 deletions RDBMS/ORACLE/docker/oracle-free/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# https://hub.docker.com/r/gvenzl/oracle-free
# Starts in 2 minutes 30 seconds, then 30 seconds
# Connection - IDE: Service=FREEPDB1, User=username, Password=password
# - CLI: sqlplus username/password@//localhost/FREEPDB1
services:
oracle:
container_name: oracle
image: gvenzl/oracle-free:latest-faststart
ports:
- "1521:1521"
environment:
ORACLE_PASSWORD: sys_user_password
APP_USER: username
APP_USER_PASSWORD: password
healthcheck:
test: ["CMD", "healthcheck.sh"]
interval: 10s
timeout: 5s
retries: 10
start_period: 5s
start_interval: 5s
20 changes: 20 additions & 0 deletions RDBMS/ORACLE/docker/oracle-free/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
start-instance:
docker compose up --detach --wait

create-start-instance:
docker compose up --force-recreate --renew-anon-volumes --remove-orphans --detach --wait

stop-instance:
docker compose down

console:
psql --dbname "$CONNECTION_STRING"

logs:
docker logs --follow oracle

bash:
docker exec --interactive --tty oracle bash

sqlplus:
docker exec --interactive --tty oracle bash -c "sqlplus username/password@//localhost/FREEPDB1"
27 changes: 27 additions & 0 deletions RDBMS/ORACLE/docker/standard/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
############
# Roles #
############

# Administrator
export POSTGRESQL_USER_NAME=postgres
export POSTGRESQL_USER_PASSWORD=password

############
# Database #
############
export POSTGRESQL_DATABASE_NAME=database

############
# Network #
############
export POSTGRESQL_INTERNAL_PORT=5432
export POSTGRESQL_EXPOSED_PORT=5434

############
# Client #
############
export CLIENT_APPLICATION_NAME=oracle

# Connexion
export CONNECTION_STRING=`echo host=localhost port=$POSTGRESQL_EXPOSED_PORT dbname=$POSTGRESQL_DATABASE_NAME user=$POSTGRESQL_USER_NAME password=$POSTGRESQL_USER_PASSWORD application_name=$CLIENT_APPLICATION_NAME`;
export CONNECTION_STRING_INTERNAL=`echo host=localhost port=$POSTGRESQL_INTERNAL_PORT dbname=$POSTGRESQL_DATABASE_NAME user=$POSTGRESQL_USER_NAME password=$POSTGRESQL_USER_PASSWORD application_name=$CLIENT_APPLICATION_NAME`;
40 changes: 40 additions & 0 deletions RDBMS/ORACLE/docker/standard/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# https://collabnix.com/how-to-run-oracle-database-in-a-docker-container-using-docker-compose/
#services:
# oracle:
# container_name: oracle
# image: container-registry.oracle.com/database/enterprise:${ORACLE_IMAGE_VERSION}
# environment:
# - ORACLE_SID=${ORACLE_SERVICE_IDENTIFIER}
# - ORACLE_PDB=${ORACLE_SERVICE_NAME}
# - ORACLE_PWD=${ORACLE_USER_PASSWORD}
# ports:
# - "${ORACLE_EXPOSED_PORT}:${ORACLE_INTERNAL_PORT}"
# healthcheck:
# test: ["CMD", "sqlplus", "-L", "${ORACLE_USER_NAME}/${ORACLE_USER_PASSWORD}@//localhost:${ORACLE_INTERNAL_PORT}/${ORACLE_SERVICE_IDENTIFIER} as sysdba", "@healthcheck.sql"]
# interval: 2s
# timeout: 10s
# retries: 30


services:
oracle:
container_name: oracle
image: container-registry.oracle.com/database/enterprise:latest
environment:
- ORACLE_SID=ORCLCDB
- ORACLE_PDB=ORCLPDB1
- ORACLE_PWD=Oracle_123
ports:
- 1521:1521
volumes:
- oracle-data:/opt/oracle/oradata
- oracle-backup:/opt/oracle/backup
healthcheck:
test: ["CMD", "sqlplus", "-L", "sys/Oracle_123@//localhost:1521/ORCLCDB as sysdba", "@healthcheck.sql"]
interval: 30s
timeout: 10s
retries: 5

volumes:
oracle-data:
oracle-backup:
17 changes: 17 additions & 0 deletions RDBMS/ORACLE/docker/standard/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
start-instance:
docker compose up --detach --wait

create-start-instance:
docker compose up --force-recreate --renew-anon-volumes --remove-orphans --detach --wait

stop-instance:
docker compose down

console:
psql --dbname "$CONNECTION_STRING"

logs:
docker logs oracle bash

bash:
docker exec --interactive --tty oracle bash
35 changes: 35 additions & 0 deletions RDBMS/PostgreSQL/architecture/parallelism/parallelism.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Parallel

https://www.crunchydata.com/blog/parallel-queries-in-postgres

## CPU to use

### in total (max_worker_processes)

```postgresql
SHOW max_worker_processes;
SET max_worker_processes = 8;
```

max_worker_processes: Sets the maximum number of total worker processes allowed for the entire PostgreSQL instance, the default value is 8. This number includes any workers used for parallel queries. The general rule of thumb is to make this 25% of total vCPU count or greater. Some set this to the CPU count of the machine to take advantage of the most parallel workers.



### for queries (max_parallel_workers)

```postgresql
SHOW max_parallel_workers;
SET max_parallel_workers = 8;
```

max_parallel_workers: Sets the maximum number of parallel query worker processes allowed for the entire PostgreSQL instance, the default value is 8. By setting this value equal to the max_worker_processes, when no maintenance work is being run, Postgres will use all workers for queries. Conversely, high-transaction-rate systems limit the parallel workers to allocate workers for maintenance.

### Per query (max_parallel_workers_per_gather)

```postgresql
SHOW max_parallel_workers_per_gather;
SET max_parallel_workers_per_gather = 4;
```

max_parallel_workers_per_gather: Specifies the maximum number of workers that can be started by a single query. The default value is 2. The general rule of thumb here is that 2 might not always be enough. You can safely set this to half of your CPU count, or even your full CPU count if you want to make the most out of parallel queries.

96 changes: 96 additions & 0 deletions RDBMS/PostgreSQL/architecture/process/process.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Process

## Stop database (properly)

Use `pg_ctl`
```shell
docker exec --user postgres --tty postgresql bash -c "pg_ctl stop"
```

https://www.postgresql.org/docs/current/app-pg-ctl.html

In docker, you can stop the container.
Docker will send a `SIGTERM` to PG, which will stop gracefully most of the time before docker stop timeout. If it takes more time, docker will send a `SIGKILL`, so you may change the timeout.

To allow 30 seconds to stop.
```shell
docker stop --time 30 $CONTAINER_NAME
```

https://docs.docker.com/reference/cli/docker/container/stop/

## Stop query

### Unproperly

#### If you kill the backend_process using OS, the database will stop to recover

You will see the database shut down and went through a recovery
```text
2024-12-24 17:58:02.037 GMT [1] LOG: server process (PID 2340) was terminated by signal 9: Killed
2024-12-24 17:58:02.037 GMT [1] DETAIL: Failed process was running: SELECT pg_sleep(3000)
2024-12-24 17:58:02.037 GMT [1] LOG: terminating any other active server processes
2024-12-24 17:58:02.038 GMT [1] LOG: all server processes terminated; reinitializing
2024-12-24 17:58:02.055 GMT [3501] LOG: database system was interrupted; last known up at 2024-12-24 17:48:45 GMT
2024-12-24 17:58:02.108 GMT [3501] LOG: database system was not properly shut down; automatic recovery in progress
2024-12-24 17:58:02.110 GMT [3501] LOG: redo starts at 0/1950438
2024-12-24 17:58:02.110 GMT [3501] LOG: invalid record length at 0/1950520: expected at least 24, got 0
2024-12-24 17:58:02.110 GMT [3501] LOG: redo done at 0/19504E8 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2024-12-24 17:58:02.114 GMT [3502] LOG: checkpoint starting: end-of-recovery immediate wait
2024-12-24 17:58:02.123 GMT [3502] LOG: checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.003 s, sync=0.001 s, total=0.010 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB; lsn=0/1950520, redo lsn=0/1950520
2024-12-24 17:58:02.127 GMT [1] LOG: database system is ready to accept connections
```

#### backend_process are NOT stopped if the caller is killed using OS

Start a query
```postgresql
just run-query
```

Look at it in `pg_stat_activity`
```postgresql
SELECT pid
FROM pg_stat_activity ssn
WHERE 1=1
AND ssn.backend_type = 'client_backend'
--AND ssn.query LIKE '%INSERT%'
```

Kill it (Ctrl-C would cancel it, which is NOT what we want)
```shell
kill -s SIGKILL $(pidof -s just)
```

Or to target a command
```shell
ps -efw | grep "just create" | grep -v grep | awk '{print $2}' | xargs kill -s SIGKILL
```

The query is still running, even though the caller does not exist anymore.
```postgresql
SELECT pid
FROM pg_stat_activity ssn
WHERE ssn.query LIKE '%INSERT%'
```

### Properly

Get its PID in `pg_stat_activity`
```postgresql
SELECT pid
FROM pg_stat_activity ssn
WHERE 1=1
AND ssn.backend_type = 'client_backend'
--AND ssn.query LIKE '%INSERT%'
```

Then ask to stop, do `cancel`
```postgresql
SELECT pg_cancel_backend(90701);
```

If it won't, do `terminate`
```postgresql
SELECT pg_terminate_backend(90701);
```
59 changes: 55 additions & 4 deletions RDBMS/PostgreSQL/architecture/storage-filesystem/datafile.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
# Datafile structure

## Files

## Structure
### Relation (heap)

https://www.postgresql.org/docs/current/storage-file-layout.html
Each relation is stored in at least one file on FS.

The filename is the relation oid, eg. `16433` for table `big_table', followed by a dot if table exceeds 1 GB.

All relation files are stored in a folder, named with the database oid, eg. `16384` for database "database".

All database folder are stored in a folder named `base`, pointed by $PGDATA or `data_directory` setting.

Eg
```text
root@845f5da6d8e1:/# ls -ltrah /var/lib/postgresql/data/base/16384/16385
-rw------- 1 postgres postgres 346M Dec 26 10:44 /var/lib/postgresql/data/base/16384/16385
```

### Find files

To get the fragment under $PGDATA for a relation
```postgresql
SELECT pg_relation_filepath('big_table')
```

You get.
```text
base/16384/16433
```

Each relation is stored in a file on FS, under $PGDATA
Which is.
```text
base/$DATABASE_OID/$TABLE_OID
```

Relation
```postgresql
SELECT *
FROM pg_class
WHERE relname = 'big_table' and oid = 16433
```

Database
```postgresql
SELECT *
FROM pg_database
WHERE oid=16384 and datname = 'database'
```

In a container, you can get all files for a relation by executing
```postgresql
SELECT pg_relation_filepath('versions')
SELECT 'docker exec --tty postgresql bash -c ' || '"' || 'du -sh ' || setting || '/' || pg_relation_filepath('big_table') || '"'
FROM pg_settings WHERE name = 'data_directory';
```

## Structure of a file

### Relation

https://www.postgresql.org/docs/current/storage-file-layout.html

Each relation is stored in several blocks (=pages), 8 kbytes each.
```postgresql
SHOW block_size;
Expand Down
32 changes: 32 additions & 0 deletions RDBMS/PostgreSQL/architecture/version-control/MVCC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# MVCC

## Lexicon

https://www.postgresql.org/docs/current/datatype-oid.html

### Transaction ID (`xid`)
> Another identifier type used by the system is xid, or transaction (abbreviated xact) identifier. This is the data type of the system columns xmin and xmax. Transaction identifiers are 32-bit quantities.
### Tuple/row Id (`ctid`)
> A final identifier type used by the system is tid, or tuple identifier (row identifier). This is the data type of the system column ctid. A tuple ID is a pair (block number, tuple index within block) that identifies the physical location of the row within its table.
### Command Id (`cid`)
> A third identifier type used by the system is cid, or command identifier. This is the data type of the system columns cmin and cmax. Command identifiers are also 32-bit quantities.
## In a nutshell

In MVCC, each write operation (INSERT, UPDATE, DELETE) creates a new version of a data item while retaining the old version. When a transaction reads a data item, the system selects one of the versions to ensure isolation of the individual transaction.

The main advantage of MVCC is that
> readers don’t block writers, and writers don’t block readers.
When writing a new version of a data item, PostgreSQL:
- add a new data item into the relevant table page;
- does NOT delete the previous versions.

When writing a new data item, Oracle:
- write the old version of the item to the rollback segment;
- overwrite the data item with new values in the data area.

https://www.interdb.jp/pg/pgsql05.html

Loading

0 comments on commit c5007e9

Please sign in to comment.