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 1pajN7-0004iz-Nm for pgsql-general@arkaria.postgresql.org; Fri, 10 Mar 2023 20:29: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 1pajN6-0007ck-13 for pgsql-general@arkaria.postgresql.org; Fri, 10 Mar 2023 20:29:48 +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 1pajN5-0007cb-Jf for pgsql-general@lists.postgresql.org; Fri, 10 Mar 2023 20:29:47 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pajN1-0003Vr-JY for pgsql-general@lists.postgresql.org; Fri, 10 Mar 2023 20:29:47 +0000 Received: by mail-pj1-x1036.google.com with SMTP id cp7-20020a17090afb8700b0023756229427so11093663pjb.1 for ; Fri, 10 Mar 2023 12:29:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; t=1678480181; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Xg5du9fe2uE/ZdOBe07/TQA1nFBohowjw32oV3riMcA=; b=PFCVe1JY6pogQOVph/uxEfXhSo5ElAHb8i7yXRZMpId20jV7RCBQhlArvUEcWVBk2s AU/3fgYpFwB1FcqE28hve1gXENNQjtAlMiJnYMVYYXXPTVzZKmg01NCgjFZ4YRNjHGLP SQ4YF5BaITu1UgnkyVe1xAmnncSCJsmE7b6wM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678480181; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Xg5du9fe2uE/ZdOBe07/TQA1nFBohowjw32oV3riMcA=; b=q1coNKH3f3fkRcK5qcYoJfuAu2AY4KFkIF4Bf0LLSrYU340FiFxtDiIjF6g0u3KveK Hg09khGKWdK9oahtzkT4LjR9Ra1yg87x9FKOPQjaYHAXfENCAbn4JeGNGXg028KWyIJ5 Oz4hAcdYIDa02igeCAPvTyPfJfJdBpPuDVjLWm3gR4mlG0CbqHjkC8GrYqWcxCy8PSjO zONZUhh3BtIcWD5dEgQzfRD0QiMxJigvq42rEYGFpYi1l4It2+j05QkiCXwJYCUd9oXF yjmyHvPyom1dUxW5op7qzE8eEiB/i5y3qw98ml3CqTko+ALh6QwSipEclV4SXTnIZIDP 5LCA== X-Gm-Message-State: AO0yUKW/+cL+8HoRebmhJeDLN0lw1JpDRcLmTb5Fovr9p8naPUxUOk0a 19+P72wcShTBziM8/RXoDCqArQ== X-Google-Smtp-Source: AK7set/4RgGULo4RlAitBA0q8W4pN44tTd9/aSKrLY/vL0v9EQgyuv1k9OsJ2Cccg5h6WEVHe3L5RA== X-Received: by 2002:a17:903:41c3:b0:19c:e449:bbaa with SMTP id u3-20020a17090341c300b0019ce449bbaamr32078178ple.28.1678480181189; Fri, 10 Mar 2023 12:29:41 -0800 (PST) Received: from smtpclient.apple ([2601:646:101:7fda:2cca:6c9:df1d:db6a]) by smtp.gmail.com with ESMTPSA id 2-20020a170902c14200b0019ebe0976aasm368682plj.193.2023.03.10.12.29.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Mar 2023 12:29:40 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.14\)) Subject: Re: select (17, 42)::s.t2 into... fails with "invalid input syntax" From: Bryn Llewellyn In-Reply-To: Date: Fri, 10 Mar 2023 12:29:39 -0800 Cc: Tom Lane PostgreSQL , pgsql-general list Content-Transfer-Encoding: quoted-printable Message-Id: <9E792FAF-873E-40AA-A3CF-A4AB55EF8230@yugabyte.com> References: <281321.1678328800@sss.pgh.pa.us> <483AF2DD-BBC9-4C73-9418-8231948F4C27@yugabyte.com> To: "David G. Johnston" X-Mailer: Apple Mail (2.3654.120.0.1.14) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > david.g.johnston@gmail.com wrote: >=20 >> bryn@yugabyte.com wrote: >>=20 >> =C2=AB >> SELECT select_expressions INTO [STRICT] target FROM =E2=80=A6; >>=20 >> where target can be a record variable, a row variable, or a = comma-separated list of simple variables and record/row fields. >> =C2=BB >>=20 >> In plain English, the "into" target cannot be a local variable whose = data type is a composite type. That comes as a complete shock. Moreover, = it isn't true=E2=80=94as Tom's advice shows. (I tried it and it worked.) = I don't know what "simple" (as in "simple variable" means. I'm guessing = that it means "single-valued" in the Codd-and-Date sense so that, for = example, 42 and 'dog' are simple but array[1, 2, 3] isn't. My test = (below), with a column "c1" and a local variable "arr", both of data = type "int[]", showed that "select=E2=80=A6 c2=E2=80=A6 into arr=E2=80=A6"= works fine here. So the wording in the doc that I copied above could be = improved. >=20 > Reading the meaning of "simple" to be "not record or row variables" = seems like the safe bet, since those are covered in the first part of = the sentence. As a composite type is the umbrella term covering both = record and row that sentence most certainly does allow for such a = variable to be the target. But when it is, each individual column of = the result gets mapped to individual fields of the composite type. This = seems like a reasonable use case to define behavior from. >=20 >> postgresql composite type constructor >>=20 >> For example, "8.16. Composite Types" = (www.postgresql.org/docs/current/rowtypes.html#id-1.5.7.24.6) talks only = about anonymous "row". And this little test seems to show that "row" and = "record" mean the same thing=E2=80=94but you seed to write (or you see) = different spellings in different contexts: >>=20 >> with >> c(r) as (values(row(1, 'a', true))) >> select c.r, pg_typeof(c.r) from c; >=20 > Composite types that don't have a system-defined name are instead = named "record". "Row" usually means that not only is the composite type = named but the name matches that of a table in the system. IOW, as noted = above, "composite type" is a type category or umbrella that covers all = of these cases. > =20 >> Confusing, eh? There seems to be some doc missing here too that = defines "type constructor" and that uses "(f1, = ..fn)::qualified_type_id". (The "create type" account should x-ref it.) >=20 > You just pointed to the relevant documentation, and adding it to = create type doesn't seem like a good fit but if someone wanted to I'm = not sure I'd veto it. > =20 >> -- Tom's approach. Not nice. >> -- Two separate "select" statements to avoid >> -- 42601: record variable cannot be part of multiple-item INTO = list. >> select (a.c1).a1, (a.c1).a2 into r from s.t as a where a.k =3D 1; >> select a.c2 into arr from s.t as a where a.k =3D 1; >=20 > Yeah, I can see this as a natural consequence of the "column per = field" behavior decision. Maybe it should be covered better in the = docs? Seems like an unfortunate corner-case annoyance seldom = encountered due to the general disuse of composite types. >=20 >> for the_row in (select a.c1, a.c2 from s.t as a order by a.k) loop >> z :=3D the_row.c1::text||' / '||the_row.c2::text; = return next; >> end loop; >=20 > Doesn't seem like a terrible work-around even in the single-row = case... If "composite type" is the umbrella term that covers "row", "record", = and the result of "create type=E2=80=A6 as (=E2=80=A6)", what is the = term of art for the latter? The account of "pg_type.typtype" says this: =C2=AB b for a base type, c for a composite type (e.g., a table's row = type), d for a domain, e for an enum type, p for a pseudo-type, or r for = a range type. =C2=BB This wording doesn't help me because there are no DDLs to create = occurrences of "row" or "record". And the companion for a table is a = real schema-object, distinct from the table itself like the result of = "create type=E2=80=A6 as (=E2=80=A6)" using the names and data types of = the table's columns. (I'm assuming here that no particular schema-object = can have a row both in pg_class and pg_type.) So it seems wrong to call = this a "row type" because doing so leaves no term of art left over for = the ephemeral result of "row(1, 'a', true)" which pg_typeof anyway calls = "record". Finally, what terms of art do PG experts use to distinguish between = single-valued data types like "integer", "text", "boolean" and so on and = multi--valued data types like "array", "row", "record", and the result = of "create type=E2=80=A6 as (=E2=80=A6)"?