Consider the following PostgreSQL partitioned table setup:
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
sale_date DATE NOT NULL,
amount NUMERIC
) PARTITION BY RANGE (sale_date);
CREATE TABLE sales_2023 PARTITION OF sales
FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
CREATE TABLE sales_2024 PARTITION OF sales
FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');
INSERT INTO sales (sale_date, amount) VALUES
('2023-06-15', 100),
('2024-03-10', 200);
SELECT tableoid::regclass, sale_date, amount FROM sales ORDER BY sale_date;What will be the output of the SELECT query?
CREATE TABLE sales ( id SERIAL PRIMARY KEY, sale_date DATE NOT NULL, amount NUMERIC ) PARTITION BY RANGE (sale_date); CREATE TABLE sales_2023 PARTITION OF sales FOR VALUES FROM ('2023-01-01') TO ('2024-01-01'); CREATE TABLE sales_2024 PARTITION OF sales FOR VALUES FROM ('2024-01-01') TO ('2025-01-01'); INSERT INTO sales (sale_date, amount) VALUES ('2023-06-15', 100), ('2024-03-10', 200); SELECT tableoid::regclass, sale_date, amount FROM sales ORDER BY sale_date;
Remember that data inserted into a partitioned table is stored in the matching partition table.
The data is routed to the correct partition based on the sale_date. The first row goes to sales_2023 because its date is in 2023, the second to sales_2024. The tableoid::regclass shows the actual partition table name.
You want to create a partitioned table in PostgreSQL to store user logs. The logs have a timestamp column and a user_id column. You expect queries mostly filter by date ranges and sometimes by user_id.
Which partitioning strategy is best for optimizing query performance?
Think about which column is most commonly used in WHERE clauses and how partition pruning works.
Since queries mostly filter by date ranges, partitioning by RANGE on the timestamp column allows PostgreSQL to skip irrelevant partitions efficiently. Partitioning by user_id would not help range queries on dates.
Which of the following CREATE TABLE statements for a partitioned table is syntactically correct in PostgreSQL?
Check the exact syntax for partitioning in PostgreSQL.
Option A is correct as it uses PARTITION BY RANGE (order_date). Option A is correct syntax for LIST partitioning. Option A uses invalid keyword PARTITIONED BY. Option A misses parentheses after RANGE.
Given this partitioned table setup:
CREATE TABLE events (
id SERIAL PRIMARY KEY,
event_type TEXT NOT NULL
) PARTITION BY LIST (event_type);
CREATE TABLE events_login PARTITION OF events FOR VALUES IN ('login');
CREATE TABLE events_logout PARTITION OF events FOR VALUES IN ('logout');
INSERT INTO events (event_type) VALUES ('signup');Why does the INSERT statement fail?
Think about how PostgreSQL routes data to partitions.
Since there is no partition defined for the value 'signup' in the event_type column, PostgreSQL cannot find a matching partition and raises an error.
You have a large PostgreSQL table partitioned by RANGE on a date column with monthly partitions. You notice queries filtering on date ranges are slow.
Which of the following actions will most likely improve query performance?
Consider how indexes work with partitioned tables in PostgreSQL.
PostgreSQL does not support global indexes on partitioned tables. Indexes must be created on each partition individually to speed up queries filtering on the partition key.