w3resource

Determine Record Counts for each Sales Partition


Count Records in Each Partition

Write a PostgreSQL query to count the number of records in each partition of the Sales table.

Solution:

-- Count records in each partition of the Sales table
SELECT tableoid::regclass AS partition_name,  -- Convert tableoid to regclass for a human-readable partition name
       COUNT(*) AS record_count               -- Count the number of records in each partition
FROM Sales                                   -- Query the partitioned Sales table
GROUP BY tableoid;                           -- Group the results by the partition identifier (tableoid)

Explanation:

  • Purpose of the Query:
    • Counts records in each partition to analyze data distribution.
  • Key Components:
    • tableoid::regclass: Retrieves the partition name dynamically.
  • Real-World Application:
    • Helps in monitoring partition sizes and load balancing.

Notes:

  • tableoid provides internal table identifiers.

For more Practice: Solve these Related Problems:

  • Write a PostgreSQL query to count the number of records in each partition and display the results sorted by record count.
  • Write a PostgreSQL query to count records in each partition and filter out partitions with fewer than a specified number of rows.
  • Write a PostgreSQL query to count records in each partition using a subquery that retrieves partition names from the system catalog.
  • Write a PostgreSQL query to count records in each partition and include each partition’s boundary values in the output.


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

Previous PostgreSQL Exercise: Retrieve Data from Multiple Partitions.

Next PostgreSQL Exercise: Query Data using Index Scan on Partitioned Table.

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.