Skip to content

Commit a7a42b7

Browse files
committed
Fix crashes when cluster indexes have storage options (fillfactor, etc).
1 parent 7084ec6 commit a7a42b7

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

bin/expected/reorg.out

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ CREATE TABLE tbl_cluster (
66
col1 int,
77
col2 timestamp,
88
","")" text,
9-
PRIMARY KEY (","")", col1)
9+
PRIMARY KEY (","")", col1) WITH (fillfactor = 75)
1010
) WITH (fillfactor = 70);
11-
CREATE INDEX ","") cluster" ON tbl_cluster (col2, length(","")"), ","")" text_pattern_ops);
11+
CREATE INDEX ","") cluster" ON tbl_cluster (col2, length(","")"), ","")" text_pattern_ops) WITH (fillfactor = 75);
1212
ALTER TABLE tbl_cluster CLUSTER ON ","") cluster";
1313
CREATE TABLE tbl_only_pkey (
1414
col1 int PRIMARY KEY,
@@ -35,8 +35,9 @@ CREATE TABLE tbl_with_dropped_column (
3535
c2 text,
3636
d3 text
3737
);
38+
ALTER INDEX tbl_with_dropped_column_pkey SET (fillfactor = 75);
3839
ALTER TABLE tbl_with_dropped_column CLUSTER ON tbl_with_dropped_column_pkey;
39-
CREATE INDEX idx_c1c2 ON tbl_with_dropped_column (c1, c2);
40+
CREATE INDEX idx_c1c2 ON tbl_with_dropped_column (c1, c2) WITH (fillfactor = 75);
4041
CREATE INDEX idx_c2c1 ON tbl_with_dropped_column (c2, c1);
4142
--
4243
-- insert data
@@ -85,8 +86,8 @@ SELECT * FROM tbl_with_dropped_column;
8586
col2 | timestamp without time zone |
8687
,") | text | not null
8788
Indexes:
88-
"tbl_cluster_pkey" PRIMARY KEY, btree (","")", col1)
89-
",") cluster" btree (col2, length(","")"), ","")" text_pattern_ops) CLUSTER
89+
"tbl_cluster_pkey" PRIMARY KEY, btree (","")", col1) WITH (fillfactor=75)
90+
",") cluster" btree (col2, length(","")"), ","")" text_pattern_ops) WITH (fillfactor=75) CLUSTER
9091

9192
\d tbl_gistkey
9293
Table "public.tbl_gistkey"
@@ -126,8 +127,8 @@ Table "public.tbl_with_dropped_column"
126127
c2 | text |
127128
c3 | text |
128129
Indexes:
129-
"tbl_with_dropped_column_pkey" PRIMARY KEY, btree (id) CLUSTER
130-
"idx_c1c2" btree (c1, c2)
130+
"tbl_with_dropped_column_pkey" PRIMARY KEY, btree (id) WITH (fillfactor=75) CLUSTER
131+
"idx_c1c2" btree (c1, c2) WITH (fillfactor=75)
131132
"idx_c2c1" btree (c2, c1)
132133

133134
SELECT col1, to_char(col2, 'YYYY-MM-DD HH24:MI:SS'), ","")" FROM tbl_cluster;

bin/pgut/pgut.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ option_find(int c, pgut_option opts1[], pgut_option opts2[])
144144
return NULL; /* not found */
145145
}
146146

147-
static void
148-
assign_option(pgut_option *opt, const char *optarg, pgut_optsrc src)
147+
void
148+
pgut_setopt(pgut_option *opt, const char *optarg, pgut_optsrc src)
149149
{
150150
const char *message;
151151

@@ -578,7 +578,7 @@ option_from_env(pgut_option options[])
578578
name[j] = '\0';
579579

580580
if ((value = getenv(name)) != NULL)
581-
assign_option(opt, value, SOURCE_ENV);
581+
pgut_setopt(opt, value, SOURCE_ENV);
582582
}
583583
}
584584

@@ -620,7 +620,7 @@ pgut_getopt(int argc, char **argv, pgut_option options[])
620620
while ((c = getopt_long(argc, argv, optstring, longopts, &optindex)) != -1)
621621
{
622622
opt = option_find(c, default_options, options);
623-
assign_option(opt, optarg, SOURCE_CMDLINE);
623+
pgut_setopt(opt, optarg, SOURCE_CMDLINE);
624624
}
625625

626626
/* Read environment variables */
@@ -637,8 +637,8 @@ pgut_getopt(int argc, char **argv, pgut_option options[])
637637
}
638638

639639
/* compare two strings ignore cases and ignore -_ */
640-
static bool
641-
key_equals(const char *lhs, const char *rhs)
640+
bool
641+
pgut_keyeq(const char *lhs, const char *rhs)
642642
{
643643
for (; *lhs && *rhs; lhs++, rhs++)
644644
{
@@ -684,13 +684,13 @@ pgut_readopt(const char *path, pgut_option options[], int elevel)
684684
{
685685
pgut_option *opt = &options[i];
686686

687-
if (key_equals(key, opt->lname))
687+
if (pgut_keyeq(key, opt->lname))
688688
{
689689
if (opt->allowed == SOURCE_DEFAULT ||
690690
opt->allowed > SOURCE_FILE)
691691
elog(elevel, "option %s cannot specified in file", opt->lname);
692692
else if (opt->source <= SOURCE_FILE)
693-
assign_option(opt, value, SOURCE_FILE);
693+
pgut_setopt(opt, value, SOURCE_FILE);
694694
break;
695695
}
696696
}

bin/pgut/pgut.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ extern bool interrupted;
105105
extern void help(bool details);
106106
extern int pgut_getopt(int argc, char **argv, pgut_option options[]);
107107
extern void pgut_readopt(const char *path, pgut_option options[], int elevel);
108+
extern void pgut_setopt(pgut_option *opt, const char *optarg, pgut_optsrc src);
109+
extern bool pgut_keyeq(const char *lhs, const char *rhs);
108110
extern void pgut_atexit_push(pgut_atexit_callback callback, void *userdata);
109111
extern void pgut_atexit_pop(pgut_atexit_callback callback, void *userdata);
110112

bin/sql/reorg.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ CREATE TABLE tbl_cluster (
66
col1 int,
77
col2 timestamp,
88
","")" text,
9-
PRIMARY KEY (","")", col1)
9+
PRIMARY KEY (","")", col1) WITH (fillfactor = 75)
1010
) WITH (fillfactor = 70);
1111

12-
CREATE INDEX ","") cluster" ON tbl_cluster (col2, length(","")"), ","")" text_pattern_ops);
12+
CREATE INDEX ","") cluster" ON tbl_cluster (col2, length(","")"), ","")" text_pattern_ops) WITH (fillfactor = 75);
1313
ALTER TABLE tbl_cluster CLUSTER ON ","") cluster";
1414

1515
CREATE TABLE tbl_only_pkey (
@@ -42,8 +42,9 @@ CREATE TABLE tbl_with_dropped_column (
4242
c2 text,
4343
d3 text
4444
);
45+
ALTER INDEX tbl_with_dropped_column_pkey SET (fillfactor = 75);
4546
ALTER TABLE tbl_with_dropped_column CLUSTER ON tbl_with_dropped_column_pkey;
46-
CREATE INDEX idx_c1c2 ON tbl_with_dropped_column (c1, c2);
47+
CREATE INDEX idx_c1c2 ON tbl_with_dropped_column (c1, c2) WITH (fillfactor = 75);
4748
CREATE INDEX idx_c2c1 ON tbl_with_dropped_column (c2, c1);
4849

4950
--

lib/reorg.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ skip_ident(Oid index, char *sql)
350350
return parse_error(index);
351351
}
352352

353+
/*
354+
* Skip until 'end' character found. The 'end' character is replaced with \0.
355+
* Returns the next character of the 'end', or NULL if 'end' is not found.
356+
*/
353357
static char *
354358
skip_until(Oid index, char *sql, char end)
355359
{
@@ -391,8 +395,13 @@ skip_until(Oid index, char *sql, char end)
391395

392396
if (nopen == 0 && instr == 0)
393397
{
394-
*sql = '\0';
395-
return sql + 1;
398+
if (*sql)
399+
{
400+
*sql = '\0';
401+
return sql + 1;
402+
}
403+
else
404+
return NULL;
396405
}
397406

398407
/* error */
@@ -427,7 +436,8 @@ parse_indexdef(IndexDef *stmt, Oid index, Oid table)
427436
parse_error(index);
428437
sql++;
429438
stmt->columns = sql;
430-
sql = skip_until(index, sql, ')');
439+
if ((sql = skip_until(index, sql, ')')) == NULL)
440+
parse_error(index);
431441
/* options */
432442
stmt->options = sql;
433443
}
@@ -467,7 +477,7 @@ reorg_get_index_keys(PG_FUNCTION_ARGS)
467477
*/
468478

469479
initStringInfo(&str);
470-
for (nattr = 0, next = stmt.columns; *next; nattr++)
480+
for (nattr = 0, next = stmt.columns; next; nattr++)
471481
{
472482
char *opcname;
473483

@@ -544,7 +554,7 @@ reorg_get_index_keys(PG_FUNCTION_ARGS)
544554
}
545555
else
546556
appendStringInfoString(&str, token);
547-
if (*next)
557+
if (next)
548558
appendStringInfoChar(&str, ',');
549559
}
550560

0 commit comments

Comments
 (0)