A few details are still not clear, but let's see, what we can do now.
First, having about 100M rows and 231 partitions sounds not that good. The resulting tables will be too small, in turn their number too high - I cannot tell the threshold, but at some point the query planning migt get too expensive. I think it is quite possible that yearly partitions would be enough. Alternatively, if you really want to fetch a whole month at a time, create monthly partitions.
Now to the actual problem.
It is not quite clear to me why you have rows in the parent table. The usual way of partitioning is that the parent is empty, and every row is redirected to one of the children.
At the same time, is you have an index on posted_at
of the parent table (as you have it on the children), finding rows in the parent based on the timestamp is easy.
On the other hand, while I'm not sure which column shared_parent_id
refers to, you can define an index on it, too - looking rows up based on this will be easy, too.
The only thing still has to be added is tell your query to look for parents in the parent table only. Let's have a look at a possible query:
WITH child_messages AS (
SELECT shared_parent_id, {other interesting columns}
FROM messages
WHERE posted_at {matches your needs}
)
SELECT *
FROM child_messages
UNION ALL
SELECT shared_parent_id, {other interesting columns}
FROM ONLY messages -- this way it does not go to the children
WHERE {unclear column} IN (SELECT shared_parent_id FROM child_messages);
The WITH
query may pick up rows from the parent, too - this you may or may not want, adjust the query accordingly.
Furthermore, the performance might not be ideal, in this case there is room for tweaking the query (eg. a JOIN
instead of the IN()
, pushing the query in the WITH
clause into a (sub)query and so on).
And a final notice: varchar(255)
is usually a sign of a value of unknown-before length - if you really want to constrain it, you may want to choose a meaningful limit. Otherwise, an unlimited varchar
(or text
) has a slight performance advantage in PostgreSQL over the limited ones. Furthermore, from your example it seems that shared_parent_id
is a number (integer) - use the best fitting type.
Your problem is related to this point in the documentation:
When creating a range partition, the lower bound specified with FROM
is an inclusive bound, whereas the upper bound specified with TO
is an exclusive bound.
(emphasis added)
So in fact in
CREATE TABLE parsel_2014_10 PARTITION OF parsel_part
FOR VALUES FROM ('2014-10-01') TO ('2014-10-31');
the date 2014-10-31
is not included in this partition, and hence nowhere in all your partition tables (and same for all ending dates)
See the example at: https://www.postgresql.org/docs/10/static/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE
The to
value of one partition must be the same as the from
value of the next one (because the to
part is exclusive and the from
is inclusive).
Best Answer
There must be some kind of misunderstanding.
tableoid
is a system column that's available for any regular table in any version of Postgres since at least v7.3. Per documentation:tableoid
is one of them. The solution I provided under the questions you refer to is also suggested in the manual here. And it works. I have been using it for years in various Postgres versions.Either you are not using Postgres, or
template
is not a table. Is it aVIEW
maybe? If you define aVIEW
like:Then system columns are not included by default.
What do you get for
Is
relkind
'r', 'v' or something else?Or do you get multiple rows? Then consider this: