Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fdc9186

Browse files
committed
Replace the built-in GIN array opclasses with a single polymorphic opclass.
We had thirty different GIN array opclasses sharing the same operators and support functions. That still didn't cover all the built-in types, nor did it cover arrays of extension-added types. What we want is a single polymorphic opclass for "anyarray". There were two missing features needed to make this possible: 1. We have to be able to declare the index storage type as ANYELEMENT when the opclass is declared to index ANYARRAY. This just takes a few more lines in index_create(). Although this currently seems of use only for GIN, there's no reason to make index_create() restrict it to that. 2. We have to be able to identify the proper GIN compare function for the index storage type. This patch proceeds by making the compare function optional in GIN opclass definitions, and specifying that the default btree comparison function for the index storage type will be looked up when the opclass omits it. Again, that seems pretty generically useful. Since the comparison function lookup is done in initGinState(), making use of the second feature adds an additional cache lookup to GIN index access setup. It seems unlikely that that would be very noticeable given the other costs involved, but maybe at some point we should consider making GinState data persist longer than it now does --- we could keep it in the index relcache entry, perhaps. Rather fortuitously, we don't seem to need to do anything to get this change to play nice with dump/reload or pg_upgrade scenarios: the new opclass definition is automatically selected to replace existing index definitions, and the on-disk data remains compatible. Also, if a user has created a custom opclass definition for a non-builtin type, this doesn't break that, since CREATE INDEX will prefer an exact match to opcintype over a match to ANYARRAY. However, if there's anyone out there with handwritten DDL that explicitly specifies _bool_ops or one of the other replaced opclass names, they'll need to adjust that. Tom Lane, reviewed by Enrique Meneses Discussion: <14436.1470940379@sss.pgh.pa.us>
1 parent a4afb2b commit fdc9186

File tree

11 files changed

+101
-503
lines changed

11 files changed

+101
-503
lines changed

doc/src/sgml/gin.sgml

+38-311
Original file line numberDiff line numberDiff line change
@@ -85,298 +85,8 @@
8585
</thead>
8686
<tbody>
8787
<row>
88-
<entry><literal>_abstime_ops</></entry>
89-
<entry><type>abstime[]</></entry>
90-
<entry>
91-
<literal>&amp;&amp;</>
92-
<literal>&lt;@</>
93-
<literal>=</>
94-
<literal>@&gt;</>
95-
</entry>
96-
</row>
97-
<row>
98-
<entry><literal>_bit_ops</></entry>
99-
<entry><type>bit[]</></entry>
100-
<entry>
101-
<literal>&amp;&amp;</>
102-
<literal>&lt;@</>
103-
<literal>=</>
104-
<literal>@&gt;</>
105-
</entry>
106-
</row>
107-
<row>
108-
<entry><literal>_bool_ops</></entry>
109-
<entry><type>boolean[]</></entry>
110-
<entry>
111-
<literal>&amp;&amp;</>
112-
<literal>&lt;@</>
113-
<literal>=</>
114-
<literal>@&gt;</>
115-
</entry>
116-
</row>
117-
<row>
118-
<entry><literal>_bpchar_ops</></entry>
119-
<entry><type>character[]</></entry>
120-
<entry>
121-
<literal>&amp;&amp;</>
122-
<literal>&lt;@</>
123-
<literal>=</>
124-
<literal>@&gt;</>
125-
</entry>
126-
</row>
127-
<row>
128-
<entry><literal>_bytea_ops</></entry>
129-
<entry><type>bytea[]</></entry>
130-
<entry>
131-
<literal>&amp;&amp;</>
132-
<literal>&lt;@</>
133-
<literal>=</>
134-
<literal>@&gt;</>
135-
</entry>
136-
</row>
137-
<row>
138-
<entry><literal>_char_ops</></entry>
139-
<entry><type>"char"[]</></entry>
140-
<entry>
141-
<literal>&amp;&amp;</>
142-
<literal>&lt;@</>
143-
<literal>=</>
144-
<literal>@&gt;</>
145-
</entry>
146-
</row>
147-
<row>
148-
<entry><literal>_cidr_ops</></entry>
149-
<entry><type>cidr[]</></entry>
150-
<entry>
151-
<literal>&amp;&amp;</>
152-
<literal>&lt;@</>
153-
<literal>=</>
154-
<literal>@&gt;</>
155-
</entry>
156-
</row>
157-
<row>
158-
<entry><literal>_date_ops</></entry>
159-
<entry><type>date[]</></entry>
160-
<entry>
161-
<literal>&amp;&amp;</>
162-
<literal>&lt;@</>
163-
<literal>=</>
164-
<literal>@&gt;</>
165-
</entry>
166-
</row>
167-
<row>
168-
<entry><literal>_float4_ops</></entry>
169-
<entry><type>float4[]</></entry>
170-
<entry>
171-
<literal>&amp;&amp;</>
172-
<literal>&lt;@</>
173-
<literal>=</>
174-
<literal>@&gt;</>
175-
</entry>
176-
</row>
177-
<row>
178-
<entry><literal>_float8_ops</></entry>
179-
<entry><type>float8[]</></entry>
180-
<entry>
181-
<literal>&amp;&amp;</>
182-
<literal>&lt;@</>
183-
<literal>=</>
184-
<literal>@&gt;</>
185-
</entry>
186-
</row>
187-
<row>
188-
<entry><literal>_inet_ops</></entry>
189-
<entry><type>inet[]</></entry>
190-
<entry>
191-
<literal>&amp;&amp;</>
192-
<literal>&lt;@</>
193-
<literal>=</>
194-
<literal>@&gt;</>
195-
</entry>
196-
</row>
197-
<row>
198-
<entry><literal>_int2_ops</></entry>
199-
<entry><type>smallint[]</></entry>
200-
<entry>
201-
<literal>&amp;&amp;</>
202-
<literal>&lt;@</>
203-
<literal>=</>
204-
<literal>@&gt;</>
205-
</entry>
206-
</row>
207-
<row>
208-
<entry><literal>_int4_ops</></entry>
209-
<entry><type>integer[]</></entry>
210-
<entry>
211-
<literal>&amp;&amp;</>
212-
<literal>&lt;@</>
213-
<literal>=</>
214-
<literal>@&gt;</>
215-
</entry>
216-
</row>
217-
<row>
218-
<entry><literal>_int8_ops</></entry>
219-
<entry><type>bigint[]</></entry>
220-
<entry>
221-
<literal>&amp;&amp;</>
222-
<literal>&lt;@</>
223-
<literal>=</>
224-
<literal>@&gt;</>
225-
</entry>
226-
</row>
227-
<row>
228-
<entry><literal>_interval_ops</></entry>
229-
<entry><type>interval[]</></entry>
230-
<entry>
231-
<literal>&amp;&amp;</>
232-
<literal>&lt;@</>
233-
<literal>=</>
234-
<literal>@&gt;</>
235-
</entry>
236-
</row>
237-
<row>
238-
<entry><literal>_macaddr_ops</></entry>
239-
<entry><type>macaddr[]</></entry>
240-
<entry>
241-
<literal>&amp;&amp;</>
242-
<literal>&lt;@</>
243-
<literal>=</>
244-
<literal>@&gt;</>
245-
</entry>
246-
</row>
247-
<row>
248-
<entry><literal>_money_ops</></entry>
249-
<entry><type>money[]</></entry>
250-
<entry>
251-
<literal>&amp;&amp;</>
252-
<literal>&lt;@</>
253-
<literal>=</>
254-
<literal>@&gt;</>
255-
</entry>
256-
</row>
257-
<row>
258-
<entry><literal>_name_ops</></entry>
259-
<entry><type>name[]</></entry>
260-
<entry>
261-
<literal>&amp;&amp;</>
262-
<literal>&lt;@</>
263-
<literal>=</>
264-
<literal>@&gt;</>
265-
</entry>
266-
</row>
267-
<row>
268-
<entry><literal>_numeric_ops</></entry>
269-
<entry><type>numeric[]</></entry>
270-
<entry>
271-
<literal>&amp;&amp;</>
272-
<literal>&lt;@</>
273-
<literal>=</>
274-
<literal>@&gt;</>
275-
</entry>
276-
</row>
277-
<row>
278-
<entry><literal>_oid_ops</></entry>
279-
<entry><type>oid[]</></entry>
280-
<entry>
281-
<literal>&amp;&amp;</>
282-
<literal>&lt;@</>
283-
<literal>=</>
284-
<literal>@&gt;</>
285-
</entry>
286-
</row>
287-
<row>
288-
<entry><literal>_oidvector_ops</></entry>
289-
<entry><type>oidvector[]</></entry>
290-
<entry>
291-
<literal>&amp;&amp;</>
292-
<literal>&lt;@</>
293-
<literal>=</>
294-
<literal>@&gt;</>
295-
</entry>
296-
</row>
297-
<row>
298-
<entry><literal>_reltime_ops</></entry>
299-
<entry><type>reltime[]</></entry>
300-
<entry>
301-
<literal>&amp;&amp;</>
302-
<literal>&lt;@</>
303-
<literal>=</>
304-
<literal>@&gt;</>
305-
</entry>
306-
</row>
307-
<row>
308-
<entry><literal>_text_ops</></entry>
309-
<entry><type>text[]</></entry>
310-
<entry>
311-
<literal>&amp;&amp;</>
312-
<literal>&lt;@</>
313-
<literal>=</>
314-
<literal>@&gt;</>
315-
</entry>
316-
</row>
317-
<row>
318-
<entry><literal>_time_ops</></entry>
319-
<entry><type>time[]</></entry>
320-
<entry>
321-
<literal>&amp;&amp;</>
322-
<literal>&lt;@</>
323-
<literal>=</>
324-
<literal>@&gt;</>
325-
</entry>
326-
</row>
327-
<row>
328-
<entry><literal>_timestamp_ops</></entry>
329-
<entry><type>timestamp[]</></entry>
330-
<entry>
331-
<literal>&amp;&amp;</>
332-
<literal>&lt;@</>
333-
<literal>=</>
334-
<literal>@&gt;</>
335-
</entry>
336-
</row>
337-
<row>
338-
<entry><literal>_timestamptz_ops</></entry>
339-
<entry><type>timestamp with time zone[]</></entry>
340-
<entry>
341-
<literal>&amp;&amp;</>
342-
<literal>&lt;@</>
343-
<literal>=</>
344-
<literal>@&gt;</>
345-
</entry>
346-
</row>
347-
<row>
348-
<entry><literal>_timetz_ops</></entry>
349-
<entry><type>time with time zone[]</></entry>
350-
<entry>
351-
<literal>&amp;&amp;</>
352-
<literal>&lt;@</>
353-
<literal>=</>
354-
<literal>@&gt;</>
355-
</entry>
356-
</row>
357-
<row>
358-
<entry><literal>_tinterval_ops</></entry>
359-
<entry><type>tinterval[]</></entry>
360-
<entry>
361-
<literal>&amp;&amp;</>
362-
<literal>&lt;@</>
363-
<literal>=</>
364-
<literal>@&gt;</>
365-
</entry>
366-
</row>
367-
<row>
368-
<entry><literal>_varbit_ops</></entry>
369-
<entry><type>bit varying[]</></entry>
370-
<entry>
371-
<literal>&amp;&amp;</>
372-
<literal>&lt;@</>
373-
<literal>=</>
374-
<literal>@&gt;</>
375-
</entry>
376-
</row>
377-
<row>
378-
<entry><literal>_varchar_ops</></entry>
379-
<entry><type>character varying[]</></entry>
88+
<entry><literal>array_ops</></entry>
89+
<entry><type>anyarray</></entry>
38090
<entry>
38191
<literal>&amp;&amp;</>
38292
<literal>&lt;@</>
@@ -441,22 +151,10 @@
441151
</para>
442152

443153
<para>
444-
There are three methods that an operator class for
154+
There are two methods that an operator class for
445155
<acronym>GIN</acronym> must provide:
446156

447-
<variablelist>
448-
<varlistentry>
449-
<term><function>int compare(Datum a, Datum b)</></term>
450-
<listitem>
451-
<para>
452-
Compares two keys (not indexed items!) and returns an integer less than
453-
zero, zero, or greater than zero, indicating whether the first key is
454-
less than, equal to, or greater than the second. Null keys are never
455-
passed to this function.
456-
</para>
457-
</listitem>
458-
</varlistentry>
459-
157+
<variablelist>
460158
<varlistentry>
461159
<term><function>Datum *extractValue(Datum itemValue, int32 *nkeys,
462160
bool **nullFlags)</></term>
@@ -645,7 +343,38 @@
645343
</listitem>
646344
</varlistentry>
647345
</variablelist>
346+
</para>
347+
348+
<para>
349+
In addition, GIN must have a way to sort the key values stored in the index.
350+
The operator class can define the sort ordering by specifying a comparison
351+
method:
648352

353+
<variablelist>
354+
<varlistentry>
355+
<term><function>int compare(Datum a, Datum b)</></term>
356+
<listitem>
357+
<para>
358+
Compares two keys (not indexed items!) and returns an integer less than
359+
zero, zero, or greater than zero, indicating whether the first key is
360+
less than, equal to, or greater than the second. Null keys are never
361+
passed to this function.
362+
</para>
363+
</listitem>
364+
</varlistentry>
365+
</variablelist>
366+
367+
Alternatively, if the operator class does not provide a <function>compare</>
368+
method, GIN will look up the default btree operator class for the index
369+
key data type, and use its comparison function. It is recommended to
370+
specify the comparison function in a GIN operator class that is meant for
371+
just one data type, as looking up the btree operator class costs a few
372+
cycles. However, polymorphic GIN operator classes (such
373+
as <literal>array_ops</>) typically cannot specify a single comparison
374+
function.
375+
</para>
376+
377+
<para>
649378
Optionally, an operator class for <acronym>GIN</acronym> can supply the
650379
following method:
651380

@@ -900,11 +629,9 @@
900629
<title>Examples</title>
901630

902631
<para>
903-
The <productname>PostgreSQL</productname> source distribution includes
904-
<acronym>GIN</acronym> operator classes for <type>tsvector</> and
905-
for one-dimensional arrays of all internal types. Prefix searching in
906-
<type>tsvector</> is implemented using the <acronym>GIN</> partial match
907-
feature.
632+
The core <productname>PostgreSQL</> distribution
633+
includes the <acronym>GIN</acronym> operator classes previously shown in
634+
<xref linkend="gin-builtin-opclasses-table">.
908635
The following <filename>contrib</> modules also contain
909636
<acronym>GIN</acronym> operator classes:
910637

doc/src/sgml/indices.sgml

+2-3
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
315315
operators with which a GIN index can be used vary depending on the
316316
indexing strategy.
317317
As an example, the standard distribution of
318-
<productname>PostgreSQL</productname> includes GIN operator classes
319-
for one-dimensional arrays, which support indexed
320-
queries using these operators:
318+
<productname>PostgreSQL</productname> includes a GIN operator class
319+
for arrays, which supports indexed queries using these operators:
321320

322321
<simplelist>
323322
<member><literal>&lt;@</literal></member>

0 commit comments

Comments
 (0)