Skip to content

Commit d4d441d

Browse files
author
Karina Litskevich
committed
[PGPRO-10079] Use StringInfo instead of fix-sized buffers
This way we don't worry about allocating enough memory as appendStringInfo does it for us Tags: vops
1 parent 8348d48 commit d4d441d

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

Diff for: vops.c

+19-17
Original file line numberDiff line numberDiff line change
@@ -823,23 +823,23 @@ static bool is_vops_type(Oid typeid)
823823
Datum vops_##TYPE##_output(PG_FUNCTION_ARGS) \
824824
{ \
825825
vops_##TYPE* tile = (vops_##TYPE*)PG_GETARG_POINTER(0); \
826-
char buf[MAX_TILE_STRLEN]; \
827-
int p = 0; \
826+
StringInfoData str; \
828827
char sep = '{'; \
829828
int i; \
829+
initStringInfo(&str); \
830830
for (i = 0; i < TILE_SIZE; i++) { \
831831
if (tile->hdr.empty_mask & ((uint64)1 << i)) { \
832-
p += sprintf(buf + p, "%c", sep); \
832+
appendStringInfo(&str, "%c", sep); \
833833
} else if (tile->hdr.null_mask & ((uint64)1 << i)) { \
834-
p += sprintf(buf + p, "%c?", sep); \
834+
appendStringInfo(&str, "%c?", sep); \
835835
} else { \
836-
p += sprintf(buf + p, "%c%.*" #FORMAT, sep, PREC, (STYPE)tile->payload[i]); \
836+
appendStringInfo(&str, "%c%.*" #FORMAT, sep, PREC, (STYPE)tile->payload[i]); \
837837
} \
838838
sep = ','; \
839839
} \
840-
strcpy(buf + p, "}"); \
841-
PG_RETURN_CSTRING(pstrdup(buf)); \
842-
}
840+
appendStringInfo(&str, "}"); \
841+
PG_RETURN_CSTRING(str.data); \
842+
}
843843

844844

845845

@@ -2497,7 +2497,7 @@ Datum vops_populate(PG_FUNCTION_ARGS)
24972497
char* sql;
24982498
char sep;
24992499
TupleDesc spi_tupdesc;
2500-
int i, j, n, n_attrs;
2500+
int i, j, n_attrs;
25012501
vops_type_info* types;
25022502
Datum* values;
25032503
bool* nulls;
@@ -2508,7 +2508,7 @@ Datum vops_populate(PG_FUNCTION_ARGS)
25082508
int64 loaded;
25092509
bool type_checked = false;
25102510
static Oid self_oid = InvalidOid;
2511-
char stmt[MAX_SQL_STMT_LEN];
2511+
StringInfoData stmt;
25122512

25132513
/* Detect case when extension is drop and created several times */
25142514
if (fcinfo->flinfo->fn_oid != self_oid)
@@ -2533,7 +2533,8 @@ Datum vops_populate(PG_FUNCTION_ARGS)
25332533
values = (Datum*)palloc(sizeof(Datum)*n_attrs);
25342534
nulls = (bool*)palloc0(sizeof(bool)*n_attrs);
25352535

2536-
n = sprintf(stmt, "select");
2536+
initStringInfo(&stmt);
2537+
appendStringInfo(&stmt, "select");
25372538
sep = ' ';
25382539
spi_tupdesc = SPI_tuptable->tupdesc;
25392540
for (i = 0; i < n_attrs; i++) {
@@ -2549,25 +2550,26 @@ Datum vops_populate(PG_FUNCTION_ARGS)
25492550
elog(ERROR, "Size of column %s is unknown", name);
25502551
}
25512552
}
2552-
n += sprintf(stmt + n, "%c%s", sep, name);
2553+
appendStringInfo(&stmt, "%c%s", sep, name);
25532554
sep = ',';
25542555
SPI_freetuple(spi_tuple);
25552556
}
25562557
SPI_freetuptable(SPI_tuptable);
25572558

2558-
n += sprintf(stmt + n, " from %s.%s",
2559+
appendStringInfo(&stmt, " from %s.%s",
25592560
get_namespace_name(get_rel_namespace(source)),
25602561
get_rel_name(source));
25612562
if (predicate && *predicate) {
2562-
n += sprintf(stmt + n, " where %s", predicate);
2563+
appendStringInfo(&stmt, " where %s", predicate);
25632564
}
25642565
if (sort && *sort) {
2565-
n += sprintf(stmt + n, " order by %s", sort);
2566+
appendStringInfo(&stmt, " order by %s", sort);
25662567
}
2567-
plan = SPI_prepare(stmt, 0, NULL);
2568+
plan = SPI_prepare(stmt.data, 0, NULL);
25682569
if (plan == NULL)
25692570
elog(ERROR, "SPI_prepare(\"%s\") failed:%s",
2570-
stmt, SPI_result_code_string(SPI_result));
2571+
stmt.data, SPI_result_code_string(SPI_result));
2572+
pfree(stmt.data);
25712573
portal = SPI_cursor_open(NULL, plan, NULL, NULL, true);
25722574

25732575
begin_batch_insert(destination);

Diff for: vops.h

-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ typedef enum
3939

4040

4141
#define TILE_SIZE 64 /* just because of maximum size of bitmask */
42-
#define MAX_SQL_STMT_LEN 1024
4342
#define MAX_CSV_LINE_LEN 4096
44-
#define MAX_TILE_STRLEN (TILE_SIZE*16)
4543
#define INIT_MAP_SIZE (1024*1024)
4644

4745
typedef long long long64;

0 commit comments

Comments
 (0)