From 2fb26f62143eb3b1c47d7e51e3c742d2a82912bc Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Sat, 19 Jan 2019 23:18:24 -0800 Subject: [PATCH] tableam: finish_bulk_insert(). Author: Reviewed-By: Discussion: https://postgr.es/m/ Backpatch: --- src/backend/access/heap/heapam_handler.c | 13 +++++++++++++ src/backend/commands/copy.c | 7 +------ src/backend/commands/createas.c | 5 ++--- src/backend/commands/matview.c | 5 ++--- src/backend/commands/tablecmds.c | 4 +--- src/include/access/tableam.h | 22 ++++++++++++++++++++++ 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index c289bd99c4..1de749ac6c 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -426,6 +426,18 @@ retry: return result; } +static void +heapam_finish_bulk_insert(Relation relation, int options) +{ + /* + * If we skipped writing WAL, then we need to sync the heap (but not + * indexes since those use WAL anyway) + */ + if (options & HEAP_INSERT_SKIP_WAL) + heap_sync(relation); +} + + static bool heapam_fetch_row_version(Relation relation, ItemPointer tid, @@ -563,6 +575,7 @@ static const TableAmRoutine heapam_methods = { .tuple_update = heapam_heap_update, .multi_insert = heap_multi_insert, .tuple_lock = heapam_lock_tuple, + .finish_bulk_insert = heapam_finish_bulk_insert, .tuple_fetch_row_version = heapam_fetch_row_version, .tuple_get_latest_tid = heap_get_latest_tid, diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 36f09b5ac9..f76b3addde 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -3098,12 +3098,7 @@ CopyFrom(CopyState cstate) FreeExecutorState(estate); - /* - * If we skipped writing WAL, then we need to sync the heap (but not - * indexes since those use WAL anyway) - */ - if (hi_options & HEAP_INSERT_SKIP_WAL) - heap_sync(cstate->rel); + table_finish_bulk_insert(cstate->rel, hi_options); return processed; } diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c index 0ac295cea3..55f6185461 100644 --- a/src/backend/commands/createas.c +++ b/src/backend/commands/createas.c @@ -28,6 +28,7 @@ #include "access/reloptions.h" #include "access/htup_details.h" #include "access/sysattr.h" +#include "access/tableam.h" #include "access/xact.h" #include "access/xlog.h" #include "catalog/namespace.h" @@ -601,9 +602,7 @@ intorel_shutdown(DestReceiver *self) FreeBulkInsertState(myState->bistate); - /* If we skipped using WAL, must heap_sync before commit */ - if (myState->hi_options & HEAP_INSERT_SKIP_WAL) - heap_sync(myState->rel); + table_finish_bulk_insert(myState->rel, myState->hi_options); /* close rel, but keep lock until commit */ table_close(myState->rel, NoLock); diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index 5a47be4b33..62b76cfd35 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -18,6 +18,7 @@ #include "access/heapam.h" #include "access/htup_details.h" #include "access/multixact.h" +#include "access/tableam.h" #include "access/xact.h" #include "access/xlog.h" #include "catalog/catalog.h" @@ -509,9 +510,7 @@ transientrel_shutdown(DestReceiver *self) FreeBulkInsertState(myState->bistate); - /* If we skipped using WAL, must heap_sync before commit */ - if (myState->hi_options & HEAP_INSERT_SKIP_WAL) - heap_sync(myState->transientrel); + table_finish_bulk_insert(myState->transientrel, myState->hi_options); /* close transientrel, but keep lock until commit */ table_close(myState->transientrel, NoLock); diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 3a04a26f01..0b1b73c434 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -4953,9 +4953,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode) { FreeBulkInsertState(bistate); - /* If we skipped writing WAL, then we need to sync the heap. */ - if (hi_options & HEAP_INSERT_SKIP_WAL) - heap_sync(newrel); + table_finish_bulk_insert(newrel, hi_options); table_close(newrel, NoLock); } diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h index e6530f2385..b5909d8b6b 100644 --- a/src/include/access/tableam.h +++ b/src/include/access/tableam.h @@ -157,6 +157,18 @@ typedef struct TableAmRoutine LockWaitPolicy wait_policy, uint8 flags, HeapUpdateFailureData *hufd); + + /* + * Perform operations necessary to complete insertions made via + * tuple_insert and multi_insert with a BulkInsertState specified. This + * e.g. may e.g. used to flush the relation when inserting with skipping + * WAL. + * + * May be NULL. + */ + void (*finish_bulk_insert) (Relation rel, int options); + + /* ------------------------------------------------------------------------ * Non-modifying operations on individual tuples. * ------------------------------------------------------------------------ @@ -445,6 +457,16 @@ table_lock_tuple(Relation rel, ItemPointer tid, Snapshot snapshot, cid, mode, wait_policy, flags, hufd); } + +static inline void +table_finish_bulk_insert(Relation rel, int options) +{ + /* optional */ + if (rel->rd_tableam && rel->rd_tableam->finish_bulk_insert) + rel->rd_tableam->finish_bulk_insert(rel, options); +} + + /* ---------------------------------------------------------------------------- * Non-modifying operations on individual tuples. * ---------------------------------------------------------------------------- -- 2.39.5