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 1jJjaX-0002Fl-0k for psycopg@arkaria.postgresql.org; Wed, 01 Apr 2020 20:03:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jJjaV-0007iW-QW for psycopg@arkaria.postgresql.org; Wed, 01 Apr 2020 20:03:47 +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 1jJjaT-0007h2-Rq for psycopg@lists.postgresql.org; Wed, 01 Apr 2020 20:03:47 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jJjaP-00088a-Od for psycopg@postgresql.org; Wed, 01 Apr 2020 20:03:44 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 559FE5C0106; Wed, 1 Apr 2020 16:03:40 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Wed, 01 Apr 2020 16:03:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=w mTjq0mcZdP6jX0yVDi+7ZMUKBsEoBzJfUuqqRI87bY=; b=vAYHnuzZxEh1dpiqE K5C8tMsCI81hjnpOsooLGryvrYv5urbdlK/DRa0Wwkx491aM+20JHrYwBdj/XVmi K/8AU4qX1rQ8pMjm7rxQcKF70JRs61iXvovKvGY8aGAMpYPz7MHG1qt1I1Y9Gj0+ RRV/tqvh1izE3H9qZhHffqPtaj/mjjZy5+9gUicjkCzpt0stKqA1JcCl8mIh0n52 pQJw1D1CF13Qx4Fdc/QM0G/w0jGr/oGxPBp1EJLNiPchQhs1UqLEWx9CqJ+ZW8J8 KbKxKXNfIL7+g0YKLvGCbLADxqryK2gKgldRJtya3WEr1GFtIJJEkvYw9jZDHLpC vOgkg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=wmTjq0mcZdP6jX0yVDi+7ZMUKBsEoBzJfUuqqRI87 bY=; b=1+iq4vLlmzFarPbKnbv3e8xDwOczU95rkAO5mblVhNY26GUnL+aqTD/Rt My36eamaxkflRjIJrOPJhohxSwLtKM6JMe/kJCbq8kuN93lCOJnMnO/HE/IiOBcQ aFJLr33s/YVndNC2tn3aPj1VyA4NbGUpwWIoLhHiL36zXU8lmFoLWG0vyzTBbrJN XjBRLqDNWZlX2OyDupKovbQuNMucNzpePfvG+oyZrc1bmdHWnfXz0sPMFTVuDPLZ o9FYo5qSePBCFnZjFcEbQ+ZgmGcnTVRKje8u+D87qO+XqUOJBBVX8Tx47oN+lRa+ 0gCoKf4uEhKPYgPHBiKiad4rNKiwA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrtddvgddugeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepjeehrddujedvrdeltddrledvnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvh gvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (75-172-90-92.tukw.qwest.net [75.172.90.92]) by mail.messagingengine.com (Postfix) with ESMTPA id 57E10306CC2E; Wed, 1 Apr 2020 16:03:39 -0400 (EDT) Subject: Re: Inserting default values into execute_values To: Daniele Varrazzo Cc: Stephen Lagree , psycopg@postgresql.org References: <6db34cfa-c531-18ff-4368-bea8c171f6db@aklaver.com> <3cdc3522-f09a-529a-e329-b50418fb4172@aklaver.com> From: Adrian Klaver Message-ID: Date: Wed, 1 Apr 2020 13:03:38 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 3/31/20 7:16 PM, Daniele Varrazzo wrote: >>> 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. Thanks, still wrapping my head around psycopg2.sql. A simple example: test=# \d t2 Table "public.t2" Column | Type | Collation | Nullable | Default --------+-------------------+-----------+----------+-------------------------------- id | integer | | not null | nextval('t2_id_seq'::regclass) name | character varying | | | Indexes: "t2_pkey" PRIMARY KEY, btree (id) import psycopg2 from psycopg2 import sql con = psycopg2.connect("dbname=test host=localhost user=aklaver") q1 = sql.SQL("insert into t2 values ({})").format(sql.SQL(",").join([sql.SQL("DEFAULT"), sql.Literal('test2')])) print(q1.as_string(con)) insert into t2 values (DEFAULT,E'test2') cur.execute(q1) test=# select * from t2; id | name ----+------- 1 | test 2 | test2 > > Cheers, > > -- Daniele > -- Adrian Klaver adrian.klaver@aklaver.com