-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78f286e
commit c5007e9
Showing
24 changed files
with
647 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
RDBMS/PostgreSQL/architecture/storage-filesystem/datafile.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.