From 82dceab0abeed669142b90d75c9c120d56e5367e Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Sun, 27 Dec 2020 13:24:57 +0530 Subject: [PATCH v16 1/4] Parallel Inserts in CREATE TABLE AS Allow the leader and each worker insert the tuples in parallel if the SELECT part of the CTAS is parallelizable. The design: The main idea is to push the CTAS dest receiver down to Gather node and from there the required information will be shared to workers so that they can perform parallel insertions. Leader will also participate in insertions. After the planning, check if the upper plan node is Gather in createas.c and mark a parallelism flag in the CTAS dest receiver and push it down to Gather node. Each worker can create its own CTAS dest receiver with the information passed from the leader. Leader inserts its share of tuples if instructed to do, and so are workers. Each worker writes atomically its number of inserted tuples into a shared memory variable, the leader combines this with its own number of inserted tuples and shares to the client. --- src/backend/access/heap/heapam.c | 11 --- src/backend/access/transam/xact.c | 28 ++++-- src/backend/commands/createas.c | 113 ++++++++++++++++++++++--- src/backend/commands/explain.c | 32 +++++++ src/backend/executor/execParallel.c | 70 +++++++++++++-- src/backend/executor/nodeGather.c | 112 ++++++++++++++++++++++-- src/backend/executor/nodeGatherMerge.c | 4 +- src/include/access/xact.h | 1 + src/include/commands/createas.h | 25 ++++++ src/include/executor/execParallel.h | 6 +- src/include/nodes/execnodes.h | 3 + 11 files changed, 361 insertions(+), 44 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index a9583f3103..86347ba273 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -2043,17 +2043,6 @@ static HeapTuple heap_prepare_insert(Relation relation, HeapTuple tup, TransactionId xid, CommandId cid, int options) { - /* - * To allow parallel inserts, we need to ensure that they are safe to be - * performed in workers. We have the infrastructure to allow parallel - * inserts in general except for the cases where inserts generate a new - * CommandId (eg. inserts into a table having a foreign key column). - */ - if (IsParallelWorker()) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TRANSACTION_STATE), - errmsg("cannot insert tuples in a parallel worker"))); - tup->t_data->t_infomask &= ~(HEAP_XACT_MASK); tup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK); tup->t_data->t_infomask |= HEAP_XMAX_INVALID; diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index 9cd0b7c11b..4e5366fe78 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -764,17 +764,35 @@ GetCurrentCommandId(bool used) if (used) { /* - * Forbid setting currentCommandIdUsed in a parallel worker, because - * we have no provision for communicating this back to the leader. We - * could relax this restriction when currentCommandIdUsed was already - * true at the start of the parallel operation. + * If in a parallel worker, only allow setting currentCommandIdUsed if + * currentCommandIdUsed was already true at the start of the parallel + * operation (by way of SetCurrentCommandIdUsed()), otherwise forbid + * setting currentCommandIdUsed because we have no provision for + * communicating this back to the leader. Once currentCommandIdUsed is + * set, the commandId used by leader and workers can't be changed, + * because CommandCounterIncrement() then prevents any attempted + * increment of the current commandId. */ - Assert(!IsParallelWorker()); + Assert(!(IsParallelWorker() && !currentCommandIdUsed)); currentCommandIdUsed = true; } return currentCommandId; } +/* + * SetCurrentCommandIdUsedForWorker + * + * For a parallel worker, record that the currentCommandId has been used. This + * must only be called at the start of a parallel operation. + */ +void +SetCurrentCommandIdUsedForWorker(void) +{ + Assert(IsParallelWorker() && !currentCommandIdUsed && currentCommandId != InvalidCommandId); + + currentCommandIdUsed = true; +} + /* * SetParallelStartTimestamps * diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c index 6bf6c5a310..62b83425fe 100644 --- a/src/backend/commands/createas.c +++ b/src/backend/commands/createas.c @@ -51,18 +51,6 @@ #include "utils/rls.h" #include "utils/snapmgr.h" -typedef struct -{ - DestReceiver pub; /* publicly-known function pointers */ - IntoClause *into; /* target relation specification */ - /* These fields are filled by intorel_startup: */ - Relation rel; /* relation to write to */ - ObjectAddress reladdr; /* address of rel, for ExecCreateTableAs */ - CommandId output_cid; /* cmin to insert in output tuples */ - int ti_options; /* table_tuple_insert performance options */ - BulkInsertState bistate; /* bulk insert state */ -} DR_intorel; - /* utility functions for CTAS definition creation */ static ObjectAddress create_ctas_internal(List *attrList, IntoClause *into); static ObjectAddress create_ctas_nodata(List *tlist, IntoClause *into); @@ -350,6 +338,14 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt, /* call ExecutorStart to prepare the plan for execution */ ExecutorStart(queryDesc, GetIntoRelEFlags(into)); + /* + * If SELECT part of the CTAS is parallelizable, then make each + * parallel worker insert the tuples that are resulted in its execution + * into the target table. We need plan state to be initialized by the + * executor to decide whether to allow parallel inserts or not. + */ + TryParallelizingInsertsInCTAS(into, queryDesc); + /* run the plan to completion */ ExecutorRun(queryDesc, ForwardScanDirection, 0L, true); @@ -418,6 +414,9 @@ CreateIntoRelDestReceiver(IntoClause *intoClause) self->pub.rDestroy = intorel_destroy; self->pub.mydest = DestIntoRel; self->into = intoClause; + self->is_parallel = false; + self->is_parallel_worker = false; + self->object_id = InvalidOid; /* other private fields will be set during intorel_startup */ return (DestReceiver *) self; @@ -438,6 +437,35 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo) ListCell *lc; int attnum; + /* + * All the necessary work such as table creation, sanity checks etc. would + * have been done by the leader. So, parallel workers just need to open the + * table, allocate bulk insert state, mark the command id as used, store it + * in the dest recevier and return. + */ + if (myState->is_parallel_worker) + { + /* In the worker */ + intoRelationDesc = table_open(myState->object_id, AccessExclusiveLock); + myState->rel = intoRelationDesc; + myState->reladdr = InvalidObjectAddress; + myState->ti_options = 0; + myState->bistate = GetBulkInsertState(); + + /* + * Right after the table is created in the leader, the command id is + * incremented (in create_ctas_internal()). The new command id is + * marked as used in intorel_startup(), then the parallel mode is + * entered. The command id and transaction id are serialized into + * parallel DSM, they are then available to all parallel workers. All + * the workers need to mark the command id as used before insertion. + */ + SetCurrentCommandIdUsedForWorker(); + myState->output_cid = GetCurrentCommandId(false); + + return; + } + Assert(into != NULL); /* else somebody forgot to set it */ /* This code supports both CREATE TABLE AS and CREATE MATERIALIZED VIEW */ @@ -540,6 +568,27 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo) else myState->bistate = NULL; + /* If parallel inserts are to be allowed, set few extra information. */ + if (myState->is_parallel) + { + myState->object_id = intoRelationAddr.objectId; + + /* + * We don't need to skip contacting FSM while inserting tuples for + * parallel mode, while extending the relations, workers instead of + * blocking on a page while another worker is inserting, can check the + * FSM for another page that can accommodate the tuples. This results + * in major benefit for parallel inserts. + */ + myState->ti_options = 0; + + /* + * rd_createSubid is marked invalid, otherwise, the table is not + * allowed to be extended by the workers. + */ + myState->rel->rd_createSubid = InvalidSubTransactionId; + } + /* * Valid smgr_targblock implies something already wrote to the relation. * This may be harmless, but this function hasn't planned for it. @@ -606,3 +655,43 @@ intorel_destroy(DestReceiver *self) { pfree(self); } + +/* + * TryParallelizingInsertsInCTAS --- determine whether or not parallel + * insertion is possible. If yes set the parallel insert state such as a flag + * in the dest receiver and store the dest receiver reference in the Gather + * node so that the required information will be sent to workers. + */ +void +TryParallelizingInsertsInCTAS(IntoClause *into, QueryDesc *queryDesc) +{ + /* + * Do not allow parallel inserts if the table is temporary. As the + * temporary tables are backend local, workers can not know about them. + * Currently, CTAS supports creation of normal(logged), temporary and + * unlogged tables. It does not support foreign or partition table + * creation. Hence the check for temporary table is enough here. + */ + if (!into->rel || into->rel->relpersistence == RELPERSISTENCE_TEMP) + return; + + if (queryDesc && IsA(queryDesc->planstate, GatherState) && + !queryDesc->planstate->ps_ProjInfo) + { + GatherState *gstate = (GatherState *) queryDesc->planstate; + DestReceiver *dest = queryDesc->dest; + + /* Okay to parallelize inserts, so mark it. */ + ((DR_intorel *) dest)->is_parallel = true; + + /* + * For parallelizing inserts in CTAS we must send information such as + * into clause (to build separate dest receiver), object id (to open + * the created table) to each workers. Since this information is + * available in the CTAS dest receiver, store a reference to it in the + * Gather state so that it will be used in ExecInitParallelPlan to pick + * the required information. + */ + gstate->dest = dest; + } +} diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 43f9b01e83..912e2d5b89 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -556,6 +556,15 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es, /* call ExecutorStart to prepare the plan for execution */ ExecutorStart(queryDesc, eflags); + /* + * If SELECT part of the CTAS is parallelizable, then make each parallel + * worker insert the tuples that are resulted in its execution into the + * target table. We need plan state to be initialized by the executor to + * decide whether to allow parallel inserts or not. + */ + if (into) + TryParallelizingInsertsInCTAS(into, queryDesc); + /* Execute the plan for statistics if asked for */ if (es->analyze) { @@ -1775,6 +1784,29 @@ ExplainNode(PlanState *planstate, List *ancestors, if (gather->single_copy || es->format != EXPLAIN_FORMAT_TEXT) ExplainPropertyBool("Single Copy", gather->single_copy, es); + + /* + * Show the create table information under Gather node in case + * parallel workers have inserted the rows. + */ + if (IsA(planstate, GatherState)) + { + GatherState *gstate = (GatherState *) planstate; + + if (IS_PARALLEL_CTAS_DEST(gstate->dest) && + ((DR_intorel *) gstate->dest)->into->rel && + ((DR_intorel *) gstate->dest)->into->rel->relname) + { + es->indent--; + ExplainIndentText(es); + appendStringInfoString(es->str, "-> "); + appendStringInfoString(es->str, "Create "); + appendStringInfo(es->str, "%s\n", + ((DR_intorel *) gstate->dest)->into->rel->relname); + ExplainIndentText(es); + es->indent++; + } + } } break; case T_GatherMerge: diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c index befde52691..270f8c13d7 100644 --- a/src/backend/executor/execParallel.c +++ b/src/backend/executor/execParallel.c @@ -23,6 +23,7 @@ #include "postgres.h" +#include "commands/createas.h" #include "executor/execParallel.h" #include "executor/executor.h" #include "executor/nodeAgg.h" @@ -65,6 +66,7 @@ #define PARALLEL_KEY_QUERY_TEXT UINT64CONST(0xE000000000000008) #define PARALLEL_KEY_JIT_INSTRUMENTATION UINT64CONST(0xE000000000000009) #define PARALLEL_KEY_WAL_USAGE UINT64CONST(0xE00000000000000A) +#define PARALLEL_KEY_INTO_CLAUSE UINT64CONST(0xE00000000000000B) #define PARALLEL_TUPLE_QUEUE_SIZE 65536 @@ -77,6 +79,9 @@ typedef struct FixedParallelExecutorState dsa_pointer param_exec; int eflags; int jit_flags; + Oid objectid; /* workers to open relation/table. */ + /* Number of tuples inserted by all the workers. */ + pg_atomic_uint64 processed; } FixedParallelExecutorState; /* @@ -578,7 +583,8 @@ ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize) ParallelExecutorInfo * ExecInitParallelPlan(PlanState *planstate, EState *estate, Bitmapset *sendParams, int nworkers, - int64 tuples_needed) + int64 tuples_needed, IntoClause *intoclause, + Oid objectid) { ParallelExecutorInfo *pei; ParallelContext *pcxt; @@ -600,6 +606,8 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, Size dsa_minsize = dsa_minimum_size(); char *query_string; int query_len; + char *intoclausestr = NULL; + int intoclause_len = 0; /* * Force any initplan outputs that we're going to pass to workers to be @@ -712,6 +720,15 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize); shm_toc_estimate_keys(&pcxt->estimator, 1); + /* Estimate space for into clause for CTAS. */ + if (IS_CTAS(intoclause) && OidIsValid(objectid)) + { + intoclausestr = nodeToString(intoclause); + intoclause_len = strlen(intoclausestr) + 1; + shm_toc_estimate_chunk(&pcxt->estimator, intoclause_len); + shm_toc_estimate_keys(&pcxt->estimator, 1); + } + /* Everyone's had a chance to ask for space, so now create the DSM. */ InitializeParallelDSM(pcxt); @@ -729,6 +746,14 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, fpes->param_exec = InvalidDsaPointer; fpes->eflags = estate->es_top_eflags; fpes->jit_flags = estate->es_jit_flags; + pg_atomic_init_u64(&fpes->processed, 0); + pei->processed = &fpes->processed; + + if (intoclausestr) + fpes->objectid = objectid; + else + fpes->objectid = InvalidOid; + shm_toc_insert(pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, fpes); /* Store query string */ @@ -758,8 +783,18 @@ ExecInitParallelPlan(PlanState *planstate, EState *estate, shm_toc_insert(pcxt->toc, PARALLEL_KEY_WAL_USAGE, walusage_space); pei->wal_usage = walusage_space; - /* Set up the tuple queues that the workers will write into. */ - pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false); + if (intoclausestr) + { + char *intoclause_space = shm_toc_allocate(pcxt->toc, intoclause_len); + + memcpy(intoclause_space, intoclausestr, intoclause_len); + shm_toc_insert(pcxt->toc, PARALLEL_KEY_INTO_CLAUSE, intoclause_space); + } + else + { + /* Set up the tuple queues that the workers will write into. */ + pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false); + } /* We don't need the TupleQueueReaders yet, though. */ pei->reader = NULL; @@ -1387,12 +1422,30 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc) void *area_space; dsa_area *area; ParallelWorkerContext pwcxt; + char *intoclausestr = NULL; + IntoClause *intoclause = NULL; /* Get fixed-size state. */ fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false); - /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */ - receiver = ExecParallelGetReceiver(seg, toc); + intoclausestr = shm_toc_lookup(toc, PARALLEL_KEY_INTO_CLAUSE, true); + if (intoclausestr) + { + /* + * If the worker is for parallel insert in CTAS, then use the proper + * dest receiver. + */ + intoclause = (IntoClause *) stringToNode(intoclausestr); + receiver = CreateIntoRelDestReceiver(intoclause); + ((DR_intorel *)receiver)->is_parallel_worker = true; + ((DR_intorel *)receiver)->object_id = fpes->objectid; + } + else + { + /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */ + receiver = ExecParallelGetReceiver(seg, toc); + } + instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true); if (instrumentation != NULL) instrument_options = instrumentation->instrument_options; @@ -1471,6 +1524,13 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc) queryDesc->estate->es_jit->instr; } + /* + * Write out the number of tuples this worker has inserted. Leader will use + * it to inform the end client. + */ + if (intoclausestr) + pg_atomic_add_fetch_u64(&fpes->processed, queryDesc->estate->es_processed); + /* Must do this after capturing instrumentation. */ ExecutorEnd(queryDesc); diff --git a/src/backend/executor/nodeGather.c b/src/backend/executor/nodeGather.c index a01b46af14..eeb51a9f43 100644 --- a/src/backend/executor/nodeGather.c +++ b/src/backend/executor/nodeGather.c @@ -32,6 +32,7 @@ #include "access/relscan.h" #include "access/xact.h" +#include "commands/createas.h" #include "executor/execdebug.h" #include "executor/execParallel.h" #include "executor/nodeGather.h" @@ -48,6 +49,7 @@ static TupleTableSlot *ExecGather(PlanState *pstate); static TupleTableSlot *gather_getnext(GatherState *gatherstate); static MinimalTuple gather_readnext(GatherState *gatherstate); static void ExecShutdownGatherWorkers(GatherState *node); +static void ExecParallelInsertInCTAS(GatherState *node); /* ---------------------------------------------------------------- @@ -131,6 +133,72 @@ ExecInitGather(Gather *node, EState *estate, int eflags) return gatherstate; } +/* ---------------------------------------------------------------- + * ExecParallelInsertInCTAS(node) + * + * Facilitates parallel inserts by parallel workers and/or + * leader for Create Table AS. + * ---------------------------------------------------------------- + */ +static void +ExecParallelInsertInCTAS(GatherState *node) +{ + /* Enable leader to insert in case no parallel workers were launched. */ + if (node->nworkers_launched == 0) + node->need_to_scan_locally = true; + + /* + * By now, for parallel workers (if launched any), would have started their + * work i.e. insertion to target table. In case the leader is chosen to + * participate for parallel inserts in CTAS, then finish its share before + * going to wait for the parallel workers to finish. + */ + if (node->need_to_scan_locally) + { + EState *estate = node->ps.state; + TupleTableSlot *outerTupleSlot; + + for(;;) + { + /* Install our DSA area while executing the plan. */ + estate->es_query_dsa = + node->pei ? node->pei->area : NULL; + + outerTupleSlot = ExecProcNode(node->ps.lefttree); + + estate->es_query_dsa = NULL; + + if(TupIsNull(outerTupleSlot)) + break; + + (void) node->dest->receiveSlot(outerTupleSlot, node->dest); + + node->ps.state->es_processed++; + } + + node->need_to_scan_locally = false; + } + + if (node->nworkers_launched > 0) + { + /* + * We wait here for the parallel workers to finish their work and + * accumulate the tuples they inserted and also their buffer/WAL usage. + * We do not destroy the parallel context here, it will be done in + * ExecShutdownGather at the end of the plan. Note that the + * ExecShutdownGatherWorkers call from ExecShutdownGather will be a + * no-op. + */ + ExecShutdownGatherWorkers(node); + + /* + * Add up the total tuples inserted by all workers, to the tuples + * inserted by the leader(if any). This will be shared to client. + */ + node->ps.state->es_processed += pg_atomic_read_u64(node->pei->processed); + } +} + /* ---------------------------------------------------------------- * ExecGather(node) * @@ -157,6 +225,7 @@ ExecGather(PlanState *pstate) { EState *estate = node->ps.state; Gather *gather = (Gather *) node->ps.plan; + bool isctas = IS_PARALLEL_CTAS_DEST(node->dest); /* * Sometimes we might have to run without parallelism; but if parallel @@ -165,6 +234,18 @@ ExecGather(PlanState *pstate) if (gather->num_workers > 0 && estate->es_use_parallel_mode) { ParallelContext *pcxt; + IntoClause *intoclause = NULL; + Oid objectid = InvalidOid; + + /* + * Take the necessary information to be passed to workers for + * parallel inserts in CTAS. + */ + if (isctas) + { + intoclause = ((DR_intorel *) node->dest)->into; + objectid = ((DR_intorel *) node->dest)->object_id; + } /* Initialize, or re-initialize, shared state needed by workers. */ if (!node->pei) @@ -172,7 +253,10 @@ ExecGather(PlanState *pstate) estate, gather->initParam, gather->num_workers, - node->tuples_needed); + node->tuples_needed, + /* CTAS info */ + intoclause, + objectid); else ExecParallelReinitialize(node->ps.lefttree, node->pei, @@ -190,13 +274,16 @@ ExecGather(PlanState *pstate) /* Set up tuple queue readers to read the results. */ if (pcxt->nworkers_launched > 0) { - ExecParallelCreateReaders(node->pei); - /* Make a working array showing the active readers */ - node->nreaders = pcxt->nworkers_launched; - node->reader = (TupleQueueReader **) - palloc(node->nreaders * sizeof(TupleQueueReader *)); - memcpy(node->reader, node->pei->reader, - node->nreaders * sizeof(TupleQueueReader *)); + if (!isctas) + { + ExecParallelCreateReaders(node->pei); + /* Make a working array showing the active readers */ + node->nreaders = pcxt->nworkers_launched; + node->reader = (TupleQueueReader **) + palloc(node->nreaders * sizeof(TupleQueueReader *)); + memcpy(node->reader, node->pei->reader, + node->nreaders * sizeof(TupleQueueReader *)); + } } else { @@ -208,9 +295,16 @@ ExecGather(PlanState *pstate) } /* Run plan locally if no workers or enabled and not single-copy. */ - node->need_to_scan_locally = (node->nreaders == 0) + node->need_to_scan_locally = (node->nreaders == 0 && !isctas) || (!gather->single_copy && parallel_leader_participation); node->initialized = true; + + /* Perform parallel inserts for CTAS. */ + if (isctas) + { + ExecParallelInsertInCTAS(node); + return NULL; + } } /* diff --git a/src/backend/executor/nodeGatherMerge.c b/src/backend/executor/nodeGatherMerge.c index 47129344f3..ee45272c17 100644 --- a/src/backend/executor/nodeGatherMerge.c +++ b/src/backend/executor/nodeGatherMerge.c @@ -216,7 +216,9 @@ ExecGatherMerge(PlanState *pstate) estate, gm->initParam, gm->num_workers, - node->tuples_needed); + node->tuples_needed, + NULL, + InvalidOid); else ExecParallelReinitialize(node->ps.lefttree, node->pei, diff --git a/src/include/access/xact.h b/src/include/access/xact.h index 7320de345c..5beae6c617 100644 --- a/src/include/access/xact.h +++ b/src/include/access/xact.h @@ -389,6 +389,7 @@ extern FullTransactionId GetCurrentFullTransactionIdIfAny(void); extern void MarkCurrentTransactionIdLoggedIfAny(void); extern bool SubTransactionIsActive(SubTransactionId subxid); extern CommandId GetCurrentCommandId(bool used); +extern void SetCurrentCommandIdUsedForWorker(void); extern void SetParallelStartTimestamps(TimestampTz xact_ts, TimestampTz stmt_ts); extern TimestampTz GetCurrentTransactionStartTimestamp(void); extern TimestampTz GetCurrentStatementStartTimestamp(void); diff --git a/src/include/commands/createas.h b/src/include/commands/createas.h index 7629230254..fc4a64989f 100644 --- a/src/include/commands/createas.h +++ b/src/include/commands/createas.h @@ -14,12 +14,35 @@ #ifndef CREATEAS_H #define CREATEAS_H +#include "access/heapam.h" #include "catalog/objectaddress.h" +#include "executor/execdesc.h" #include "nodes/params.h" +#include "nodes/plannodes.h" #include "parser/parse_node.h" #include "tcop/dest.h" #include "utils/queryenvironment.h" +typedef struct +{ + DestReceiver pub; /* publicly-known function pointers */ + IntoClause *into; /* target relation specification */ + /* These fields are filled by intorel_startup: */ + Relation rel; /* relation to write to */ + ObjectAddress reladdr; /* address of rel, for ExecCreateTableAs */ + CommandId output_cid; /* cmin to insert in output tuples */ + int ti_options; /* table_tuple_insert performance options */ + BulkInsertState bistate; /* bulk insert state */ + bool is_parallel; /* is parallelism to be considered? */ + bool is_parallel_worker; /* true for parallel worker */ + /* Used by parallel workers for opening the table created in the leader. */ + Oid object_id; +} DR_intorel; + +#define IS_CTAS(intoclause) (intoclause && IsA(intoclause, IntoClause)) +#define IS_PARALLEL_CTAS_DEST(dest) (dest && dest->mydest == DestIntoRel && \ + IS_CTAS(((DR_intorel *) dest)->into) && \ + ((DR_intorel *) dest)->is_parallel) extern ObjectAddress ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt, ParamListInfo params, QueryEnvironment *queryEnv, @@ -29,4 +52,6 @@ extern int GetIntoRelEFlags(IntoClause *intoClause); extern DestReceiver *CreateIntoRelDestReceiver(IntoClause *intoClause); +extern void TryParallelizingInsertsInCTAS(IntoClause *into, + QueryDesc *queryDesc); #endif /* CREATEAS_H */ diff --git a/src/include/executor/execParallel.h b/src/include/executor/execParallel.h index 5a39a5b29c..9f959f741b 100644 --- a/src/include/executor/execParallel.h +++ b/src/include/executor/execParallel.h @@ -35,11 +35,15 @@ typedef struct ParallelExecutorInfo /* These two arrays have pcxt->nworkers_launched entries: */ shm_mq_handle **tqueue; /* tuple queues for worker output */ struct TupleQueueReader **reader; /* tuple reader/writer support */ + /* Number of tuples inserted by all workers. */ + volatile pg_atomic_uint64 *processed; } ParallelExecutorInfo; extern ParallelExecutorInfo *ExecInitParallelPlan(PlanState *planstate, EState *estate, Bitmapset *sendParam, int nworkers, - int64 tuples_needed); + int64 tuples_needed, + IntoClause *intoclause, + Oid objectid); extern void ExecParallelCreateReaders(ParallelExecutorInfo *pei); extern void ExecParallelFinish(ParallelExecutorInfo *pei); extern void ExecParallelCleanup(ParallelExecutorInfo *pei); diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 61ba4c3666..a5b281f783 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -23,6 +23,7 @@ #include "nodes/tidbitmap.h" #include "partitioning/partdefs.h" #include "storage/condition_variable.h" +#include "tcop/dest.h" #include "utils/hsearch.h" #include "utils/queryenvironment.h" #include "utils/reltrigger.h" @@ -2326,6 +2327,8 @@ typedef struct GatherState int nreaders; /* number of still-active workers */ int nextreader; /* next one to try to read from */ struct TupleQueueReader **reader; /* array with nreaders active entries */ + /* Dest receiver is stored when parallel inserts is allowed in CTAS. */ + DestReceiver *dest; } GatherState; /* ---------------- -- 2.25.1