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 1mczn8-0008TI-JM for pgsql-general@arkaria.postgresql.org; Wed, 20 Oct 2021 00:49:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mczn6-0007Su-IS for pgsql-general@arkaria.postgresql.org; Wed, 20 Oct 2021 00:49:12 +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 1mcynV-0004r6-Pi for pgsql-general@lists.postgresql.org; Tue, 19 Oct 2021 23:45:33 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mcynR-0000YP-BA for pgsql-general@lists.postgresql.org; Tue, 19 Oct 2021 23:45:33 +0000 Received: by mail-pf1-x42e.google.com with SMTP id k26so1359220pfi.5 for ; Tue, 19 Oct 2021 16:45:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=Jk3Z8QHOsvXZ9gzsG5jVoqeHEg/IEKrVS09b5EYFE7c=; b=LMHSXxCu4IC89bkincgQcT6SFMSp7xGegcrYPiXEOTkKos0boPBKlePwxQCpzIJfsB 16ezIBw1Ytg5mok/eUD9OdeUZ5zwcRnMop1n3ww1xzXLhq1VMk8QuixQEBlbKE3N97FC 2PK+8q58nILNmXqtP50mhkufB1lAHjjhC2ivqOWO0xohZ9P4HEYkTWSnDITr2ncZLBw1 1LFoEpkaBuDngSPj/scxWXlNGUWxpaYsymue7Jz6u+yIeTJSc3/YS4cw86eCIA5gTlwQ w0+WfW79uvLGKe/UIQNuFOi6p6q0xP8ty8VVwRY5B+pCUkZ71EMn+sEkV5X8giRajUzM J/1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Jk3Z8QHOsvXZ9gzsG5jVoqeHEg/IEKrVS09b5EYFE7c=; b=KkaLMISvgaiNvmlsoBQZVo6aiwKrQHxFwsLzkV5Nrtzh/lcqyLYg5yNwBQEKKOkwWh j3P7opg27UwV443UF1+c5tw/14a9RqvTob/1kW/y0C4vw738loTewoQOleIgeuURHpHb SKRgk0VV6aoJvVhYwtMgcyWR/V7uZbiv5IimxX2E1SwPKzl4jDBm9IgcuMAg29/8MAx+ TwBPVMfMSKuQdqjkvH7N3cFVG8YZPCz/kYEzQStKXl4SH1P+kzOHSLlMNuDRXjEpB88B GWBrr5ndFo0Swy1u2ELJQW80lDqL//aI2ELVWc+h6vQmt4T4Wl/k7mGNXjkTMV+cpoMW Oe1w== X-Gm-Message-State: AOAM531w4CunnqVU57bkq7PbSNADSMJshKqjkPC5sG4r1j/0CzOdzxsz WssVrjqFhNfTFRQQzTknoxfQorKCSp4= X-Google-Smtp-Source: ABdhPJyZUXSjbynv9DjRmsLOM6u3D2BwRjslBy6vC30FdPMIUrKIOdNpVbGCtQAzb1stF3EG4Y+s9Q== X-Received: by 2002:a05:6a00:230e:b0:44c:4f2d:9b00 with SMTP id h14-20020a056a00230e00b0044c4f2d9b00mr2928001pfh.24.1634687126266; Tue, 19 Oct 2021 16:45:26 -0700 (PDT) Received: from tomato.moose.housegordon.com (moose.housegordon.com. [184.68.105.38]) by smtp.googlemail.com with ESMTPSA id c8sm294359pgn.72.2021.10.19.16.45.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 19 Oct 2021 16:45:25 -0700 (PDT) Subject: Re: connecting multiple INSERT CTEs to same record? To: "David G. Johnston" Cc: "pgsql-general@lists.postgresql.org" References: <68708e28-dd17-13d6-2acd-9ca4f9735379@gmail.com> From: Assaf Gordon Message-ID: <415b07c1-230f-bcb6-0b65-0514a1908777@gmail.com> Date: Tue, 19 Oct 2021 17:45:24 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2021-10-19 1:13 p.m., David G. Johnston wrote: > > Instead of assigning a unique identifier to student after inserting it > into the table, assign the identifier first.  Generally this is done by > using “nextval()” Aha! Such elegant solution! Seems obvious in hindsight, but I just couldn't figure it out nor find any mentions to it online. For others who might stumble upon this thread in the future, the solution becomes: ==== with -- pre-assign unique IDs for each record, -- without inserting them to the table yet. new_data_with_ids as ( select nextval(pg_get_serial_sequence('students','id')) as new_student_id, new_data.* from new_data ) , -- Now insert the new names, with their pre-assigned IDs new_students as ( insert into students(id,name) select new_student_id, name from new_data_with_ids returning * -- optional ) , -- And use the IDs for other tables, too new_classes as ( insert into classes(student_id, subject) select new_student_id, subject from new_data_with_ids returning * --optional ) -- return the new IDs with the data select * from new_data_with_ids ; === Thank you! Regards, - Assaf Gordon