pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
array UNNESTed to rows stable with respect to order?
5+ messages / 4 participants
[nested] [flat]

* array UNNESTed to rows stable with respect to order?
@ 2018-04-17 20:20  Thiemo Kellner <thiemo@gelassene-pferde.biz>
  0 siblings, 2 replies; 5+ messages in thread

From: Thiemo Kellner @ 2018-04-17 20:20 UTC (permalink / raw)
  To: PostgreSQL General <pgsql-general@lists.postgresql.org>

Hi all

I have created following statement to get the ordered parameter list of 
functions. I use UNNEST to get rows from array. This works fine but I am 
not sure whether the ordering remains in the later use. Background is 
PL/pgSQL to revoke grants to get a pristine start for granting. As the 
order of the parameter is important, I should use a statement that 
returns the properly ordered list of parameters. Maybe I did take a 
wrong turn and one can achieve this simpler.

Suggestions are very welcome.

Kind regards Thiemo

with PRO_UNNESTED_TYPES as(
   select
     oid as PROOID,
     PRONAME,
     unnest(PROARGTYPES) as PROARGTYPE,
     PRONAMESPACE,
     PROOWNER
   from
     PG_CATALOG.PG_PROC
) select
   P.PRONAME,
   string_agg(
     T.TYPNAME,
     ', '
   ) as PARAMETER_LIST_STRING,
   G.GRANTEE
from
   PRO_UNNESTED_TYPES P
inner join PG_CATALOG.PG_TYPE T on
   P.PROARGTYPE = T.OID
inner join PG_CATALOG.PG_NAMESPACE N on
   P.PRONAMESPACE = N.OID
inner join INFORMATION_SCHEMA.ROUTINE_PRIVILEGES G on
   -- copied from INFORMATION_SCHEMA.ROUTINE_PRIVILEGES source
  -- as seen in DBeaver 4.3.2
(
     (
       P.PRONAME::text || '_'::text
     )|| P.PROOID::text
   )::INFORMATION_SCHEMA.SQL_IDENTIFIER = G.SPECIFIC_NAME
where
   N.NSPNAME = current_user
   and G.GRANTEE != current_user
group by
   P.PROOID,
   P.PRONAME,
   G.GRANTEE
order by
   P.PRONAME asc,
   G.GRANTEE asc,
   PARAMETER_LIST_STRING asc;



-- 
Öffentlicher PGP-Schlüssel:
http://pgp.mit.edu/pks/lookup?op=get&search=0xCA167FB0E717AFFC

Attachments:

thiemo.vcf (text/x-vcard, 691B) download begin:vcard fn:Thiemo Kellner n:Kellner;Thiemo adr:;;Landstr. 34;Weilheim-Bannholz;BW;79809;Deutschland email;internet:thiemo@gelassene-pferde.biz tel;work:+49 1578 772 37 37 tel;cell:+41 78 947 36 21 note;quoted-printable:Auf Gelassene Pferde kann man bauen!=0D=0A= +49 (0)1578-772 37 37 (Mo, Di)=0D=0A= +41 (0)78 947 36 21 (Mi - Fr)=0D=0A= sip: thiemo.kellner@iptel.org=0D=0A= Skype: thiemo.kellner=0D=0A= http://www.gelassene-pferde.biz=0D=0A= Mitglied bei http://www.keep-it-natural.org=0D=0A= =C3=96ffentlicher PGP-Schl=C3=BCssel: http://pgp.mit.edu/pks/lookup?op=3D= get&search=3D0x8F70EFD2D972CBEF x-mozilla-html:FALSE url:http://www.gelassene-pferde.biz version:2.1 end:vcard
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: array UNNESTed to rows stable with respect to order?
@ 2018-04-17 20:40  David G. Johnston <david.g.johnston@gmail.com>
  parent: Thiemo Kellner <thiemo@gelassene-pferde.biz>
  1 sibling, 1 reply; 5+ messages in thread

From: David G. Johnston @ 2018-04-17 20:40 UTC (permalink / raw)
  To: Thiemo Kellner <thiemo@gelassene-pferde.biz>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>

On Tue, Apr 17, 2018 at 1:20 PM, Thiemo Kellner <thiemo@gelassene-pferde.biz
> wrote:

> This works fine but I am not sure whether the ordering remains in the
> later use.


​It does not.  If the array is not naturally ordered you will want to
attach a "with ordinality" clause to it for performing future ordering.

select * from unnest(ARRAY[3,6,4]::integer[]) with ordinality

Use LATERAL to move the unnest from the select-list section to the FROM
clause.

​David J.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: array UNNESTed to rows stable with respect to order?
@ 2018-04-17 20:49  Paul Jungwirth <pj@illuminatedcomputing.com>
  parent: Thiemo Kellner <thiemo@gelassene-pferde.biz>
  1 sibling, 1 reply; 5+ messages in thread

From: Paul Jungwirth @ 2018-04-17 20:49 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 04/17/2018 01:20 PM, Thiemo Kellner wrote:
>  I use UNNEST to get rows from array. This works fine but I am
> not sure whether the ordering remains in the later use.

I think you are looking for `WITH ORDINALITY` (in pg 9.4+). For instance 
you could rewrite your first CTE like so:

SELECT  oid as PROOID,
         PRONAME,
         t as PROARGTYPE,
         i,
         PRONAMESPACE,
         PROOWNER
FROM    PG_CATALOG.PG_PROC,
         UNNEST(PROARGTYPES) WITH ORDINALITY AS proargtypes(t, i)
;

Yours,

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: array UNNESTed to rows stable with respect to order?
@ 2018-04-18 06:08  Thiemo Kellner, NHC Barhufpflege <thiemo.kellner@gelassene-pferde.biz>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Thiemo Kellner, NHC Barhufpflege @ 2018-04-18 06:08 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>

Zitat von "David G. Johnston" <david.g.johnston@gmail.com>:

> ?It does not.  If the array is not naturally ordered you will want to
> attach a "with ordinality" clause to it for performing future ordering.

Thanks for the hints.

Kind regards

-- 
Öffentlicher PGP-Schlüssel:  
http://pgp.mit.edu/pks/lookup?op=get&search=0x8F70EFD2D972CBEF

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: array UNNESTed to rows stable with respect to order?
@ 2018-04-18 06:08  Thiemo Kellner <thiemo@gelassene-pferde.biz>
  parent: Paul Jungwirth <pj@illuminatedcomputing.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Thiemo Kellner @ 2018-04-18 06:08 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Zitat von Paul Jungwirth <pj@illuminatedcomputing.com>:

> I think you are looking for `WITH ORDINALITY` (in pg 9.4+). For  
> instance you could rewrite your first CTE like so:

Thanks for the hint.

Kind regards

-- 
Öffentlicher PGP-Schlüssel:  
http://pgp.mit.edu/pks/lookup?op=get&search=0x8F70EFD2D972CBEF

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-04-18 06:08 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-04-17 20:20 array UNNESTed to rows stable with respect to order? Thiemo Kellner <thiemo@gelassene-pferde.biz>
2018-04-17 20:40 ` David G. Johnston <david.g.johnston@gmail.com>
2018-04-18 06:08   ` Thiemo Kellner, NHC Barhufpflege <thiemo.kellner@gelassene-pferde.biz>
2018-04-17 20:49 ` Paul Jungwirth <pj@illuminatedcomputing.com>
2018-04-18 06:08   ` Thiemo Kellner <thiemo@gelassene-pferde.biz>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox