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 1kkTS6-0006TI-8J for psycopg@arkaria.postgresql.org; Wed, 02 Dec 2020 14:49:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kkTS4-0006Zy-Vb for psycopg@arkaria.postgresql.org; Wed, 02 Dec 2020 14:49:52 +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 1kkTS4-0006Zr-Ik for psycopg@lists.postgresql.org; Wed, 02 Dec 2020 14:49:52 +0000 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kkTRx-0001x7-T5 for psycopg@postgresql.org; Wed, 02 Dec 2020 14:49:51 +0000 Received: by mail-ot1-x336.google.com with SMTP id z23so1804166oti.13 for ; Wed, 02 Dec 2020 06:49:45 -0800 (PST) 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=Orq8Vc04R7yIYX7v1lFrPPR6MAXKtbLVPO0LBV/wKqc=; b=G4HfCtcgnHSM+LVMB4XeMQRl9jWBjQt5WFWlCnRIMIw0iyM3leQXL4BNUaI3ubrPMG 4cTk3GD7b8wU80qAUbWTcIZFRosM0Qjrz8WpWDSloJDok7e78DtUss7Q8JSMJBm6MBFH OWyczAwxVCuMFHb5fGR0IjMNAjTuxK0ecDYQwatuKnWEwJEtkAY0G8dBUcFNO3gxZrwP 6HKWvIMQPaja8uuGFHwGqwxiAn6s1W+G7vVb6KfymX7EYG2zOitmePjUOYDG5b52HgUU 6oWX03/A9SO0dUZubYDvf+SzcYsWsnCxFbvQxZr6mViJdZkeqH3XbGI9GT3EUdQ4QPJD fH6A== 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=Orq8Vc04R7yIYX7v1lFrPPR6MAXKtbLVPO0LBV/wKqc=; b=bU14AXNHD783BRxiFs4Qw4jI/oQo2d9cRwXEbFVPpm0uV/6Uw7AxR9bfXjdMGzOQsh 9RnypqOtLDw/M57FR/MoOnjHj0t/CB0T8b5+oyiYqnSGkcK5gEMTHJs+t+IH09uX2FkT OueKjxztYvDlszdiitFVld2Kjs5KINzzTE5kx0NsEHzHM9OR2Enm+OsFYkrNFzJH71sK 9fUQhxEpUVWQGr2nLyef/4m/9KQzrQPUTurjcyhHZzJ1KEXTe9bGQ0OV1Qjvk60PwqXf 0tCvGcG9Lct9h5OM+1UxIzMlAowH1Q4ta+pV8SbSpzZsczzRgQXV/3Mt83jnSMrx5MxH 0i7g== X-Gm-Message-State: AOAM533tgryH5drRUlRh3eZnXvU02dnBMB+7W5/bCfzv+5hp4lmSjAwS 5O/8B8/L4H/sYzTcOrey9B3yNPZops0bxDznwJM= X-Google-Smtp-Source: ABdhPJzSE2K6gLowgONsOfPKqUoRPEBPGYonmZ6p+jqluRQdkIWQ5aXuWrTvSEeWFXkmDwKwMTY1YLT4qPllsBePZKY= X-Received: by 2002:a9d:6019:: with SMTP id h25mr2125552otj.12.1606920584737; Wed, 02 Dec 2020 06:49:44 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Vladimir Ryabtsev Date: Wed, 2 Dec 2020 06:49:33 -0800 Message-ID: Subject: Re: Executing on the connection? To: Daniele Varrazzo Cc: psycopg@postgresql.org Content-Type: multipart/alternative; boundary="000000000000a2b7d505b57c5a2c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000a2b7d505b57c5a2c Content-Type: text/plain; charset="UTF-8" I support this idea. Other drivers have such shortcuts (or even lack cursor() equivalent at all). I would even prefer a shorter shortcut, like conn.fetchone(query, params) But it breaks parameter consistency at some degree and bloats connection interface, so it's up to you. Vladimir On Wed, 2 Dec 2020 at 03:20, Daniele Varrazzo wrote: > Hello, > > if there is a thing that people approaching psycopg find confusing is > the connection/cursor duality. The connection object as a wrapper for > the database connection is clear to understand for anyone who has used > psql. But the cursor as a means to give command is spurious: you can > give commands but all the cursors are serialised, not parallelised, on > the same connection, and it isn't useful for transactions either... > > The only unique thing a cursor has is to hold a result and consume it; > for the rest it doesn't own neither the postgres session nor the > transaction. But it gives them command. Weird. > > You are surely familiar with the psycopg2 usage pattern: > > conn = psycopg2.connect(dsn) > cur = conn.cursor() > cur.execute(query, params): > for record in cur: > ... # do something > > The cursor() can take parameters, e.g. to create server-side "named" > cursors, but most often people will use the standard client-side > cursor, which is a lightweight object, little more than a wrapper for > a PGresult. > > One little change I've made to psycopg3 cursors is to make it return > "self" on execute() (it currently returns None, so it's totally > unused). This allows chaining a fetch operation right after execute, > so the pattern above can be reduced to: > > conn = psycopg3.connect(dsn) > cur = conn.cursor() > record = cur.execute(query, params).fetchone() > # or > for record in cur.execute(query, params): > ... # do something > > And of course nastymess such as: > > conn.cursor().execute(query).fetchone() > psycopg3.connect(dsn).cursor().execute(query).fetchone() > > But, taste. > > I'm toying with the idea of adding a 'connection.execute(query, > [params])' methd, which would basically just create a cursor > internally, query on it, and return it. No parameter could be passed > to the cursor() call, so it could only create the most standard, > client-side cursor (or whatever the default for the connection is, if > there is some form of cursor_factory, which hasn't been implemented in > psycopg3 yet). For anything more fancy, cursor() should be called > explicitly. > > As a result people could use: > > conn = psycopg3.connect(dsn) > record = conn.execute(query, params).fetchone() > # or > for record in conn.execute(query, params): > ... # do something > > No other methods bloating the connection interface: no executemany(), > copy(), callproc (actually there will be no callproc at all in > psycopg3: postgres has no fast path for function call and too much > semantics around stored procedure that a single callproc() couldn't > cover). > > Being the cursor client-side, its close() doesn't actually do anythin > apart from making it unusable, so just disposing of it without calling > close() is totally safe. > > Thoughts? > > Cheers! > > -- Daniele > > > --000000000000a2b7d505b57c5a2c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I support this idea. Other drivers have such shortcuts (or= even lack cursor() equivalent at all).

I would even pre= fer a shorter shortcut, like

conn.fetchone(query, = params)

But it breaks parameter consistency at som= e degree and bloats connection interface, so it's up to you.
=
Vladimir

On Wed, 2 Dec 2020 at 03:20, Daniele Varrazzo= <daniele.varrazzo@gmail.c= om> wrote: