Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So after digging around and experimenting I figured out what I needed and thought I would document my discovery.</p> <p>Here is the docs page on how basic postgres partitioning works for anyone interested: <a href="http://www.postgresql.org/docs/9.1/static/ddl-partitioning.html" rel="nofollow">http://www.postgresql.org/docs/9.1/static/ddl-partitioning.html</a></p> <p>The key point to know is that for every partition, you have to set constraints that specify what can be found in the partition (splitting the id so only 200,000 records are in each table would simply require one constraint per table). This makes it so when you query the master table, it quickly checks the request against the constraints of all the children tables. Only tables for which the query falls in the range of the table's constraints are further queried. If you do this right, only one table is truly queried. </p> <p><strong>So as for what I learned:</strong></p> <p>First off, I don't think it is possible at all to drop a table from its own trigger. My solution for this was just to mark it to drop through some other mechanism (like a table of things to drop later). The issue with this is it stays around until the Cron job runs. Since altering a table from its own trigger is also impossible, the table will still be a partition, just marked to be dropped later. If you have your partitioning correct this may cause no problems. In my case there can be two partitions overlapping (one that is marked to be dropped, and one that should be there). The side effect of this is that for queries specifying things that could be in either of these tables will hit both tables. This is fine for me until the Cron job comes by so I am good, but some cases might not work with this issue.</p> <p>Secondly, I realized that inserts that were altering tables they didn't write to were working fine, but deletes and updates that triggered the same trigger chain were failing because the tables were in use by the session. I assumed this was something crazy, but what the real issue was is that I was partitioned on a date and a foreign key, but was deleting by the id of the record. This caused postgres to check all the constraints of all of the tables, which gave it no insight on which table to use so it just checked all of them. All I had to do to get the delete to work was specify the info I partitioned on so it knew what table to look in instead of scanning the whole set of tables looking for an id. </p> <p><strong>TLDR</strong> If you partition a postgres table on some arbitrary (set of) column(s), make sure you delete or update the records by providing the columns you partitioned on so postgres can know what table to look in and not have to scan the whole set.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload