Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 7022413A224D for ; Thu, 26 Jan 2012 22:14:20 -0400 (AST) Received: from lo.gmane.org ([80.91.229.12]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RqbKV-00062m-3B for pgsql-sql@postgresql.org; Fri, 27 Jan 2012 02:14:20 +0000 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RqbKH-0005sf-Bp for pgsql-sql@postgresql.org; Fri, 27 Jan 2012 03:14:05 +0100 Received: from g231144176.adsl.alicedsl.de ([92.231.144.176]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 27 Jan 2012 03:14:05 +0100 Received: from tim by g231144176.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 27 Jan 2012 03:14:05 +0100 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-sql@postgresql.org To: pgsql-sql@postgresql.org From: Tim Landscheidt Subject: Re: Getting a list of a table's attributes that are sortable sorted by uniqueness Date: Fri, 27 Jan 2012 02:13:52 +0000 Organization: Lines: 67 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: g231144176.adsl.alicedsl.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:GkO0AdT6wZRgXD5vWdNPM6s+0bI= X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201201/107 X-Sequence-Number: 36278 I wrote: > I frequently use pg_dump to dump databases and compare them > with diff. To get rid of most "false positives", I'd like > to patch pg_dump to sort the table so that its dumped order > isn't changed more than necessary by insertions & Co. > So I'm looking for a query that will return a list of a > table's attributes that are sortable (e. g. no XML fields) > and sorted by "uniqueness", i. e. first attributes repre- > senting the primary key, then other unique keys, then the > rest. > Before I dive into the depths of PostgreSQL's system cata- > logues, has anyone already solved this problem? Progress report: The query: | SELECT attname, | attnum, | keyrank, | columnrankinkey | FROM pg_attribute | LEFT JOIN | (SELECT DISTINCT ON (tableid, | columnnr) indrelid as tableid, | indkey [subscript] AS columnnr, | CASE | WHEN indisprimary THEN 0 | WHEN indisunique THEN 1 | ELSE 2 | END as keyrank, | subscript as columnrankinkey | FROM | (SELECT indrelid, | indkey, | generate_subscripts(indkey, 1) as subscript, | indisprimary, | indisunique | FROM pg_index | ORDER BY indrelid, | indkey, | indisprimary DESC, indisunique DESC) AS s | ORDER BY tableid, columnnr, CASE | WHEN indisprimary THEN 0 | WHEN indisunique THEN 1 | ELSE 2 | END, columnrankinkey) AS s2 ON attrelid = tableid | AND attnum = columnnr | WHERE attrelid = 'tablename'::regclass | AND NOT attisdropped | AND attnum > 0 | ORDER BY keyrank, | columnrankinkey, | attnum; does almost what I want except: - Attributes that can't be sorted (XML) aren't skipped, and - "UNIQUE(A, B)" and "UNIQUE(C, D)" would give "A, C, B, D" (untested) so the "rank" of a non-primary key has yet to be included. Stay tuned. Tim (looking forward to "UNNEST ... WITH ORDINALITY")