Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vzK3g-000tfR-35 for pgsql-hackers@arkaria.postgresql.org; Sun, 08 Mar 2026 19:45:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vzK3e-00BUNl-2L for pgsql-hackers@arkaria.postgresql.org; Sun, 08 Mar 2026 19:44:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vzK3e-00BUNc-0y for pgsql-hackers@lists.postgresql.org; Sun, 08 Mar 2026 19:44:58 +0000 Received: from mail-qk1-x744.google.com ([2607:f8b0:4864:20::744]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vzK3b-00000001faw-2FLD for pgsql-hackers@postgresql.org; Sun, 08 Mar 2026 19:44:57 +0000 Received: by mail-qk1-x744.google.com with SMTP id af79cd13be357-8cb4136d865so1472012685a.1 for ; Sun, 08 Mar 2026 12:44:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dunslane-net.20230601.gappssmtp.com; s=20230601; t=1772999093; x=1773603893; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=EeRzgR3CcNmzx2na29gjUbEnvH9DzJkYbJqlo1u6xxk=; b=ACzvIUevNcwqRiHllvOevHYevWL2ls1SW3cCEGW7hsa/M/vZmj8NTgK+CiekpUgK62 4N1eGyP7mh31iOF73PcZd6j898NpI5npVmPv+EOE/ZAlzBCkqYK+6Ez66xi1ottOrQYM uAANp/vyLcFZg4RibgXPSo6pidfKdkNsVCZdP0YXVtdlb4udAQTh8/Z5V4wIOdMajsG0 +yPQvcT0wfr1+HEbmZUB3ApD9PorWquERVfZK/rLG+ADIDz70YO6ev331JnHTYMp52nf 5c+Y9uuRjrGTkE+f0bNW1BDxAtg3v+I2f7O+3ui9FRimtk/1JJZOJiQOHQBhf2/1TWE2 UKsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772999093; x=1773603893; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=EeRzgR3CcNmzx2na29gjUbEnvH9DzJkYbJqlo1u6xxk=; b=JY0VmHhRKHiPwvwVLxRMVce10Jf9DAvGfWu99bSlu7tshjCnmhzVBI279RRrW71NgN uao+NFhZinV0rPWmOZRLoO1kfyHIdNUvsRJa9V+GThdAVUsLPpYDaxxmx0tiaymSIbF7 QDFj2aL1WKy31/qU4uDCElnAcyxqlrzJ4zjZ4TeeH5oQnbqdx2CuEBXToqRrHtZ2B0cF 5+8gc3HKQOoosi+QBqDxkGuJKv8qPlUuf8946CgpjpfpWQIM8ECOwwtu0tAFP44g6FFj pFYh3ICg1a+gh4PUxX7WzPV4hZyzoadl/nNpy70gLmCNi2wK/zT4PY23WtxrcjBtJDpu H+lQ== X-Forwarded-Encrypted: i=1; AJvYcCUaPqH9UGGnWtkBgkCS/mQvhlR9BHlp3itxkVFWmKJDmXKucdzb142fLFvXGty6qwwlwY9HJ8ZNUxHVJue1@postgresql.org X-Gm-Message-State: AOJu0YwOAm8c62WI4A993YyPPDYtQqRpd1kKrMR5tOkvsQMMZTwtUG2D /e7rju/5W3MxmAj/F/5L2YtWZwsm4cNcaRXqicRLmo36uP1hgx9PiL7EYQe4WfB/Q6s= X-Gm-Gg: ATEYQzy/tuiaM7vKHZBBAVBZcli5kS8aWKiWMEzfLjafK1ZoYFIHY2ARvH3eocs912v VDPi9QREHmxd1yuBgaF34AceenLt6xqkXFOdkksAMV7htLx0+o+w4eTV83xPAKRq82yuPBwy9xn ZsnkPfo6bxrcG1TXVqUr5Xmj8W0ha9puu2viCEThv3FJ6hpMzENYQgeBqrgOpECburGCPK4U5Nx uzS/s3LRjRmKZxNDqLjwAlIbrPaurRFP2u4RuRuVw2gjXRxgxo2WqSWJX37jkeA9pUrQ70Of7C4 agojMvE1SFjaJvguRlO+zjpu+HAD3p/vR1juG2ivqTwfuJGDTdBN+CSImqt9LycUXrP+dOcCWkN u4OU05V55hiWDz3xSaEoG79ubRcky0ypt/YT+19eYMBZSWJHgKGTN4e8MELLENML+2vKwAqZLP2 iKlrwBBQdz4D3zXfIdiJFldU7KHDPM9w== X-Received: by 2002:a05:620a:4152:b0:8cb:8272:cb2d with SMTP id af79cd13be357-8cd6d4f1415mr1090658585a.56.1772999092980; Sun, 08 Mar 2026 12:44:52 -0700 (PDT) Received: from ?IPV6:2605:a601:a6b0:500::1cb? ([2605:a601:a6b0:500::1cb]) by smtp.googlemail.com with ESMTPSA id af79cd13be357-8cd772f9f9bsm375427985a.24.2026.03.08.12.44.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 08 Mar 2026 12:44:52 -0700 (PDT) Message-ID: Date: Sun, 8 Mar 2026 15:44:51 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Emitting JSON to file using COPY TO To: jian he Cc: Joe Conway , Junwang Zhao , Florents Tselai , "Andrey M. Borodin" , Dean Rasheed , Daniel Verite , Davin Shearer , PostgreSQL-development References: <74d3b4e2-d201-489d-9b9a-1a6d0eb492a6@dunslane.net> <6cbbf7f3-da4a-4201-98a6-4347a3c35db0@joeconway.com> <8154f8e2-7d9c-442f-a7cb-a5caf38f6a12@dunslane.net> <57db4b6e-51ba-4984-9343-bc62a3ee0b7c@dunslane.net> From: Andrew Dunstan Content-Language: en-US Autocrypt: addr=andrew@dunslane.net; keydata= xsBNBE7KWFkBCAClridxur2AIc7eW2AR7izbfp3EnNefie2HbLF0izW5Ik5UjX2HBXBx4syI gY6b0ugohXrr274+baoAlvSbq6cAoQuEVrk5IZFzt20b1Xkx65FwGSEj526yiKLocqkJceSq Xr9xcA5SGY+FZv441chh5SU92v4q6z+6LPpoHOh97ptAVXZYNTtU0LevyvD5lja0TzbvJm6C eFXitJfnm1pLEr0DGJCR/iUOl/N62Kh4855zZC7NHIjQHPOvV5Stz/l5ilDhvGVk+xkXFPys SjZoUr1rXhYLpiyi5sR0X9FHXT0KnGuz1F5ERO7ZTLSSQ6fJwPj6gOk9K+vvoKvoeql5ABEB AAHNJEFuZHJldyBEdW5zdGFuIDxhbmRyZXdAZHVuc2xhbmUubmV0PsLAlwQTAQgAQQIbAwIX gAIZAQULCQgHAwUVCgkICwUWAgMBAAIeBRYhBOQ+WEYd/Hy/RGkVpZn6f8tZ/DuBBQJoGNGd BQkdEO8nAAoJEJn6f8tZ/DuBq74H/jkTR4Zi3stbw+xC7v2u3QozssK7MYPL2AsVfh7OealS h182fiWXpfvmmAB7WUHbhk9GC2RAOnHI/2d2jgKaMLAHsGYOT0YopTVIwRY43fCw/mK67yxc wmDcX+zyKfLaivNbf5A7QPLNwda98bEAMSJ8Sn652Uc6cA8t3uKGsVzbRBQOoYzjgvBCfSrE 9ql3PDNg0l4BfAqabd2f70ZUm9VAMEPrgv/v2xI7M2XiL4g5BVmqLCOwxLM8RMCotCuoweUr VO43DeBCIDwLxotMJKvGWDjBzQYlU1NPUAtNcz/gN9ITUe1VUGjyvGj4u1lxBOcQQUw7l1+T 5moZ4iZxXzvOwE0ETspYWQEIANGc4zQULOxhbqO2dyD51YhqCNRmm9oKWaqf+wmW4tpDe/VV cxAnNizd4LWCHfzpb5cHAtGkOPePMfzWVf6nvdF7d3eglbtf59+zG7O7llV0xSSoFiieQBsr GvqDInXYX/4mRRXMtyhM353/tixC9RWLs1oofyYmCPPXXY7h9R7en3B8BoVrRFcdzlIY/NFN hFGW/9dkEiGjgna2Rk6e15kln4ZvFBWUg23p93w/pqXcxY6+k/8TEk+C4R+M6w7o2PLGOjdZ +kPiUcw5H85zf/yZJwQXzisXaNduwWB6Vads9YC9dj6kPR1c4VGRqAaYL++LAEOqrlvm2Tvq QqZRtnEAEQEAAcLAfAQYAQgAJgIbDBYhBOQ+WEYd/Hy/RGkVpZn6f8tZ/DuBBQJoGNI2BQkd EODdAAoJEJn6f8tZ/DuBfw0IAKTsfD40teP/pp+bsLLMSxPXUYrrprTj7WFB5v61p6dkpSr/ qXmMlyahdxQFaPmfVgVirB1Vk/kHiWNnnGjfUV9nB2Zg9LI0Xb9/ts3LsUiRWXzG3tkMY6XL vsVOxW4XFRND9l2q+WW93aZ1DZl+fqWfYgMvsusFRhmGFOKTRfKPta2Pkv+AhA24N4+PrR5p bU4k2MO8PAGiK8eaYKGFG1bHKuAvoDoF7WXJ3FHxuWqLnKEt4dfOLm5pAe3zq1Lt6q8azT9i QWGpSAK5vQUWQHBHpiDjdPeqKZ6HiAXIIKfSmb+jrvXBqoP+D6/K7rUjG2aXiRtTIAXms9sm VRu7cmw= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2026-03-08 Su 12:16 PM, jian he wrote: > hi. > > V27-0002 is still not bullet-proof. > > drop table if exists t1; > create table t1(a int); > insert into t1 values (1); > copy (select * from t1) to stdout json; > {"a":1} > WARNING: resource was not closed: TupleDesc 0x7171d0ca3440 (18239,-1) > > Also see ExecAssignScanProjectionInfo->ExecConditionalAssignProjectionInfo > So in v28-0002, I changed to > + /* > + * composite_to_json() requires a stable TupleDesc. Since the slot's > + * descriptor (slot->tts_tupleDescriptor) can change during the execution > + * of a SELECT query, we use cstate->queryDesc->tupDesc instead. This > + * precaution is only necessary when the output slot's TupleDesc is of > + * type RECORDOID. > + */ > + if (!cstate->rel && slot->tts_tupleDescriptor->tdtypeid == RECORDOID) > + slot->tts_tupleDescriptor = cstate->queryDesc->tupDesc; Hmm. But should we be scribbling on slot->tts_tupleDescriptor like that? How about something like this?: -        * Full table or query without column list.  Ensure the slot uses -        * cstate->tupDesc so that the datum is stamped with the right type; -        * for queries output type is RECORDOID this must be the blessed -        * descriptor so that composite_to_json can look it up via -        * lookup_rowtype_tupdesc. +        * Full table or query without column list.  For queries, the slot's +        * TupleDesc may carry RECORDOID, which is not registered in the type +        * cache and would cause composite_to_json's lookup_rowtype_tupdesc +        * call to fail.  Build a HeapTuple stamped with the blessed +        * descriptor so the type can be looked up correctly.          */         if (!cstate->rel && slot->tts_tupleDescriptor->tdtypeid == RECORDOID) -           slot->tts_tupleDescriptor = cstate->queryDesc->tupDesc; +       { +           HeapTuple   tup; -       rowdata = ExecFetchSlotHeapTupleDatum(slot); +           tup = heap_form_tuple(cstate->tupDesc, +                                 slot->tts_values, +                                 slot->tts_isnull); +           rowdata = HeapTupleGetDatum(tup); +       } +       else +       { +           rowdata = ExecFetchSlotHeapTupleDatum(slot); +       } cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com