إدارة الجداول المقسّمة
تعلّم أفضل الممارسات لإضافة الأقسام وحذفها وتعديلها، إلى جانب التعامل مع الأقسام الافتراضية.
إدارة الجداول المقسّمة درس مجاني في Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Advanced PostgreSQL: Indexing, Partitioning, Replication، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Managing Partitioned Tables
As your database grows, managing partitioned tables becomes crucial. This lesson explores how to dynamically add, remove, and modify partitions to keep your data organized and performant.
We'll cover adding new partitions for future data, detaching old partitions for archiving, and attaching existing tables as new partitions. We'll also look at handling data that doesn't fit any defined partition.
Setting Up a Partitioned Table
Before we manage partitions, let's create a base partitioned table. We'll use a simple sales_data table, partitioned by sale_date using a range partitioning strategy.
This table will serve as our example throughout the lesson.
CREATE TABLE sales_data (
id SERIAL,
sale_date DATE,
amount DECIMAL(10, 2)
) PARTITION BY RANGE (sale_date);
-- Add an initial partition for December 2023
CREATE TABLE sales_2023_12 PARTITION OF sales_data
FOR VALUES FROM ('2023-12-01') TO ('2024-01-01');
INSERT INTO sales_data (sale_date, amount) VALUES ('2023-12-15', 150.75);
SELECT * FROM sales_data;Adding New Partitions
As time passes, you'll need to add new partitions for incoming data. This is typically done using the CREATE TABLE ... PARTITION OF ... command, similar to how we created the initial partition.
This method creates a new, empty child table and links it to the parent partitioned table for a specified range of values.
Example: Adding a New Month
Let's add a partition for January 2024. This ensures that any new sales data for January will automatically be directed to its own dedicated storage segment.
Notice how we define the specific date range for this new partition.
-- Add a partition for January 2024
CREATE TABLE sales_2024_01 PARTITION OF sales_data
FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');
INSERT INTO sales_data (sale_date, amount) VALUES ('2024-01-20', 200.50);
-- Verify data is in the correct partition (optional check)
SELECT tableoid::regclass, * FROM sales_data WHERE sale_date = '2024-01-20';Detaching Old Partitions
Over time, older partitions might contain historical data that is rarely accessed but still needs to be preserved. Detaching a partition allows you to remove it from the partitioned table structure without deleting the data.
The detached partition becomes a standalone, regular table, which can then be archived, moved, or even dropped if no longer needed.
Example: Archiving Old Data
Let's detach the sales_2023_12 partition. After detachment, it will exist as a separate table named sales_2023_12, no longer part of the sales_data partitioned table.
This is useful for reducing the active dataset size for queries.
-- Detach the December 2023 partition
ALTER TABLE sales_data DETACH PARTITION sales_2023_12;
-- The detached table still exists and contains its data
SELECT * FROM sales_2023_12;
-- Trying to query sales_data for Dec 2023 will now return nothing
SELECT * FROM sales_data WHERE sale_date = '2023-12-15';Attaching Existing Tables
Sometimes you might have a large dataset in a regular table that you want to integrate into a partitioned table. Instead of inserting all data, you can attach that existing table as a new partition.
The table to be attached must be empty or contain only data that satisfies the partition's constraints. This is a very fast operation as it doesn't involve data movement.
Example: Attaching a Prepared Table
Imagine we have a standalone table, sales_2024_02_temp, containing February 2024 sales. We can quickly attach it to our sales_data partitioned table.
First, we create and populate the temporary table, then attach it.
-- Create a standalone table for February 2024 sales
CREATE TABLE sales_2024_02_temp (
id SERIAL,
sale_date DATE,
amount DECIMAL(10, 2)
);
INSERT INTO sales_2024_02_temp (sale_date, amount) VALUES ('2024-02-10', 300.25);
-- Attach this table as a new partition
ALTER TABLE sales_data ATTACH PARTITION sales_2024_02_temp
FOR VALUES FROM ('2024-02-01') TO ('2024-03-01');
-- Now, sales_data can query the February data
SELECT * FROM sales_data WHERE sale_date = '2024-02-10';Handling Undefined Data with DEFAULT
What happens if data comes in that doesn't fit any of your defined partition ranges? By default, it would cause an error. To prevent this, you can create a DEFAULT partition.
The default partition acts as a catch-all for any rows that don't match the criteria of other existing partitions. It's essential for robustness and preventing data loss.
Example: Creating a Default Partition
Let's add a default partition to our sales_data table. Then, we'll try inserting a sale from a future date (e.g., 2025) for which no explicit partition exists.
This data will automatically be routed to the sales_default partition.
-- Create a default partition
CREATE TABLE sales_default PARTITION OF sales_data DEFAULT;
-- Insert data that doesn't fit existing ranges (e.g., 2025)
INSERT INTO sales_data (sale_date, amount) VALUES ('2025-01-05', 450.99);
-- Verify data went to the default partition
SELECT tableoid::regclass, * FROM sales_data WHERE sale_date = '2025-01-05';Partition Management Check
You've learned how to dynamically adjust your partitioned table structure.
Recap: Partition Management
In this lesson, you mastered key aspects of managing partitioned tables:
- Adding new partitions: Use
CREATE TABLE ... PARTITION OF ...to prepare for new data ranges. - Detaching partitions: The
ALTER TABLE ... DETACH PARTITION ...command removes a partition from the set, turning it into an independent table for archiving or separate management. - Attaching partitions: Use
ALTER TABLE ... ATTACH PARTITION ...to quickly integrate an existing regular table as a new partition. - Default partitions: Create a
DEFAULTpartition to catch any data that doesn't match other defined partition boundaries, ensuring data integrity.
These techniques empower you to maintain flexible, performant, and scalable database systems.
الأسئلة الشائعة
هل درس «إدارة الجداول المقسّمة» مجاني؟
نعم — نص درس «إدارة الجداول المقسّمة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Advanced PostgreSQL: Indexing, Partitioning, Replication، انتقل إلى CoddyKit PRO. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
ماذا ستتعلم في «إدارة الجداول المقسّمة»؟
تعلّم أفضل الممارسات لإضافة الأقسام وحذفها وتعديلها، إلى جانب التعامل مع الأقسام الافتراضية. تتمرن على Advanced PostgreSQL: Indexing, Partitioning, Replication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Advanced PostgreSQL: Indexing, Partitioning, Replication؟
لا تُشترط خبرة سابقة. Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «إدارة الجداول المقسّمة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Advanced PostgreSQL: Indexing, Partitioning, Replication هذا؟
نعم. كل درس في Advanced PostgreSQL: Indexing, Partitioning, Replication يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التقسيم بالتجزئة للتوزيع
- تقنيات التقسيم الفرعي
- إدارة الجداول المقسّمة
- التقسيم حسب النطاق الزمني