w3resource

View Detailed Partition Information for the Orders Table


Query Partition Metadata

Write a PostgreSQL query to list all partitions under the Orders table.

Solution:

-- Retrieve list of partitions for the Orders table
SELECT inhrelid::regclass AS partition_name  -- Convert partition OID to a human-readable name
FROM pg_inherits                           -- Query the system catalog table that stores partition inheritance relationships
WHERE inhparent = 'Orders'::regclass;      -- Filter to include only partitions whose parent table is Orders

Explanation:

  • Purpose of the Query:
    • Displays all partitions linked to the parent table Orders.
  • Key Components:
    • pg_inherits: System catalog table storing partition relationships.
  • Real-World Application:
    • Helps in managing partitions dynamically.

Notes:

  • Useful before adding or removing partitions.

For more Practice: Solve these Related Problems:

  • Write a PostgreSQL query to list all partitions of a table along with their corresponding parent table and inheritance details.
  • Write a PostgreSQL query to retrieve partition metadata including partition names, boundaries, and row counts from system catalogs.
  • Write a PostgreSQL query to display the creation dates of all partitions for a given partitioned table.
  • Write a PostgreSQL query to list partitions along with their storage parameters and last vacuum timestamps.


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous PostgreSQL Exercise: Querying Default Partition Data.

Next PostgreSQL Exercise: Querying Partition Size in PostgreSQL.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.