tableam: Allow choice of toast AM.
authorRobert Haas <rhaas@postgresql.org>
Tue, 7 Jan 2020 19:23:25 +0000 (14:23 -0500)
committerRobert Haas <rhaas@postgresql.org>
Tue, 7 Jan 2020 19:23:25 +0000 (14:23 -0500)
Previously, the toast table had to be implemented by the same AM that
was used for the main table, which was bad, because the detoasting
code won't work with anything but heap. This commit doesn't fix the
latter problem, although there's another patch coming which does,
but it does let you pick something that works (i.e. heap, right now).

Patch by me, reviewed by Andres Freund.

Discussion: http://postgr.es/m/CA+TgmoZv-=2iWM4jcw5ZhJeL18HF96+W1yJeYrnGMYdkFFnEpQ@mail.gmail.com

src/backend/access/heap/heapam_handler.c
src/backend/catalog/toasting.c
src/include/access/tableam.h

index a6c369eaea74813425441e5f04efc3ee239ac837..1ed60f4f4e544daa9124e105eaed3b9fb7b5f5ad 100644 (file)
@@ -2037,6 +2037,15 @@ heapam_relation_needs_toast_table(Relation rel)
    return (tuple_length > TOAST_TUPLE_THRESHOLD);
 }
 
+/*
+ * TOAST tables for heap relations are just heap relations.
+ */
+static Oid
+heapam_relation_toast_am(Relation rel)
+{
+   return rel->rd_rel->relam;
+}
+
 
 /* ------------------------------------------------------------------------
  * Planner related callbacks for the heap AM
@@ -2535,6 +2544,7 @@ static const TableAmRoutine heapam_methods = {
 
    .relation_size = table_block_relation_size,
    .relation_needs_toast_table = heapam_relation_needs_toast_table,
+   .relation_toast_am = heapam_relation_toast_am,
 
    .relation_estimate_size = heapam_estimate_rel_size,
 
index 2dfac45b5bcac9e46be6c45064306ec683d951fb..33344476cac12925f2909ec1e5759150af6259ea 100644 (file)
@@ -258,7 +258,7 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
                                           toast_typid,
                                           InvalidOid,
                                           rel->rd_rel->relowner,
-                                          rel->rd_rel->relam,
+                                          table_relation_toast_am(rel),
                                           tupdesc,
                                           NIL,
                                           RELKIND_TOASTVALUE,
index b14082a6f6409a8db985a1bd2690f8de51dc3c0f..c30a435b72c850c8a8690d3e678872d9943aea17 100644 (file)
@@ -581,6 +581,13 @@ typedef struct TableAmRoutine
     */
    bool        (*relation_needs_toast_table) (Relation rel);
 
+   /*
+    * This callback should return the OID of the table AM that implements
+    * TOAST tables for this AM.  If the relation_needs_toast_table callback
+    * always returns false, this callback is not required.
+    */
+   Oid         (*relation_toast_am) (Relation rel);
+
 
    /* ------------------------------------------------------------------------
     * Planner related functions.
@@ -1603,6 +1610,16 @@ table_relation_needs_toast_table(Relation rel)
    return rel->rd_tableam->relation_needs_toast_table(rel);
 }
 
+/*
+ * Return the OID of the AM that should be used to implement the TOAST table
+ * for this relation.
+ */
+static inline Oid
+table_relation_toast_am(Relation rel)
+{
+   return rel->rd_tableam->relation_toast_am(rel);
+}
+
 
 /* ----------------------------------------------------------------------------
  * Planner related functionality