-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
5 changed files
with
319 additions
and
0 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,61 @@ | ||
# Postgres :: Execution plan and Join strategies | ||
|
||
|
||
|
||
```sql | ||
SET enable_hashjoin = off; | ||
SET enable_mergejoin = off; | ||
SET enable_nestloop = off; | ||
``` | ||
|
||
|
||
```sql | ||
|
||
|
||
``` | ||
|
||
|
||
```sql | ||
|
||
|
||
``` | ||
|
||
|
||
|
||
```sql | ||
|
||
|
||
``` | ||
|
||
## REFRESH MATERIALIZED VIEW sales; | ||
|
||
|
||
|
||
https://postgresqlblog.hashnode.dev/mastering-parameter-sensitive-plans-in-postgresql-for-better-query-performance | ||
|
||
## VACUUM | ||
|
||
The PostgreSQL VACUUM operation is designed to reclaim storage occupied by dead tuples, which are rows that have been updated or deleted. These dead tuples are not immediately removed to allow for transactional consistency and MVCC (Multi-Version Concurrency Control). | ||
|
||
https://postgresqlblog.hashnode.dev/understanding-postgresql-vacuum-boosting-query-performance-with-memory-reclamation | ||
|
||
|
||
## MVCC | ||
|
||
|
||
|
||
SQL: Finding the bottleneck | ||
|
||
## References | ||
* https://github.com/shiviyer/Blogs/wiki | ||
* https://github.com/shiviyer/Troubleshooting-PostgreSQL-Performance | ||
* https://www.cybertec-postgresql.com/en/join-strategies-and-performance-in-postgresql/ | ||
* https://www.cybertec-postgresql.com/en/postgresql-speeding-up-group-by-and-joins/ | ||
* https://www.cybertec-postgresql.com/en/tag/performance-tuning/ | ||
* https://www.cybertec-postgresql.com/en/postgresql-hash-index-performance/ | ||
* https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob_plain;f=src/backend/access/hash/README | ||
* https://www.timescale.com/learn/strategies-for-improving-postgres-join-performance | ||
* https://postgresqlblog.hashnode.dev/optimizing-postgresql-performance-with-the-sort-merge-join-algorithm-a-detailed-guide | ||
* https://postgresqlblog.hashnode.dev/understanding-postgresql-vacuum-boosting-query-performance-with-memory-reclamation | ||
* https://djangocas.dev/blog/postgresql/postgresql-mini-cookbook-performance-tuning-debugging-and-testing/ | ||
* |
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,134 @@ | ||
# MERGE Command in PostgreSQL | ||
|
||
MERGE — conditionally insert, update, or delete rows of a table | ||
|
||
PostgreSQL 15 introduced the MERGE statement that simplifies data manipulation by combining INSERT, UPDATE, and DELETE operations into a single statement. The MERGE statement is often referred to as UPSERT statement. | ||
|
||
|
||
|
||
https://www.postgresql.org/docs/current/sql-merge.html | ||
|
||
https://dzone.com/articles/introducing-the-merge-command-in-postgresql-15 | ||
|
||
https://neon.tech/postgresql/postgresql-tutorial/postgresql-merge | ||
|
||
|
||
```sql | ||
MERGE INTO target_table | ||
USING source_query | ||
ON merge_condition | ||
WHEN MATCH [AND condition] THEN {merge_update | merge_delete | DO NOTHING } | ||
WHEN NOT MATCHED [AND condition] THEN { merge_insert | DO NOTHING }; | ||
``` | ||
|
||
|
||
```sql | ||
CREATE TABLE leads( | ||
lead_id serial PRIMARY key, | ||
name VARCHAR(255) NOT NULL, | ||
email VARCHAR(255) NOT NULL UNIQUE, | ||
active bool NOT NULL DEFAULT TRUE | ||
); | ||
CREATE TABLE customers( | ||
customer_id serial PRIMARY key, | ||
name VARCHAR(255) NOT NULL, | ||
email VARCHAR(255) NOT NULL UNIQUE, | ||
active bool NOT NULL DEFAULT TRUE | ||
); | ||
``` | ||
|
||
```sql | ||
INSERT INTO leads(name, email) | ||
VALUES | ||
('John Doe', '[[email protected]](../cdn-cgi/l/email-protection.html)'), | ||
('Jane Doe', '[[email protected]](../cdn-cgi/l/email-protection.html)') | ||
RETURNING *; | ||
``` | ||
|
||
|
||
```sql | ||
MERGE INTO customers c | ||
USING leads l ON c.email = l.email | ||
WHEN NOT MATCHED THEN | ||
INSERT (name, email) | ||
VALUES(l.name, l.email); | ||
``` | ||
|
||
```sql | ||
SELECT * FROM customers; | ||
``` | ||
|
||
```sql | ||
INSERT INTO leads(name, email) | ||
VALUES('Alice Smith', '[[email protected]](../cdn-cgi/l/email-protection.html)'); | ||
|
||
UPDATE leads | ||
SET name = 'Jane Gate' | ||
WHERE lead_id = 2; | ||
``` | ||
|
||
|
||
```sql | ||
SELECT * FROM leads | ||
ORDER BY id; | ||
``` | ||
|
||
|
||
|
||
```sql | ||
MERGE INTO customers c | ||
USING leads l ON c.email = l.email | ||
WHEN NOT MATCHED THEN | ||
INSERT (name, email) | ||
VALUES(l.name, l.email) | ||
WHEN MATCHED THEN | ||
UPDATE SET | ||
name = l.name, | ||
email = l.email; | ||
``` | ||
|
||
```sql | ||
INSERT INTO leads(name, email) | ||
VALUES('Bob Climo', '[[email protected]](../cdn-cgi/l/email-protection.html)'); | ||
``` | ||
|
||
|
||
```sql | ||
UPDATE leads | ||
SET active = false | ||
WHERE lead_id = 2; | ||
``` | ||
|
||
|
||
```sql | ||
UPDATE leads | ||
SET email = '[[email protected]](../cdn-cgi/l/email-protection.html)' | ||
WHERE lead_id = 1; | ||
``` | ||
|
||
|
||
```sql | ||
SELECT * FROM leads | ||
ORDER BY lead_id; | ||
``` | ||
|
||
|
||
```sql | ||
MERGE INTO customers c | ||
USING leads l ON c.email = l.email | ||
WHEN NOT MATCHED THEN | ||
INSERT (name, email) | ||
VALUES(l.name, l.email) | ||
WHEN MATCHED AND l.active = false THEN | ||
DELETE | ||
WHEN MATCHED AND l.active = true THEN | ||
UPDATE SET | ||
name = l.name, | ||
email = l.email; | ||
``` | ||
|
||
|
||
|
||
```sql | ||
SELECT * FROM customers; | ||
``` |
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,42 @@ | ||
# PostgreSQL pg_cron | ||
|
||
Run periodic jobs in PostgreSQL | ||
|
||
https://github.com/citusdata/pg_cron | ||
|
||
Automating MERGE is important for error-prone MERGE execution and reducing maintenance hassle. PostgreSQL supports pg_cron extension. This extension is used to configure scheduled tasks for a PostgreSQL database. A typical pg_cron command mentioned below shows the scheduling options: | ||
|
||
SELECT cron.schedule('Minute Hour Date Month Day of the week', 'Task'); | ||
|
||
|
||
|
||
``` | ||
┌───────────── min (0 - 59) | ||
│ ┌────────────── hour (0 - 23) | ||
│ │ ┌─────────────── day of month (1 - 31) or last day of the month ($) | ||
│ │ │ ┌──────────────── month (1 - 12) | ||
│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to | ||
│ │ │ │ │ Saturday, or use names; 7 is also Sunday) | ||
│ │ │ │ │ | ||
│ │ │ │ │ | ||
* * * * * | ||
``` | ||
|
||
```sql | ||
ELECT cron.schedule('30 * * * *', $$MERGE INTO station_main sm | ||
|
||
USING station_1 s | ||
|
||
ON sm.station_id=s.station_id | ||
|
||
when matched then | ||
|
||
update set data=s.data | ||
|
||
WHEN NOT MATCHED THEN | ||
|
||
INSERT (station_id, data) | ||
|
||
VALUES (s.station_id, s.data);$$); | ||
``` | ||
|
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,3 @@ | ||
# POSTGRES :: Avaivility | ||
|
||
Easy Steps to Configure Hot Standby with Logical Replication in PostgreSQL 16 https://postgresqlblog.hashnode.dev/easy-steps-to-configure-hot-standby-with-logical-replication-in-postgresql-16 |
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,79 @@ | ||
# Upsert | ||
|
||
https://neon.tech/postgresql/postgresql-tutorial/postgresql-upsert | ||
|
||
|
||
|
||
|
||
```sql | ||
CREATE TABLE inventory( | ||
id INT PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
price DECIMAL(10,2) NOT NULL, | ||
quantity INT NOT NULL | ||
); | ||
|
||
INSERT INTO inventory(id, name, price, quantity) | ||
VALUES | ||
(1, 'A', 15.99, 100), | ||
(2, 'B', 25.49, 50), | ||
(3, 'C', 19.95, 75) | ||
RETURNING *; | ||
``` | ||
|
||
|
||
|
||
|
||
```sql | ||
INSERT INTO inventory (id, name, price, quantity) | ||
VALUES (1, 'A', 16.99, 120) | ||
ON CONFLICT(id) | ||
DO UPDATE SET | ||
price = EXCLUDED.price, | ||
quantity = EXCLUDED.quantity; | ||
``` | ||
|
||
|
||
|
||
```sql | ||
SELECT * FROM inventory | ||
WHERE id = 1; | ||
``` | ||
|
||
|
||
```sql | ||
INSERT INTO inventory (id, name, price, quantity) | ||
VALUES (4, 'D', 29.99, 20) | ||
ON CONFLICT(id) | ||
DO UPDATE SET | ||
price = EXCLUDED.price, | ||
quantity = EXCLUDED.quantity; | ||
``` | ||
|
||
|
||
```sql | ||
SELECT * FROM inventory | ||
ORDER BY id; | ||
``` | ||
|
||
|
||
```sql | ||
|
||
``` | ||
|
||
|
||
```sql | ||
|
||
``` | ||
|
||
|
||
```sql | ||
|
||
``` | ||
|
||
|
||
```sql | ||
|
||
``` | ||
|
||
|