Skip to content

Commit 4c8ab1b

Browse files
committed
Add btree and hash opclasses for pg_lsn.
This is needed to allow ORDER BY, DISTINCT, etc to work as expected for pg_lsn values. We had previously decided to put this off for 9.5, but in view of commit eeca4cd there's no reason to avoid a catversion bump for 9.4beta2, and this does make a pretty significant usability difference for pg_lsn. Michael Paquier, with fixes from Andres Freund and Tom Lane
1 parent eeca4cd commit 4c8ab1b

File tree

11 files changed

+196
-4
lines changed

11 files changed

+196
-4
lines changed

src/backend/utils/adt/pg_lsn.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*-------------------------------------------------------------------------
22
*
33
* pg_lsn.c
4-
* Internal PostgreSQL LSN operations
4+
* Operations for the pg_lsn datatype.
55
*
6-
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
6+
* Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
@@ -13,6 +13,7 @@
1313
*/
1414
#include "postgres.h"
1515

16+
#include "access/hash.h"
1617
#include "funcapi.h"
1718
#include "libpq/pqformat.h"
1819
#include "utils/builtins.h"
@@ -153,6 +154,29 @@ pg_lsn_ge(PG_FUNCTION_ARGS)
153154
PG_RETURN_BOOL(lsn1 >= lsn2);
154155
}
155156

157+
/* btree index opclass support */
158+
Datum
159+
pg_lsn_cmp(PG_FUNCTION_ARGS)
160+
{
161+
XLogRecPtr a = PG_GETARG_LSN(0);
162+
XLogRecPtr b = PG_GETARG_LSN(1);
163+
164+
if (a > b)
165+
PG_RETURN_INT32(1);
166+
else if (a == b)
167+
PG_RETURN_INT32(0);
168+
else
169+
PG_RETURN_INT32(-1);
170+
}
171+
172+
/* hash index opclass support */
173+
Datum
174+
pg_lsn_hash(PG_FUNCTION_ARGS)
175+
{
176+
/* We can use hashint8 directly */
177+
return hashint8(fcinfo);
178+
}
179+
156180

157181
/*----------------------------------------------------------
158182
* Arithmetic operators on PostgreSQL LSNs.

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201405111
56+
#define CATALOG_VERSION_NO 201406041
5757

5858
#endif

src/include/catalog/pg_amop.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,16 @@ DATA(insert ( 2968 2950 2950 3 s 2972 403 0 ));
512512
DATA(insert ( 2968 2950 2950 4 s 2977 403 0 ));
513513
DATA(insert ( 2968 2950 2950 5 s 2975 403 0 ));
514514

515+
/*
516+
* btree pg_lsn_ops
517+
*/
518+
519+
DATA(insert ( 3253 3220 3220 1 s 3224 403 0 ));
520+
DATA(insert ( 3253 3220 3220 2 s 3226 403 0 ));
521+
DATA(insert ( 3253 3220 3220 3 s 3222 403 0 ));
522+
DATA(insert ( 3253 3220 3220 4 s 3227 403 0 ));
523+
DATA(insert ( 3253 3220 3220 5 s 3225 403 0 ));
524+
515525
/*
516526
* hash index _ops
517527
*/
@@ -581,6 +591,8 @@ DATA(insert ( 2231 1042 1042 1 s 1054 405 0 ));
581591
DATA(insert ( 2235 1033 1033 1 s 974 405 0 ));
582592
/* uuid_ops */
583593
DATA(insert ( 2969 2950 2950 1 s 2972 405 0 ));
594+
/* pg_lsn_ops */
595+
DATA(insert ( 3254 3220 3220 1 s 3222 405 0 ));
584596
/* numeric_ops */
585597
DATA(insert ( 1998 1700 1700 1 s 1752 405 0 ));
586598
/* array_ops */

src/include/catalog/pg_amproc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ DATA(insert ( 2789 27 27 1 2794 ));
134134
DATA(insert ( 2968 2950 2950 1 2960 ));
135135
DATA(insert ( 2994 2249 2249 1 2987 ));
136136
DATA(insert ( 3194 2249 2249 1 3187 ));
137+
DATA(insert ( 3253 3220 3220 1 3251 ));
137138
DATA(insert ( 3522 3500 3500 1 3514 ));
138139
DATA(insert ( 3626 3614 3614 1 3622 ));
139140
DATA(insert ( 3683 3615 3615 1 3668 ));
@@ -174,6 +175,7 @@ DATA(insert ( 2229 25 25 1 400 ));
174175
DATA(insert ( 2231 1042 1042 1 1080 ));
175176
DATA(insert ( 2235 1033 1033 1 329 ));
176177
DATA(insert ( 2969 2950 2950 1 2963 ));
178+
DATA(insert ( 3254 3220 3220 1 3252 ));
177179
DATA(insert ( 3523 3500 3500 1 3515 ));
178180
DATA(insert ( 3903 3831 3831 1 3902 ));
179181
DATA(insert ( 4034 3802 3802 1 4045 ));

src/include/catalog/pg_opclass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ DATA(insert ( 2742 _reltime_ops PGNSP PGUID 2745 1024 t 703 ));
215215
DATA(insert ( 2742 _tinterval_ops PGNSP PGUID 2745 1025 t 704 ));
216216
DATA(insert ( 403 uuid_ops PGNSP PGUID 2968 2950 t 0 ));
217217
DATA(insert ( 405 uuid_ops PGNSP PGUID 2969 2950 t 0 ));
218+
DATA(insert ( 403 pg_lsn_ops PGNSP PGUID 3253 3220 t 0 ));
219+
DATA(insert ( 405 pg_lsn_ops PGNSP PGUID 3254 3220 t 0 ));
218220
DATA(insert ( 403 enum_ops PGNSP PGUID 3522 3500 t 0 ));
219221
DATA(insert ( 405 enum_ops PGNSP PGUID 3523 3500 t 0 ));
220222
DATA(insert ( 403 tsvector_ops PGNSP PGUID 3626 3614 t 0 ));

src/include/catalog/pg_operator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ DATA(insert OID = 2977 ( ">=" PGNSP PGUID b f f 2950 2950 16 2976 2974 uuid_
15951595
DESCR("greater than or equal");
15961596

15971597
/* pg_lsn operators */
1598-
DATA(insert OID = 3222 ( "=" PGNSP PGUID b f f 3220 3220 16 3222 3223 pg_lsn_eq eqsel eqjoinsel ));
1598+
DATA(insert OID = 3222 ( "=" PGNSP PGUID b t t 3220 3220 16 3222 3223 pg_lsn_eq eqsel eqjoinsel ));
15991599
DESCR("equal");
16001600
DATA(insert OID = 3223 ( "<>" PGNSP PGUID b f f 3220 3220 16 3223 3222 pg_lsn_ne neqsel neqjoinsel ));
16011601
DESCR("not equal");

src/include/catalog/pg_opfamily.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ DATA(insert OID = 1029 ( 783 point_ops PGNSP PGUID ));
134134
DATA(insert OID = 2745 ( 2742 array_ops PGNSP PGUID ));
135135
DATA(insert OID = 2968 ( 403 uuid_ops PGNSP PGUID ));
136136
DATA(insert OID = 2969 ( 405 uuid_ops PGNSP PGUID ));
137+
DATA(insert OID = 3253 ( 403 pg_lsn_ops PGNSP PGUID ));
138+
DATA(insert OID = 3254 ( 405 pg_lsn_ops PGNSP PGUID ));
137139
DATA(insert OID = 3522 ( 403 enum_ops PGNSP PGUID ));
138140
DATA(insert OID = 3523 ( 405 enum_ops PGNSP PGUID ));
139141
DATA(insert OID = 3626 ( 403 tsvector_ops PGNSP PGUID ));

src/include/catalog/pg_proc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,6 +4293,10 @@ DATA(insert OID = 3238 ( pg_lsn_recv PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 3
42934293
DESCR("I/O");
42944294
DATA(insert OID = 3239 ( pg_lsn_send PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 17 "3220" _null_ _null_ _null_ _null_ pg_lsn_send _null_ _null_ _null_ ));
42954295
DESCR("I/O");
4296+
DATA(insert OID = 3251 ( pg_lsn_cmp PGNSP PGUID 12 1 0 0 0 f f f f t f i 2 0 23 "3220 3220" _null_ _null_ _null_ _null_ pg_lsn_cmp _null_ _null_ _null_ ));
4297+
DESCR("less-equal-greater");
4298+
DATA(insert OID = 3252 ( pg_lsn_hash PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 23 "3220" _null_ _null_ _null_ _null_ pg_lsn_hash _null_ _null_ _null_ ));
4299+
DESCR("hash");
42964300

42974301
/* enum related procs */
42984302
DATA(insert OID = 3504 ( anyenum_in PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 3500 "2275" _null_ _null_ _null_ _null_ anyenum_in _null_ _null_ _null_ ));

src/include/utils/pg_lsn.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ extern Datum pg_lsn_lt(PG_FUNCTION_ARGS);
2929
extern Datum pg_lsn_gt(PG_FUNCTION_ARGS);
3030
extern Datum pg_lsn_le(PG_FUNCTION_ARGS);
3131
extern Datum pg_lsn_ge(PG_FUNCTION_ARGS);
32+
extern Datum pg_lsn_cmp(PG_FUNCTION_ARGS);
33+
extern Datum pg_lsn_hash(PG_FUNCTION_ARGS);
3234

3335
extern Datum pg_lsn_mi(PG_FUNCTION_ARGS);
3436

src/test/regress/expected/pg_lsn.out

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,133 @@ SELECT '0/16AE7F8'::pg_lsn - '0/16AE7F7'::pg_lsn;
6464
1
6565
(1 row)
6666

67+
-- Check btree and hash opclasses
68+
EXPLAIN (COSTS OFF)
69+
SELECT DISTINCT (i || '/' || j)::pg_lsn f
70+
FROM generate_series(1, 10) i,
71+
generate_series(1, 10) j,
72+
generate_series(1, 5) k
73+
ORDER BY f;
74+
QUERY PLAN
75+
--------------------------------------------------------------------------
76+
Sort
77+
Sort Key: (((((i.i)::text || '/'::text) || (j.j)::text))::pg_lsn)
78+
-> HashAggregate
79+
Group Key: ((((i.i)::text || '/'::text) || (j.j)::text))::pg_lsn
80+
-> Nested Loop
81+
-> Function Scan on generate_series k
82+
-> Materialize
83+
-> Nested Loop
84+
-> Function Scan on generate_series i
85+
-> Function Scan on generate_series j
86+
(10 rows)
87+
88+
SELECT DISTINCT (i || '/' || j)::pg_lsn f
89+
FROM generate_series(1, 10) i,
90+
generate_series(1, 10) j,
91+
generate_series(1, 5) k
92+
ORDER BY f;
93+
f
94+
-------
95+
1/1
96+
1/2
97+
1/3
98+
1/4
99+
1/5
100+
1/6
101+
1/7
102+
1/8
103+
1/9
104+
1/10
105+
2/1
106+
2/2
107+
2/3
108+
2/4
109+
2/5
110+
2/6
111+
2/7
112+
2/8
113+
2/9
114+
2/10
115+
3/1
116+
3/2
117+
3/3
118+
3/4
119+
3/5
120+
3/6
121+
3/7
122+
3/8
123+
3/9
124+
3/10
125+
4/1
126+
4/2
127+
4/3
128+
4/4
129+
4/5
130+
4/6
131+
4/7
132+
4/8
133+
4/9
134+
4/10
135+
5/1
136+
5/2
137+
5/3
138+
5/4
139+
5/5
140+
5/6
141+
5/7
142+
5/8
143+
5/9
144+
5/10
145+
6/1
146+
6/2
147+
6/3
148+
6/4
149+
6/5
150+
6/6
151+
6/7
152+
6/8
153+
6/9
154+
6/10
155+
7/1
156+
7/2
157+
7/3
158+
7/4
159+
7/5
160+
7/6
161+
7/7
162+
7/8
163+
7/9
164+
7/10
165+
8/1
166+
8/2
167+
8/3
168+
8/4
169+
8/5
170+
8/6
171+
8/7
172+
8/8
173+
8/9
174+
8/10
175+
9/1
176+
9/2
177+
9/3
178+
9/4
179+
9/5
180+
9/6
181+
9/7
182+
9/8
183+
9/9
184+
9/10
185+
10/1
186+
10/2
187+
10/3
188+
10/4
189+
10/5
190+
10/6
191+
10/7
192+
10/8
193+
10/9
194+
10/10
195+
(100 rows)
196+

src/test/regress/sql/pg_lsn.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,17 @@ SELECT '0/16AE7F7' < '0/16AE7F8'::pg_lsn;
2323
SELECT '0/16AE7F8' > pg_lsn '0/16AE7F7';
2424
SELECT '0/16AE7F7'::pg_lsn - '0/16AE7F8'::pg_lsn;
2525
SELECT '0/16AE7F8'::pg_lsn - '0/16AE7F7'::pg_lsn;
26+
27+
-- Check btree and hash opclasses
28+
EXPLAIN (COSTS OFF)
29+
SELECT DISTINCT (i || '/' || j)::pg_lsn f
30+
FROM generate_series(1, 10) i,
31+
generate_series(1, 10) j,
32+
generate_series(1, 5) k
33+
ORDER BY f;
34+
35+
SELECT DISTINCT (i || '/' || j)::pg_lsn f
36+
FROM generate_series(1, 10) i,
37+
generate_series(1, 10) j,
38+
generate_series(1, 5) k
39+
ORDER BY f;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy