Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jWxOM-0004N2-TI for pgsql-hackers@arkaria.postgresql.org; Fri, 08 May 2020 07:25:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jWxOL-0001kH-Jc for pgsql-hackers@arkaria.postgresql.org; Fri, 08 May 2020 07:25:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jWxOL-0001jx-7z for pgsql-hackers@lists.postgresql.org; Fri, 08 May 2020 07:25:53 +0000 Received: from mail-qt1-x841.google.com ([2607:f8b0:4864:20::841]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jWxOI-0003zj-VF for pgsql-hackers@lists.postgresql.org; Fri, 08 May 2020 07:25:52 +0000 Received: by mail-qt1-x841.google.com with SMTP id x8so437317qtr.2 for ; Fri, 08 May 2020 00:25:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=41f3re64UUMJeFWoyWUBQVXIEF9TbZS3AouwMekonew=; b=ywskvrK54JTRSz3ziWinEGOpfdaTl7fvxfohsGfBBU7N62eHbXOpC6Aa5ZxapHlteV Ielhs42HPYY3qPcIO5Jg4iXfXS6ysYR5rpzRXzUUL1UQfcBSX+9CClu/dumRlElZTOSk nP5hUI4sO+/CJrBlp+K62KdZLmIoTaACKIlFjlUpbbrHL9UC8NcaC//HJcSOVzM2nzFe YgsHmnjfgdZCclNd9be9lXPiBSH25CIMKNl9FhtafIjSttFe5wcUEgk8v1HE3I5TOzfn 9SZs5YpXUz3NhBiRmgRDpaLcXB9Oe18A0GJJkX3azpU8VPrDYD6JTzbTMDpWDwAlOG4R EAEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=41f3re64UUMJeFWoyWUBQVXIEF9TbZS3AouwMekonew=; b=USoLXi+DNhTevgC4AMlp17pkxsKz/PHvZJ+mkG6oq8AeMLwHHdje9ThBvW+IOCe548 V1dUk9UZoIcCf6YQ/NapYZgSKhiHEqh1tuT/u9Wt3on9ct2Vwm93CxIvzJWPHUKfxUL3 bsnjsh/CkdLZhhBtTkn84G9chFxU8V7iQxn91mKGeh8hBYW0ZtPoEBauoM33tKxBJ2Et tU9Hq11g6/2duNJ90wJ2NcBn0I+F1eYrNpwhU4sgFVbKlQFtYvKvIrlxssR8WGAWOYDq an1inobLT3M/I5dQv1yF+8TrWsypBu4W1tBa+Gq+CIoR0bmeqvAhSOp2V5QAKPxDlTvW abGw== X-Gm-Message-State: AGi0PuZYlu9dPeLYCjVrOcy0PZyw4v+sulaiCdQf6a9qCPo1HGO6KmlY 9RubTR9XrehCIOxoPjRmtoJll+yxfXc= X-Google-Smtp-Source: APiQypLu2QmigyxZtEqxBk+ZORPOHUk+ptjKCcifCbX1NxeX++ZaSDXDDHLC2f7/5KfPL0WP4erQ4w== X-Received: by 2002:ac8:2297:: with SMTP id f23mr1574154qta.60.1588922748277; Fri, 08 May 2020 00:25:48 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id n8sm870823qtn.12.2020.05.08.00.25.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 May 2020 00:25:47 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id C5074800827; Fri, 8 May 2020 02:25:45 -0500 (CDT) Date: Fri, 8 May 2020 02:25:45 -0500 From: Justin Pryzby To: pgsql-hackers@lists.postgresql.org Subject: should INSERT SELECT use a BulkInsertState? Message-ID: <20200508072545.GA9701@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Seems to me it should, at least conditionally. At least if there's a function scan or a relation or .. I mentioned a bit about our use-case here: https://www.postgresql.org/message-id/20200219173742.GA30939%40telsasoft.com => I'd prefer our loaders to write their own data rather than dirtying large fractions of buffer cache and leaving it around for other backends to clean up. commit 7f9e061363e58f30eee0cccc8a0e46f637bf137b Author: Justin Pryzby Date: Fri May 8 02:17:32 2020 -0500 Make INSERT SELECT use a BulkInsertState diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 20a4c474cc..6da4325225 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -578,7 +578,7 @@ ExecInsert(ModifyTableState *mtstate, table_tuple_insert_speculative(resultRelationDesc, slot, estate->es_output_cid, 0, - NULL, + mtstate->bistate, specToken); /* insert index entries for tuple */ @@ -617,7 +617,7 @@ ExecInsert(ModifyTableState *mtstate, /* insert the tuple normally */ table_tuple_insert(resultRelationDesc, slot, estate->es_output_cid, - 0, NULL); + 0, mtstate->bistate); /* insert index entries for tuple */ if (resultRelInfo->ri_NumIndices > 0) @@ -2332,6 +2332,14 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) mtstate->mt_arowmarks = (List **) palloc0(sizeof(List *) * nplans); mtstate->mt_nplans = nplans; + mtstate->bistate = NULL; + if (operation == CMD_INSERT) + { + Plan *p = linitial(node->plans); + Assert(nplans == 1); + if (!IsA(p, Result) && !IsA(p, ValuesScan)) + mtstate->bistate = GetBulkInsertState(); + } /* set up epqstate with dummy subplan data for the moment */ EvalPlanQualInit(&mtstate->mt_epqstate, estate, NULL, NIL, node->epqParam); @@ -2809,6 +2817,9 @@ ExecEndModifyTable(ModifyTableState *node) */ for (i = 0; i < node->mt_nplans; i++) ExecEndNode(node->mt_plans[i]); + + if (node->bistate) + FreeBulkInsertState(node->bistate); } void diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 4fee043bb2..daf365f181 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -14,6 +14,7 @@ #ifndef EXECNODES_H #define EXECNODES_H +#include "access/heapam.h" #include "access/tupconvert.h" #include "executor/instrument.h" #include "fmgr.h" @@ -1177,6 +1178,7 @@ typedef struct ModifyTableState List **mt_arowmarks; /* per-subplan ExecAuxRowMark lists */ EPQState mt_epqstate; /* for evaluating EvalPlanQual rechecks */ bool fireBSTriggers; /* do we need to fire stmt triggers? */ + BulkInsertState bistate; /* State for bulk insert like INSERT SELECT */ /* * Slot for storing tuples in the root partitioned table's rowtype during