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.94.2) (envelope-from ) id 1s2VZ5-003bGg-6Z for pgsql-general@arkaria.postgresql.org; Thu, 02 May 2024 12:29:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s2VZ2-000Gmf-QG for pgsql-general@arkaria.postgresql.org; Thu, 02 May 2024 12:29:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s2VZ2-000GmW-94 for pgsql-general@lists.postgresql.org; Thu, 02 May 2024 12:29:29 +0000 Received: from mail-wr1-f44.google.com ([209.85.221.44]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s2VYz-00151x-G8 for pgsql-general@postgresql.org; Thu, 02 May 2024 12:29:26 +0000 Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-34d7b0dac54so1059641f8f.0 for ; Thu, 02 May 2024 05:29:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714652964; x=1715257764; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=JsM0SnuZtJTaDZVZLUvi/hpjZTi50M01ec2GegnSpYU=; b=Y7krpl0VB0rWkpq1CoG6U35JT7+ypjiE/cGUlQpNDRj03XULdwE2goqnno09DMG/GD XCl+mGYUfBih2WGOS0xii/ML+VXBpqwv4QFXJ3yoLugBvXIuUjKF++ekuRfGp9bXi85G yKZYki7FhF5S5JuSxbqTi57VBvRuCocqYLSZqvallKRqm7roBRzF+00sA8pAk4rkswk9 iDylsTc1sfdMUYVfUvlghqCbzSzCXfHVHHAOciz4Jl0Adcin+6Yaf2vLsD6AJRMjb3YK J8W0B1VJ37gDxb5VcCKUbsmGwFImrZ6K0Ie26Aw6Y9nY378w1o/GBMrFZNk4LqThFMFL 1G6A== X-Gm-Message-State: AOJu0YwrGfIWWocSr55yqQjOx+vMhN0ZuBvBfRjHA/7c/sZG7s+ehOTH ttIbtPopjGPZEpLPYfYEqxqdZxmVT9/oQUmucR9L5miugefmQHiyFlDsjzkiiWpw3tokArAk+S0 2qpXtj2J+ug+z/cGvu1wyqJ1WYWCdx/xc X-Google-Smtp-Source: AGHT+IGmXYbAKxg5XWVcztK0gZND9YuxmLikRKsiI767OTKP9MVA/TL9CAbtm0Gv/FdTlHVj9VQeReZj3u0hXV1uC0M= X-Received: by 2002:a5d:440b:0:b0:343:8485:4edd with SMTP id z11-20020a5d440b000000b0034384854eddmr2117609wrq.23.1714652963536; Thu, 02 May 2024 05:29:23 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Thom Brown Date: Thu, 2 May 2024 13:28:56 +0100 Message-ID: Subject: Re: Listing only the user defined types (with owners) To: Durumdara Cc: Postgres General Content-Type: multipart/alternative; boundary="000000000000ce1201061777c030" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ce1201061777c030 Content-Type: text/plain; charset="UTF-8" On Thu, 2 May 2024 at 12:40, Durumdara wrote: > Hello! > > I have a script which can change the table owners to the database owner. > > I select the tables like this: > > FOR r IN SELECT tablename FROM pg_tables WHERE (schemaname = 'public') > and (tableowner <> act_dbowner) > LOOP > ... > > For types I found pg_type, but this contains all types. > > For example I have only one user defined type, like "T_TEST", but this > pg_type relation contains the basic data types, other data types, from any > schema. > > Do you have a working Query which lists the user defined types with the > owners? > > Thank you for your help! > You can always cheat and copy what psql does when you tell it to list all user types with extended output (\dt+): postgres=# SET log_min_duration_statement = 0; SET postgres=# SET client_min_messages TO LOG; LOG: duration: 0.137 ms statement: SET client_min_messages TO LOG; SET postgres=# \dT+ LOG: duration: 2.901 ms statement: SELECT n.nspname as "Schema", pg_catalog.format_type(t.oid, NULL) AS "Name", t.typname AS "Internal name", CASE WHEN t.typrelid != 0 THEN CAST('tuple' AS pg_catalog.text) WHEN t.typlen < 0 THEN CAST('var' AS pg_catalog.text) ELSE CAST(t.typlen AS pg_catalog.text) END AS "Size", pg_catalog.array_to_string( ARRAY( SELECT e.enumlabel FROM pg_catalog.pg_enum e WHERE e.enumtypid = t.oid ORDER BY e.enumsortorder ), E'\n' ) AS "Elements", pg_catalog.pg_get_userbyid(t.typowner) AS "Owner", CASE WHEN pg_catalog.cardinality(t.typacl) = 0 THEN '(none)' ELSE pg_catalog.array_to_string(t.typacl, E'\n') END AS "Access privileges", pg_catalog.obj_description(t.oid, 'pg_type') as "Description" FROM pg_catalog.pg_type t LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid) AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' AND pg_catalog.pg_type_is_visible(t.oid) ORDER BY 1, 2; List of data types Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description --------+------+---------------+-------+----------+-----------+-------------------+------------- public | test | test | tuple | | thombrown | | (1 row) Regards Thom --000000000000ce1201061777c030 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, 2 May 2024 at 12:40, Durumdara &l= t;durumdara@gmail.com> wrote:=
Hello!

I have a script which ca= n change the table owners to the database owner.

I= select the tables like this:

=C2=A0 =C2=A0 FOR r IN SELECT tablename FROM pg_tables WHERE (schemana= me =3D 'public') and (tableowner <> act_dbowner)
=C2=A0 = =C2=A0 LOOP
...

For types I f= ound pg_type, but this contains all types.

For exa= mple I have only one user defined type, like "T_TEST", but this p= g_type relation contains the basic data types,=C2=A0 other data types, from= any schema.

Do you have a working Query which lis= ts the user defined types with the owners?

Thank y= ou for=C2=A0your help!

You can = always cheat and copy what psql does when you tell it to list all user type= s with extended output (\dt+):

postgres=3D# SET log_min_durat= ion_statement =3D 0;
SET
postgres=3D# SET client_min_messages TO LOG;=
LOG: =C2=A0duration: 0.137 ms =C2=A0statement: SET client_min_messages = TO LOG;
SET
postgres=3D# \dT+
LOG: =C2=A0duration: 2.901 ms =C2=A0= statement: SELECT n.nspname as "Schema",
=C2=A0 pg_catalog.for= mat_type(t.oid, NULL) AS "Name",
=C2=A0 t.typname AS "Int= ernal name",
=C2=A0 CASE WHEN t.typrelid !=3D 0
=C2=A0 =C2=A0 = =C2=A0 THEN CAST('tuple' AS pg_catalog.text)
=C2=A0 =C2=A0 WHEN = t.typlen < 0
=C2=A0 =C2=A0 =C2=A0 THEN CAST('var' AS pg_catal= og.text)
=C2=A0 =C2=A0 ELSE CAST(t.typlen AS pg_catalog.text)
=C2=A0 = END AS "Size",
=C2=A0 pg_catalog.array_to_string(
=C2=A0 = =C2=A0 =C2=A0 ARRAY(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 SELECT e.enumlab= el
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FROM pg_catalog.pg_enum e
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 WHERE e.enumtypid =3D t.oid
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 ORDER BY e.enumsortorder
=C2=A0 =C2=A0 =C2=A0 )= ,
=C2=A0 =C2=A0 =C2=A0 E'\n'
=C2=A0 ) AS "Elements"= ,
=C2=A0 pg_catalog.pg_get_userbyid(t.typowner) AS "Owner",CASE WHEN pg_catalog.cardinality(t.typacl) =3D 0 THEN '(none)' ELS= E pg_catalog.array_to_string(t.typacl, E'\n') END AS "Access p= rivileges",
=C2=A0 =C2=A0 pg_catalog.obj_description(t.oid, 'pg= _type') as "Description"
FROM pg_catalog.pg_type t
=C2= =A0 =C2=A0 =C2=A0LEFT JOIN pg_catalog.pg_namespace n ON n.oid =3D t.typname= space
WHERE (t.typrelid =3D 0 OR (SELECT c.relkind =3D 'c' FROM = pg_catalog.pg_class c WHERE c.oid =3D t.typrelid))
=C2=A0 AND NOT EXISTS= (SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid =3D t.typelem AND el.typa= rray =3D t.oid)
=C2=A0 =C2=A0 =C2=A0 AND n.nspname <> 'pg_cata= log'
=C2=A0 =C2=A0 =C2=A0 AND n.nspname <> 'information_sc= hema'
=C2=A0 AND pg_catalog.pg_type_is_visible(t.oid)
ORDER BY 1,= 2;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Li= st of data types
=C2=A0Schema | Name | Internal name | Size =C2=A0| Elem= ents | =C2=A0 Owner =C2=A0 | Access privileges | Description
--------+-= -----+---------------+-------+----------+-----------+-------------------+--= -----------
=C2=A0public | test | test =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0| tuple | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| thombrown | =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |
(1 row)
=C2=A0
Regards

Thom
--000000000000ce1201061777c030--