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 1kcdwB-0002df-LM for psycopg@arkaria.postgresql.org; Wed, 11 Nov 2020 00:24:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcdwA-0000tt-EF for psycopg@arkaria.postgresql.org; Wed, 11 Nov 2020 00:24:34 +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 1kcdwA-0000tm-6Z for psycopg@lists.postgresql.org; Wed, 11 Nov 2020 00:24:34 +0000 Received: from mail-lf1-x130.google.com ([2a00:1450:4864:20::130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kcdw0-0003Dm-Tm for psycopg@postgresql.org; Wed, 11 Nov 2020 00:24:33 +0000 Received: by mail-lf1-x130.google.com with SMTP id w142so733477lff.8 for ; Tue, 10 Nov 2020 16:24:24 -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=wbr8BDRkCUs2sxpkDucXUH6doXHD0MdJZtON23mm8VQ=; b=cFXcOLLDzgRgSDFesRaDx2kvmJVYb7Gnqs/FlVdOjLM6aKWpQH1Imr/vL1TOrm9WNw hWTVl5rd2CCreqKPL4fFdwzUH6D9zZdnPkMjqULBOdBtHH+JPsVm6gelGIKcj6U1Tn/9 misasmJQIQ9xOAftakFwNpcIch4f5RD1iMysm2CKF3GWuigtZ6tlU7rtAI7xHneAzT2w e9hAM21FEVzZXopV2qyQONWTX3CAVqnf6Yrku0OqDMDEFAApiCOdDTLs/ASBFMGTr6Qg ESlqttakidgWBn3pFuuxZBnfkZV6oFl2XODuRAGKehCb1N9OwEAUJwDWB2mFaFt8DF62 NE+g== 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=wbr8BDRkCUs2sxpkDucXUH6doXHD0MdJZtON23mm8VQ=; b=q6mkxL3Pbvdd8eVJFxZj5wWkjDYWrEH9epLSCcCkC3dnTrPCxCOtSoAoCj72R8M1us 7YB/0zUiyBiUMfAFaf8cNm8iczCfjh28DG1S2D3qlyPrUZrZQr8fVc1LkH3kwrSxUxXR N99PJ7vXPcbalRUxI5wWTNQjz05jsCA0aQltmorXywoeRugb3/I9EC1/Ud+ryswN/cmr yh3NsR+c3TxY+7d1AAL5LrRMfGj+LdU/6kXhbwC7WdYzK4E2NkHYcLbD2HQZtWIYPeAK j1ZrFBTC+EkJzRhwH7Ucc30XunLNFF4XF6046t6FQcWzPs2hfS41IY429yEDd5c4JgJ5 eG/A== X-Gm-Message-State: AOAM531icHfIybcrCVHcsR0oixhT5wR1wJzu01sRf19n5/6AqDdN/vNY 0CMdiIAeEc2bmwPfxULwotCcuQCGM/2RER+mQcI= X-Google-Smtp-Source: ABdhPJzG0zdA66lPyPiW7jmmFQOz2l5gtpChfeSJKYypgOxUmDPS9Ti6gy+tnLgax0Vbrj3nWtIu64O8CHoSCwQyNkM= X-Received: by 2002:a05:6512:3af:: with SMTP id v15mr8012530lfp.144.1605054262944; Tue, 10 Nov 2020 16:24:22 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Daniele Varrazzo Date: Wed, 11 Nov 2020 00:24:11 +0000 Message-ID: Subject: Re: Using composite types in psycopg3 To: Vladimir Ryabtsev Cc: 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 Tue, 10 Nov 2020 at 21:59, Vladimir Ryabtsev wrote: > psycopg2 returns the 'result' as a basic string, while > in asyncpg and py-postgresql I have structured data > (roughly 'List[Tuple[int, List[str]]]'). > > I tried the same in psycopg3 and it is little bit better, but > not entirely: it shows the outer list, the tuples inside it, > but the innermost list is still represented as a basic string: > '{one,"one more"}'. > > Is it something you are still working on? Any workarounds? Yes: by obtaining data from the db in binary mode you can get information about deeply nested objects. psycopg2 works only in text mode, psycopg3 in both. In [1]: query = """ ...: with test as ( ...: select 1 as id, 'one' val ...: union all ...: select 1, 'one more' ...: union all ...: select 2, 'two' ...: ) ...: select array( ...: select (id, array_agg(val)) ...: from test ...: group by id ...: )""" In [2]: import psycopg3 In [3]: from psycopg3.pq import Format In [4]: cnn = psycopg3.connect("") In [5]: cnn.cursor().execute(query).fetchone()[0] Out[5]: [('1', '{one,"one more"}'), ('2', '{two}')] In [6]: cnn.cursor(format=Format.BINARY).execute(query).fetchone()[0] Out[6]: [(1, ['one', 'one more']), (2, ['two'])] Binary loading/dumping is not supported yet for all the data types, but the plan is to cover all the builtins. Still not sure about the interface to request text/binary results, or whether binary shouldn't be the default as opposed to text. There is still ground to cover, but we are getting there. -- Daniele