Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit26aaf97

Browse files
committed
Make StringInfo available to frontend code.
There's plenty places in frontend code that could benefit from astring buffer implementation. Some because it yields simpler andfaster code, and some others because of the desire to share codebetween backend and frontend.While there is a string buffer implementation available to frontendcode, libpq's PQExpBuffer, it is clunkier than stringinfo, itintroduces a libpq dependency, doesn't allow for sharing betweenfrontend and backend code, and has a higher API/ABI stabilityrequirement due to being exposed via libpq.Therefore it seems best to just making StringInfo being usable byfrontend code. There's not much to do for that, except for rewritingtwo subsequent elog/ereport calls into others types of errorreporting, and deciding on a maximum string length.For the maximum string size I decided to privately define MaxAllocSizeto the same value as used in the backend. It seems likely that we'llwant to reconsider this for both backend and frontend code in the nottoo far away future.For now I've left stringinfo.h in lib/, rather than common/, to reducethe likelihood of unnecessary breakage. We could alternatively decideto provide a redirecting stringinfo.h in lib/, or just not providecompatibility.Author: Andres FreundReviewed-By: Kyotaro Horiguchi, Daniel GustafssonDiscussion:https://postgr.es/m/20190920051857.2fhnvhvx4qdddviz@alap3.anarazel.de
1 parent01368e5 commit26aaf97

File tree

7 files changed

+49
-43
lines changed

7 files changed

+49
-43
lines changed

‎src/backend/lib/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ OBJS = \
2323
knapsack.o\
2424
pairingheap.o\
2525
rbtree.o\
26-
stringinfo.o
2726

2827
include$(top_srcdir)/src/backend/common.mk

‎src/bin/pg_waldump/compat.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include<time.h>
2222

23-
#include"lib/stringinfo.h"
2423
#include"utils/datetime.h"
2524

2625
/* copied from timestamp.c */
@@ -63,29 +62,3 @@ timestamptz_to_str(TimestampTz dt)
6362

6463
returnbuf;
6564
}
66-
67-
/*
68-
* Provide a hacked up compat layer for StringInfos so xlog desc functions can
69-
* be linked/called.
70-
*/
71-
void
72-
appendStringInfo(StringInfostr,constchar*fmt,...)
73-
{
74-
va_listargs;
75-
76-
va_start(args,fmt);
77-
vprintf(fmt,args);
78-
va_end(args);
79-
}
80-
81-
void
82-
appendStringInfoString(StringInfostr,constchar*string)
83-
{
84-
appendStringInfo(str,"%s",string);
85-
}
86-
87-
void
88-
appendStringInfoChar(StringInfostr,charch)
89-
{
90-
appendStringInfo(str,"%c",ch);
91-
}

‎src/bin/pg_waldump/pg_waldump.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
514514
intblock_id;
515515
uint8info=XLogRecGetInfo(record);
516516
XLogRecPtrxl_prev=XLogRecGetPrev(record);
517+
StringInfoDatas;
517518

518519
XLogDumpRecordLen(record,&rec_len,&fpi_len);
519520

@@ -530,8 +531,10 @@ XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
530531
else
531532
printf("desc: %s ",id);
532533

533-
/* the desc routine will printf the description directly to stdout */
534-
desc->rm_desc(NULL,record);
534+
initStringInfo(&s);
535+
desc->rm_desc(&s,record);
536+
printf("%s",s.data);
537+
pfree(s.data);
535538

536539
if (!config->bkp_details)
537540
{

‎src/common/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ OBJS_COMMON = \
6767
saslprep.o\
6868
scram-common.o\
6969
string.o\
70+
stringinfo.o\
7071
unicode_norm.o\
7172
username.o\
7273
wait_error.o

‎src/backend/lib/stringinfo.crenamed to‎src/common/stringinfo.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@
22
*
33
* stringinfo.c
44
*
5-
* StringInfo provides an indefinitely-extensible string data type.
6-
* It can be used to buffer either ordinary C strings (null-terminated text)
7-
* or arbitrary binary data. All storage is allocated with palloc().
5+
* StringInfo provides an extensible string data type (currently limited to a
6+
* length of 1GB). It can be used to buffer either ordinary C strings
7+
* (null-terminated text) or arbitrary binary data. All storage is allocated
8+
* with palloc() (falling back to malloc in frontend code).
89
*
910
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
1011
* Portions Copyright (c) 1994, Regents of the University of California
1112
*
12-
* src/backend/lib/stringinfo.c
13+
* src/common/stringinfo.c
1314
*
1415
*-------------------------------------------------------------------------
1516
*/
17+
18+
#ifndefFRONTEND
19+
1620
#include"postgres.h"
21+
#include"utils/memutils.h"
22+
23+
#else
24+
25+
#include"postgres_fe.h"
26+
27+
/* It's possible we could use a different value for this in frontend code */
28+
#defineMaxAllocSize((Size) 0x3fffffff)/* 1 gigabyte - 1 */
29+
30+
#endif
1731

1832
#include"lib/stringinfo.h"
19-
#include"utils/memutils.h"
2033

2134

2235
/*
@@ -261,10 +274,10 @@ appendBinaryStringInfoNT(StringInfo str, const char *data, int datalen)
261274
* can save some palloc overhead by enlarging the buffer before starting
262275
* to store data in it.
263276
*
264-
* NB: because we use repalloc() to enlarge the buffer, the string buffer
265-
* will remain allocated in the same memory context that was current when
266-
* initStringInfo was called, even if another context is now current.
267-
* This is the desired and indeed critical behavior!
277+
* NB:In the backend,because we use repalloc() to enlarge the buffer, the
278+
*string bufferwill remain allocated in the same memory context that was
279+
*current wheninitStringInfo was called, even if another context is now
280+
*current.This is the desired and indeed critical behavior!
268281
*/
269282
void
270283
enlargeStringInfo(StringInfostr,intneeded)
@@ -276,13 +289,29 @@ enlargeStringInfo(StringInfo str, int needed)
276289
* an overflow or infinite loop in the following.
277290
*/
278291
if (needed<0)/* should not happen */
292+
{
293+
#ifndefFRONTEND
279294
elog(ERROR,"invalid string enlargement request size: %d",needed);
295+
#else
296+
fprintf(stderr,"invalid string enlargement request size: %d\n",needed);
297+
exit(EXIT_FAILURE);
298+
#endif
299+
}
280300
if (((Size)needed) >= (MaxAllocSize- (Size)str->len))
301+
{
302+
#ifndefFRONTEND
281303
ereport(ERROR,
282304
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
283305
errmsg("out of memory"),
284306
errdetail("Cannot enlarge string buffer containing %d bytes by %d more bytes.",
285307
str->len,needed)));
308+
#else
309+
fprintf(stderr,
310+
_("out of memory\n\nCannot enlarge string buffer containing %d bytes by %d more bytes.\n"),
311+
str->len,needed);
312+
exit(EXIT_FAILURE);
313+
#endif
314+
}
286315

287316
needed+=str->len+1;/* total space required now */
288317

‎src/include/lib/stringinfo.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* stringinfo.h
44
* Declarations/definitions for "StringInfo" functions.
55
*
6-
* StringInfo provides an indefinitely-extensible string data type.
7-
* It can be used to buffer either ordinary C strings (null-terminated text)
8-
* or arbitrary binary data. All storage is allocated with palloc().
6+
* StringInfo provides an extensible string data type (currently limited to a
7+
* length of 1GB). It can be used to buffer either ordinary C strings
8+
* (null-terminated text) or arbitrary binary data. All storage is allocated
9+
* with palloc() (falling back to malloc in frontend code).
910
*
1011
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
1112
* Portions Copyright (c) 1994, Regents of the University of California

‎src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ sub mkvcbuild
123123
base64.c config_info.c controldata_utils.c d2s.c exec.c f2s.c file_perm.c ip.c
124124
keywords.c kwlookup.c link-canary.c md5.c
125125
pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
126-
saslprep.c scram-common.c string.c unicode_norm.c username.c
126+
saslprep.c scram-common.c string.cstringinfo.cunicode_norm.c username.c
127127
wait_error.c);
128128

129129
if ($solution->{options}->{openssl})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp