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 1jJSwQ-0004ri-7R for psycopg@arkaria.postgresql.org; Wed, 01 Apr 2020 02:17:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jJSwP-0000XQ-4h for psycopg@arkaria.postgresql.org; Wed, 01 Apr 2020 02:17:17 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJSwO-0000Uw-VE for psycopg@lists.postgresql.org; Wed, 01 Apr 2020 02:17:16 +0000 Received: from mail-lj1-x241.google.com ([2a00:1450:4864:20::241]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jJSwM-0007uQ-Qk for psycopg@postgresql.org; Wed, 01 Apr 2020 02:17:15 +0000 Received: by mail-lj1-x241.google.com with SMTP id n17so24132166lji.8 for ; Tue, 31 Mar 2020 19:17:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WIV7ghtgECxgToGZykuTyMLiQ6Hxsi3GLafJ1V0vY8A=; b=RA0c3+a9bEuQVYG71DfuYby/rtrjvhNnM7fMPHL+JlDShYRJhZnHV+8NKGpr4xds6M AnMHIxjH1uaks1vkhIRH33X29moYkKj/4/kSCm4HiHkk7v/DE3JFFG4FaT1f7mEuwYG9 LR133aaft4G2HdfNz93tkfjWGr20Jz8I2DPmI3v4Z18d8CtrIBjraeWHWM1unM16OuwU 0+gqxRMF+5ak4VgNl1V5FuaGyPmg25Fp5dzziVsVTzbBjfiwPlAaKD0zi7gCGd2xk0f0 P978MtUUTR58KHOWPJCRX9QwzUDCOmO8E50guLfjHzgeZGLHQpw4o8gsxFsGxb3yOGZm MnZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WIV7ghtgECxgToGZykuTyMLiQ6Hxsi3GLafJ1V0vY8A=; b=ed7jSBuWL9Gi69XSFZCywSTnlzcmpmXTRu2t0ksUIzUx5LiLVWVNE5V/zYR2LsA5KU DEtXtgugXIp+hRGTdHoMyJmKYbiYr9q0ZP8L2eSCsT1kmojmqFQHWft1YoIsjcPDO7xg Hnj0rBLbulP3j/wXcnuNcRyJSRzxrCtpQApposm3QHuyHMgKoOFNMhvjGYChV8gXQzeF UZlXviQ8eJv8g+4DkiAz2Bst6sJunRwbyzh7HdC/6Hb/K23r5aenABcc3hWKn9x8kdcR aOAmnQlNlTf+3oB1AMWeRAmZzzM1MeDqt7npJSV94gJdsO4HUN332ZWqmkcF61eHe5n3 bAdg== X-Gm-Message-State: AGi0PuZGtJaWajiA0/imM3YekwNIXl9ZEn1d0svN9IS7dIZ30Wn6Bu76 5HZwERBQIX9SHz8QjAYe1Z971+6NnyMDicqG22Q= X-Google-Smtp-Source: APiQypIgtlnonaqA1CcRbe08rwAvsv0hEiLgOA+wVYZFF/XovVvvICc3iIwnznpBGrbTztnEc5dDdICJs3FT21sUgTE= X-Received: by 2002:a2e:a310:: with SMTP id l16mr12120222lje.255.1585707431630; Tue, 31 Mar 2020 19:17:11 -0700 (PDT) MIME-Version: 1.0 References: <6db34cfa-c531-18ff-4368-bea8c171f6db@aklaver.com> <3cdc3522-f09a-529a-e329-b50418fb4172@aklaver.com> In-Reply-To: <3cdc3522-f09a-529a-e329-b50418fb4172@aklaver.com> From: Daniele Varrazzo Date: Wed, 1 Apr 2020 15:16:58 +1300 Message-ID: Subject: Re: Inserting default values into execute_values To: Adrian Klaver Cc: Stephen Lagree , psycopg@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > > On 3/31/20 3:27 PM, Stephen Lagree wrote: > >> Hello, > >> > >> I am trying to insert into a table to generate sequential ids. Is > >> there a way to do this repeatedly using execute_values if there is > >> only one column and it is auto incremented? The point of execute_values is to convert a sequence of records into a VALUES thing (that's what the placeholder is for) and shoot it to the db in one go. I think your task is much simpler than that. In order to do what you want to do you use execute_batch and use a list of empty tuples for instance; psycopg2.extras.execute_batch(cur, "insert into testins (id) values (default)", [() for i in range(10)]) but I think this is still silly: you are still sending a lot of strings from client to serve which do very little. You can easily do the same loop entirely in the database, executing a statement such as: do $$ declare i int; begin for i in select * from generate_series(1, 10) loop insert into testins (id) values (default); end loop; end $$ language plpgsql; but this is still means doing n separate inserts. Even faster would be just not rely on the DEFAULT literal, if you know the table you are inserting into or you don't mind introspecting the schema: insert into testins (id) select nextval('testins_id_seq') from generate_series(1, 10); On Wed, 1 Apr 2020 at 12:08, Adrian Klaver wrote: > > A solution from Daniele Varrazzo. I can't find the mailing list post > > where it appeared, just where I use it in code: Thank you for fishing that out! But I think since the introduction of the 'psycopg2.sql' module the correct way to do that is to use something like 'sql.SQL("DEFAULT")' to compose into a query. Cheers, -- Daniele