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

Commit 1266164

Browse files
committed
Add function (actually an int4 and an int8 version) that generates
a series of numbers, optionally using an explicit step size other than the default value (one). Use function in the information_schema to replace hard-wired knowledge of INDEX_MAX_KEYS. initdb forced due to pg_proc change. Documentation update still needed -- will be committed separately.
1 parent 9dac526 commit 1266164

File tree

7 files changed

+205
-19
lines changed

7 files changed

+205
-19
lines changed

src/backend/catalog/information_schema.sql

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Copyright 2003, PostgreSQL Global Development Group
66
*
7-
* $PostgreSQL: pgsql/src/backend/catalog/information_schema.sql,v 1.22 2004/01/24 23:45:13 tgl Exp $
7+
* $PostgreSQL: pgsql/src/backend/catalog/information_schema.sql,v 1.23 2004/02/03 08:29:56 joe Exp $
88
*/
99

1010
/*
@@ -399,17 +399,9 @@ GRANT SELECT ON columns TO PUBLIC;
399399
CREATE FUNCTION _pg_keypositions() RETURNS SETOF integer
400400
LANGUAGE sql
401401
IMMUTABLE
402-
AS 'select 1 union all select 2 union all select 3 union all
403-
select 4 union all select 5 union all select 6 union all
404-
select 7 union all select 8 union all select 9 union all
405-
select 10 union all select 11 union all select 12 union all
406-
select 13 union all select 14 union all select 15 union all
407-
select 16 union all select 17 union all select 18 union all
408-
select 19 union all select 20 union all select 21 union all
409-
select 22 union all select 23 union all select 24 union all
410-
select 25 union all select 26 union all select 27 union all
411-
select 28 union all select 29 union all select 30 union all
412-
select 31 union all select 32';
402+
AS 'select g.s
403+
from generate_series(1,current_setting(''max_index_keys'')::int,1)
404+
as g(s)';
413405

414406
CREATE VIEW constraint_column_usage AS
415407
SELECT CAST(current_database() AS sql_identifier) AS table_catalog,

src/backend/utils/adt/int.c

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.59 2003/12/01 21:52:37 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.60 2004/02/03 08:29:56 joe Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -34,6 +34,7 @@
3434
#include <ctype.h>
3535
#include <limits.h>
3636

37+
#include "funcapi.h"
3738
#include "libpq/pqformat.h"
3839
#include "utils/builtins.h"
3940

@@ -44,6 +45,13 @@
4445
#define SHRT_MIN (-0x8000)
4546
#endif
4647

48+
typedef struct
49+
{
50+
int32 current;
51+
int32 finish;
52+
int32 step;
53+
} generate_series_fctx;
54+
4755
/*****************************************************************************
4856
* USER I/O ROUTINES *
4957
*****************************************************************************/
@@ -1021,3 +1029,84 @@ int2shr(PG_FUNCTION_ARGS)
10211029

10221030
PG_RETURN_INT16(arg1 >> arg2);
10231031
}
1032+
1033+
/*
1034+
* non-persistent numeric series generator
1035+
*/
1036+
Datum
1037+
generate_series_int4(PG_FUNCTION_ARGS)
1038+
{
1039+
return generate_series_step_int4(fcinfo);
1040+
}
1041+
1042+
Datum
1043+
generate_series_step_int4(PG_FUNCTION_ARGS)
1044+
{
1045+
FuncCallContext *funcctx;
1046+
generate_series_fctx *fctx;
1047+
int32 result;
1048+
MemoryContext oldcontext;
1049+
1050+
/* stuff done only on the first call of the function */
1051+
if (SRF_IS_FIRSTCALL())
1052+
{
1053+
int32 start = PG_GETARG_INT32(0);
1054+
int32 finish = PG_GETARG_INT32(1);
1055+
int32 step = 1;
1056+
1057+
/* see if we were given an explicit step size */
1058+
if (PG_NARGS() == 3)
1059+
step = PG_GETARG_INT32(2);
1060+
if (step == 0)
1061+
ereport(ERROR,
1062+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1063+
errmsg("step size may not equal zero")));
1064+
1065+
/* create a function context for cross-call persistence */
1066+
funcctx = SRF_FIRSTCALL_INIT();
1067+
1068+
/*
1069+
* switch to memory context appropriate for multiple function
1070+
* calls
1071+
*/
1072+
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
1073+
1074+
/* allocate memory for user context */
1075+
fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
1076+
1077+
/*
1078+
* Use fctx to keep state from call to call.
1079+
* Seed current with the original start value
1080+
*/
1081+
fctx->current = start;
1082+
fctx->finish = finish;
1083+
fctx->step = step;
1084+
1085+
funcctx->user_fctx = fctx;
1086+
MemoryContextSwitchTo(oldcontext);
1087+
}
1088+
1089+
/* stuff done on every call of the function */
1090+
funcctx = SRF_PERCALL_SETUP();
1091+
1092+
/*
1093+
* get the saved state and use current as the result for
1094+
* this iteration
1095+
*/
1096+
fctx = funcctx->user_fctx;
1097+
result = fctx->current;
1098+
1099+
if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1100+
(fctx->step < 0 && fctx->current >= fctx->finish))
1101+
{
1102+
/* increment current in preparation for next iteration */
1103+
fctx->current += fctx->step;
1104+
1105+
/* do when there is more left to send */
1106+
SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
1107+
}
1108+
else
1109+
/* do when there is no more left */
1110+
SRF_RETURN_DONE(funcctx);
1111+
}
1112+

src/backend/utils/adt/int8.c

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,29 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.50 2003/12/01 21:52:37 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.51 2004/02/03 08:29:56 joe Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#include "postgres.h"
1515

1616
#include <ctype.h>
17+
#include <limits.h>
1718
#include <math.h>
1819

20+
#include "funcapi.h"
1921
#include "libpq/pqformat.h"
2022
#include "utils/int8.h"
2123

2224

2325
#define MAXINT8LEN 25
2426

27+
typedef struct
28+
{
29+
int64 current;
30+
int64 finish;
31+
int64 step;
32+
} generate_series_fctx;
2533

2634
/***********************************************************************
2735
**
@@ -936,3 +944,84 @@ int8_text(PG_FUNCTION_ARGS)
936944

937945
PG_RETURN_TEXT_P(result);
938946
}
947+
948+
/*
949+
* non-persistent numeric series generator
950+
*/
951+
Datum
952+
generate_series_int8(PG_FUNCTION_ARGS)
953+
{
954+
return generate_series_step_int8(fcinfo);
955+
}
956+
957+
Datum
958+
generate_series_step_int8(PG_FUNCTION_ARGS)
959+
{
960+
FuncCallContext *funcctx;
961+
generate_series_fctx *fctx;
962+
int64 result;
963+
MemoryContext oldcontext;
964+
965+
/* stuff done only on the first call of the function */
966+
if (SRF_IS_FIRSTCALL())
967+
{
968+
int64 start = PG_GETARG_INT64(0);
969+
int64 finish = PG_GETARG_INT64(1);
970+
int64 step = 1;
971+
972+
/* see if we were given an explicit step size */
973+
if (PG_NARGS() == 3)
974+
step = PG_GETARG_INT64(2);
975+
if (step == 0)
976+
ereport(ERROR,
977+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
978+
errmsg("step size may not equal zero")));
979+
980+
/* create a function context for cross-call persistence */
981+
funcctx = SRF_FIRSTCALL_INIT();
982+
983+
/*
984+
* switch to memory context appropriate for multiple function
985+
* calls
986+
*/
987+
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
988+
989+
/* allocate memory for user context */
990+
fctx = (generate_series_fctx *) palloc(sizeof(generate_series_fctx));
991+
992+
/*
993+
* Use fctx to keep state from call to call.
994+
* Seed current with the original start value
995+
*/
996+
fctx->current = start;
997+
fctx->finish = finish;
998+
fctx->step = step;
999+
1000+
funcctx->user_fctx = fctx;
1001+
MemoryContextSwitchTo(oldcontext);
1002+
}
1003+
1004+
/* stuff done on every call of the function */
1005+
funcctx = SRF_PERCALL_SETUP();
1006+
1007+
/*
1008+
* get the saved state and use current as the result for
1009+
* this iteration
1010+
*/
1011+
fctx = funcctx->user_fctx;
1012+
result = fctx->current;
1013+
1014+
if ((fctx->step > 0 && fctx->current <= fctx->finish) ||
1015+
(fctx->step < 0 && fctx->current >= fctx->finish))
1016+
{
1017+
/* increment current in preparation for next iteration */
1018+
fctx->current += fctx->step;
1019+
1020+
/* do when there is more left to send */
1021+
SRF_RETURN_NEXT(funcctx, Int64GetDatum(result));
1022+
}
1023+
else
1024+
/* do when there is no more left */
1025+
SRF_RETURN_DONE(funcctx);
1026+
}
1027+

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-2003, 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.216 2004/01/14 23:01:55 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.217 2004/02/03 08:29:56 joe Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200401141
56+
#define CATALOG_VERSION_NO 200402021
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.318 2004/01/06 23:55:19 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.319 2004/02/03 08:29:56 joe Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3424,6 +3424,17 @@ DESCR("constraint description with pretty-print option");
34243424
DATA(insert OID = 2509 ( pg_get_expr PGNSP PGUID 12 f f t f s 3 25 "25 26 16" _null_ pg_get_expr_ext - _null_ ));
34253425
DESCR("deparse an encoded expression with pretty-print option");
34263426

3427+
/* non-persistent series generator */
3428+
DATA(insert OID = 1066 ( generate_series PGNSP PGUID 12 f f t t v 3 23 "23 23 23" _null_ generate_series_step_int4 - _null_ ));
3429+
DESCR("non-persistent series generator");
3430+
DATA(insert OID = 1067 ( generate_series PGNSP PGUID 12 f f t t v 2 23 "23 23" _null_ generate_series_int4 - _null_ ));
3431+
DESCR("non-persistent series generator");
3432+
3433+
DATA(insert OID = 1068 ( generate_series PGNSP PGUID 12 f f t t v 3 20 "20 20 20" _null_ generate_series_step_int8 - _null_ ));
3434+
DESCR("non-persistent series generator");
3435+
DATA(insert OID = 1069 ( generate_series PGNSP PGUID 12 f f t t v 2 20 "20 20" _null_ generate_series_int8 - _null_ ));
3436+
DESCR("non-persistent series generator");
3437+
34273438

34283439
/*
34293440
* Symbolic values for provolatile column: these indicate whether the result

src/include/utils/builtins.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.233 2004/01/19 19:04:40 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.234 2004/02/03 08:29:57 joe Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -175,6 +175,8 @@ extern Datum int2xor(PG_FUNCTION_ARGS);
175175
extern Datum int2not(PG_FUNCTION_ARGS);
176176
extern Datum int2shl(PG_FUNCTION_ARGS);
177177
extern Datum int2shr(PG_FUNCTION_ARGS);
178+
extern Datum generate_series_int4(PG_FUNCTION_ARGS);
179+
extern Datum generate_series_step_int4(PG_FUNCTION_ARGS);
178180

179181
/* name.c */
180182
extern Datum namein(PG_FUNCTION_ARGS);

src/include/utils/int8.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/int8.h,v 1.40 2003/12/01 21:52:38 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/int8.h,v 1.41 2004/02/03 08:29:57 joe Exp $
1111
*
1212
* NOTES
1313
* These data types are supported on all 64-bit architectures, and may
@@ -112,4 +112,7 @@ extern Datum oidtoi8(PG_FUNCTION_ARGS);
112112
extern Datum int8_text(PG_FUNCTION_ARGS);
113113
extern Datum text_int8(PG_FUNCTION_ARGS);
114114

115+
extern Datum generate_series_int8(PG_FUNCTION_ARGS);
116+
extern Datum generate_series_step_int8(PG_FUNCTION_ARGS);
117+
115118
#endif /* INT8_H */

0 commit comments

Comments
 (0)