1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>F.18. intagg — integer aggregator and enumerator</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="hstore.html" title="F.17. hstore — hstore key/value datatype" /><link rel="next" href="intarray.html" title="F.19. intarray — manipulate arrays of integers" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">F.18. intagg — integer aggregator and enumerator</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="hstore.html" title="F.17. hstore — hstore key/value datatype">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Up</a></td><th width="60%" align="center">Appendix F. Additional Supplied Modules and Extensions</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="intarray.html" title="F.19. intarray — manipulate arrays of integers">Next</a></td></tr></table><hr /></div><div class="sect1" id="INTAGG"><div class="titlepage"><div><div><h2 class="title" style="clear: both">F.18. intagg — integer aggregator and enumerator <a href="#INTAGG" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="intagg.html#INTAGG-FUNCTIONS">F.18.1. Functions</a></span></dt><dt><span class="sect2"><a href="intagg.html#INTAGG-SAMPLES">F.18.2. Sample Uses</a></span></dt></dl></div><a id="id-1.11.7.28.2" class="indexterm"></a><p>
3 The <code class="filename">intagg</code> module provides an integer aggregator and an
4 enumerator. <code class="filename">intagg</code> is now obsolete, because there
5 are built-in functions that provide a superset of its capabilities.
6 However, the module is still provided as a compatibility wrapper around
7 the built-in functions.
8 </p><div class="sect2" id="INTAGG-FUNCTIONS"><div class="titlepage"><div><div><h3 class="title">F.18.1. Functions <a href="#INTAGG-FUNCTIONS" class="id_link">#</a></h3></div></div></div><a id="id-1.11.7.28.4.2" class="indexterm"></a><a id="id-1.11.7.28.4.3" class="indexterm"></a><p>
9 The aggregator is an aggregate function
10 <code class="function">int_array_aggregate(integer)</code>
11 that produces an integer array
12 containing exactly the integers it is fed.
13 This is a wrapper around <code class="function">array_agg</code>,
14 which does the same thing for any array type.
15 </p><a id="id-1.11.7.28.4.5" class="indexterm"></a><p>
16 The enumerator is a function
17 <code class="function">int_array_enum(integer[])</code>
18 that returns <code class="type">setof integer</code>. It is essentially the reverse
19 operation of the aggregator: given an array of integers, expand it
20 into a set of rows. This is a wrapper around <code class="function">unnest</code>,
21 which does the same thing for any array type.
22 </p></div><div class="sect2" id="INTAGG-SAMPLES"><div class="titlepage"><div><div><h3 class="title">F.18.2. Sample Uses <a href="#INTAGG-SAMPLES" class="id_link">#</a></h3></div></div></div><p>
23 Many database systems have the notion of a many to many table. Such a table
24 usually sits between two indexed tables, for example:
26 </p><pre class="programlisting">
27 CREATE TABLE left_table (id INT PRIMARY KEY, ...);
28 CREATE TABLE right_table (id INT PRIMARY KEY, ...);
29 CREATE TABLE many_to_many(id_left INT REFERENCES left_table,
30 id_right INT REFERENCES right_table);
33 It is typically used like this:
35 </p><pre class="programlisting">
37 FROM right_table JOIN many_to_many ON (right_table.id = many_to_many.id_right)
38 WHERE many_to_many.id_left = <em class="replaceable"><code>item</code></em>;
41 This will return all the items in the right hand table for an entry
42 in the left hand table. This is a very common construct in SQL.
44 Now, this methodology can be cumbersome with a very large number of
45 entries in the <code class="structname">many_to_many</code> table. Often,
46 a join like this would result in an index scan
47 and a fetch for each right hand entry in the table for a particular
48 left hand entry. If you have a very dynamic system, there is not much you
49 can do. However, if you have some data which is fairly static, you can
50 create a summary table with the aggregator.
52 </p><pre class="programlisting">
53 CREATE TABLE summary AS
54 SELECT id_left, int_array_aggregate(id_right) AS rights
59 This will create a table with one row per left item, and an array
60 of right items. Now this is pretty useless without some way of using
61 the array; that's why there is an array enumerator. You can do
63 </p><pre class="programlisting">
64 SELECT id_left, int_array_enum(rights) FROM summary WHERE id_left = <em class="replaceable"><code>item</code></em>;
67 The above query using <code class="function">int_array_enum</code> produces the same results
70 </p><pre class="programlisting">
71 SELECT id_left, id_right FROM many_to_many WHERE id_left = <em class="replaceable"><code>item</code></em>;
74 The difference is that the query against the summary table has to get
75 only one row from the table, whereas the direct query against
76 <code class="structname">many_to_many</code> must index scan and fetch a row for each entry.
78 On one system, an <code class="command">EXPLAIN</code> showed a query with a cost of 8488 was
79 reduced to a cost of 329. The original query was a join involving the
80 <code class="structname">many_to_many</code> table, which was replaced by:
82 </p><pre class="programlisting">
83 SELECT id_right, count(id_right) FROM
84 ( SELECT id_left, int_array_enum(rights) AS id_right
86 JOIN (SELECT id FROM left_table
87 WHERE id = <em class="replaceable"><code>item</code></em>) AS lefts
88 ON (summary.id_left = lefts.id)
93 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="hstore.html" title="F.17. hstore — hstore key/value datatype">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules and Extensions">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="intarray.html" title="F.19. intarray — manipulate arrays of integers">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.17. hstore — hstore key/value datatype </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> F.19. intarray — manipulate arrays of integers</td></tr></table></div></body></html>