0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · Leçon

Jointures et agrégations par partition

Découvrez comment PostgreSQL peut joindre et agréger des tables partitionnées partition par partition pour obtenir d’importants gains de performance.

Jointures et agrégations par partition est une leçon Advanced PostgreSQL: Indexing, Partitioning, Replication gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Advanced PostgreSQL: Indexing, Partitioning, Replication, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Advanced PostgreSQL: Indexing, Partitioning, Replication comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

What Are Partition-wise Operations

When two tables are partitioned the same way, PostgreSQL can join matching partitions to each other instead of joining the whole tables. This is a partition-wise join.

The same idea applies to grouping: a partition-wise aggregate.

Enabling the Feature

These optimizations are off by default because they increase planning cost. Turn them on per session or in postgresql.conf.

SET enable_partitionwise_join = on;
SET enable_partitionwise_aggregate = on;

Matching Partition Boundaries

For a partition-wise join the tables must share the same partition key type and identical bounds. Otherwise the planner cannot pair partitions.

Example Setup

Two tables partitioned by the same range on customer_id.

CREATE TABLE orders (customer_id int, total numeric)
    PARTITION BY RANGE (customer_id);
CREATE TABLE refunds (customer_id int, amount numeric)
    PARTITION BY RANGE (customer_id);

The Join

With matching partitions, this join runs as several small joins, each fitting in memory more easily.

SELECT o.customer_id, sum(o.total), sum(r.amount)
FROM orders o
JOIN refunds r USING (customer_id)
GROUP BY o.customer_id;

Why It Is Faster

Smaller per-partition joins mean:

  • Smaller hash tables that fit in work_mem
  • Better cache locality
  • Opportunities for parallel workers per partition

Reading the Plan

Use EXPLAIN to confirm. You will see an Append node over several join nodes rather than one giant join.

EXPLAIN
SELECT * FROM orders o JOIN refunds r USING (customer_id);

Partition-wise Aggregate

If you group by the partition key, each partition is aggregated independently and the results are concatenated. No global sort or hash across the whole table is needed.

SELECT customer_id, sum(total)
FROM orders
GROUP BY customer_id;

Planning Cost Trade-off

With many partitions, considering each one increases planning time. Enable these settings only when partition counts are moderate and the query benefits clearly.

Combining with Pruning

Partition-wise joins combine well with partition pruning: pruning removes irrelevant partitions first, then the remaining ones are joined pair-by-pair.

When It Does Not Apply

If only one table is partitioned, or the bounds differ, PostgreSQL falls back to a normal join over the appended partitions.

Quick Check

What is required for a partition-wise join?

Recap

You learned to speed up queries on partitioned tables with partition-wise joins and aggregates. Enable the settings, ensure matching bounds, verify with EXPLAIN, and combine with pruning for the best results.

Questions Fréquemment Posées

La leçon « Jointures et agrégations par partition » est-elle gratuite ?

Oui — le texte complet de « Jointures et agrégations par partition » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Advanced PostgreSQL: Indexing, Partitioning, Replication, passe à CoddyKit PRO. Le cours Advanced PostgreSQL: Indexing, Partitioning, Replication comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Jointures et agrégations par partition » ?

Découvrez comment PostgreSQL peut joindre et agréger des tables partitionnées partition par partition pour obtenir d’importants gains de performance. Tu pratiques Advanced PostgreSQL: Indexing, Partitioning, Replication avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Advanced PostgreSQL: Indexing, Partitioning, Replication ?

Aucune expérience préalable n'est requise. Advanced PostgreSQL: Indexing, Partitioning, Replication sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.

Combien de temps prend la leçon « Jointures et agrégations par partition » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Advanced PostgreSQL: Indexing, Partitioning, Replication ?

Oui. Chaque leçon Advanced PostgreSQL: Indexing, Partitioning, Replication inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Optimisation des requêtes avec le partitionnement
  2. Attacher et détacher des partitions
  3. Élagage et exclusion des partitions
  4. Jointures et agrégations par partition
← Retour à Advanced PostgreSQL: Indexing, Partitioning, Replication