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

Commit 0144eb9

Browse files
committed
Add the full set of comparison functions for type TID, including a btree
opclass. This is not so much because anyone's likely to create an index on TID, as that sorting TIDs can be useful. Also added max and min aggregates while at it, so that one can investigate the clusteredness of a table with queries like SELECT min(ctid), max(ctid) FROM tab WHERE ... Greg Stark and Tom Lane
1 parent bc660c4 commit 0144eb9

File tree

9 files changed

+167
-51
lines changed

9 files changed

+167
-51
lines changed

src/backend/utils/adt/tid.c

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/tid.c,v 1.53 2006/07/14 05:28:28 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/tid.c,v 1.54 2006/07/21 20:51:32 tgl Exp $
1212
*
1313
* NOTES
1414
* input routine largely stolen from boxin().
@@ -98,16 +98,11 @@ Datum
9898
tidout(PG_FUNCTION_ARGS)
9999
{
100100
ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0);
101-
BlockId blockId;
102101
BlockNumber blockNumber;
103102
OffsetNumber offsetNumber;
104103
char buf[32];
105104

106-
if (!ItemPointerIsValid(itemPtr))
107-
PG_RETURN_CSTRING(pstrdup("()"));
108-
109-
blockId = &(itemPtr->ip_blkid);
110-
blockNumber = BlockIdGetBlockNumber(blockId);
105+
blockNumber = BlockIdGetBlockNumber(&(itemPtr->ip_blkid));
111106
offsetNumber = itemPtr->ip_posid;
112107

113108
/* Perhaps someday we should output this as a record. */
@@ -163,15 +158,36 @@ tidsend(PG_FUNCTION_ARGS)
163158
* PUBLIC ROUTINES *
164159
*****************************************************************************/
165160

161+
static int32
162+
tid_cmp_internal(ItemPointer arg1, ItemPointer arg2)
163+
{
164+
/*
165+
* Don't use ItemPointerGetBlockNumber or ItemPointerGetOffsetNumber here,
166+
* because they assert ip_posid != 0 which might not be true for a
167+
* user-supplied TID.
168+
*/
169+
BlockNumber b1 = BlockIdGetBlockNumber(&(arg1->ip_blkid));
170+
BlockNumber b2 = BlockIdGetBlockNumber(&(arg2->ip_blkid));
171+
172+
if (b1 < b2)
173+
return -1;
174+
else if (b1 > b2)
175+
return 1;
176+
else if (arg1->ip_posid < arg2->ip_posid)
177+
return -1;
178+
else if (arg1->ip_posid > arg2->ip_posid)
179+
return 1;
180+
else
181+
return 0;
182+
}
183+
166184
Datum
167185
tideq(PG_FUNCTION_ARGS)
168186
{
169187
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
170188
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
171189

172-
PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) ==
173-
BlockIdGetBlockNumber(&(arg2->ip_blkid)) &&
174-
arg1->ip_posid == arg2->ip_posid);
190+
PG_RETURN_BOOL(tid_cmp_internal(arg1,arg2) == 0);
175191
}
176192

177193
Datum
@@ -180,11 +196,73 @@ tidne(PG_FUNCTION_ARGS)
180196
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
181197
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
182198

183-
PG_RETURN_BOOL(BlockIdGetBlockNumber(&(arg1->ip_blkid)) !=
184-
BlockIdGetBlockNumber(&(arg2->ip_blkid)) ||
185-
arg1->ip_posid != arg2->ip_posid);
199+
PG_RETURN_BOOL(tid_cmp_internal(arg1,arg2) != 0);
186200
}
187201

202+
Datum
203+
tidlt(PG_FUNCTION_ARGS)
204+
{
205+
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
206+
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
207+
208+
PG_RETURN_BOOL(tid_cmp_internal(arg1,arg2) < 0);
209+
}
210+
211+
Datum
212+
tidle(PG_FUNCTION_ARGS)
213+
{
214+
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
215+
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
216+
217+
PG_RETURN_BOOL(tid_cmp_internal(arg1,arg2) <= 0);
218+
}
219+
220+
Datum
221+
tidgt(PG_FUNCTION_ARGS)
222+
{
223+
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
224+
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
225+
226+
PG_RETURN_BOOL(tid_cmp_internal(arg1,arg2) > 0);
227+
}
228+
229+
Datum
230+
tidge(PG_FUNCTION_ARGS)
231+
{
232+
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
233+
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
234+
235+
PG_RETURN_BOOL(tid_cmp_internal(arg1,arg2) >= 0);
236+
}
237+
238+
Datum
239+
bttidcmp(PG_FUNCTION_ARGS)
240+
{
241+
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
242+
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
243+
244+
PG_RETURN_INT32(tid_cmp_internal(arg1, arg2));
245+
}
246+
247+
Datum
248+
tidlarger(PG_FUNCTION_ARGS)
249+
{
250+
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
251+
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
252+
253+
PG_RETURN_ITEMPOINTER(tid_cmp_internal(arg1,arg2) >= 0 ? arg1 : arg2);
254+
}
255+
256+
Datum
257+
tidsmaller(PG_FUNCTION_ARGS)
258+
{
259+
ItemPointer arg1 = PG_GETARG_ITEMPOINTER(0);
260+
ItemPointer arg2 = PG_GETARG_ITEMPOINTER(1);
261+
262+
PG_RETURN_ITEMPOINTER(tid_cmp_internal(arg1,arg2) <= 0 ? arg1 : arg2);
263+
}
264+
265+
188266
/*
189267
* Functions to get latest tid of a specified tuple.
190268
*

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.338 2006/07/11 19:49:13 teodor Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.339 2006/07/21 20:51:33 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200607111
56+
#define CATALOG_VERSION_NO 200607211
5757

5858
#endif

src/include/catalog/pg_aggregate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_aggregate.h,v 1.54 2006/03/10 20:15:26 neilc Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_aggregate.h,v 1.55 2006/07/21 20:51:33 tgl Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -117,6 +117,7 @@ DATA(insert ( 2129 text_larger - 666 25 _null_ ));
117117
DATA(insert ( 2130 numeric_larger - 1756 1700 _null_ ));
118118
DATA(insert ( 2050 array_larger - 1073 2277 _null_ ));
119119
DATA(insert ( 2244 bpchar_larger - 1060 1042 _null_ ));
120+
DATA(insert ( 2797 tidlarger - 2800 27 _null_ ));
120121

121122
/* min */
122123
DATA(insert ( 2131 int8smaller - 412 20 _null_ ));
@@ -137,6 +138,7 @@ DATA(insert ( 2145 text_smaller - 664 25 _null_ ));
137138
DATA(insert ( 2146 numeric_smaller - 1754 1700 _null_ ));
138139
DATA(insert ( 2051 array_smaller - 1072 2277 _null_ ));
139140
DATA(insert ( 2245 bpchar_smaller - 1058 1042 _null_ ));
141+
DATA(insert ( 2798 tidsmaller - 2799 27 _null_ ));
140142

141143
/*
142144
* Using int8inc for count() is cheating a little, since it really only

src/include/catalog/pg_amop.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
2424
* Portions Copyright (c) 1994, Regents of the University of California
2525
*
26-
* $PostgreSQL: pgsql/src/include/catalog/pg_amop.h,v 1.72 2006/07/11 19:49:13 teodor Exp $
26+
* $PostgreSQL: pgsql/src/include/catalog/pg_amop.h,v 1.73 2006/07/21 20:51:33 tgl Exp $
2727
*
2828
* NOTES
2929
* the genbki.sh script reads this file and generates .bki
@@ -156,6 +156,16 @@ DATA(insert ( 1989 0 3 f 607 ));
156156
DATA(insert ( 1989 0 4 f 612 ));
157157
DATA(insert ( 1989 0 5 f 610 ));
158158

159+
/*
160+
* btree tid_ops
161+
*/
162+
163+
DATA(insert ( 2789 0 1 f 2799 ));
164+
DATA(insert ( 2789 0 2 f 2801 ));
165+
DATA(insert ( 2789 0 3 f 387 ));
166+
DATA(insert ( 2789 0 4 f 2802 ));
167+
DATA(insert ( 2789 0 5 f 2800 ));
168+
159169
/*
160170
* btree oidvector_ops
161171
*/

src/include/catalog/pg_amproc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
2020
* Portions Copyright (c) 1994, Regents of the University of California
2121
*
22-
* $PostgreSQL: pgsql/src/include/catalog/pg_amproc.h,v 1.58 2006/05/02 15:23:16 teodor Exp $
22+
* $PostgreSQL: pgsql/src/include/catalog/pg_amproc.h,v 1.59 2006/07/21 20:51:33 tgl Exp $
2323
*
2424
* NOTES
2525
* the genbki.sh script reads this file and generates .bki
@@ -124,6 +124,7 @@ DATA(insert ( 2098 0 1 2187 ));
124124
DATA(insert ( 2099 0 1 377 ));
125125
DATA(insert ( 2233 0 1 380 ));
126126
DATA(insert ( 2234 0 1 381 ));
127+
DATA(insert ( 2789 0 1 2794 ));
127128

128129

129130
/* hash */

src/include/catalog/pg_opclass.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
2828
* Portions Copyright (c) 1994, Regents of the University of California
2929
*
30-
* $PostgreSQL: pgsql/src/include/catalog/pg_opclass.h,v 1.70 2006/05/02 15:23:16 teodor Exp $
30+
* $PostgreSQL: pgsql/src/include/catalog/pg_opclass.h,v 1.71 2006/07/21 20:51:33 tgl Exp $
3131
*
3232
* NOTES
3333
* the genbki.sh script reads this file and generates .bki
@@ -162,6 +162,7 @@ DATA(insert OID = 2222 ( 405 bool_ops PGNSP PGUID 16 t 0 ));
162162
#define BOOL_HASH_OPS_OID 2222
163163
DATA(insert OID = 2223 ( 405 bytea_ops PGNSP PGUID 17 t 0 ));
164164
DATA(insert OID = 2224 ( 405 int2vector_ops PGNSP PGUID 22 t 0 ));
165+
DATA(insert OID = 2789 ( 403 tid_ops PGNSP PGUID 27 t 0 ));
165166
DATA(insert OID = 2225 ( 405 xid_ops PGNSP PGUID 28 t 0 ));
166167
DATA(insert OID = 2226 ( 405 cid_ops PGNSP PGUID 29 t 0 ));
167168
DATA(insert OID = 2227 ( 405 abstime_ops PGNSP PGUID 702 t 0 ));

src/include/catalog/pg_operator.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_operator.h,v 1.143 2006/05/02 11:28:55 teodor Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_operator.h,v 1.144 2006/07/21 20:51:33 tgl Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -128,9 +128,15 @@ DATA(insert OID = 388 ( "!" PGNSP PGUID r f 20 0 1700 0 0 0 0 0
128128
DATA(insert OID = 389 ( "!!" PGNSP PGUID l f 0 20 1700 0 0 0 0 0 0 numeric_fac - - ));
129129
DATA(insert OID = 385 ( "=" PGNSP PGUID b t 29 29 16 385 0 0 0 0 0 cideq eqsel eqjoinsel ));
130130
DATA(insert OID = 386 ( "=" PGNSP PGUID b t 22 22 16 386 0 0 0 0 0 int2vectoreq eqsel eqjoinsel ));
131-
DATA(insert OID = 387 ( "=" PGNSP PGUID b f 27 27 16 387 402 0 0 0 0 tideq eqsel eqjoinsel ));
131+
132+
DATA(insert OID = 387 ( "=" PGNSP PGUID b f 27 27 16 387 402 2799 2799 2799 2800 tideq eqsel eqjoinsel ));
132133
#define TIDEqualOperator 387
133-
DATA(insert OID = 402 ( "<>" PGNSP PGUID b f 27 27 16 402 387 0 0 0 0 tidne neqsel neqjoinsel ));
134+
DATA(insert OID = 402 ( "<>" PGNSP PGUID b f 27 27 16 402 387 0 0 0 0 tidne neqsel neqjoinsel ));
135+
DATA(insert OID = 2799 ( "<" PGNSP PGUID b f 27 27 16 2800 2802 0 0 0 0 tidlt scalarltsel scalarltjoinsel ));
136+
#define TIDLessOperator 2799
137+
DATA(insert OID = 2800 ( ">" PGNSP PGUID b f 27 27 16 2799 2801 0 0 0 0 tidgt scalargtsel scalargtjoinsel ));
138+
DATA(insert OID = 2801 ( "<=" PGNSP PGUID b f 27 27 16 2802 2800 0 0 0 0 tidle scalarltsel scalarltjoinsel ));
139+
DATA(insert OID = 2802 ( ">=" PGNSP PGUID b f 27 27 16 2801 2799 0 0 0 0 tidge scalargtsel scalargtjoinsel ));
134140

135141
DATA(insert OID = 410 ( "=" PGNSP PGUID b t 20 20 16 410 411 412 412 412 413 int8eq eqsel eqjoinsel ));
136142
DATA(insert OID = 411 ( "<>" PGNSP PGUID b f 20 20 16 411 410 0 0 0 0 int8ne neqsel neqjoinsel ));
@@ -262,6 +268,7 @@ DATA(insert OID = 596 ( "|/" PGNSP PGUID l f 0 701 701 0 0 0 0 0 0 ds
262268
DATA(insert OID = 597 ( "||/" PGNSP PGUID l f 0 701 701 0 0 0 0 0 0 dcbrt - - ));
263269
DATA(insert OID = 1284 ( "|" PGNSP PGUID l f 0 704 702 0 0 0 0 0 0 tintervalstart - - ));
264270
DATA(insert OID = 606 ( "<#>" PGNSP PGUID b f 702 702 704 0 0 0 0 0 0 mktinterval - - ));
271+
265272
DATA(insert OID = 607 ( "=" PGNSP PGUID b t 26 26 16 607 608 609 609 609 610 oideq eqsel eqjoinsel ));
266273
#define MIN_OIDCMP 607 /* used by cache code */
267274
DATA(insert OID = 608 ( "<>" PGNSP PGUID b f 26 26 16 608 607 0 0 0 0 oidne neqsel neqjoinsel ));

0 commit comments

Comments
 (0)